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

Last change on this file since 26301 was 26301, checked in by jdquinn, 4 years ago

CHG: test2002 MATLAB -> Python; clean up

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