Example a_backup/optimisation/fitting-2D-linear: A two-dimensional surface fit using least-squares.

This example demonstrates the use of the non-linear least-squares optimisation method to fit a two-dimensional linear mesh to a set of data. While the other optimisation methods could be used to achieve this fit, the least-squares method is much more efficient.
Initial mesh showing the original mesh and the data points (gold spheres). The fitted mesh.


The comfile run by this example is as follows:

if (! defined $example) {
	$example = ".";
}

if (! defined $draw) {
	$draw = 1;
}

# read in the data to be fit to
gfx read nodes "$example/data.exnode";
# and the initial mesh
gfx read nodes "$example/mesh.exnode";
gfx read nodes "$example/mesh.exelem";

# create a field which dynamically finds the nearest location to the data_coordinates on the 1-D mesh 
gfx define field found_location find_mesh_location find_nearest mesh cmiss_mesh_2d mesh_field coordinates source_field data_coordinates;

# define a field for storing these locations so not recalculated during optimisation
# (You can use the dynamically found locations but it is slower and probably unstable without smoothing) 
gfx define field stored_location finite_element element_xi;
# define storage for the the stored_location field at the data points:
gfx modify nodes group data define stored_location;
# set stored_location field from found_location at the data points
gfx evaluate ngroup data destination stored_location source found_location; 

# define a field giving the coordinates at the stored_location on the mesh:
gfx define field projected_coordinates embedded element_xi stored_location field coordinates;
# define a field which calculates the error vector between the data point and its element project:
gfx define field error_vector add fields data_coordinates projected_coordinates scale_factors 1 -1;

# define objective function for least-squares method as sum of squared error_vector over the data points
# (least-squares method works directly with individual terms from sum-of-squares field types like this)
gfx define field objective_function nodeset_sum_squares field error_vector nodeset data.cmiss_nodes;

if ($draw) {
	# draw the mesh
	gfx modify g_element mesh general clear circle_discretization 6 default_coordinate coordinates element_discretization "4*4*4" native_discretization none;
	gfx modify g_element mesh node_points glyph sphere general size "0.5*0.5*0.5" centre 0,0,0 font default select_on material green selected_material default_selected;
	gfx modify g_element mesh lines select_on material default selected_material default_selected;
	# and the data points
	gfx modify g_element data general clear circle_discretization 6 default_coordinate coordinates element_discretization "4*4*4" native_discretization none;
	gfx modify g_element data node_points coordinate data_coordinates glyph sphere general size "0.3*0.3*0.3" centre 0,0,0 font default select_on material gold selected_material default_selected;
	
	gfx create window;
}

# if testing, run the optimisation otherwise the user can manually choose to run the optimisation
if ($TESTING) {
	gfx minimise LEAST_SQUARES_QUASI_NEWTON region "/" objective_fields objective_function independent_fields coordinates hide_output;
	gfx write nodes group mesh "fitted.exnode";
}

Files used by this example are:

Name                   Modified     Size

fitting-2D-linear.com 20-Apr-2012 2.8k data.exnode 20-Apr-2012 145k filter.pl 20-Apr-2012 236 mesh.exelem 20-Apr-2012 33k mesh.exnode 20-Apr-2012 6.7k subset.exnode 20-Apr-2012 14k

Download the entire example:

Name                                                     Modified     Size

examples_a_backup_optimisation_fitting-2D-linear.tar.gz 12-Aug-2014 1.1M

Testing status by version:

StatusTestedReal time (s)
i686-linux
cmgui-wxSuccessSun Mar 6 00:26:08 201610
cmgui-wx-debugSuccessSun Mar 6 00:28:05 201613
cmgui-wx-debug-memorycheckSuccessSun Mar 6 00:33:07 201619
cmgui-wx-debug-valgrindFailureSun Mar 6 02:22:48 2016433
last breakSun Mar 6 02:15:00 2016433
last successTue Feb 10 02:47:00 2015454
x86_64-linux
cmgui-wxFailureSun Mar 6 00:01:37 20160
last breakSun Mar 6 00:01:00 20160
last successWed Jun 3 00:09:00 20159
cmgui-wx-debugFailureSun Mar 6 00:01:37 20160
last breakSun Mar 6 00:01:00 20160
last successWed Jun 3 00:12:00 201512
cmgui-wx-debug-memorycheckFailureSun Mar 6 00:01:38 20161
last breakSun Mar 6 00:01:00 20161
last successWed Jun 3 00:15:00 201515
cmgui-wx-debug-valgrindFailureSun Mar 6 00:03:11 20169
last breakSun Mar 6 00:03:00 20169
last successWed Jun 3 01:39:00 2015348

Testing status by file:


Html last generated: Sun Mar 6 05:50:59 2016

Input last modified: Fri Apr 20 16:00:29 2012


CMISS Help / Examples / a_backup / optimisation / fitting-2D-linear