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

Last change on this file since 26209 was 26209, checked in by schlegel, 4 years ago

BUG: isclimatology not transferring correctly to cycle_forcing from SMB in old models

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