Example a/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 mesh2d 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.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 17-Mar-2014 2.8k data.exnode 17-Mar-2014 145k filter.pl 17-Mar-2014 236 mesh.exelem 17-Mar-2014 33k mesh.exnode 17-Mar-2014 6.7k subset.exnode 17-Mar-2014 14k

Download the entire example:

Name                                              Modified     Size

examples_a_optimisation_fitting-2D-linear.tar.gz 09-Mar-2016 1.1M

Testing status by version:

StatusTestedReal time (s)
i686-linux
cmgui-wxFailureSun Mar 6 00:13:16 20162
last breakTue Feb 24 03:11:00 20152
cmgui-wx-debugFailureSun Mar 6 00:09:57 20163
last breakTue Feb 24 03:11:00 20152
cmgui-wx-debug-memorycheckFailureSun Mar 6 00:10:25 20162
last breakTue Feb 24 00:02:00 20153
cmgui-wx-debug-valgrindFailureSun Mar 6 00:48:34 201633
last breakSun Mar 6 00:48:00 201633
x86_64-linux
cmgui-wxFailureSun Mar 6 00:01:31 20160
last breakSun Mar 6 00:01:00 20160
cmgui-wx-debugFailureSun Mar 6 00:01:31 20160
last breakSun Mar 6 00:01:00 20160
cmgui-wx-debug-memorycheckFailureSun Mar 6 00:01:31 20160
last breakSun Mar 6 00:01:00 20160
cmgui-wx-debug-valgrindFailureSun Mar 6 00:03:29 20169
last breakSun Mar 6 00:03:00 20169

Testing status by file:


Html last generated: Wed Mar 9 16:02:25 2016

Input last modified: Wed Mar 9 15:49:41 2016


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