Example a_backup/optimisation/fitting-1D-linear: Fitting a 1-D linear mesh.

This example fits a one-dimensional mesh to a series of data points. The initial and fitted meshes are shown below.
Initial mesh. Mesh fitted using QN method.

Screenshot of example a_backup/optimisation/fitting-1D-linear


The comfile run by this example is as follows:

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

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

# draw the mesh
gfx modify g_element "/" general clear;
gfx modify g_element "/" lines tessellation default select_on material default selected_material default_selected;
gfx modify g_element "/" node_points glyph sphere general size "0.1*0.1*0.1" centre 0,0,0 font default select_on material green selected_material default_selected;
# and the data points
gfx modify g_element "/" data_points coordinate data_coordinates glyph cross general size "0.1*0.1*0.1" centre 0,0,0 font default select_on material gold selected_material default_selected;
gfx create window;
gfx modify window 1 layout 2d ortho_axes -y -z; # 2-D view
gfx modify window 1 node_tool edit; # enable node editing for trying new initial positions
gfx modify window 1 set node_tool; # switch to node tool for node editing

# 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_1d mesh_field coordinates source_field data_coordinates;
# Use of this field as part of the objective means the nearest mesh location is updated during
# optimisation which can be slow for big problems (which this is not), and is susceptible to
# diverging from bad initial positions without addition of some sort of smoothing factors. Refer to
# other optimisation examples e.g. fitting-2D-linear, smooth_volume_fitting for fitting with fixed
# data point projections, or try the 'variant' commands listed later. Example smooth_volume_fitting
# demonstrates how smoothing terms can be added to improve stability, which could be applied to
# this problem e.g. to limit allowable strains in each element during optimisation.

# define a field giving the coordinates at the found_location on the mesh:
gfx define field projected_coordinates embedded element_xi found_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;
# get the magnitude of that vector as a measure of error for each data point:
gfx define field error magnitude field error_vector;

if (0) {
	# variant: store fixed embedding locations so not recalculate during optimisation
	# create and define stored location field at the data points:
	gfx define field stored_location finite_element element_xi;
	gfx modify data all define stored_location;
	# evaluate the current found_location into the stored_location:
	gfx evaluate dgroup "/" destination stored_location source found_location;
	# change the projections to use the stored_location 
	gfx define field projected_coordinates embedded element_xi stored_location field coordinates;
}

#visualise the error bars:
gfx modify g_element "/" data_points coordinate data_coordinates LOCAL glyph line general size "0*0.1*0.1" centre 0,0,0 font default orientation error_vector scale_factors "-1*0*0" select_on material silver selected_material default_selected;

# define a scalar objective function to minimise which is the sum of each of the error terms over the data nodeset:
gfx define field objective_function nodeset_sum field error nodeset cmiss_data;

# 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_lsq nodeset_sum_squares field error_vector nodeset cmiss_data;

# use the quasi-Newton Opt++ method to minimise the objective function field.
# [this method minimises a scalar valued field by altering the provided independent field(s)]
gfx minimise QUASI_NEWTON region "/" objective_fields objective_function independent_fields coordinates hide_output;

if ($TESTING) {
   # write out the fitted nodes for testing
   gfx write node "fitted-qn.exnode"
   
	# perform the same fit using alternative methods
	
	# reset nodes to original locations
	gfx read node "$example/mesh.exnode";

	# Perform least-squares optimisation
	gfx minimise LEAST_SQUARES_QUASI_NEWTON region "/" objective_fields objective_function_lsq independent_fields coordinates hide_output;
	
   # write out the fitted nodes for testing
   gfx write node "fitted-lsq.exnode"

}

Files used by this example are:

Name                   Modified     Size

fitting-1D-linear.com 20-Apr-2012 4.4k data.exnode 20-Apr-2012 490 mesh.exelem 20-Apr-2012 1.3k mesh.exnode 20-Apr-2012 347

Download the entire example:

Name                                                     Modified     Size

examples_a_backup_optimisation_fitting-1D-linear.tar.gz 12-Aug-2014 13k

Testing status by version:

StatusTestedReal time (s)
i686-linux
cmgui-wxSuccessSun Mar 6 00:14:24 20163
cmgui-wx-debugSuccessSun Mar 6 00:11:51 20162
cmgui-wx-debug-memorycheckSuccessSun Mar 6 00:13:46 20163
cmgui-wx-debug-valgrindFailureSun Mar 6 01:12:31 201650
last breakSun Mar 6 01:11:00 201650
last successTue Feb 10 01:04:00 201553
x86_64-linux
cmgui-wxFailureSun Mar 6 00:01:49 20161
last breakSun Mar 6 00:01:00 20161
last successWed Jun 3 00:05:00 20152
cmgui-wx-debugFailureSun Mar 6 00:01:49 20160
last breakSun Mar 6 00:01:00 20160
last successWed Jun 3 00:05:00 20151
cmgui-wx-debug-memorycheckFailureSun Mar 6 00:01:37 20160
last breakSun Mar 6 00:01:00 20160
last successWed Jun 3 00:05:00 20151
cmgui-wx-debug-valgrindFailureSun Mar 6 00:03:11 20169
last breakSun Mar 6 00:03:00 20169
last successWed Jun 3 00:36:00 201542

Testing status by file:


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

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


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