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

Last change on this file since 27030 was 27030, checked in by Mathieu Morlighem, 3 years ago

CHG: added saveasstruct method for DJUICE

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