source: issm/trunk-jpl/src/m/classes/model.m

Last change on this file was 28224, checked in by Mathieu Morlighem, 11 months ago

CHG: fixed modeltostruct, skipping subobjects

File size: 80.5 KB
Line 
1%MODEL class definition
2%
3% Usage:
4% md = model(varargin)
5
6classdef model
7 properties (SetAccess=public) %Model fields
8 % {{{
9 %Careful here: no other class should be used as default value this is a bug of matlab
10 mesh = 0;
11 mask = 0;
12
13 geometry = 0;
14 constants = 0;
15 smb = 0;
16 basalforcings = 0;
17 materials = 0;
18 damage = 0;
19 friction = 0;
20 flowequation = 0;
21 timestepping = 0;
22 initialization = 0;
23 rifts = 0;
24 dsl = 0;
25 solidearth = 0;
26
27 debug = 0;
28 verbose = 0;
29 settings = 0;
30 toolkits = 0;
31 cluster = 0;
32
33 balancethickness = 0;
34 stressbalance = 0;
35 groundingline = 0;
36 hydrology = 0;
37 debris = 0;
38 masstransport = 0;
39 thermal = 0;
40 steadystate = 0;
41 transient = 0;
42 levelset = 0;
43 calving = 0;
44 frontalforcings = 0;
45 love = 0;
46 esa = 0;
47 sampling = 0;
48
49 autodiff = 0;
50 inversion = 0;
51 qmu = 0;
52 amr = 0;
53 results = 0;
54 outputdefinition = 0;
55 radaroverlay = 0;
56 miscellaneous = 0;
57 private = 0;
58 stochasticforcing= 0;
59
60 %}}}
61 end
62 methods (Static)
63 function md = loadobj(md) % {{{
64 % This function is directly called by matlab when a model object is
65 % loaded. If the input is a struct it is an old version of model and
66 % old fields must be recovered (make sure they are in the deprecated
67 % model properties)
68
69 if verLessThan('matlab','7.9'),
70 disp('Warning: your matlab version is old and there is a risk that load does not work correctly');
71 disp(' if the model is not loaded correctly, rename temporarily loadobj so that matlab does not use it');
72
73 % This is a Matlab bug: all the fields of md have their default value
74 % Example of error message:
75 % Warning: Error loading an object of class 'model':
76 % Undefined function or method 'exist' for input arguments of type 'cell'
77 %
78 % This has been fixed in MATLAB 7.9 (R2009b) and later versions
79 end
80
81 if isstruct(md)
82 disp('Recovering model object from a previous version');
83 md = structtomodel(model,md);
84 end
85
86 %2012 August 4th
87 if isa(md.materials,'materials'),
88 disp('Recovering old materials');
89 if numel(md.materials.rheology_Z)==1 & isnan(md.materials.rheology_Z),
90 md.materials=matice(md.materials);
91 else
92 md.materials=matdamageice(md.materials);
93 end
94 end
95 %2013 April 12
96 if numel(md.stressbalance.loadingforce==1)
97 md.stressbalance.loadingforce=0*ones(md.mesh.numberofvertices,3);
98 end
99 %2013 April 17
100 if isa(md.hydrology,'hydrology'),
101 disp('Recovering old hydrology class');
102 md.hydrology=hydrologyshreve(md.materials);
103 end
104 %2013 October 9
105 if ~isa(md.damage,'damage'),
106 md.damage=damage();
107 md.damage.D=zeros(md.mesh.numberofvertices,1);
108 md.damage.spcdamage=NaN*ones(md.mesh.numberofvertices,1);
109 end
110 %2013 November 18
111 if ~isa(md.outputdefinition,'outputdefinition'),
112 md.outputdefinition=outputdefinition();
113 end
114 %2014 March 26th
115 if isa(md.mesh,'mesh'),
116 disp('Recovering old mesh class');
117 if isprop(md.mesh,'dimension'),
118 if md.mesh.dimension==2,
119 md.mesh=mesh2d(md.mesh);
120 else
121 md.mesh=mesh3dprisms(md.mesh);
122 end
123 else
124 md.mesh=mesh2dvertical(md.mesh);
125 end
126 end
127 %2014 November 12
128 if isa(md.calving,'double'); md.calving=calving(); end
129 %2016 October 11
130 if isa(md.esa,'double'); md.esa=esa(); end
131 %2017 February 10th
132 if isa(md.settings,'settings'), %this 'isa' verification: 2018 October 24th
133 if md.settings.solver_residue_threshold==0,
134 md.settings.solver_residue_threshold = 1e-6;
135 end
136 end
137 %2017 May 4th
138 if isa(md.amr,'double'); md.amr=amr(); end
139 %2017 Aug 29th
140 if isa(md.love,'double'); md.love=love(); end
141 %2017 Oct 26th
142 if isa(md.calving,'calvingdev')
143 disp('Warning: calvingdev is now calvingvonmises');
144 md.calving=calvingvonmises(md.calving);
145 end
146 %2017 Dec 21st (needs to be here)
147 if isempty(md.settings)
148 disp('Warning: md.settings had to be reset, make sure to adjust md.settings.output_frequency and other fields');
149 md.settings = issmsettings();
150 end
151 %2018 Dec 1st
152 if md.settings.sb_coupling_frequency==0
153 md.settings.sb_coupling_frequency=1;
154 end
155 %2019 Jan..
156 if isa(md.frontalforcings,'double');
157 if(isprop('meltingrate',md.calving) & ~isnan(md.calving.meltingrate))
158 gia disp('Warning: md.calving.meltingrate is now in md.frontalforcings');
159 end
160 md.frontalforcings=frontalforcings(md.calving);
161 end
162 %2019 Feb 26
163 if isa(md.settings.results_on_nodes,'double')
164 if md.settings.results_on_nodes == 0
165 md.settings.results_on_nodes = {};
166 else
167 md.settings.results_on_nodes = {'all'};
168 end
169 end
170 %2019 Mar 28, updated 2021 April 23
171 if isa(md.smb,'SMBcomponents') | isa(md.smb,'SMBmeltcomponents') | isa(md.smb,'SMBforcing') | isa(md.smb,'SMBgemb')
172 if any(strcmp(fieldnames(md.smb),'isclimatology'))
173 if isa(md.smb.isclimatology,'double')
174 if prod(size(md.smb.isclimatology)) ~= 1
175 md.smb.isclimatology = 0;
176 end
177 md.timestepping.cycle_forcing=md.smb.isclimatology;
178 end
179 end
180 end
181 %2019 Dec 16
182 if isa(md.dsl,'double')
183 md.dsl=dsl();
184 end
185 %2020 April 24
186 if isa(md.smb,'SMBgemb')
187 if isa(md.smb.isconstrainsurfaceT,'double')
188 if prod(size(md.smb.isconstrainsurfaceT)) ~= 1
189 md.smb.isconstrainsurfaceT = 0;
190 end
191 end
192 end
193 %2021 February 17
194 if isa(md.sampling,'double'); md.sampling=sampling(); end
195 %VV
196 if ~isa(md.stochasticforcing,'stochasticforcing'); md.stochasticforcing=stochasticforcing(); end
197 %2022 Oct 28
198 if ~isa(md.debris,'debris'); md.debris=debris(); end
199 end% }}}
200 end
201 methods
202 function md = model(varargin) % {{{
203
204 switch nargin
205 case 0
206 md=setdefaultparameters(md,'earth');
207 otherwise
208 options=pairoptions(varargin{:});
209 planet=getfieldvalue(options,'planet','earth');
210 md=setdefaultparameters(md,planet);
211 end
212
213 end
214 %}}}
215 function disp(self) % {{{
216 disp(sprintf('%19s: %-23s -- %s','mesh' ,['[1x1 ' class(self.mesh) ']'],'mesh properties'));
217 disp(sprintf('%19s: %-23s -- %s','mask' ,['[1x1 ' class(self.mask) ']'],'defines grounded and floating elements'));
218 disp(sprintf('%19s: %-23s -- %s','geometry' ,['[1x1 ' class(self.geometry) ']'],'surface elevation, bedrock topography, ice thickness,...'));
219 disp(sprintf('%19s: %-23s -- %s','constants' ,['[1x1 ' class(self.constants) ']'],'physical constants'));
220 disp(sprintf('%19s: %-23s -- %s','smb' ,['[1x1 ' class(self.smb) ']'],'surface mass balance'));
221 disp(sprintf('%19s: %-23s -- %s','basalforcings' ,['[1x1 ' class(self.basalforcings) ']'],'bed forcings'));
222 disp(sprintf('%19s: %-23s -- %s','materials' ,['[1x1 ' class(self.materials) ']'],'material properties'));
223 disp(sprintf('%19s: %-23s -- %s','damage' ,['[1x1 ' class(self.damage) ']'],'parameters for damage evolution solution'));
224 disp(sprintf('%19s: %-23s -- %s','friction' ,['[1x1 ' class(self.friction) ']'],'basal friction/drag properties'));
225 disp(sprintf('%19s: %-23s -- %s','flowequation' ,['[1x1 ' class(self.flowequation) ']'],'flow equations'));
226 disp(sprintf('%19s: %-23s -- %s','timestepping' ,['[1x1 ' class(self.timestepping) ']'],'time stepping for transient models'));
227 disp(sprintf('%19s: %-23s -- %s','initialization' ,['[1x1 ' class(self.initialization) ']'],'initial guess/state'));
228 disp(sprintf('%19s: %-23s -- %s','rifts' ,['[1x1 ' class(self.rifts) ']'],'rifts properties'));
229 disp(sprintf('%19s: %-23s -- %s','solidearth' ,['[1x1 ' class(self.solidearth) ']'],'solidearth inputs and settings'));
230 disp(sprintf('%19s: %-23s -- %s','dsl' ,['[1x1 ' class(self.dsl) ']'],'dynamic sea-level '));
231 disp(sprintf('%19s: %-23s -- %s','debug' ,['[1x1 ' class(self.debug) ']'],'debugging tools (valgrind, gprof)'));
232 disp(sprintf('%19s: %-23s -- %s','verbose' ,['[1x1 ' class(self.verbose) ']'],'verbosity level in solve'));
233 disp(sprintf('%19s: %-23s -- %s','settings' ,['[1x1 ' class(self.settings) ']'],'settings properties'));
234 disp(sprintf('%19s: %-23s -- %s','toolkits' ,['[1x1 ' class(self.toolkits) ']'],'PETSc options for each solution'));
235 disp(sprintf('%19s: %-23s -- %s','cluster' ,['[1x1 ' class(self.cluster) ']'],'cluster parameters (number of CPUs...)'));
236 disp(sprintf('%19s: %-23s -- %s','balancethickness',['[1x1 ' class(self.balancethickness) ']'],'parameters for balancethickness solution'));
237 disp(sprintf('%19s: %-23s -- %s','stressbalance' ,['[1x1 ' class(self.stressbalance) ']'],'parameters for stressbalance solution'));
238 disp(sprintf('%19s: %-23s -- %s','groundingline' ,['[1x1 ' class(self.groundingline) ']'],'parameters for groundingline solution'));
239 disp(sprintf('%19s: %-23s -- %s','hydrology' ,['[1x1 ' class(self.hydrology) ']'],'parameters for hydrology solution'));
240 disp(sprintf('%19s: %-23s -- %s','debris' ,['[1x1 ' class(self.debris) ']'],'parameters for debris solution'));
241 disp(sprintf('%19s: %-23s -- %s','masstransport' ,['[1x1 ' class(self.masstransport) ']'],'parameters for masstransport solution'));
242 disp(sprintf('%19s: %-23s -- %s','thermal' ,['[1x1 ' class(self.thermal) ']'],'parameters for thermal solution'));
243 disp(sprintf('%19s: %-23s -- %s','steadystate' ,['[1x1 ' class(self.steadystate) ']'],'parameters for steadystate solution'));
244 disp(sprintf('%19s: %-23s -- %s','transient' ,['[1x1 ' class(self.transient) ']'],'parameters for transient solution'));
245 disp(sprintf('%19s: %-23s -- %s','levelset' ,['[1x1 ' class(self.levelset) ']'],'parameters for moving boundaries (level-set method)'));
246 disp(sprintf('%19s: %-23s -- %s','calving' ,['[1x1 ' class(self.calving) ']'],'parameters for calving'));
247 disp(sprintf('%19s: %-23s -- %s','frontalforcings' ,['[1x1 ' class(self.frontalforcings) ']'],'parameters for frontalforcings'));
248 disp(sprintf('%19s: %-23s -- %s','esa' ,['[1x1 ' class(self.esa) ']'],'parameters for elastic adjustment solution'));
249 disp(sprintf('%19s: %-23s -- %s','love' ,['[1x1 ' class(self.love) ']'],'parameters for love solution'));
250 disp(sprintf('%19s: %-23s -- %s','sampling' ,['[1x1 ' class(self.sampling) ']'],'parameters for stochastic sampler'));
251 disp(sprintf('%19s: %-23s -- %s','autodiff' ,['[1x1 ' class(self.autodiff) ']'],'automatic differentiation parameters'));
252 disp(sprintf('%19s: %-23s -- %s','inversion' ,['[1x1 ' class(self.inversion) ']'],'parameters for inverse methods'));
253 disp(sprintf('%19s: %-23s -- %s','qmu' ,['[1x1 ' class(self.qmu) ']'],'Dakota properties'));
254 disp(sprintf('%19s: %-23s -- %s','amr' ,['[1x1 ' class(self.amr) ']'],'adaptive mesh refinement properties'));
255 disp(sprintf('%19s: %-23s -- %s','outputdefinition',['[1x1 ' class(self.outputdefinition) ']'],'output definition'));
256 disp(sprintf('%19s: %-23s -- %s','results' ,['[1x1 ' class(self.results) ']'],'model results'));
257 disp(sprintf('%19s: %-23s -- %s','radaroverlay' ,['[1x1 ' class(self.radaroverlay) ']'],'radar image for plot overlay'));
258 disp(sprintf('%19s: %-23s -- %s','miscellaneous' ,['[1x1 ' class(self.miscellaneous) ']'],'miscellaneous fields'));
259 disp(sprintf('%19s: %-23s -- %s','stochasticforcing',['[1x1 ' class(self.stochasticforcing) ']'],'stochasticity applied to model forcings'));
260 end % }}}
261 function md = setdefaultparameters(md,planet) % {{{
262
263 %initialize subclasses
264 md.mesh = mesh2d();
265 md.mask = mask();
266 md.constants = constants();
267 md.geometry = geometry();
268 md.initialization = initialization();
269 md.smb = SMBforcing();
270 md.basalforcings = basalforcings();
271 md.friction = friction();
272 md.rifts = rifts();
273 md.solidearth = solidearth(planet);
274 md.dsl = dsl();
275 md.timestepping = timestepping();
276 md.groundingline = groundingline();
277 md.materials = matice();
278 md.damage = damage();
279 md.flowequation = flowequation();
280 md.debug = debug();
281 md.verbose = verbose();
282 md.settings = issmsettings();
283 md.toolkits = toolkits();
284 md.cluster = generic();
285 md.balancethickness = balancethickness();
286 md.stressbalance = stressbalance();
287 md.hydrology = hydrologyshreve();
288 md.debris = debris();
289 md.masstransport = masstransport();
290 md.thermal = thermal();
291 md.steadystate = steadystate();
292 md.transient = transient();
293 md.levelset = levelset();
294 md.calving = calving();
295 md.frontalforcings = frontalforcings();
296 md.love = love();
297 md.esa = esa();
298 md.sampling = sampling();
299 md.autodiff = autodiff();
300 md.inversion = inversion();
301 md.qmu = qmu();
302 md.amr = amr();
303 md.radaroverlay = radaroverlay();
304 md.results = struct();
305 md.outputdefinition = outputdefinition();
306 md.miscellaneous = miscellaneous();
307 md.private = private();
308 md.stochasticforcing= stochasticforcing();
309 end
310 %}}}
311 function md = checkmessage(md,string) % {{{
312 if(nargout~=1) error('wrong usage, model must be an output'); end
313 disp(['model not consistent: ' string]);
314 md.private.isconsistent=false;
315 end
316 %}}}
317 function md = collapse(md)% {{{
318 %COLLAPSE - collapses a 3d mesh into a 2d mesh
319 %
320 % This routine collapses a 3d model into a 2d model
321 % and collapses all the fields of the 3d model by
322 % taking their depth-averaged values
323 %
324 % Usage:
325 % md=collapse(md)
326 %
327 % See also: EXTRUDE, MODELEXTRACT
328
329 %Check that the model is really a 3d model
330 if ~strcmp(md.mesh.elementtype(),'Penta'),
331 error('collapse error message: only 3d mesh can be collapsed')
332 end
333
334 %Start with changing all the fields from the 3d mesh
335
336 %dealing with the friction law
337 %drag is limited to nodes that are on the bedrock.
338 if isa(md.friction,'friction'),
339 md.friction.coefficient=project2d(md,md.friction.coefficient,1);
340 md.friction.p=project2d(md,md.friction.p,1);
341 md.friction.q=project2d(md,md.friction.q,1);
342 elseif isa(md.friction,'frictioncoulomb'),
343 md.friction.coefficient=project2d(md,md.friction.coefficient,1);
344 md.friction.coefficientcoulomb=project2d(md,md.friction.coefficientcoulomb,1);
345 md.friction.p=project2d(md,md.friction.p,1);
346 md.friction.q=project2d(md,md.friction.q,1);
347 elseif isa(md.friction,'frictionhydro'),
348 md.friction.q=project2d(md,md.friction.q,1);
349 md.friction.C=project2d(md,md.friction.C,1);
350 md.friction.As=project2d(md,md.friction.As,1);
351 md.friction.effective_pressure=project2d(md,md.friction.effective_pressure,1);
352 elseif isa(md.friction,'frictionwaterlayer'),
353 md.friction.coefficient=project2d(md,md.friction.coefficient,1);
354 md.friction.p=project2d(md,md.friction.p,1);
355 md.friction.q=project2d(md,md.friction.q,1);
356 md.friction.water_layer=project2d(md,md.friction.water_layer,1);
357 elseif isa(md.friction,'frictionweertman'),
358 md.friction.C=project2d(md,md.friction.C,1);
359 md.friction.m=project2d(md,md.friction.m,1);
360 elseif isa(md.friction,'frictionweertmantemp'),
361 md.friction.C=project2d(md,md.friction.C,1);
362 md.friction.m=project2d(md,md.friction.m,1);
363 elseif isa(md.friction,'frictionjosh'),
364 md.friction.coefficient=project2d(md,md.friction.coefficient,1);
365 md.friction.pressure_adjusted_temperature=project2d(md,md.friction.pressure_adjusted_temperature,1);
366 else
367 disp('friction type not supported');
368 end
369
370 %observations
371 if ~isnan(md.inversion.vx_obs),
372 md.inversion.vx_obs=project2d(md,md.inversion.vx_obs,md.mesh.numberoflayers);
373 end
374 if ~isnan(md.inversion.vy_obs),
375 md.inversion.vy_obs=project2d(md,md.inversion.vy_obs,md.mesh.numberoflayers);
376 end
377 if ~isnan(md.inversion.vel_obs),
378 md.inversion.vel_obs=project2d(md,md.inversion.vel_obs,md.mesh.numberoflayers);
379 end
380 if ~isnan(md.inversion.thickness_obs),
381 md.inversion.thickness_obs=project2d(md,md.inversion.thickness_obs,md.mesh.numberoflayers);
382 end
383 if ~isnan(md.inversion.cost_functions_coefficients),
384 md.inversion.cost_functions_coefficients=project2d(md,md.inversion.cost_functions_coefficients,md.mesh.numberoflayers);
385 end
386 if numel(md.inversion.min_parameters)>1,
387 md.inversion.min_parameters=project2d(md,md.inversion.min_parameters,md.mesh.numberoflayers);
388 end
389 if numel(md.inversion.max_parameters)>1,
390 md.inversion.max_parameters=project2d(md,md.inversion.max_parameters,md.mesh.numberoflayers);
391 end
392 if isa(md.smb,'SMBforcing') & ~isnan(md.smb.mass_balance),
393 md.smb.mass_balance=project2d(md,md.smb.mass_balance,md.mesh.numberoflayers);
394 elseif isa(md.smb,'SMBhenning') & ~isnan(md.smb.smbref),
395 md.smb.smbref=project2d(md,md.smb.smbref,md.mesh.numberoflayers);
396 end
397
398 %results
399 if ~isnan(md.initialization.vx),
400 md.initialization.vx=DepthAverage(md,md.initialization.vx);
401 end
402 if ~isnan(md.initialization.vy),
403 md.initialization.vy=DepthAverage(md,md.initialization.vy);
404 end
405 if ~isnan(md.initialization.vz),
406 md.initialization.vz=DepthAverage(md,md.initialization.vz);
407 end
408 if ~isnan(md.initialization.vel),
409 md.initialization.vel=DepthAverage(md,md.initialization.vel);
410 end
411 if ~isnan(md.initialization.temperature),
412 md.initialization.temperature=DepthAverage(md,md.initialization.temperature);
413 end
414 if ~isnan(md.initialization.pressure),
415 md.initialization.pressure=project2d(md,md.initialization.pressure,1);
416 end
417 if ~isnan(md.initialization.sediment_head),
418 md.initialization.sediment_head=project2d(md,md.initialization.sediment_head,1);
419 end
420 if ~isnan(md.initialization.epl_head),
421 md.initialization.epl_head=project2d(md,md.initialization.epl_head,1);
422 end
423 if ~isnan(md.initialization.epl_thickness),
424 md.initialization.epl_thickness=project2d(md,md.initialization.epl_thickness,1);
425 end
426 if ~isnan(md.initialization.waterfraction),
427 md.initialization.waterfraction=project2d(md,md.initialization.waterfraction,1);
428 end
429 if ~isnan(md.initialization.watercolumn),
430 md.initialization.watercolumn=project2d(md,md.initialization.watercolumn,1);
431 end
432 if ~isnan(md.initialization.debris),
433 md.initialization.debris=project2d(md,md.initialization.debris,1);
434 end
435
436
437 %elementstype
438 if ~isnan(md.flowequation.element_equation)
439 md.flowequation.element_equation=project2d(md,md.flowequation.element_equation,1);
440 md.flowequation.vertex_equation=project2d(md,md.flowequation.vertex_equation,1);
441 md.flowequation.borderSSA=project2d(md,md.flowequation.borderSSA,1);
442 md.flowequation.borderHO=project2d(md,md.flowequation.borderHO,1);
443 md.flowequation.borderFS=project2d(md,md.flowequation.borderFS,1);
444 end
445
446 %boundary conditions
447 md.stressbalance.spcvx=project2d(md,md.stressbalance.spcvx,md.mesh.numberoflayers);
448 md.stressbalance.spcvy=project2d(md,md.stressbalance.spcvy,md.mesh.numberoflayers);
449 md.stressbalance.spcvz=project2d(md,md.stressbalance.spcvz,md.mesh.numberoflayers);
450 md.stressbalance.referential=project2d(md,md.stressbalance.referential,md.mesh.numberoflayers);
451 md.stressbalance.loadingforce=project2d(md,md.stressbalance.loadingforce,md.mesh.numberoflayers);
452 if numel(md.masstransport.spcthickness)>1,
453 md.masstransport.spcthickness=project2d(md,md.masstransport.spcthickness,md.mesh.numberoflayers);
454 end
455 if numel(md.damage.spcdamage)>1,
456 md.damage.spcdamage=project2d(md,md.damage.spcdamage,md.mesh.numberoflayers);
457 end
458 if numel(md.levelset.spclevelset)>1,
459 md.levelset.spclevelset=project2d(md,md.levelset.spclevelset,md.mesh.numberoflayers);
460 end
461 md.thermal.spctemperature=project2d(md,md.thermal.spctemperature,md.mesh.numberoflayers);
462
463 % Hydrologydc variables
464 if isa(md.hydrology,'hydrologydc');
465 md.hydrology.spcsediment_head=project2d(md,md.hydrology.spcsediment_head,1);
466 md.hydrology.mask_eplactive_node=project2d(md,md.hydrology.mask_eplactive_node,1);
467 md.hydrology.sediment_transmitivity=project2d(md,md.hydrology.sediment_transmitivity,1);
468 md.hydrology.basal_moulin_input=project2d(md,md.hydrology.basal_moulin_input,1);
469 if(md.hydrology.isefficientlayer==1)
470 md.hydrology.spcepl_head=project2d(md,md.hydrology.spcepl_head,1);
471 end
472 end
473
474 %materials
475 md.materials.rheology_B=DepthAverage(md,md.materials.rheology_B);
476 md.materials.rheology_n=project2d(md,md.materials.rheology_n,1);
477 if isprop(md.materials,'rheology_E')
478 md.materials.rheology_E=project2d(md,md.materials.rheology_E,1);
479 end
480
481 %damage:
482 if md.damage.isdamage,
483 md.damage.D=DepthAverage(md,md.damage.D);
484 end
485
486 %special for thermal modeling:
487 if ~isnan(md.basalforcings.groundedice_melting_rate),
488 md.basalforcings.groundedice_melting_rate=project2d(md,md.basalforcings.groundedice_melting_rate,1);
489 end
490 if isprop(md.basalforcings,'floatingice_melting_rate') & ~isnan(md.basalforcings.floatingice_melting_rate),
491 md.basalforcings.floatingice_melting_rate=project2d(md,md.basalforcings.floatingice_melting_rate,1);
492 end
493 md.basalforcings.geothermalflux=project2d(md,md.basalforcings.geothermalflux,1); %bedrock only gets geothermal flux
494
495 if isprop(md.calving,'coeff') & ~isnan(md.calving.coeff),
496 md.calving.coeff=project2d(md,md.calving.coeff,1);
497 end
498 if isprop(md.frontalforcings,'meltingrate') & ~isnan(md.frontalforcings.meltingrate),
499 md.frontalforcings.meltingrate=project2d(md,md.frontalforcings.meltingrate,1);
500 end
501
502 %update of connectivity matrix
503 md.mesh.average_vertex_connectivity=25;
504
505 %Collapse the mesh
506 nodes2d=md.mesh.numberofvertices2d;
507 elements2d=md.mesh.numberofelements2d;
508
509 %parameters
510 md.geometry.surface=project2d(md,md.geometry.surface,1);
511 md.geometry.thickness=project2d(md,md.geometry.thickness,1);
512 md.geometry.base=project2d(md,md.geometry.base,1);
513 if ~isnan(md.geometry.bed),
514 md.geometry.bed=project2d(md,md.geometry.bed,1);
515 end
516 if ~isnan(md.mask.ocean_levelset),
517 md.mask.ocean_levelset=project2d(md,md.mask.ocean_levelset,1);
518 end
519 if ~isnan(md.mask.ice_levelset),
520 md.mask.ice_levelset=project2d(md,md.mask.ice_levelset,1);
521 end
522
523 %lat long
524 if numel(md.mesh.lat)==md.mesh.numberofvertices,
525 md.mesh.lat=project2d(md,md.mesh.lat,1);
526 end
527 if numel(md.mesh.long)==md.mesh.numberofvertices,
528 md.mesh.long=project2d(md,md.mesh.long,1);
529 end
530
531 %outputdefinitions
532 for i=1:length(md.outputdefinition.definitions)
533 if isobject(md.outputdefinition.definitions{i})
534 %get subfields
535 solutionsubfields=fields(md.outputdefinition.definitions{i});
536 for j=1:length(solutionsubfields),
537 field=md.outputdefinition.definitions{i}.(solutionsubfields{j});
538 if length(field)==md.mesh.numberofvertices | length(field)==md.mesh.numberofelements,
539 md.outputdefinition.definitions{i}.(solutionsubfields{j})=project2d(md,md.outputdefinition.definitions{i}.(solutionsubfields{j}),1);
540 end
541 end
542 end
543 end
544
545 %Initialize 2d mesh
546 mesh=mesh2d();
547 mesh.x=md.mesh.x2d;
548 mesh.y=md.mesh.y2d;
549 mesh.numberofvertices=md.mesh.numberofvertices2d;
550 mesh.numberofelements=md.mesh.numberofelements2d;
551 mesh.elements=md.mesh.elements2d;
552 if numel(md.mesh.lat)==md.mesh.numberofvertices,
553 mesh.lat=project2d(md,md.mesh.lat,1);
554 end
555 if numel(md.mesh.long)==md.mesh.numberofvertices,
556 mesh.long=project2d(md,md.mesh.long,1);
557 end
558 mesh.epsg=md.mesh.epsg;
559 if numel(md.mesh.scale_factor)==md.mesh.numberofvertices,
560 mesh.scale_factor=project2d(md,md.mesh.scale_factor,1);
561 end
562 if ~isnan(md.mesh.vertexonboundary),
563 mesh.vertexonboundary=project2d(md,md.mesh.vertexonboundary,1);
564 end
565 if ~isnan(md.mesh.elementconnectivity),
566 mesh.elementconnectivity=project2d(md,md.mesh.elementconnectivity,1);
567 end
568 md.mesh=mesh;
569 md.mesh.vertexconnectivity=NodeConnectivity(md.mesh.elements,md.mesh.numberofvertices);
570 md.mesh.elementconnectivity=ElementConnectivity(md.mesh.elements,md.mesh.vertexconnectivity);
571 md.mesh.segments=contourenvelope(md.mesh);
572
573 end % }}}
574 function md2 = extract(md,area,varargin) % {{{
575 %extract - extract a model according to an Argus contour or flag list
576 %
577 % This routine extracts a submodel from a bigger model with respect to a given contour
578 % md must be followed by the corresponding exp file or flags list
579 % It can either be a domain file (argus type, .exp extension), or an array of element flags.
580 % If user wants every element outside the domain to be
581 % extract2d, add '~' to the name of the domain file (ex: '~HO.exp');
582 % an empty string '' will be considered as an empty domain
583 % a string 'all' will be considered as the entire domain
584 %
585 % Usage:
586 % md2=extract(md,area);
587 %
588 % Examples:
589 % md2=extract(md,'Domain.exp');
590 %
591 % See also: EXTRUDE, COLLAPSE
592
593 %copy model
594 md1=md;
595
596 %recover optoins:
597 options=pairoptions(varargin{:});
598
599 %some checks
600 if ((nargin<2) | (nargout~=1)),
601 help extract
602 error('extract error message: bad usage');
603 end
604
605 %get elements that are inside area
606 flag_elem=FlagElements(md1,area);
607 if ~any(flag_elem),
608 error('extracted model is empty');
609 end
610
611 %kick out all elements with 3 dirichlets
612 if getfieldvalue(options,'spccheck',1)
613 spc_elem=find(~flag_elem);
614 spc_node=sort(unique(md1.mesh.elements(spc_elem,:)));
615 flag=ones(md1.mesh.numberofvertices,1);
616 flag(spc_node)=0;
617 pos=find(sum(flag(md1.mesh.elements),2)==0);
618 flag_elem(pos)=0;
619 end
620
621 %extracted elements and nodes lists
622 pos_elem=find(flag_elem);
623 pos_node=sort(unique(md1.mesh.elements(pos_elem,:)));
624
625 %keep track of some fields
626 numberofvertices1=md1.mesh.numberofvertices;
627 numberofelements1=md1.mesh.numberofelements;
628 numberofvertices2=length(pos_node);
629 numberofelements2=length(pos_elem);
630 flag_node=zeros(numberofvertices1,1);
631 flag_node(pos_node)=1;
632
633 %Create Pelem and Pnode (transform old nodes in new nodes and same thing for the elements)
634 Pelem=zeros(numberofelements1,1);
635 Pelem(pos_elem)=[1:numberofelements2]';
636 Pnode=zeros(numberofvertices1,1);
637 Pnode(pos_node)=[1:numberofvertices2]';
638
639 %renumber the elements (some nodes won't exist anymore)
640 elements_1=md1.mesh.elements;
641 elements_2=elements_1(pos_elem,:);
642 elements_2(:,1)=Pnode(elements_2(:,1));
643 elements_2(:,2)=Pnode(elements_2(:,2));
644 elements_2(:,3)=Pnode(elements_2(:,3));
645 if isa(md1.mesh,'mesh3dprisms'),
646 elements_2(:,4)=Pnode(elements_2(:,4));
647 elements_2(:,5)=Pnode(elements_2(:,5));
648 elements_2(:,6)=Pnode(elements_2(:,6));
649 end
650
651 %OK, now create the new model!
652
653 %take every field from model
654 md2=md1;
655
656 %automatically modify fields
657
658 %loop over model fields
659 model_fields=fields(md1);
660 for i=1:length(model_fields),
661 %get field
662 field=md1.(model_fields{i});
663 fieldsize=size(field);
664 if isobject(field), %recursive call
665 object_fields=fields(md1.(model_fields{i}));
666 for j=1:length(object_fields),
667 %get field
668 field=md1.(model_fields{i}).(object_fields{j});
669 fieldsize=size(field);
670 %size = number of nodes * n
671 if fieldsize(1)==numberofvertices1
672 md2.(model_fields{i}).(object_fields{j})=field(pos_node,:);
673 elseif (fieldsize(1)==numberofvertices1+1)
674 md2.(model_fields{i}).(object_fields{j})=[field(pos_node,:); field(end,:)];
675 %size = number of elements * n
676 elseif fieldsize(1)==numberofelements1
677 md2.(model_fields{i}).(object_fields{j})=field(pos_elem,:);
678 elseif (fieldsize(1)==numberofelements1+1)
679 md2.(model_fields{i}).(object_fields{j})=[field(pos_elem,:); field(end,:)];
680 end
681 end
682 else
683 %size = number of nodes * n
684 if fieldsize(1)==numberofvertices1
685 md2.(model_fields{i})=field(pos_node,:);
686 elseif (fieldsize(1)==numberofvertices1+1)
687 md2.(model_fields{i})=[field(pos_node,:); field(end,:)];
688 %size = number of elements * n
689 elseif fieldsize(1)==numberofelements1
690 md2.(model_fields{i})=field(pos_elem,:);
691 elseif (fieldsize(1)==numberofelements1+1)
692 md2.(model_fields{i})=[field(pos_elem,:); field(end,:)];
693 end
694 end
695 end
696
697 %modify some specific fields
698
699 %Mesh
700 md2.mesh.numberofelements=numberofelements2;
701 md2.mesh.numberofvertices=numberofvertices2;
702 md2.mesh.elements=elements_2;
703
704 %mesh.uppervertex mesh.lowervertex
705 if isa(md1.mesh,'mesh3dprisms'),
706 md2.mesh.uppervertex=md1.mesh.uppervertex(pos_node);
707 pos=find(~isnan(md2.mesh.uppervertex));
708 md2.mesh.uppervertex(pos)=Pnode(md2.mesh.uppervertex(pos));
709
710 md2.mesh.lowervertex=md1.mesh.lowervertex(pos_node);
711 pos=find(~isnan(md2.mesh.lowervertex));
712 md2.mesh.lowervertex(pos)=Pnode(md2.mesh.lowervertex(pos));
713
714 md2.mesh.upperelements=md1.mesh.upperelements(pos_elem);
715 pos=find(~isnan(md2.mesh.upperelements));
716 md2.mesh.upperelements(pos)=Pelem(md2.mesh.upperelements(pos));
717
718 md2.mesh.lowerelements=md1.mesh.lowerelements(pos_elem);
719 pos=find(~isnan(md2.mesh.lowerelements));
720 md2.mesh.lowerelements(pos)=Pelem(md2.mesh.lowerelements(pos));
721 end
722
723 %Initial 2d mesh
724 if isa(md1.mesh,'mesh3dprisms'),
725 flag_elem_2d=flag_elem(1:md1.mesh.numberofelements2d);
726 pos_elem_2d=find(flag_elem_2d);
727 flag_node_2d=flag_node(1:md1.mesh.numberofvertices2d);
728 pos_node_2d=find(flag_node_2d);
729
730 md2.mesh.numberofelements2d=length(pos_elem_2d);
731 md2.mesh.numberofvertices2d=length(pos_node_2d);
732 md2.mesh.elements2d=md1.mesh.elements2d(pos_elem_2d,:);
733 md2.mesh.elements2d(:,1)=Pnode(md2.mesh.elements2d(:,1));
734 md2.mesh.elements2d(:,2)=Pnode(md2.mesh.elements2d(:,2));
735 md2.mesh.elements2d(:,3)=Pnode(md2.mesh.elements2d(:,3));
736
737 md2.mesh.x2d=md1.mesh.x(pos_node_2d);
738 md2.mesh.y2d=md1.mesh.y(pos_node_2d);
739 end
740
741 %Edges
742 if(dimension(md.mesh)==2),
743 if size(md2.mesh.edges,2)>1, %do not use ~isnan because there are some NaNs...
744 %renumber first two columns
745 pos=find(md2.mesh.edges(:,4)~=-1);
746 md2.mesh.edges(: ,1)=Pnode(md2.mesh.edges(:,1));
747 md2.mesh.edges(: ,2)=Pnode(md2.mesh.edges(:,2));
748 md2.mesh.edges(: ,3)=Pelem(md2.mesh.edges(:,3));
749 md2.mesh.edges(pos,4)=Pelem(md2.mesh.edges(pos,4));
750 %remove edges when the 2 vertices are not in the domain.
751 md2.mesh.edges=md2.mesh.edges(find(md2.mesh.edges(:,1) & md2.mesh.edges(:,2)),:);
752 %Replace all zeros by -1 in the last two columns
753 pos=find(md2.mesh.edges(:,3)==0);
754 md2.mesh.edges(pos,3)=-1;
755 pos=find(md2.mesh.edges(:,4)==0);
756 md2.mesh.edges(pos,4)=-1;
757 %Invert -1 on the third column with last column (Also invert first two columns!!)
758 pos=find(md2.mesh.edges(:,3)==-1);
759 md2.mesh.edges(pos,3)=md2.mesh.edges(pos,4);
760 md2.mesh.edges(pos,4)=-1;
761 values=md2.mesh.edges(pos,2);
762 md2.mesh.edges(pos,2)=md2.mesh.edges(pos,1);
763 md2.mesh.edges(pos,1)=values;
764 %Finally remove edges that do not belong to any element
765 pos=find(md2.mesh.edges(:,3)==-1 & md2.mesh.edges(:,4)==-1);
766 md2.mesh.edges(pos,:)=[];
767 end
768 end
769
770 %Penalties
771 if ~isnan(md2.stressbalance.vertex_pairing),
772 for i=1:size(md1.stressbalance.vertex_pairing,1);
773 md2.stressbalance.vertex_pairing(i,:)=Pnode(md1.stressbalance.vertex_pairing(i,:));
774 end
775 md2.stressbalance.vertex_pairing=md2.stressbalance.vertex_pairing(find(md2.stressbalance.vertex_pairing(:,1)),:);
776 end
777 if ~isnan(md2.masstransport.vertex_pairing),
778 for i=1:size(md1.masstransport.vertex_pairing,1);
779 md2.masstransport.vertex_pairing(i,:)=Pnode(md1.masstransport.vertex_pairing(i,:));
780 end
781 md2.masstransport.vertex_pairing=md2.masstransport.vertex_pairing(find(md2.masstransport.vertex_pairing(:,1)),:);
782 end
783
784 %recreate segments
785 if isa(md1.mesh,'mesh2d') | isa(md1.mesh','mesh3dsurface'),
786 md2.mesh.vertexconnectivity=NodeConnectivity(md2.mesh.elements,md2.mesh.numberofvertices);
787 md2.mesh.elementconnectivity=ElementConnectivity(md2.mesh.elements,md2.mesh.vertexconnectivity);
788 md2.mesh.segments=contourenvelope(md2.mesh);
789 md2.mesh.vertexonboundary=zeros(numberofvertices2,1);
790 md2.mesh.vertexonboundary(md2.mesh.segments(:,1:2))=1;
791 else
792 %First do the connectivity for the contourenvelope in 2d
793 md2.mesh.vertexconnectivity=NodeConnectivity(md2.mesh.elements2d,md2.mesh.numberofvertices2d);
794 md2.mesh.elementconnectivity=ElementConnectivity(md2.mesh.elements2d,md2.mesh.vertexconnectivity);
795 segments=contourenvelope(md2.mesh);
796 md2.mesh.vertexonboundary=zeros(numberofvertices2/md2.mesh.numberoflayers,1);
797 md2.mesh.vertexonboundary(segments(:,1:2))=1;
798 md2.mesh.vertexonboundary=repmat(md2.mesh.vertexonboundary,md2.mesh.numberoflayers,1);
799 %Then do it for 3d as usual
800 md2.mesh.vertexconnectivity=NodeConnectivity(md2.mesh.elements,md2.mesh.numberofvertices);
801 md2.mesh.elementconnectivity=ElementConnectivity(md2.mesh.elements,md2.mesh.vertexconnectivity);
802 end
803
804 %Boundary conditions: Dirichlets on new boundary
805 %Catch the elements that have not been extracted
806 orphans_elem=find(~flag_elem);
807 orphans_node=unique(md1.mesh.elements(orphans_elem,:))';
808 %Figure out which node are on the boundary between md2 and md1
809 nodestoflag1=intersect(orphans_node,pos_node);
810 nodestoflag2=Pnode(nodestoflag1);
811 if numel(md1.stressbalance.spcvx)>1 & numel(md1.stressbalance.spcvy)>1 & numel(md1.stressbalance.spcvz)>1,
812 if isprop(md1.inversion,'vx_obs') & numel(md1.inversion.vx_obs)>1 & numel(md1.inversion.vy_obs)>1
813 md2.stressbalance.spcvx(nodestoflag2)=md2.inversion.vx_obs(nodestoflag2);
814 md2.stressbalance.spcvy(nodestoflag2)=md2.inversion.vy_obs(nodestoflag2);
815 %MOLHO
816 md2.stressbalance.spcvx_base(nodestoflag2)=md2.inversion.vx_obs(nodestoflag2);
817 md2.stressbalance.spcvy_base(nodestoflag2)=md2.inversion.vy_obs(nodestoflag2);
818 md2.stressbalance.spcvx_shear(nodestoflag2)=0.;
819 md2.stressbalance.spcvy_shear(nodestoflag2)=0.;
820 else
821 md2.stressbalance.spcvx(nodestoflag2)=NaN;
822 md2.stressbalance.spcvy(nodestoflag2)=NaN;
823 disp(' ')
824 disp('!! extract warning: spc values should be checked !!')
825 disp(' ')
826 end
827 %put 0 for vz
828 md2.stressbalance.spcvz(nodestoflag2)=0;
829 end
830 if ~isnan(md1.thermal.spctemperature),
831 md2.thermal.spctemperature(nodestoflag2,1)=1;
832 end
833
834 %Results fields
835 if isstruct(md1.results),
836 md2.results=struct();
837 solutionfields=fields(md1.results);
838 for i=1:length(solutionfields),
839 if isstruct(md1.results.(solutionfields{i}))
840 %get subfields
841 % loop over time steps
842 for p=1:length(md1.results.(solutionfields{i}))
843 current = md1.results.(solutionfields{i})(p);
844 solutionsubfields=fields(current);
845 for j=1:length(solutionsubfields),
846 field=md1.results.(solutionfields{i})(p).(solutionsubfields{j});
847 if length(field)==numberofvertices1,
848 md2.results.(solutionfields{i})(p).(solutionsubfields{j})=field(pos_node);
849 elseif length(field)==numberofelements1,
850 md2.results.(solutionfields{i})(p).(solutionsubfields{j})=field(pos_elem);
851 else
852 md2.results.(solutionfields{i})(p).(solutionsubfields{j})=field;
853 end
854 end
855 end
856 else
857 field=md1.results.(solutionfields{i});
858 if length(field)==numberofvertices1,
859 md2.results.(solutionfields{i})=field(pos_node);
860 elseif length(field)==numberofelements1,
861 md2.results.(solutionfields{i})=field(pos_elem);
862 else
863 md2.results.(solutionfields{i})=field;
864 end
865 end
866 end
867 end
868
869 %OutputDefinitions fields
870 for i=1:length(md1.outputdefinition.definitions),
871 if isobject(md1.outputdefinition.definitions{i})
872 %get subfields
873 solutionsubfields=fields(md1.outputdefinition.definitions{i});
874 for j=1:length(solutionsubfields),
875 field=md1.outputdefinition.definitions{i}.(solutionsubfields{j});
876 if length(field)==numberofvertices1,
877 md2.outputdefinition.definitions{i}.(solutionsubfields{j})=field(pos_node);
878 elseif length(field)==numberofelements1,
879 md2.outputdefinition.definitions{i}.(solutionsubfields{j})=field(pos_elem);
880 elseif size(field,1)==numberofvertices1+1
881 md2.outputdefinition.definitions{i}.(solutionsubfields{j})=[field(pos_node,:); field(end,:)];
882 end
883 end
884 end
885 end
886
887 %independents
888 for i=1:length(md1.autodiff.independents)
889 independentfield=fields(md1.autodiff.independents{i});
890 for j=1:length(independentfield)
891 field=md1.autodiff.independents{i}.(independentfield{j});
892 if length(field)==numberofvertices1
893 md2.autodiff.independents{i}.(independentfield{j})=field(pos_node);
894 elseif length(field)==numberofelements1
895 md2.autodiff.independents{i}.(independentfield{j})=field(pos_elem);
896 end
897 end
898 end
899
900 %Keep track of pos_node and pos_elem
901 md2.mesh.extractedvertices=pos_node;
902 md2.mesh.extractedelements=pos_elem;
903 end % }}}
904 function md2 = refine(md) % {{{
905 %refine - split all triangles into 3 to refine the mesh everywhere
906 %
907 % This function only works for 2d triangle meshes
908 %
909 % Usage:
910 % md2=refine(md);
911 %
912 % See also: EXTRUDE, COLLAPSE, EXTRACT
913
914 %Check incoming
915 if ~strcmp(elementtype(md.mesh),'Tria')
916 error('not supported for 3d meshes');
917 end
918
919 %copy model
920 md2=md;
921
922 disp('Getting edges');
923 %initialization of some variables
924 nbe = md.mesh.numberofelements;
925 nbv = md.mesh.numberofvertices;
926 index = md.mesh.elements;
927 elementslist=1:nbe;
928 %1: list of edges
929 edges=[index(:,[1,2]); index(:,[2,3]); index(:,[3,1])];
930 %2: find unique edges
931 [edges,I,J]=unique(sort(edges,2),'rows');
932 %3: unique edge numbers
933 vec=J;
934 %4: unique edges numbers in each triangle (2 triangles sharing the same edge will have the same edge number)
935 edges_tria=[vec(elementslist+nbe) vec(elementslist+2*nbe) vec(elementslist)];
936
937 % We divide each element as follows
938 %
939 % e2
940 % n1 ------------+------------ n3
941 % \ / \ /
942 % \ 1 / \ 3 /
943 % \ / \ /
944 % \ / 2 \ /
945 % \ / \ /
946 % e3 +____________\/ e1
947 % \ /
948 % \ /
949 % \ 4 /
950 % \ /
951 % \ /
952 % n2
953
954 %Create new coordinates
955 disp('Remeshing...');
956 x_edges = 0.5*(md.mesh.x(edges(:,1)) + md.mesh.x(edges(:,2)));
957 y_edges = 0.5*(md.mesh.y(edges(:,1)) + md.mesh.y(edges(:,2)));
958 xnew = [md2.mesh.x;x_edges];
959 ynew = [md2.mesh.y;y_edges];
960 indexnew = [...
961 index(:,1) nbv+edges_tria(:,3) nbv+edges_tria(:,2);...
962 nbv+edges_tria(:,2) nbv+edges_tria(:,3) nbv+edges_tria(:,1);...
963 nbv+edges_tria(:,2) nbv+edges_tria(:,1) index(:,3);...
964 nbv+edges_tria(:,3) index(:,2) nbv+edges_tria(:,1)];
965 %md2.mesh.numberofelements = 4*nbe;
966 %md2.mesh.numberofvertices = nbv + size(edges,1);
967
968 %Call Bamg to update other mesh properties
969 [bamgmesh_out bamggeom_out]=BamgConvertMesh(indexnew,xnew,ynew);
970 md2.mesh.x = bamgmesh_out.Vertices(:,1);
971 md2.mesh.y = bamgmesh_out.Vertices(:,2);
972 md2.mesh.elements = bamgmesh_out.Triangles(:,1:3);
973 md2.mesh.edges = bamgmesh_out.IssmEdges;
974 md2.mesh.segments = bamgmesh_out.IssmSegments(:,1:3);
975 md2.mesh.segmentmarkers = bamgmesh_out.IssmSegments(:,4);
976 md2.mesh.numberofelements = size(md2.mesh.elements,1);
977 md2.mesh.numberofvertices = length(md2.mesh.x);
978 md2.mesh.numberofedges = size(md2.mesh.edges,1);
979 md2.mesh.vertexonboundary = zeros(md2.mesh.numberofvertices,1); md2.mesh.vertexonboundary(md2.mesh.segments(:,1:2)) = 1;
980
981 %Deal with boundary
982 md2.mesh.vertexonboundary = [md.mesh.vertexonboundary;sum(md.mesh.vertexonboundary(edges),2)==2];
983 md2.mesh.elementconnectivity=bamgmesh_out.ElementConnectivity;
984 md2.mesh.elementconnectivity(find(isnan(md2.mesh.elementconnectivity)))=0;
985 disp([' Old number of elements: ' num2str(nbe)]);
986 disp([' New number of elements: ' num2str(4*nbe)]);
987
988 disp('Interpolate all fields');
989 numberofvertices1 = md.mesh.numberofvertices;
990 numberofelements1 = md.mesh.numberofelements;
991 nbv2 = md2.mesh.numberofvertices;
992
993 %Create transformation vectors
994 nbedges = size(edges,1);
995 Pelem = sparse(1:4*nbe,repmat([1:nbe],1,4),ones(4*nbe,1),4*nbe,nbe);
996 Pnode = sparse([1:nbv,repmat([nbv+1:nbv+nbedges],1,2)],[1:nbv edges(:)'],[ones(nbv,1);1/2*ones(2*nbedges,1)],md2.mesh.numberofvertices,nbv);
997
998 %Deal with mesh
999 if numel(md.mesh.scale_factor)==md.mesh.numberofvertices
1000 md2.mesh.scale_factor=Pnode*md.mesh.scale_factor;
1001 end
1002
1003 %loop over model fields
1004 model_fields=setxor(fields(md),{'mesh'});
1005 %remove mesh from this field
1006 for i=1:length(model_fields),
1007 %get field
1008 field=md.(model_fields{i});
1009 fieldsize=size(field);
1010 if isobject(field), %recursive call
1011 object_fields=fields(md.(model_fields{i}));
1012 for j=1:length(object_fields),
1013 %get field
1014 field=md.(model_fields{i}).(object_fields{j});
1015 fieldsize=size(field);
1016 %size = number of nodes * n
1017 if fieldsize(1)==numberofvertices1
1018 md2.(model_fields{i}).(object_fields{j})=Pnode*field;
1019 elseif (fieldsize(1)==numberofvertices1+1)
1020 md2.(model_fields{i}).(object_fields{j})=[Pnode*field(1:end-1,:); field(end,:)];
1021 %size = number of elements * n
1022 elseif fieldsize(1)==numberofelements1
1023 md2.(model_fields{i}).(object_fields{j})=Pelem*field;
1024 elseif (fieldsize(1)==numberofelements1+1)
1025 md2.(model_fields{i}).(object_fields{j})=[Pelem*field(1:end-1,:); field(end,:)];
1026 end
1027 end
1028 else
1029 %size = number of nodes * n
1030 if fieldsize(1)==numberofvertices1
1031 md2.(model_fields{i})=Pnode*field;
1032 elseif (fieldsize(1)==numberofvertices1+1)
1033 md2.(model_fields{i})=[Pnode*field(1:end-1,:); field(end,:)];
1034 %size = number of elements * n
1035 elseif fieldsize(1)==numberofelements1
1036 md2.(model_fields{i})=Pelem*field;
1037 elseif (fieldsize(1)==numberofelements1+1)
1038 md2.(model_fields{i})=[Pelem*field(1:end-1,:); field(end,:)];
1039 end
1040 end
1041 end
1042
1043 end % }}}
1044 function md = extrude(md,varargin) % {{{
1045 %EXTRUDE - vertically extrude a 2d mesh
1046 %
1047 % vertically extrude a 2d mesh and create corresponding 3d mesh.
1048 % The vertical distribution can:
1049 % - follow a polynomial law
1050 % - follow two polynomial laws, one for the lower part and one for the upper part of the mesh
1051 % - be discribed by a list of coefficients (between 0 and 1)
1052 %
1053 %
1054 % Usage:
1055 % md=extrude(md,numlayers,extrusionexponent);
1056 % md=extrude(md,numlayers,lowerexponent,upperexponent);
1057 % md=extrude(md,listofcoefficients);
1058 %
1059 % Example:
1060 % md=extrude(md,15,1.3);
1061 % md=extrude(md,15,1.3,1.2);
1062 % md=extrude(md,[0 0.2 0.5 0.7 0.9 0.95 1]);
1063 %
1064 % See also: MODELEXTRACT, COLLAPSE
1065
1066 %some checks on list of arguments
1067 if ((nargin>4) | (nargin<2) | (nargout~=1)),
1068 help extrude;
1069 error('extrude error message');
1070 end
1071 if numel(md.geometry.base)~=md.mesh.numberofvertices || numel(md.geometry.surface)~=md.mesh.numberofvertices
1072 error('model has not been parameterized yet: base and/or surface not set');
1073 end
1074
1075 %Extrude the mesh
1076 if nargin==2, %list of coefficients
1077 clist=varargin{1};
1078 if any(clist<0) | any(clist>1),
1079 error('extrusioncoefficients must be between 0 and 1');
1080 end
1081 extrusionlist=sort(unique([clist(:);0;1]));
1082 numlayers=length(extrusionlist);
1083 elseif nargin==3, %one polynomial law
1084 if varargin{2}<=0,
1085 help extrude;
1086 error('extrusionexponent must be >=0');
1087 end
1088 numlayers=varargin{1};
1089 extrusionlist=((0:1:numlayers-1)/(numlayers-1)).^varargin{2};
1090 elseif nargin==4, %two polynomial laws
1091 numlayers=varargin{1};
1092 lowerexp=varargin{2};
1093 upperexp=varargin{3};
1094
1095 if varargin{2}<=0 | varargin{3}<=0,
1096 help extrude;
1097 error('lower and upper extrusionexponents must be >=0');
1098 end
1099
1100 lowerextrusionlist=[(0:2/(numlayers-1):1).^lowerexp]/2;
1101 upperextrusionlist=[(0:2/(numlayers-1):1).^upperexp]/2;
1102 extrusionlist=sort(unique([lowerextrusionlist 1-upperextrusionlist]));
1103
1104 end
1105
1106 if numlayers<2,
1107 error('number of layers should be at least 2');
1108 end
1109 if strcmp(md.mesh.domaintype(),'3D')
1110 error('Cannot extrude a 3d mesh (extrude cannot be called more than once)');
1111 end
1112
1113 %Initialize with 2d mesh
1114 mesh2d = md.mesh;
1115 md.mesh=mesh3dprisms();
1116 md.mesh.x = mesh2d.x;
1117 md.mesh.y = mesh2d.y;
1118 md.mesh.elements = mesh2d.elements;
1119 md.mesh.numberofelements = mesh2d.numberofelements;
1120 md.mesh.numberofvertices = mesh2d.numberofvertices;
1121
1122 md.mesh.lat = mesh2d.lat;
1123 md.mesh.long = mesh2d.long;
1124 md.mesh.epsg = mesh2d.epsg;
1125 md.mesh.scale_factor = mesh2d.scale_factor;
1126
1127 md.mesh.vertexonboundary = mesh2d.vertexonboundary;
1128 md.mesh.vertexconnectivity = mesh2d.vertexconnectivity;
1129 md.mesh.elementconnectivity = mesh2d.elementconnectivity;
1130 md.mesh.average_vertex_connectivity = mesh2d.average_vertex_connectivity;
1131
1132 md.mesh.extractedvertices = mesh2d.extractedvertices;
1133 md.mesh.extractedelements = mesh2d.extractedelements;
1134
1135 md.mesh.segments2d = mesh2d.segments;
1136
1137 x3d=[];
1138 y3d=[];
1139 z3d=[]; %the lower node is on the bed
1140 thickness3d=md.geometry.thickness; %thickness and bed for these nodes
1141 bed3d=md.geometry.base;
1142
1143 %Create the new layers
1144 for i=1:numlayers,
1145 x3d=[x3d; md.mesh.x];
1146 y3d=[y3d; md.mesh.y];
1147 %nodes are distributed between bed and surface accordingly to the given exponent
1148 z3d=[z3d; bed3d+thickness3d*extrusionlist(i)];
1149 end
1150 number_nodes3d=size(x3d,1); %number of 3d nodes for the non extruded part of the mesh
1151
1152 %Extrude elements
1153 elements3d=[];
1154 for i=1:numlayers-1,
1155 elements3d=[elements3d;[md.mesh.elements+(i-1)*md.mesh.numberofvertices md.mesh.elements+i*md.mesh.numberofvertices]]; %Create the elements of the 3d mesh for the non extruded part
1156 end
1157 number_el3d=size(elements3d,1); %number of 3d nodes for the non extruded part of the mesh
1158
1159 %Keep a trace of lower and upper nodes
1160 lowervertex=NaN*ones(number_nodes3d,1);
1161 uppervertex=NaN*ones(number_nodes3d,1);
1162 lowervertex(md.mesh.numberofvertices+1:end)=1:(numlayers-1)*md.mesh.numberofvertices;
1163 uppervertex(1:(numlayers-1)*md.mesh.numberofvertices)=md.mesh.numberofvertices+1:number_nodes3d;
1164 md.mesh.lowervertex=lowervertex;
1165 md.mesh.uppervertex=uppervertex;
1166
1167 %same for lower and upper elements
1168 lowerelements=NaN*ones(number_el3d,1);
1169 upperelements=NaN*ones(number_el3d,1);
1170 lowerelements(md.mesh.numberofelements+1:end)=1:(numlayers-2)*md.mesh.numberofelements;
1171 upperelements(1:(numlayers-2)*md.mesh.numberofelements)=md.mesh.numberofelements+1:(numlayers-1)*md.mesh.numberofelements;
1172 md.mesh.lowerelements=lowerelements;
1173 md.mesh.upperelements=upperelements;
1174
1175 %Save old mesh
1176 md.mesh.x2d=md.mesh.x;
1177 md.mesh.y2d=md.mesh.y;
1178 md.mesh.elements2d=md.mesh.elements;
1179 md.mesh.numberofelements2d=md.mesh.numberofelements;
1180 md.mesh.numberofvertices2d=md.mesh.numberofvertices;
1181
1182 %Build global 3d mesh
1183 md.mesh.elements=elements3d;
1184 md.mesh.x=x3d;
1185 md.mesh.y=y3d;
1186 md.mesh.z=z3d;
1187 md.mesh.numberofelements=number_el3d;
1188 md.mesh.numberofvertices=number_nodes3d;
1189 md.mesh.numberoflayers=numlayers;
1190
1191 %Ok, now deal with the other fields from the 2d mesh:
1192
1193 %bedinfo and surface info
1194 md.mesh.vertexonbase=project3d(md,'vector',ones(md.mesh.numberofvertices2d,1),'type','node','layer',1);
1195 md.mesh.vertexonsurface=project3d(md,'vector',ones(md.mesh.numberofvertices2d,1),'type','node','layer',md.mesh.numberoflayers);
1196 md.mesh.vertexonboundary=project3d(md,'vector',md.mesh.vertexonboundary,'type','node');
1197
1198 %lat long
1199 md.mesh.lat=project3d(md,'vector',md.mesh.lat,'type','node');
1200 md.mesh.long=project3d(md,'vector',md.mesh.long,'type','node');
1201 md.mesh.scale_factor=project3d(md,'vector',md.mesh.scale_factor,'type','node');
1202
1203 md.geometry=extrude(md.geometry,md);
1204 md.friction = extrude(md.friction,md);
1205 md.inversion = extrude(md.inversion,md);
1206 md.smb = extrude(md.smb,md);
1207 md.initialization = extrude(md.initialization,md);
1208
1209 md.flowequation=md.flowequation.extrude(md);
1210 md.stressbalance=extrude(md.stressbalance,md);
1211 md.thermal=md.thermal.extrude(md);
1212 md.masstransport=md.masstransport.extrude(md);
1213 md.levelset=extrude(md.levelset,md);
1214 md.calving=extrude(md.calving,md);
1215 md.frontalforcings=extrude(md.frontalforcings,md);
1216 md.hydrology = extrude(md.hydrology,md);
1217 md.debris = extrude(md.debris,md);
1218 md.solidearth = extrude(md.solidearth,md);
1219 md.dsl = extrude(md.dsl,md);
1220 md.stochasticforcing = extrude(md.stochasticforcing,md);
1221
1222 %connectivity
1223 if ~isnan(md.mesh.elementconnectivity)
1224 md.mesh.elementconnectivity=repmat(md.mesh.elementconnectivity,numlayers-1,1);
1225 md.mesh.elementconnectivity(find(md.mesh.elementconnectivity==0))=NaN;
1226 for i=2:numlayers-1,
1227 md.mesh.elementconnectivity((i-1)*md.mesh.numberofelements2d+1:(i)*md.mesh.numberofelements2d,:)...
1228 =md.mesh.elementconnectivity((i-1)*md.mesh.numberofelements2d+1:(i)*md.mesh.numberofelements2d,:)+md.mesh.numberofelements2d;
1229 end
1230 md.mesh.elementconnectivity(find(isnan(md.mesh.elementconnectivity)))=0;
1231 end
1232
1233 md.materials=extrude(md.materials,md);
1234 md.damage=extrude(md.damage,md);
1235 md.mask=extrude(md.mask,md);
1236 md.qmu=extrude(md.qmu,md);
1237 md.basalforcings=extrude(md.basalforcings,md);
1238 md.outputdefinition=extrude(md.outputdefinition,md);
1239
1240 %increase connectivity if less than 25:
1241 if md.mesh.average_vertex_connectivity<=25,
1242 md.mesh.average_vertex_connectivity=100;
1243 end
1244 end % }}}
1245 function md = structtomodel(md,structmd) % {{{
1246
1247 if ~isstruct(structmd) error('input model is not a structure'); end
1248
1249 %loaded model is a struct, initialize output and recover all fields
1250 md = structtoobj(model,structmd);
1251
1252 %Old field now classes
1253 if (isfield(structmd,'timestepping') & isnumeric(md.timestepping)), md.timestepping=timestepping(); end
1254 if (isfield(structmd,'mask') & isnumeric(md.mask)),md.mask=mask(); end
1255
1256 %Field name change
1257 if isfield(structmd,'drag'), md.friction.coefficient=structmd.drag; end
1258 if isfield(structmd,'p'), md.friction.p=structmd.p; end
1259 if isfield(structmd,'q'), md.friction.q=structmd.p; end
1260 if isfield(structmd,'melting'), md.basalforcings.floatingice_melting_rate=structmd.melting; end
1261 if isfield(structmd,'melting_rate'), md.basalforcings.floatingice_melting_rate=structmd.melting_rate; end
1262 if isfield(structmd,'melting_rate'), md.basalforcings.groundedice_melting_rate=structmd.melting_rate; end
1263 if isfield(structmd,'accumulation'), md.smb.mass_balance=structmd.accumulation; end
1264 if isfield(structmd,'numberofgrids'), md.mesh.numberofvertices=structmd.numberofgrids; end
1265 if isfield(structmd,'numberofgrids2d'), md.mesh.numberofvertices2d=structmd.numberofgrids2d; end
1266 if isfield(structmd,'uppergrids'), md.mesh.uppervertex=structmd.uppergrids; end
1267 if isfield(structmd,'lowergrids'), md.mesh.lowervertex=structmd.lowergrids; end
1268 if isfield(structmd,'gridonbase'), md.mesh.vertexonbase=structmd.gridonbase; end
1269 if isfield(structmd,'gridonsurface'), md.mesh.vertexonsurface=structmd.gridonsurface; end
1270 if isfield(structmd,'extractedgrids'), md.mesh.extractedvertices=structmd.extractedgrids; end
1271 if isfield(structmd,'gridonboundary'), md.mesh.vertexonboundary=structmd.gridonboundary; end
1272 if isfield(structmd,'petscoptions') & ~isempty(structmd.petscoptions), md.toolkits=structmd.petscoptions; end
1273 if isfield(structmd,'g'), md.constants.g=structmd.g; end
1274 if isfield(structmd,'yts'), md.constants.yts=structmd.yts; end
1275 if isfield(structmd,'surface_mass_balance'), md.smb.mass_balance=structmd.surface_mass_balance; end
1276 if isfield(structmd,'basal_melting_rate'), md.basalforcings.floatingice_melting_rate=structmd.basal_melting_rate; end
1277 if isfield(structmd,'geothermalflux'), md.basalforcings.geothermalflux=structmd.geothermalflux; end
1278 if isfield(structmd,'drag'), md.friction.coefficient=structmd.drag; end
1279 if isfield(structmd,'drag_coefficient'), md.friction.coefficient=structmd.drag_coefficient; end
1280 if isfield(structmd,'drag_p'), md.friction.p=structmd.drag_p; end
1281 if isfield(structmd,'drag_q'), md.friction.q=structmd.drag_q; end
1282 if isfield(structmd,'riftproperties'), %old implementation
1283 md.rifts=rifts();
1284 md.rifts.riftproperties=structmd.riftproperties;
1285 md.rifts.riftstruct=structmd.rifts;
1286 md.rifts.riftproperties=structmd.riftinfo;
1287 end
1288 if isfield(structmd,'bamg'), md.private.bamg=structmd.bamg; end
1289 if isfield(structmd,'lowmem'), md.settings.lowmem=structmd.lowmem; end
1290 if isfield(structmd,'io_gather'), md.settings.io_gather=structmd.io_gather; end
1291 if isfield(structmd,'spcwatercolumn'), md.hydrology.spcwatercolumn=structmd.spcwatercolumn; end
1292 if isfield(structmd,'hydro_n'), md.hydrology.n=structmd.hydro_n; end
1293 if isfield(structmd,'hydro_p'), md.hydrology.p=structmd.hydro_p; end
1294 if isfield(structmd,'hydro_q'), md.hydrology.q=structmd.hydro_q; end
1295 if isfield(structmd,'hydro_CR'), md.hydrology.CR=structmd.hydro_CR; end
1296 if isfield(structmd,'hydro_kn'), md.hydrology.kn=structmd.hydro_kn; end
1297 if isfield(structmd,'spctemperature'), md.thermal.spctemperature=structmd.spctemperature; end
1298 if isfield(structmd,'min_thermal_constraints'), md.thermal.penalty_threshold=structmd.min_thermal_constraints; end
1299 if isfield(structmd,'artificial_diffusivity'), md.thermal.stabilization=structmd.artificial_diffusivity; end
1300 if isfield(structmd,'max_nonlinear_iterations'), md.thermal.maxiter=structmd.max_nonlinear_iterations; end
1301 if isfield(structmd,'stabilize_constraints'), md.thermal.penalty_lock=structmd.stabilize_constraints; end
1302 if isfield(structmd,'penalty_offset'), md.thermal.penalty_factor=structmd.penalty_offset; end
1303 if isfield(structmd,'name'), md.miscellaneous.name=structmd.name; end
1304 if isfield(structmd,'notes'), md.miscellaneous.notes=structmd.notes; end
1305 if isfield(structmd,'dummy'), md.miscellaneous.dummy=structmd.dummy; end
1306 if isfield(structmd,'dt'), md.timestepping.time_step=structmd.dt; end
1307 if isfield(structmd,'ndt'), md.timestepping.final_time=structmd.ndt; end
1308 if isfield(structmd,'time_adapt'), md.timestepping.time_adapt=structmd.time_adapt; end
1309 if isfield(structmd,'cfl_coefficient'), md.timestepping.cfl_coefficient=structmd.cfl_coefficient; end
1310 if isfield(structmd,'spcthickness'), md.masstransport.spcthickness=structmd.spcthickness; end
1311 if isfield(structmd,'spcthickness'), md.debris.spcthickness=structmd.spcthickness; end
1312 if isfield(structmd,'artificial_diffusivity'), md.masstransport.stabilization=structmd.artificial_diffusivity; end
1313 if isfield(structmd,'hydrostatic_adjustment'), md.masstransport.hydrostatic_adjustment=structmd.hydrostatic_adjustment; end
1314 if isfield(structmd,'penalties'), md.masstransport.vertex_pairing=structmd.penalties; end
1315 if isfield(structmd,'penalty_offset'), md.masstransport.penalty_factor=structmd.penalty_offset; end
1316 if isfield(structmd,'B'), md.materials.rheology_B=structmd.B; end
1317 if isfield(structmd,'n'), md.materials.rheology_n=structmd.n; end
1318 if isfield(structmd,'rheology_B'), md.materials.rheology_B=structmd.rheology_B; end
1319 if isfield(structmd,'rheology_n'), md.materials.rheology_n=structmd.rheology_n; end
1320 if isfield(structmd,'rheology_Z'), md.damage.D=(1-structmd.rheology_Z); end
1321 if isfield(structmd,'spcthickness'), md.balancethickness.spcthickness=structmd.spcthickness; end
1322 if isfield(structmd,'artificial_diffusivity'), md.balancethickness.stabilization=structmd.artificial_diffusivity; end
1323 if isfield(structmd,'dhdt'), md.balancethickness.thickening_rate=structmd.dhdt; end
1324 if isfield(structmd,'isSIA'), md.flowequation.isSIA=structmd.isSIA; end
1325 if isfield(structmd,'isFS'), md.flowequation.isFS=structmd.isFS; end
1326 if isfield(structmd,'elements_type'), md.flowequation.element_equation=structmd.elements_type; end
1327 if isfield(structmd,'vertices_type'), md.flowequation.vertex_equation=structmd.vertices_type; end
1328 if isfield(structmd,'eps_rel'), md.steadystate.reltol=structmd.eps_rel; end
1329 if isfield(structmd,'max_steadystate_iterations'), md.steadystate.maxiter=structmd.max_steadystate_iterations; end
1330 if isfield(structmd,'isdiagnostic'), md.transient.isstressbalance=structmd.isdiagnostic; end
1331 if isfield(structmd,'isprognostic'), md.transient.ismasstransport=structmd.isprognostic; end
1332 if isfield(structmd,'isthermal'), md.transient.isthermal=structmd.isthermal; end
1333 if isfield(structmd,'control_analysis'), md.inversion.iscontrol=structmd.control_analysis; end
1334 if isfield(structmd,'weights'), md.inversion.cost_functions_coefficients=structmd.weights; end
1335 if isfield(structmd,'nsteps'), md.inversion.nsteps=structmd.nsteps; end
1336 if isfield(structmd,'maxiter_per_step'), md.inversion.maxiter_per_step=structmd.maxiter_per_step; end
1337 if isfield(structmd,'cm_min'), md.inversion.min_parameters=structmd.cm_min; end
1338 if isfield(structmd,'cm_max'), md.inversion.max_parameters=structmd.cm_max; end
1339 if isfield(structmd,'vx_obs'), md.inversion.vx_obs=structmd.vx_obs; end
1340 if isfield(structmd,'vy_obs'), md.inversion.vy_obs=structmd.vy_obs; end
1341 if isfield(structmd,'vel_obs'), md.inversion.vel_obs=structmd.vel_obs; end
1342 if isfield(structmd,'thickness_obs'), md.inversion.thickness_obs=structmd.thickness_obs; end
1343 if isfield(structmd,'vx'), md.initialization.vx=structmd.vx; end
1344 if isfield(structmd,'vy'), md.initialization.vy=structmd.vy; end
1345 if isfield(structmd,'vz'), md.initialization.vz=structmd.vz; end
1346 if isfield(structmd,'vel'), md.initialization.vel=structmd.vel; end
1347 if isfield(structmd,'pressure'), md.initialization.pressure=structmd.pressure; end
1348 if isfield(structmd,'temperature'), md.initialization.temperature=structmd.temperature; end
1349 if isfield(structmd,'waterfraction'), md.initialization.waterfraction=structmd.waterfraction; end
1350 if isfield(structmd,'watercolumn'), md.initialization.watercolumn=structmd.watercolumn; end
1351 if isfield(structmd,'surface'), md.geometry.surface=structmd.surface; end
1352 if isfield(structmd,'bed'), md.geometry.base=structmd.bed; end
1353 if isfield(structmd,'thickness'), md.geometry.thickness=structmd.thickness; end
1354 if isfield(structmd,'bathymetry'), md.geometry.bed=structmd.bathymetry; end
1355 if isfield(structmd,'thickness_coeff'), md.geometry.hydrostatic_ratio=structmd.thickness_coeff; end
1356 if isfield(structmd,'connectivity'), md.mesh.average_vertex_connectivity=structmd.connectivity; end
1357 if isfield(structmd,'extractednodes'), md.mesh.extractedvertices=structmd.extractednodes; end
1358 if isfield(structmd,'extractedelements'), md.mesh.extractedelements=structmd.extractedelements; end
1359 if isfield(structmd,'nodeonboundary'), md.mesh.vertexonboundary=structmd.nodeonboundary; end
1360 if isfield(structmd,'lat'), md.mesh.lat=structmd.lat; end
1361 if isfield(structmd,'long'), md.mesh.long=structmd.long; end
1362 if isfield(structmd,'scale_factor'), md.mesh.scale_factor=structmd.scale_factor; end
1363 if isfield(structmd,'segments'), md.mesh.segments=structmd.segments; end
1364 if isfield(structmd,'segmentmarkers'), md.mesh.segmentmarkers=structmd.segmentmarkers; end
1365 if isfield(structmd,'numlayers'), md.mesh.numberoflayers=structmd.numlayers; end
1366 if isfield(structmd,'numberofelements'), md.mesh.numberofelements=structmd.numberofelements; end
1367 if isfield(structmd,'numberofvertices'), md.mesh.numberofvertices=structmd.numberofvertices; end
1368 if isfield(structmd,'numberofnodes'), md.mesh.numberofvertices=structmd.numberofnodes; end
1369 if isfield(structmd,'numberofedges'), md.mesh.numberofedges=structmd.numberofedges; end
1370 if isfield(structmd,'numberofelements2d'), md.mesh.numberofelements2d=structmd.numberofelements2d; end
1371 if isfield(structmd,'numberofnodes2d'), md.mesh.numberofvertices2d=structmd.numberofnodes2d; end
1372 if isfield(structmd,'nodeconnectivity'), md.mesh.vertexconnectivity=structmd.nodeconnectivity; end
1373 if isfield(structmd,'elementconnectivity'), md.mesh.elementconnectivity=structmd.elementconnectivity; end
1374 if isfield(structmd,'uppernodes'), md.mesh.uppervertex=structmd.uppernodes; end
1375 if isfield(structmd,'lowernodes'), md.mesh.lowervertex=structmd.lowernodes; end
1376 if isfield(structmd,'upperelements'), md.mesh.upperelements=structmd.upperelements; end
1377 if isfield(structmd,'lowerelements'), md.mesh.lowerelements=structmd.lowerelements; end
1378 if isfield(structmd,'nodeonsurface'), md.mesh.vertexonsurface=structmd.nodeonsurface; end
1379 if isfield(structmd,'nodeonbase'), md.mesh.vertexonbase=structmd.nodeonbase; end
1380 if isfield(structmd,'elements2d'), md.mesh.elements2d=structmd.elements2d; end
1381 if isfield(structmd,'y2d'), md.mesh.y2d=structmd.y2d; end
1382 if isfield(structmd,'x2d'), md.mesh.x2d=structmd.x2d; end
1383 if isfield(structmd,'elements'), md.mesh.elements=structmd.elements; end
1384 if isfield(structmd,'edges'),
1385 md.mesh.edges=structmd.edges;
1386 md.mesh.edges(isnan(md.mesh.edges))=-1;
1387 end
1388 if isfield(structmd,'y'), md.mesh.y=structmd.y; end
1389 if isfield(structmd,'x'), md.mesh.x=structmd.x; end
1390 if isfield(structmd,'z'), md.mesh.z=structmd.z; end
1391 if isfield(structmd,'diagnostic_ref'), md.stressbalance.referential=structmd.diagnostic_ref; end
1392 if isfield(structmd,'npart'); md.qmu.numberofpartitions=structmd.npart; end
1393 if isfield(structmd,'part'); md.qmu.partition=structmd.part; end
1394
1395 if isnumeric(md.verbose),
1396 md.verbose=verbose;
1397 end
1398
1399 if isfield(structmd,'spcvelocity'),
1400 md.stressbalance.spcvx=NaN*ones(md.mesh.numberofvertices,1);
1401 md.stressbalance.spcvy=NaN*ones(md.mesh.numberofvertices,1);
1402 md.stressbalance.spcvz=NaN*ones(md.mesh.numberofvertices,1);
1403 pos=find(structmd.spcvelocity(:,1)); md.stressbalance.spcvx(pos)=structmd.spcvelocity(pos,4);
1404 pos=find(structmd.spcvelocity(:,2)); md.stressbalance.spcvy(pos)=structmd.spcvelocity(pos,5);
1405 pos=find(structmd.spcvelocity(:,3)); md.stressbalance.spcvz(pos)=structmd.spcvelocity(pos,6);
1406 end
1407 if isfield(structmd,'spcvx'),
1408 md.stressbalance.spcvx=NaN*ones(md.mesh.numberofvertices,1);
1409 pos=find(~isnan(structmd.spcvx)); md.stressbalance.spcvx(pos)=structmd.spcvx(pos);
1410 end
1411 if isfield(structmd,'spcvy'),
1412 md.stressbalance.spcvy=NaN*ones(md.mesh.numberofvertices,1);
1413 pos=find(~isnan(structmd.spcvy)); md.stressbalance.spcvy(pos)=structmd.spcvy(pos);
1414 end
1415 if isfield(structmd,'spcvz'),
1416 md.stressbalance.spcvz=NaN*ones(md.mesh.numberofvertices,1);
1417 pos=find(~isnan(structmd.spcvz)); md.stressbalance.spcvz(pos)=structmd.spcvz(pos);
1418 end
1419 if isfield(structmd,'pressureload'),
1420 if ~isempty(structmd.pressureload) & ismember(structmd.pressureload(end,end),[118 119 120]),
1421 pos=find(structmd.pressureload(:,end)==120); md.stressbalance.icefront(pos,end)=0;
1422 pos=find(structmd.pressureload(:,end)==118); md.stressbalance.icefront(pos,end)=1;
1423 pos=find(structmd.pressureload(:,end)==119); md.stressbalance.icefront(pos,end)=2;
1424 end
1425 end
1426 if isfield(structmd,'elements_type') & structmd.elements_type(end,end)>50,
1427 pos=find(structmd.elements_type==59); md.flowequation.element_equation(pos,end)=0;
1428 pos=find(structmd.elements_type==55); md.flowequation.element_equation(pos,end)=1;
1429 pos=find(structmd.elements_type==56); md.flowequation.element_equation(pos,end)=2;
1430 pos=find(structmd.elements_type==60); md.flowequation.element_equation(pos,end)=3;
1431 pos=find(structmd.elements_type==62); md.flowequation.element_equation(pos,end)=4;
1432 pos=find(structmd.elements_type==57); md.flowequation.element_equation(pos,end)=5;
1433 pos=find(structmd.elements_type==58); md.flowequation.element_equation(pos,end)=6;
1434 pos=find(structmd.elements_type==61); md.flowequation.element_equation(pos,end)=7;
1435 end
1436 if isfield(structmd,'vertices_type') & structmd.vertices_type(end,end)>50,
1437 pos=find(structmd.vertices_type==59); md.flowequation.vertex_equation(pos,end)=0;
1438 pos=find(structmd.vertices_type==55); md.flowequation.vertex_equation(pos,end)=1;
1439 pos=find(structmd.vertices_type==56); md.flowequation.vertex_equation(pos,end)=2;
1440 pos=find(structmd.vertices_type==60); md.flowequation.vertex_equation(pos,end)=3;
1441 pos=find(structmd.vertices_type==62); md.flowequation.vertex_equation(pos,end)=4;
1442 pos=find(structmd.vertices_type==57); md.flowequation.vertex_equation(pos,end)=5;
1443 pos=find(structmd.vertices_type==58); md.flowequation.vertex_equation(pos,end)=6;
1444 pos=find(structmd.vertices_type==61); md.flowequation.vertex_equation(pos,end)=7;
1445 end
1446 if isfield(structmd,'rheology_law') & isnumeric(structmd.rheology_law),
1447 if (structmd.rheology_law==272), md.materials.rheology_law='None'; end
1448 if (structmd.rheology_law==368), md.materials.rheology_law='Paterson'; end
1449 if (structmd.rheology_law==369), md.materials.rheology_law='Arrhenius'; end
1450 end
1451 if isfield(structmd,'groundingline_migration') & isnumeric(structmd.groundingline_migration),
1452 if (structmd.groundingline_migration==272), md.groundingline.migration='None'; end
1453 if (structmd.groundingline_migration==273), md.groundingline.migration='AggressiveMigration'; end
1454 if (structmd.groundingline_migration==274), md.groundingline.migration='SoftMigration'; end
1455 end
1456 if isfield(structmd,'control_type') & isnumeric(structmd.control_type),
1457 if (structmd.control_type==143), md.inversion.control_parameters={'FrictionCoefficient'}; end
1458 if (structmd.control_type==190), md.inversion.control_parameters={'RheologyBbar'}; end
1459 if (structmd.control_type==147), md.inversion.control_parameters={'Thickeningrate'}; end
1460 end
1461 if isfield(structmd,'cm_responses') & ismember(structmd.cm_responses(end,end),[165:170 383 388 389]),
1462 pos=find(structmd.cm_responses==166); md.inversion.cost_functions(pos)=101;
1463 pos=find(structmd.cm_responses==167); md.inversion.cost_functions(pos)=102;
1464 pos=find(structmd.cm_responses==168); md.inversion.cost_functions(pos)=103;
1465 pos=find(structmd.cm_responses==169); md.inversion.cost_functions(pos)=104;
1466 pos=find(structmd.cm_responses==170); md.inversion.cost_functions(pos)=105;
1467 pos=find(structmd.cm_responses==165); md.inversion.cost_functions(pos)=201;
1468 pos=find(structmd.cm_responses==389); md.inversion.cost_functions(pos)=501;
1469 pos=find(structmd.cm_responses==388); md.inversion.cost_functions(pos)=502;
1470 pos=find(structmd.cm_responses==382); md.inversion.cost_functions(pos)=503;
1471 end
1472
1473 if isfield(structmd,'artificial_diffusivity') & structmd.artificial_diffusivity==2,
1474 md.thermal.stabilization=2;
1475 md.masstransport.stabilization=1;
1476 md.balancethickness.stabilization=1;
1477 end
1478 if isnumeric(md.masstransport.hydrostatic_adjustment)
1479 if md.masstransport.hydrostatic_adjustment==269,
1480 md.masstransport.hydrostatic_adjustment='Incremental';
1481 else
1482 md.masstransport.hydrostatic_adjustment='Absolute';
1483 end
1484 end
1485
1486 %New fields
1487 if ~isfield(structmd,'upperelements') & isa(md.mesh,'mesh3dprisms')
1488 md.mesh.upperelements=transpose(1:md.mesh.numberofelements)+md.mesh.numberofelements2d;
1489 md.mesh.upperelements(end-md.mesh.numberofelements2d+1:end)=NaN;
1490 end
1491 if ~isfield(structmd,'lowerelements') & isa(md.mesh,'mesh3dprisms')
1492 md.mesh.lowerelements=transpose(1:md.mesh.numberofelements)-md.mesh.numberofelements2d;
1493 md.mesh.lowerelements(1:md.mesh.numberofelements2d)=NaN;
1494 end
1495 if ~isfield(structmd,'diagnostic_ref');
1496 md.stressbalance.referential=NaN*ones(md.mesh.numberofvertices,6);
1497 end
1498 if ~isfield(structmd,'loadingforce');
1499 md.stressbalance.loadingforce=0*ones(md.mesh.numberofvertices,3);
1500 end
1501
1502 %2013 August 9
1503 if isfield(structmd,'prognostic') & isa(structmd.prognostic,'prognostic'),
1504 disp('Recovering old prognostic class');
1505 md.masstransport=masstransport(structmd.prognostic);
1506 end
1507 %2013 August 9
1508 if isfield(structmd,'diagnostic') & (isa(structmd.diagnostic,'diagnostic') || isa(structmd.diagnostic,'stressbalance')),
1509 disp('Recovering old diagnostic class');
1510 md.stressbalance=stressbalance(structmd.diagnostic);
1511 end
1512 %2014 January 9th
1513 if isfield(structmd,'surfaceforcings') & isa(md.smb,'surfaceforcings'),
1514 disp('Recovering old surfaceforcings class');
1515 mass_balance=structmd.surfaceforcings.mass_balance;
1516 md.smb=SMB();
1517 md.smb.mass_balance=mass_balance;
1518 end
1519 %2015 September 10
1520 if isfield(structmd,'surfaceforcings') & isa(structmd.surfaceforcings,'SMB'),
1521 disp('Recovering old SMB class');
1522 md.smb=SMBforcing(structmd.surfaceforcings);
1523 end
1524 if isfield(structmd,'surfaceforcings') & isa(structmd.surfaceforcings,'SMBhenning'),
1525 disp('Recovering old SMBhenning class');
1526 md.smb=SMBhenning(structmd.surfaceforcings);
1527 end
1528 if isfield(structmd,'slr') && ~isempty(structmd.slr)
1529 md.solidearth = solidearth('Earth');
1530 disp('Recovering old slr class');
1531 if isfield(structmd.slr,'sealevel'),
1532 md.solidearth.sealevel=structmd.slr.sealevel;
1533 end
1534 md.solidearth.planetradius=structmd.slr.planetradius;
1535 md.solidearth.requested_outputs=structmd.slr.requested_outputs;
1536 md.solidearth.transitions=structmd.slr.transitions;
1537
1538 md.solidearth.transitions=structmd.slr.transitions;
1539 md.solidearth.settings.reltol=structmd.slr.reltol;
1540 md.solidearth.settings.abstol=structmd.slr.abstol;
1541 md.solidearth.settings.maxiter=structmd.slr.maxiter;
1542 md.solidearth.settings.rigid=structmd.slr.rigid;
1543 md.solidearth.settings.elastic=structmd.slr.elastic;
1544 md.solidearth.settings.rotation=structmd.slr.rotation;
1545 md.solidearth.settings.runfrequency=structmd.slr.geodetic_run_frequency;
1546 md.solidearth.settings.computesealevelchange=structmd.slr.geodetic;
1547 md.solidearth.settings.degacc=structmd.slr.degacc;
1548 md.solidearth.settings.horiz=structmd.slr.horiz;
1549 md.solidearth.settings.ocean_area_scaling=structmd.slr.ocean_area_scaling;
1550
1551 md.solidearth.surfaceload.icethicknesschange=structmd.slr.deltathickness;
1552 md.solidearth.surfaceload.waterheightchange=structmd.slr.hydro_rate;
1553
1554 md.solidearth.lovenumbers.h=structmd.slr.love_h;
1555 md.solidearth.lovenumbers.k=structmd.slr.love_k;
1556 md.solidearth.lovenumbers.l=structmd.slr.love_l;
1557 md.solidearth.lovenumbers.th=structmd.slr.tide_love_h;
1558 md.solidearth.lovenumbers.tk=structmd.slr.tide_love_k;
1559 md.solidearth.lovenumbers.tk2secular=structmd.slr.fluid_love;
1560
1561 md.solidearth.rotational.equatorialmoi=structmd.slr.equatorial_moi;
1562 md.solidearth.rotational.polarmoi=structmd.slr.polar_moi;
1563 md.solidearth.rotational.angularvelocity=structmd.slr.angular_velocity;
1564 end
1565 end% }}}
1566 function md = tetras(md,varargin) % {{{
1567 %TETRAS - split 3d prismatic mesh into 3 tetrahedrons
1568 %
1569 % Usage:
1570 % md=tetra(md)
1571
1572 if ~isa(md.mesh,'mesh3dprisms')
1573 error('mesh is not a 3d prismatic mesh');
1574 end
1575
1576 %Initialize tetra mesh
1577 md.mesh=mesh3dtetras(md.mesh);
1578
1579 %Subdivision from Philipp Furnstahl (http://studierstube.icg.tugraz.at/thesis/fuernstahl_thesis.pdf)
1580 steiner = 0;
1581 nbv = md.mesh.numberofvertices;
1582 nbt = 3*md.mesh.numberofelements;
1583 elements = zeros(nbt,4);
1584 for i=1:md.mesh.numberofelements
1585 v1=md.mesh.elements(i,1); v2=md.mesh.elements(i,2); v3=md.mesh.elements(i,3);
1586 v4=md.mesh.elements(i,4); v5=md.mesh.elements(i,5); v6=md.mesh.elements(i,6);
1587 if(min(v2,v4)<min(v1,v5) & min(v1,v6)<min(v3,v4) & min(v3,v5)<min(v2,v6)),
1588 steiner = steiner+1; nbv = nbv+1; nbt = nbt+5; v7 = nbv;
1589 md.mesh.x=[md.mesh.x; mean(md.mesh.x(md.mesh.elements(i,:)))];
1590 md.mesh.y=[md.mesh.y; mean(md.mesh.y(md.mesh.elements(i,:)))];
1591 md.mesh.z=[md.mesh.z; mean(md.mesh.z(md.mesh.elements(i,:)))];
1592 elements(3*(i-1)+1,:) = [v1 v2 v3 v7];
1593 elements(3*(i-1)+2,:) = [v1 v2 v4 v7];
1594 elements(3*(i-1)+3,:) = [v2 v4 v5 v7];
1595 elements(end+1,:) = [v2 v3 v5 v7];
1596 elements(end+1,:) = [v3 v5 v6 v7];
1597 elements(end+1,:) = [v1 v3 v6 v7];
1598 elements(end+1,:) = [v1 v4 v6 v7];
1599 elements(end+1,:) = [v4 v5 v6 v7];
1600 elseif(min(v2,v4)<min(v1,v5) & min(v1,v6)<min(v3,v4) & min(v3,v5)>min(v2,v6)),
1601 elements(3*(i-1)+1,:) = [v1 v2 v4 v6];
1602 elements(3*(i-1)+2,:) = [v2 v4 v5 v6];
1603 elements(3*(i-1)+3,:) = [v1 v2 v3 v6];
1604 elseif(min(v2,v4)<min(v1,v5) & min(v1,v6)>min(v3,v4) & min(v3,v5)<min(v2,v6)),
1605 elements(3*(i-1)+1,:) = [v1 v2 v3 v4];
1606 elements(3*(i-1)+2,:) = [v2 v3 v4 v5];
1607 elements(3*(i-1)+3,:) = [v3 v4 v5 v6];
1608 elseif(min(v2,v4)<min(v1,v5) & min(v1,v6)>min(v3,v4) & min(v3,v5)>min(v2,v6)),
1609 elements(3*(i-1)+1,:) = [v1 v2 v3 v4];
1610 elements(3*(i-1)+2,:) = [v2 v4 v5 v6];
1611 elements(3*(i-1)+3,:) = [v2 v3 v4 v6];
1612 elseif(min(v2,v4)>min(v1,v5) & min(v1,v6)<min(v3,v4) & min(v3,v5)<min(v2,v6)),
1613 elements(3*(i-1)+1,:) = [v1 v4 v5 v6];
1614 elements(3*(i-1)+2,:) = [v1 v2 v3 v5];
1615 elements(3*(i-1)+3,:) = [v1 v3 v5 v6];
1616 elseif(min(v2,v4)>min(v1,v5) & min(v1,v6)<min(v3,v4) & min(v3,v5)>min(v2,v6)),
1617 elements(3*(i-1)+1,:) = [v1 v4 v5 v6];
1618 elements(3*(i-1)+2,:) = [v1 v2 v5 v6];
1619 elements(3*(i-1)+3,:) = [v1 v2 v3 v6];
1620 elseif(min(v2,v4)>min(v1,v5) & min(v1,v6)>min(v3,v4) & min(v3,v5)<min(v2,v6)),
1621 elements(3*(i-1)+1,:) = [v1 v3 v4 v5];
1622 elements(3*(i-1)+2,:) = [v1 v2 v3 v5];
1623 elements(3*(i-1)+3,:) = [v3 v4 v5 v6];
1624 elseif(min(v2,v4)>min(v1,v5) & min(v1,v6)<min(v3,v4) & min(v3,v5)<min(v2,v6)),
1625 elements(3*(i-1)+1,:) = [v1 v5 v6 v4];
1626 elements(3*(i-1)+2,:) = [v1 v2 v3 v5];
1627 elements(3*(i-1)+3,:) = [v5 v6 v3 v1];
1628 elseif(min(v2,v4)>min(v1,v5) & min(v1,v6)>min(v3,v4) & min(v3,v5)>min(v2,v6)),
1629 steiner = steiner+1; nbv = nbv+1; nbt = nbt+5; v7 = nbv;
1630 md.mesh.x=[md.mesh.x; mean(md.mesh.x(md.mesh.elements(i,:)))];
1631 md.mesh.y=[md.mesh.y; mean(md.mesh.y(md.mesh.elements(i,:)))];
1632 md.mesh.z=[md.mesh.z; mean(md.mesh.z(md.mesh.elements(i,:)))];
1633 elements(3*(i-1)+1,:) = [v1 v2 v3 v7];
1634 elements(3*(i-1)+2,:) = [v1 v4 v5 v7];
1635 elements(3*(i-1)+3,:) = [v1 v2 v5 v7];
1636 elements(end+1,:) = [v2 v5 v6 v7];
1637 elements(end+1,:) = [v2 v3 v6 v7];
1638 elements(end+1,:) = [v3 v4 v6 v7];
1639 elements(end+1,:) = [v1 v3 v4 v7];
1640 elements(end+1,:) = [v4 v5 v6 v7];
1641 else
1642 error('Case not supported'); %not supposed to happen!
1643 end
1644 %Reorder elements to make sure they are direct
1645 for j=1:3
1646 element = elements(3*(i-1)+j,:);
1647 matrix = [md.mesh.x(element), md.mesh.y(element), md.mesh.z(element), ones(4,1)];
1648 if det(matrix)>0,
1649 elements(3*(i-1)+j,1)=element(2);
1650 elements(3*(i-1)+j,2)=element(1);
1651 end
1652 end
1653 end
1654 %%Split in 3 tetras
1655 %subelement1 = [1 2 3 5];
1656 %subelement2 = [4 6 5 1];
1657 %subelement3 = [5 6 3 1];
1658 %elements=[md.mesh.elements(:,subelement1);md.mesh.elements(:,subelement2);md.mesh.elements(:,subelement3)];
1659 if steiner==0,
1660 disp('No Steiner point required to split prismatic mesh into tets');
1661 else
1662 disp([num2str(steiner) ' Steiner points had to be included'])
1663 error('Steiner point not supported yet');
1664 end
1665
1666 pos_elements = repmat([1:md.mesh.numberofelements]',3,1);
1667
1668 md.mesh.elements=elements;
1669 md.mesh.numberofelements=size(elements,1);
1670
1671 %p and q (same deal, except for element that are on the bedrock: )
1672 if ~isnan(md.friction.p),
1673 md.friction.p=md.friction.p(pos_elements);
1674 md.friction.q=md.friction.q(pos_elements);
1675 end
1676
1677 %elementstype
1678 if ~isnan(md.flowequation.element_equation)
1679 oldelements_type=md.flowequation.element_equation;
1680 md.flowequation.element_equation=md.flowequation.element_equation(pos_elements);
1681 end
1682
1683 %connectivity
1684 md.mesh.elementconnectivity=NaN;
1685
1686 %materials
1687 if ~isnan(md.materials.rheology_n),
1688 md.materials.rheology_n=md.materials.rheology_n(pos_elements);
1689 end
1690
1691 %increase connectivity if less than 25:
1692 if md.mesh.average_vertex_connectivity<=25,
1693 md.mesh.average_vertex_connectivity=100;
1694 end
1695 end % }}}
1696 function memory(self) % {{{
1697
1698 disp(sprintf('\nMemory imprint:\n'));
1699
1700 fields=properties('model');
1701 mem=0;
1702
1703 for i=1:length(fields),
1704 field=self.(fields{i});
1705 s=whos('field');
1706 mem=mem+s.bytes/1e6;
1707 disp(sprintf('%19s: %6.2f Mb',fields{i},s.bytes/1e6));
1708 end
1709 disp(sprintf('%19s--%10s','--------------','--------------'));
1710 disp(sprintf('%19s: %g Mb','Total',mem));
1711 end
1712 % }}}
1713 function netcdf(self,filename) % {{{
1714 %NETCDF - save model as netcdf
1715 %
1716 % Usage:
1717 % netcdf(md,filename)
1718 %
1719 % Example:
1720 % netcdf(md,'model.nc');
1721
1722 disp('Saving model as NetCDF');
1723 %1. Create NetCDF file
1724 ncid=netcdf.create(filename,'CLOBBER');
1725 netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'),'Conventions','CF-1.4');
1726 netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'),'Title',['ISSM model (' self.miscellaneous.name ')']);
1727 netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'),'Author',getenv('USER'));
1728 netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'),'Date',datestr(now));
1729
1730 %Preallocate variable id, needed to write variables in netcdf file
1731 var_id=zeros(1000,1);%preallocate
1732
1733 for step=1:2,
1734 counter=0;
1735 [var_id,counter]=structtonc(ncid,'md',self,0,var_id,counter,step);
1736 if step==1, netcdf.endDef(ncid); end
1737 end
1738
1739 if counter>1000,
1740 warning(['preallocation of var_id need to be updated from ' num2str(1000) ' to ' num2str(counter)]);
1741 end
1742
1743 netcdf.close(ncid)
1744 end % }}}
1745 function xylim(self) % {{{
1746
1747 xlim([min(self.mesh.x) max(self.mesh.x)]);
1748 ylim([min(self.mesh.y) max(self.mesh.y)])
1749 end % }}}
1750 function md=upload(md) % {{{
1751 %the goal of this routine is to upload the model onto a server, and to empty it.
1752 %So first, save the model with a unique name and upload the file to the server:
1753 random_part=fix(rand(1)*10000);
1754 id=[md.miscellaneous.name '-' regexprep(datestr(now),'[^\w'']','') '-' num2str(random_part) '-' getenv('USER') '-' oshostname() '.upload'];
1755 save('id','md');
1756
1757 %Now, upload the file:
1758 issmscpout(md.settings.upload_server,md.settings.upload_path,md.settings.upload_login,md.settings.upload_port,{id},1);
1759
1760 %Now, empty this model of everything except settings, and record name of file we just uploaded!
1761 settings_back=md.settings;
1762 md=model();
1763 md.settings=settings_back;
1764 md.settings.upload_filename=id;
1765
1766 %get locally rid of file that was uploaded
1767 delete(id);
1768
1769 end % }}}
1770 function md=download(md) % {{{
1771
1772 %the goal of this routine is to download the internals of the current model from a server, because
1773 %this model is empty, except for the settings which tell us where to go and find this model!
1774
1775 %Download the file:
1776 issmscpin(md.settings.upload_server, md.settings.upload_login, md.settings.upload_port, md.settings.upload_path, {md.settings.upload_filename});
1777
1778 name=md.settings.upload_filename;
1779
1780 %Now, load this model:
1781 md=loadmodel(md.settings.upload_filename);
1782
1783 %get locally rid of file that was downloaded
1784 delete(name);
1785
1786 end % }}}
1787 function saveasstruct(md,filename) % {{{
1788
1789 %Get all model fields
1790 mdfields=properties('model');
1791
1792 disp('Converting all model fields to struct...');
1793 warning off MATLAB:structOnObject
1794 for i=1:length(mdfields),
1795
1796 %convert md field to struct
1797 field=mdfields{i};
1798 field_struct = struct(md.(field));
1799
1800 %Check that there is no class remaining in the field
1801 subfields = fields(field_struct);
1802 for i=1:numel(subfields)
1803 if isobject(field_struct.(subfields{i}))
1804 disp(['skipping ' subfields{i} ' because it is an object'])
1805 field_struct = rmfield(field_struct, subfields{i});
1806 end
1807 end
1808 md.(field) = field_struct;
1809 end
1810
1811 disp('Converting model to struct...');
1812 md=struct(md);
1813
1814 disp(['Saving as ' filename '...']);
1815 warning on MATLAB:structOnObject
1816 save(filename,'md','-v7.3')
1817
1818 end % }}}
1819 function savemodeljs(md,modelname,websiteroot,varargin) % {{{
1820
1821 %the goal of this routine is to save the model as a javascript array that can be included in any html
1822 %file:
1823
1824 options=pairoptions(varargin{:});
1825 optimization=getfieldvalue(options,'optimize',0);
1826
1827
1828 %disp:
1829 disp(['saving model ''' modelname ''' in file ' websiteroot '/js/' modelname '.js']);
1830
1831 %open file for writing and declare the model:
1832 fid=fopen([websiteroot '/js/' modelname '.js'],'w');
1833 fprintf(fid,'var %s=new model();\n',modelname);
1834
1835 %now go through all the classes and fwrite all the corresponding fields:
1836
1837 fields=properties('model');
1838 for i=1:length(fields),
1839 field=fields{i};
1840
1841 %Some properties do not need to be saved
1842 if ismember(field,{'results','cluster' }),
1843 continue;
1844 end
1845
1846 %some optimization:
1847 if optimization==1,
1848 %optimize for plotting only:
1849 if ~ismember(field,{'geometry','mesh','mask'}),
1850 continue;
1851 end
1852 end
1853
1854 %Check that current field is an object
1855 if ~isobject(md.(field))
1856 error(['field ''' char(field) ''' is not an object']);
1857 end
1858
1859 %savemodeljs for current object
1860 %disp(['javascript saving ' field '...']);
1861 savemodeljs(md.(field),fid,modelname);
1862 end
1863
1864 %done, close file:
1865 fclose(fid);
1866 end
1867 end
1868end
Note: See TracBrowser for help on using the repository browser.