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

Last change on this file since 16369 was 16369, checked in by Eric.Larour, 11 years ago

CHG: reverting

File size: 58.9 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 surfaceforcings = 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
25 debug = 0;
26 verbose = 0;
27 settings = 0;
28 toolkits = 0;
29 cluster = 0;
30
31 balancethickness = 0;
32 stressbalance = 0;
33 groundingline = 0;
34 hydrology = 0;
35 masstransport = 0;
36 thermal = 0;
37 steadystate = 0;
38 transient = 0;
39 gia = 0;
40
41 autodiff = 0;
42 flaim = 0;
43 inversion = 0;
44 qmu = 0;
45
46 results = 0;
47 radaroverlay = 0;
48 miscellaneous = 0;
49 private = 0;
50
51 %}}}
52 end
53 methods (Static)
54 function md = loadobj(md) % {{{
55 % This function is directly called by matlab when a model object is
56 % loaded. If the input is a struct it is an old version of model and
57 % old fields must be recovered (make sure they are in the deprecated
58 % model properties)
59
60 if verLessThan('matlab','7.9'),
61 disp('Warning: your matlab version is old and there is a risk that load does not work correctly');
62 disp(' if the model is not loaded correctly, rename temporarily loadobj so that matlab does not use it');
63
64 % This is a Matlab bug: all the fields of md have their default value
65 % Example of error message:
66 % Warning: Error loading an object of class 'model':
67 % Undefined function or method 'exist' for input arguments of type 'cell'
68 %
69 % This has been fixed in MATLAB 7.9 (R2009b) and later versions
70 end
71
72 if isstruct(md)
73 disp('Recovering model object from a previous version');
74 md = structtomodel(model,md);
75 end
76
77 %2012 August 4th
78 if isa(md.materials,'materials'),
79 disp('Recovering old materials');
80 if numel(md.materials.rheology_Z)==1 & isnan(md.materials.rheology_Z),
81 md.materials=matice(md.materials);
82 else
83 md.materials=matdamageice(md.materials);
84 end
85 end
86 %2013 April 12
87 if numel(md.stressbalance.loadingforce==1)
88 md.stressbalance.loadingforce=0*ones(md.mesh.numberofvertices,3);
89 end
90 %2013 April 17
91 if isa(md.hydrology,'hydrology'),
92 disp('Recovering old hydrology class');
93 md.hydrology=hydrologyshreve(md.materials);
94 end
95 %2013 October 9
96 if ~isa(md.damage,'damage'),
97 md.damage=damage();
98 md.damage.D=zeros(md.mesh.numberofvertices,1);
99 md.damage.spcdamage=NaN*ones(md.mesh.numberofvertices,1);
100 end
101 end% }}}
102 end
103 methods
104 function md = model(varargin) % {{{
105
106 switch nargin
107 case 0
108 md=setdefaultparameters(md);
109 otherwise
110 error('model constructor error message: 0 of 1 argument only in input.');
111 end
112 end
113 %}}}
114 function md = checkmessage(md,string) % {{{
115 if(nargout~=1) error('wrong usage, model must be an output'); end
116 disp(['model not consistent: ' string]);
117 md.private.isconsistent=false;
118 end
119 %}}}
120 function md = collapse(md)% {{{
121 %COLLAPSE - collapses a 3d mesh into a 2d mesh
122 %
123 % This routine collapses a 3d model into a 2d model
124 % and collapses all the fileds of the 3d model by
125 % taking their depth-averaged values
126 %
127 % Usage:
128 % md=collapse(md)
129 %
130 % See also: EXTRUDE, MODELEXTRACT
131
132 %Check that the model is really a 3d model
133 if ~md.mesh.dimension==3,
134 error('collapse error message: only 3d mesh can be collapsed')
135 end
136
137 %Start with changing alle the fields from the 3d mesh
138
139 %drag is limited to nodes that are on the bedrock.
140 md.friction.coefficient=project2d(md,md.friction.coefficient,1);
141
142 %p and q (same deal, except for element that are on the bedrock: )
143 md.friction.p=project2d(md,md.friction.p,1);
144 md.friction.q=project2d(md,md.friction.q,1);
145
146 %observations
147 if ~isnan(md.inversion.vx_obs), md.inversion.vx_obs=project2d(md,md.inversion.vx_obs,md.mesh.numberoflayers); end;
148 if ~isnan(md.inversion.vy_obs), md.inversion.vy_obs=project2d(md,md.inversion.vy_obs,md.mesh.numberoflayers); end;
149 if ~isnan(md.inversion.vel_obs), md.inversion.vel_obs=project2d(md,md.inversion.vel_obs,md.mesh.numberoflayers); end;
150 if ~isnan(md.inversion.cost_functions_coefficients), md.inversion.cost_functions_coefficients=project2d(md,md.inversion.cost_functions_coefficients,md.mesh.numberoflayers); end;
151 if numel(md.inversion.min_parameters)>1, md.inversion.min_parameters=project2d(md,md.inversion.min_parameters,md.mesh.numberoflayers); end;
152 if numel(md.inversion.max_parameters)>1, md.inversion.max_parameters=project2d(md,md.inversion.max_parameters,md.mesh.numberoflayers); end;
153 if ~isnan(md.surfaceforcings.mass_balance),
154 md.surfaceforcings.mass_balance=project2d(md,md.surfaceforcings.mass_balance,md.mesh.numberoflayers);
155 end;
156 if ~isnan(md.balancethickness.thickening_rate), md.balancethickness.thickening_rate=project2d(md,md.balancethickness.thickening_rate,md.mesh.numberoflayers); end;
157
158 %results
159 if ~isnan(md.initialization.vx),md.initialization.vx=DepthAverage(md,md.initialization.vx);end;
160 if ~isnan(md.initialization.vy),md.initialization.vy=DepthAverage(md,md.initialization.vy);end;
161 if ~isnan(md.initialization.vz),md.initialization.vz=DepthAverage(md,md.initialization.vz);end;
162 if ~isnan(md.initialization.vel),md.initialization.vel=DepthAverage(md,md.initialization.vel);end;
163 if ~isnan(md.initialization.temperature),md.initialization.temperature=DepthAverage(md,md.initialization.temperature);end;
164
165 %gia
166 if ~isnan(md.gia.mantle_viscosity), md.gia.mantle_viscosity=project2d(md,md.gia.mantle_viscosity,1); end
167 if ~isnan(md.gia.lithosphere_thickness), md.gia.lithosphere_thickness=project2d(md,md.gia.lithosphere_thickness,1); end
168
169 %bedinfo and surface info
170 md.mesh.elementonbed=ones(md.mesh.numberofelements2d,1);
171 md.mesh.elementonsurface=ones(md.mesh.numberofelements2d,1);
172 md.mesh.vertexonbed=ones(md.mesh.numberofvertices2d,1);
173 md.mesh.vertexonsurface=ones(md.mesh.numberofvertices2d,1);
174
175 %elementstype
176 if ~isnan(md.flowequation.element_equation)
177 md.flowequation.element_equation=project2d(md,md.flowequation.element_equation,1);
178 md.flowequation.vertex_equation=project2d(md,md.flowequation.vertex_equation,1);
179 md.flowequation.borderSSA=project2d(md,md.flowequation.borderSSA,1);
180 md.flowequation.borderHO=project2d(md,md.flowequation.borderHO,1);
181 md.flowequation.borderFS=project2d(md,md.flowequation.borderFS,1);
182 end
183
184 %boundary conditions
185 md.stressbalance.spcvx=project2d(md,md.stressbalance.spcvx,md.mesh.numberoflayers);
186 md.stressbalance.spcvy=project2d(md,md.stressbalance.spcvy,md.mesh.numberoflayers);
187 md.stressbalance.spcvz=project2d(md,md.stressbalance.spcvz,md.mesh.numberoflayers);
188 md.stressbalance.referential=project2d(md,md.stressbalance.referential,md.mesh.numberoflayers);
189 md.stressbalance.loadingforce=project2d(md,md.stressbalance.loadingforce,md.mesh.numberoflayers);
190 md.masstransport.spcthickness=project2d(md,md.masstransport.spcthickness,md.mesh.numberoflayers);
191 md.damage.spcdamage=project2d(md,md.damage.spcdamage,md.mesh.numberoflayers)
192 md.thermal.spctemperature=project2d(md,md.thermal.spctemperature,md.mesh.numberoflayers);
193
194 %materials
195 md.materials.rheology_B=DepthAverage(md,md.materials.rheology_B);
196 md.materials.rheology_n=project2d(md,md.materials.rheology_n,1);
197
198 %damage:
199 md.damage.D=DepthAverage(md,md.damage.D);
200
201 %special for thermal modeling:
202 md.basalforcings.melting_rate=project2d(md,md.basalforcings.melting_rate,1);
203 md.basalforcings.geothermalflux=project2d(md,md.basalforcings.geothermalflux,1); %bedrock only gets geothermal flux
204
205 %update of connectivity matrix
206 md.mesh.average_vertex_connectivity=25;
207
208 %Collapse the mesh
209 nodes2d=md.mesh.numberofvertices2d;
210 elements2d=md.mesh.numberofelements2d;
211
212 %parameters
213 md.geometry.surface=project2d(md,md.geometry.surface,1);
214 md.geometry.thickness=project2d(md,md.geometry.thickness,1);
215 md.geometry.bed=project2d(md,md.geometry.bed,1);
216 md.geometry.bathymetry=project2d(md,md.geometry.bathymetry,1);
217 md.mesh.vertexonboundary=project2d(md,md.mesh.vertexonboundary,1);
218 md.mesh.elementconnectivity=project2d(md,md.mesh.elementconnectivity,1);
219 md.mask.groundedice_levelset=project2d(md,md.mask.groundedice_levelset,1);
220 md.mask.ice_levelset=project2d(md,md.mask.ice_levelset,1);
221
222 %lat long
223 if numel(md.mesh.lat) ==md.mesh.numberofvertices, md.mesh.lat=project2d(md,md.mesh.lat,1); end
224 if numel(md.mesh.long)==md.mesh.numberofvertices, md.mesh.long=project2d(md,md.mesh.long,1); end
225
226 %Initialize with the 2d mesh
227 md.mesh.x=md.mesh.x2d;
228 md.mesh.y=md.mesh.y2d;
229 md.mesh.z=zeros(size(md.mesh.x2d));
230 md.mesh.numberofvertices=md.mesh.numberofvertices2d;
231 md.mesh.numberofelements=md.mesh.numberofelements2d;
232 md.mesh.elements=md.mesh.elements2d;
233
234 %Keep a trace of lower and upper nodes
235 md.mesh.lowervertex=NaN;
236 md.mesh.uppervertex=NaN;
237 md.mesh.lowerelements=NaN;
238 md.mesh.upperelements=NaN;
239
240 %Remove old mesh
241 md.mesh.x2d=NaN;
242 md.mesh.y2d=NaN;
243 md.mesh.elements2d=NaN;
244 md.mesh.numberofelements2d=md.mesh.numberofelements;
245 md.mesh.numberofvertices2d=md.mesh.numberofvertices;
246 md.mesh.numberoflayers=0;
247
248 %Update mesh type
249 md.mesh.dimension=2;
250 end % }}}
251 function md2 = extract(md,area) % {{{
252 %extract - extract a model according to an Argus contour or flag list
253 %
254 % This routine extracts a submodel from a bigger model with respect to a given contour
255 % md must be followed by the corresponding exp file or flags list
256 % It can either be a domain file (argus type, .exp extension), or an array of element flags.
257 % If user wants every element outside the domain to be
258 % extract2d, add '~' to the name of the domain file (ex: '~HO.exp');
259 % an empty string '' will be considered as an empty domain
260 % a string 'all' will be considered as the entire domain
261 %
262 % Usage:
263 % md2=extract(md,area);
264 %
265 % Examples:
266 % md2=extract(md,'Domain.exp');
267 %
268 % See also: EXTRUDE, COLLAPSE
269
270 %copy model
271 md1=md;
272
273 %some checks
274 if ((nargin~=2) | (nargout~=1)),
275 help extract
276 error('extract error message: bad usage');
277 end
278
279 %get elements that are inside area
280 flag_elem=FlagElements(md1,area);
281 if ~any(flag_elem),
282 error('extracted model is empty');
283 end
284
285 %kick out all elements with 3 dirichlets
286 spc_elem=find(~flag_elem);
287 spc_node=sort(unique(md1.mesh.elements(spc_elem,:)));
288 flag=ones(md1.mesh.numberofvertices,1);
289 flag(spc_node)=0;
290 pos=find(sum(flag(md1.mesh.elements),2)==0);
291 flag_elem(pos)=0;
292
293 %extracted elements and nodes lists
294 pos_elem=find(flag_elem);
295 pos_node=sort(unique(md1.mesh.elements(pos_elem,:)));
296
297 %keep track of some fields
298 numberofvertices1=md1.mesh.numberofvertices;
299 numberofelements1=md1.mesh.numberofelements;
300 numberofvertices2=length(pos_node);
301 numberofelements2=length(pos_elem);
302 flag_node=zeros(numberofvertices1,1);
303 flag_node(pos_node)=1;
304
305 %Create Pelem and Pnode (transform old nodes in new nodes and same thing for the elements)
306 Pelem=zeros(numberofelements1,1);
307 Pelem(pos_elem)=[1:numberofelements2]';
308 Pnode=zeros(numberofvertices1,1);
309 Pnode(pos_node)=[1:numberofvertices2]';
310
311 %renumber the elements (some nodes won't exist anymore)
312 elements_1=md1.mesh.elements;
313 elements_2=elements_1(pos_elem,:);
314 elements_2(:,1)=Pnode(elements_2(:,1));
315 elements_2(:,2)=Pnode(elements_2(:,2));
316 elements_2(:,3)=Pnode(elements_2(:,3));
317 if md1.mesh.dimension==3,
318 elements_2(:,4)=Pnode(elements_2(:,4));
319 elements_2(:,5)=Pnode(elements_2(:,5));
320 elements_2(:,6)=Pnode(elements_2(:,6));
321 end
322
323 %OK, now create the new model!
324
325 %take every field from model
326 md2=md1;
327
328 %automatically modify fields
329
330 %loop over model fields
331 model_fields=fields(md1);
332 for i=1:length(model_fields),
333 %get field
334 field=md1.(model_fields{i});
335 fieldsize=size(field);
336 if isobject(field), %recursive call
337 object_fields=fields(md1.(model_fields{i}));
338 for j=1:length(object_fields),
339 %get field
340 field=md1.(model_fields{i}).(object_fields{j});
341 fieldsize=size(field);
342 %size = number of nodes * n
343 if fieldsize(1)==numberofvertices1
344 md2.(model_fields{i}).(object_fields{j})=field(pos_node,:);
345 elseif (fieldsize(1)==numberofvertices1+1)
346 md2.(model_fields{i}).(object_fields{j})=[field(pos_node,:); field(end,:)];
347 %size = number of elements * n
348 elseif fieldsize(1)==numberofelements1
349 md2.(model_fields{i}).(object_fields{j})=field(pos_elem,:);
350 end
351 end
352 else
353 %size = number of nodes * n
354 if fieldsize(1)==numberofvertices1
355 md2.(model_fields{i})=field(pos_node,:);
356 elseif (fieldsize(1)==numberofvertices1+1)
357 md2.(model_fields{i})=[field(pos_node,:); field(end,:)];
358 %size = number of elements * n
359 elseif fieldsize(1)==numberofelements1
360 md2.(model_fields{i})=field(pos_elem,:);
361 end
362 end
363 end
364
365 %modify some specific fields
366
367 %Mesh
368 md2.mesh.numberofelements=numberofelements2;
369 md2.mesh.numberofvertices=numberofvertices2;
370 md2.mesh.elements=elements_2;
371
372 %mesh.uppervertex mesh.lowervertex
373 if md1.mesh.dimension==3
374 md2.mesh.uppervertex=md1.mesh.uppervertex(pos_node);
375 pos=find(~isnan(md2.mesh.uppervertex));
376 md2.mesh.uppervertex(pos)=Pnode(md2.mesh.uppervertex(pos));
377
378 md2.mesh.lowervertex=md1.mesh.lowervertex(pos_node);
379 pos=find(~isnan(md2.mesh.lowervertex));
380 md2.mesh.lowervertex(pos)=Pnode(md2.mesh.lowervertex(pos));
381
382 md2.mesh.upperelements=md1.mesh.upperelements(pos_elem);
383 pos=find(~isnan(md2.mesh.upperelements));
384 md2.mesh.upperelements(pos)=Pelem(md2.mesh.upperelements(pos));
385
386 md2.mesh.lowerelements=md1.mesh.lowerelements(pos_elem);
387 pos=find(~isnan(md2.mesh.lowerelements));
388 md2.mesh.lowerelements(pos)=Pelem(md2.mesh.lowerelements(pos));
389 end
390
391 %Initial 2d mesh
392 if md1.mesh.dimension==3
393 flag_elem_2d=flag_elem(1:md1.mesh.numberofelements2d);
394 pos_elem_2d=find(flag_elem_2d);
395 flag_node_2d=flag_node(1:md1.mesh.numberofvertices2d);
396 pos_node_2d=find(flag_node_2d);
397
398 md2.mesh.numberofelements2d=length(pos_elem_2d);
399 md2.mesh.numberofvertices2d=length(pos_node_2d);
400 md2.mesh.elements2d=md1.mesh.elements2d(pos_elem_2d,:);
401 md2.mesh.elements2d(:,1)=Pnode(md2.mesh.elements2d(:,1));
402 md2.mesh.elements2d(:,2)=Pnode(md2.mesh.elements2d(:,2));
403 md2.mesh.elements2d(:,3)=Pnode(md2.mesh.elements2d(:,3));
404
405 md2.mesh.x2d=md1.mesh.x(pos_node_2d);
406 md2.mesh.y2d=md1.mesh.y(pos_node_2d);
407 end
408
409 %Edges
410 if size(md2.mesh.edges,2)>1, %do not use ~isnan because there are some NaNs...
411 %renumber first two columns
412 pos=find(md2.mesh.edges(:,4)~=-1);
413 md2.mesh.edges(: ,1)=Pnode(md2.mesh.edges(:,1));
414 md2.mesh.edges(: ,2)=Pnode(md2.mesh.edges(:,2));
415 md2.mesh.edges(: ,3)=Pelem(md2.mesh.edges(:,3));
416 md2.mesh.edges(pos,4)=Pelem(md2.mesh.edges(pos,4));
417 %remove edges when the 2 vertices are not in the domain.
418 md2.mesh.edges=md2.mesh.edges(find(md2.mesh.edges(:,1) & md2.mesh.edges(:,2)),:);
419 %Replace all zeros by -1 in the last two columns
420 pos=find(md2.mesh.edges(:,3)==0);
421 md2.mesh.edges(pos,3)=-1;
422 pos=find(md2.mesh.edges(:,4)==0);
423 md2.mesh.edges(pos,4)=-1;
424 %Invert -1 on the third column with last column (Also invert first two columns!!)
425 pos=find(md2.mesh.edges(:,3)==-1);
426 md2.mesh.edges(pos,3)=md2.mesh.edges(pos,4);
427 md2.mesh.edges(pos,4)=-1;
428 values=md2.mesh.edges(pos,2);
429 md2.mesh.edges(pos,2)=md2.mesh.edges(pos,1);
430 md2.mesh.edges(pos,1)=values;
431 %Finally remove edges that do not belong to any element
432 pos=find(md2.mesh.edges(:,3)==-1 & md2.mesh.edges(:,4)==-1);
433 md2.mesh.edges(pos,:)=[];
434 end
435
436 %Penalties
437 if ~isnan(md2.stressbalance.vertex_pairing),
438 for i=1:size(md1.stressbalance.vertex_pairing,1);
439 md2.stressbalance.vertex_pairing(i,:)=Pnode(md1.stressbalance.vertex_pairing(i,:));
440 end
441 md2.stressbalance.vertex_pairing=md2.stressbalance.vertex_pairing(find(md2.stressbalance.vertex_pairing(:,1)),:);
442 end
443 if ~isnan(md2.masstransport.vertex_pairing),
444 for i=1:size(md1.masstransport.vertex_pairing,1);
445 md2.masstransport.vertex_pairing(i,:)=Pnode(md1.masstransport.vertex_pairing(i,:));
446 end
447 md2.masstransport.vertex_pairing=md2.masstransport.vertex_pairing(find(md2.masstransport.vertex_pairing(:,1)),:);
448 end
449
450 %recreate segments
451 if md1.mesh.dimension==2
452 md2.mesh.vertexconnectivity=NodeConnectivity(md2.mesh.elements,md2.mesh.numberofvertices);
453 md2.mesh.elementconnectivity=ElementConnectivity(md2.mesh.elements,md2.mesh.vertexconnectivity);
454 md2.mesh.segments=contourenvelope(md2);
455 md2.mesh.vertexonboundary=zeros(numberofvertices2,1); md2.mesh.vertexonboundary(md2.mesh.segments(:,1:2))=1;
456 else
457 %First do the connectivity for the contourenvelope in 2d
458 md2.mesh.vertexconnectivity=NodeConnectivity(md2.mesh.elements2d,md2.mesh.numberofvertices2d);
459 md2.mesh.elementconnectivity=ElementConnectivity(md2.mesh.elements2d,md2.mesh.vertexconnectivity);
460 md2.mesh.segments=contourenvelope(md2);
461 md2.mesh.vertexonboundary=zeros(numberofvertices2/md2.mesh.numberoflayers,1); md2.mesh.vertexonboundary(md2.mesh.segments(:,1:2))=1;
462 md2.mesh.vertexonboundary=repmat(md2.mesh.vertexonboundary,md2.mesh.numberoflayers,1);
463 %Then do it for 3d as usual
464 md2.mesh.vertexconnectivity=NodeConnectivity(md2.mesh.elements,md2.mesh.numberofvertices);
465 md2.mesh.elementconnectivity=ElementConnectivity(md2.mesh.elements,md2.mesh.vertexconnectivity);
466 end
467
468 %Boundary conditions: Dirichlets on new boundary
469 %Catch the elements that have not been extracted
470 orphans_elem=find(~flag_elem);
471 orphans_node=unique(md1.mesh.elements(orphans_elem,:))';
472 %Figure out which node are on the boundary between md2 and md1
473 nodestoflag1=intersect(orphans_node,pos_node);
474 nodestoflag2=Pnode(nodestoflag1);
475 if numel(md1.stressbalance.spcvx)>1 & numel(md1.stressbalance.spcvy)>2 & numel(md1.stressbalance.spcvz)>2,
476 if numel(md1.inversion.vx_obs)>1 & numel(md1.inversion.vy_obs)>1
477 md2.stressbalance.spcvx(nodestoflag2)=md2.inversion.vx_obs(nodestoflag2);
478 md2.stressbalance.spcvy(nodestoflag2)=md2.inversion.vy_obs(nodestoflag2);
479 else
480 md2.stressbalance.spcvx(nodestoflag2)=NaN;
481 md2.stressbalance.spcvy(nodestoflag2)=NaN;
482 disp(' ')
483 disp('!! extract warning: spc values should be checked !!')
484 disp(' ')
485 end
486 %put 0 for vz
487 md2.stressbalance.spcvz(nodestoflag2)=0;
488 end
489 if ~isnan(md1.thermal.spctemperature),
490 md2.thermal.spctemperature(nodestoflag2,1)=1;
491 end
492
493 %Results fields
494 if isstruct(md1.results),
495 md2.results=struct();
496 solutionfields=fields(md1.results);
497 for i=1:length(solutionfields),
498 if isstruct(md1.results.(solutionfields{i}))
499 %get subfields
500 solutionsubfields=fields(md1.results.(solutionfields{i}));
501 for j=1:length(solutionsubfields),
502 field=md1.results.(solutionfields{i}).(solutionsubfields{j});
503 if length(field)==numberofvertices1,
504 md2.results.(solutionfields{i}).(solutionsubfields{j})=field(pos_node);
505 elseif length(field)==numberofelements1,
506 md2.results.(solutionfields{i}).(solutionsubfields{j})=field(pos_elem);
507 else
508 md2.results.(solutionfields{i}).(solutionsubfields{j})=field;
509 end
510 end
511 else
512 field=md1.results.(solutionfields{i});
513 if length(field)==numberofvertices1,
514 md2.results.(solutionfields{i})=field(pos_node);
515 elseif length(field)==numberofelements1,
516 md2.results.(solutionfields{i})=field(pos_elem);
517 else
518 md2.results.(solutionfields{i})=field;
519 end
520 end
521 end
522 end
523
524 %Keep track of pos_node and pos_elem
525 md2.mesh.extractedvertices=pos_node;
526 md2.mesh.extractedelements=pos_elem;
527 end % }}}
528 function md = extrude(md,varargin) % {{{
529 %EXTRUDE - vertically extrude a 2d mesh
530 %
531 % vertically extrude a 2d mesh and create corresponding 3d mesh.
532 % The vertical distribution can:
533 % - follow a polynomial law
534 % - follow two polynomial laws, one for the lower part and one for the upper part of the mesh
535 % - be discribed by a list of coefficients (between 0 and 1)
536 %
537 %
538 % Usage:
539 % md=extrude(md,numlayers,extrusionexponent);
540 % md=extrude(md,numlayers,lowerexponent,upperexponent);
541 % md=extrude(md,listofcoefficients);
542 %
543 % Example:
544 % md=extrude(md,8,3);
545 % md=extrude(md,8,3,2);
546 % md=extrude(md,[0 0.2 0.5 0.7 0.9 0.95 1]);
547 %
548 % See also: MODELEXTRACT, COLLAPSE
549
550 %some checks on list of arguments
551 if ((nargin>4) | (nargin<2) | (nargout~=1)),
552 help extrude;
553 error('extrude error message');
554 end
555
556 %Extrude the mesh
557 if nargin==2, %list of coefficients
558 clist=varargin{1};
559 if any(clist<0) | any(clist>1),
560 error('extrusioncoefficients must be between 0 and 1');
561 end
562 extrusionlist=sort(unique([clist(:);0;1]));
563 numlayers=length(extrusionlist);
564 elseif nargin==3, %one polynomial law
565 if varargin{2}<=0,
566 help extrude;
567 error('extrusionexponent must be >=0');
568 end
569 numlayers=varargin{1};
570 extrusionlist=((0:1:numlayers-1)/(numlayers-1)).^varargin{2};
571 elseif nargin==4, %two polynomial laws
572 numlayers=varargin{1};
573 lowerexp=varargin{2};
574 upperexp=varargin{3};
575
576 if varargin{2}<=0 | varargin{3}<=0,
577 help extrude;
578 error('lower and upper extrusionexponents must be >=0');
579 end
580
581 lowerextrusionlist=[(0:2/(numlayers-1):1).^lowerexp]/2;
582 upperextrusionlist=[(0:2/(numlayers-1):1).^upperexp]/2;
583 extrusionlist=sort(unique([lowerextrusionlist 1-upperextrusionlist]));
584
585 end
586
587 if numlayers<2,
588 error('number of layers should be at least 2');
589 end
590 if md.mesh.dimension==3,
591 error('Cannot extrude a 3d mesh (extrude cannot be called more than once)');
592 end
593
594 %Initialize with the 2d mesh
595 x3d=[];
596 y3d=[];
597 z3d=[]; %the lower node is on the bed
598 thickness3d=md.geometry.thickness; %thickness and bed for these nodes
599 bed3d=md.geometry.bed;
600
601 %Create the new layers
602 for i=1:numlayers,
603 x3d=[x3d; md.mesh.x];
604 y3d=[y3d; md.mesh.y];
605 %nodes are distributed between bed and surface accordingly to the given exponent
606 z3d=[z3d; bed3d+thickness3d*extrusionlist(i)];
607 end
608 number_nodes3d=size(x3d,1); %number of 3d nodes for the non extruded part of the mesh
609
610 %Extrude elements
611 elements3d=[];
612 for i=1:numlayers-1,
613 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
614 end
615 number_el3d=size(elements3d,1); %number of 3d nodes for the non extruded part of the mesh
616
617 %Keep a trace of lower and upper nodes
618 mesh.lowervertex=NaN*ones(number_nodes3d,1);
619 mesh.uppervertex=NaN*ones(number_nodes3d,1);
620 mesh.lowervertex(md.mesh.numberofvertices+1:end)=1:(numlayers-1)*md.mesh.numberofvertices;
621 mesh.uppervertex(1:(numlayers-1)*md.mesh.numberofvertices)=md.mesh.numberofvertices+1:number_nodes3d;
622 md.mesh.lowervertex=mesh.lowervertex;
623 md.mesh.uppervertex=mesh.uppervertex;
624
625 %same for lower and upper elements
626 mesh.lowerelements=NaN*ones(number_el3d,1);
627 mesh.upperelements=NaN*ones(number_el3d,1);
628 mesh.lowerelements(md.mesh.numberofelements+1:end)=1:(numlayers-2)*md.mesh.numberofelements;
629 mesh.upperelements(1:(numlayers-2)*md.mesh.numberofelements)=md.mesh.numberofelements+1:(numlayers-1)*md.mesh.numberofelements;
630 md.mesh.lowerelements=mesh.lowerelements;
631 md.mesh.upperelements=mesh.upperelements;
632
633 %Save old mesh
634 md.mesh.x2d=md.mesh.x;
635 md.mesh.y2d=md.mesh.y;
636 md.mesh.elements2d=md.mesh.elements;
637 md.mesh.numberofelements2d=md.mesh.numberofelements;
638 md.mesh.numberofvertices2d=md.mesh.numberofvertices;
639
640 %Update mesh type
641 md.mesh.dimension=3;
642
643 %Build global 3d mesh
644 md.mesh.elements=elements3d;
645 md.mesh.x=x3d;
646 md.mesh.y=y3d;
647 md.mesh.z=z3d;
648 md.mesh.numberofelements=number_el3d;
649 md.mesh.numberofvertices=number_nodes3d;
650 md.mesh.numberoflayers=numlayers;
651
652 %Ok, now deal with the other fields from the 2d mesh:
653
654 %lat long
655 md.mesh.lat=project3d(md,'vector',md.mesh.lat,'type','node');
656 md.mesh.long=project3d(md,'vector',md.mesh.long,'type','node');
657
658 %drag coefficient is limited to nodes that are on the bedrock.
659 md.friction.coefficient=project3d(md,'vector',md.friction.coefficient,'type','node','layer',1);
660
661 %p and q (same deal, except for element that are on the bedrock: )
662 md.friction.p=project3d(md,'vector',md.friction.p,'type','element');
663 md.friction.q=project3d(md,'vector',md.friction.q,'type','element');
664
665 %observations
666 md.inversion.vx_obs=project3d(md,'vector',md.inversion.vx_obs,'type','node');
667 md.inversion.vy_obs=project3d(md,'vector',md.inversion.vy_obs,'type','node');
668 md.inversion.vel_obs=project3d(md,'vector',md.inversion.vel_obs,'type','node');
669 md.surfaceforcings.mass_balance=project3d(md,'vector',md.surfaceforcings.mass_balance,'type','node');
670 md.surfaceforcings.precipitation=project3d(md,'vector',md.surfaceforcings.precipitation,'type','node');
671 md.balancethickness.thickening_rate=project3d(md,'vector',md.balancethickness.thickening_rate,'type','node');
672 md.surfaceforcings.monthlytemperatures=project3d(md,'vector',md.surfaceforcings.monthlytemperatures,'type','node');
673
674 %results
675 if ~isnan(md.initialization.vx),md.initialization.vx=project3d(md,'vector',md.initialization.vx,'type','node');end;
676 if ~isnan(md.initialization.vy),md.initialization.vy=project3d(md,'vector',md.initialization.vy,'type','node');end;
677 if ~isnan(md.initialization.vz),md.initialization.vz=project3d(md,'vector',md.initialization.vz,'type','node');end;
678 if ~isnan(md.initialization.vel),md.initialization.vel=project3d(md,'vector',md.initialization.vel,'type','node');end;
679 if ~isnan(md.initialization.temperature),md.initialization.temperature=project3d(md,'vector',md.initialization.temperature,'type','node');end;
680 if ~isnan(md.initialization.waterfraction),md.initialization.waterfraction=project3d(md,'vector',md.initialization.waterfraction,'type','node');end;
681 if ~isnan(md.initialization.watercolumn),md.initialization.watercolumn=project3d(md,'vector',md.initialization.watercolumn,'type','node','layer',1);end;
682
683 %bedinfo and surface info
684 md.mesh.elementonbed=project3d(md,'vector',ones(md.mesh.numberofelements2d,1),'type','element','layer',1);
685 md.mesh.elementonsurface=project3d(md,'vector',ones(md.mesh.numberofelements2d,1),'type','element','layer',md.mesh.numberoflayers-1);
686 md.mesh.vertexonbed=project3d(md,'vector',ones(md.mesh.numberofvertices2d,1),'type','node','layer',1);
687 md.mesh.vertexonsurface=project3d(md,'vector',ones(md.mesh.numberofvertices2d,1),'type','node','layer',md.mesh.numberoflayers);
688
689 %elementstype
690 if ~isnan(md.flowequation.element_equation)
691 oldelements_type=md.flowequation.element_equation;
692 md.flowequation.element_equation=zeros(number_el3d,1);
693 md.flowequation.element_equation=project3d(md,'vector',oldelements_type,'type','element');
694 end
695
696 %verticestype
697 if ~isnan(md.flowequation.vertex_equation)
698 oldvertices_type=md.flowequation.vertex_equation;
699 md.flowequation.vertex_equation=zeros(number_nodes3d,1);
700 md.flowequation.vertex_equation=project3d(md,'vector',oldvertices_type,'type','node');
701 end
702 md.flowequation.borderSSA=project3d(md,'vector',md.flowequation.borderSSA,'type','node');
703 md.flowequation.borderHO=project3d(md,'vector',md.flowequation.borderHO,'type','node');
704 md.flowequation.borderFS=project3d(md,'vector',md.flowequation.borderFS,'type','node');
705
706 %boundary conditions
707 md.stressbalance.spcvx=project3d(md,'vector',md.stressbalance.spcvx,'type','node');
708 md.stressbalance.spcvy=project3d(md,'vector',md.stressbalance.spcvy,'type','node');
709 md.stressbalance.spcvz=project3d(md,'vector',md.stressbalance.spcvz,'type','node');
710 md.thermal.spctemperature=project3d(md,'vector',md.thermal.spctemperature,'type','node','layer',md.mesh.numberoflayers,'padding',NaN);
711 md.masstransport.spcthickness=project3d(md,'vector',md.masstransport.spcthickness,'type','node');
712 md.balancethickness.spcthickness=project3d(md,'vector',md.balancethickness.spcthickness,'type','node');
713 md.damage.spcdamage=project3d(md,'vector',md.damage.spcdamage,'type','node');
714 md.stressbalance.referential=project3d(md,'vector',md.stressbalance.referential,'type','node');
715 md.stressbalance.loadingforce=project3d(md,'vector',md.stressbalance.loadingforce,'type','node');
716
717 %connectivity
718 md.mesh.elementconnectivity=repmat(md.mesh.elementconnectivity,numlayers-1,1);
719 md.mesh.elementconnectivity(find(md.mesh.elementconnectivity==0))=NaN;
720 for i=2:numlayers-1,
721 md.mesh.elementconnectivity((i-1)*md.mesh.numberofelements2d+1:(i)*md.mesh.numberofelements2d,:)...
722 =md.mesh.elementconnectivity((i-1)*md.mesh.numberofelements2d+1:(i)*md.mesh.numberofelements2d,:)+md.mesh.numberofelements2d;
723 end
724 md.mesh.elementconnectivity(find(isnan(md.mesh.elementconnectivity)))=0;
725
726 %materials
727 md.materials.rheology_B=project3d(md,'vector',md.materials.rheology_B,'type','node');
728 md.materials.rheology_n=project3d(md,'vector',md.materials.rheology_n,'type','element');
729
730 %damage
731 md.damage.D=project3d(md,'vector',md.damage.D,'type','node');
732
733 %parameters
734 md.geometry.surface=project3d(md,'vector',md.geometry.surface,'type','node');
735 md.geometry.thickness=project3d(md,'vector',md.geometry.thickness,'type','node');
736 md.gia.mantle_viscosity=project3d(md,'vector',md.gia.mantle_viscosity,'type','node');
737 md.gia.lithosphere_thickness=project3d(md,'vector',md.gia.lithosphere_thickness,'type','node');
738 md.geometry.hydrostatic_ratio=project3d(md,'vector',md.geometry.hydrostatic_ratio,'type','node');
739 md.geometry.bed=project3d(md,'vector',md.geometry.bed,'type','node');
740 md.geometry.bathymetry=project3d(md,'vector',md.geometry.bathymetry,'type','node');
741 md.mesh.vertexonboundary=project3d(md,'vector',md.mesh.vertexonboundary,'type','node');
742 md.mask.groundedice_levelset=project3d(md,'vector',md.mask.groundedice_levelset,'type','node');
743 md.mask.ice_levelset=project3d(md,'vector',md.mask.ice_levelset,'type','node');
744 if ~isnan(md.inversion.cost_functions_coefficients),md.inversion.cost_functions_coefficients=project3d(md,'vector',md.inversion.cost_functions_coefficients,'type','node');end;
745 if ~isnan(md.inversion.min_parameters),md.inversion.min_parameters=project3d(md,'vector',md.inversion.min_parameters,'type','node');end;
746 if ~isnan(md.inversion.max_parameters),md.inversion.max_parameters=project3d(md,'vector',md.inversion.max_parameters,'type','node');end;
747 if ~isnan(md.qmu.partition),md.qmu.partition=project3d(md,'vector',md.qmu.partition','type','node');end
748 if(md.surfaceforcings.isdelta18o),md.surfaceforcings.temperatures_lgm=project3d(md,'vector',md.surfaceforcings.temperatures_lgm,'type','node');end
749 if(md.surfaceforcings.isdelta18o),md.surfaceforcings.temperatures_presentday=project3d(md,'vector',md.surfaceforcings.temperatures_presentday,'type','node');end
750 if(md.surfaceforcings.isdelta18o),md.surfaceforcings.precipitations_presentday=project3d(md,'vector',md.surfaceforcings.precipitations_presentday,'type','node');end
751
752 %Put lithostatic pressure if there is an existing pressure
753 if ~isnan(md.initialization.pressure),
754 md.initialization.pressure=md.constants.g*md.materials.rho_ice*(md.geometry.surface-md.mesh.z);
755 end
756
757 %special for thermal modeling:
758 md.basalforcings.melting_rate=project3d(md,'vector',md.basalforcings.melting_rate,'type','node','layer',1);
759 if ~isnan(md.basalforcings.geothermalflux)
760 md.basalforcings.geothermalflux=project3d(md,'vector',md.basalforcings.geothermalflux,'type','node','layer',1); %bedrock only gets geothermal flux
761 end
762
763 %increase connectivity if less than 25:
764 if md.mesh.average_vertex_connectivity<=25,
765 md.mesh.average_vertex_connectivity=100;
766 end
767 end % }}}
768 function md = structtomodel(md,structmd) % {{{
769
770 if ~isstruct(structmd) error('input model is not a structure'); end
771
772 %loaded model is a struct, initialize output and recover all fields
773 md = structtoobj(model,structmd);
774
775 %Old field now classes
776 if (isfield(structmd,'timestepping') & isnumeric(md.timestepping)), md.timestepping=timestepping(); end
777 if (isfield(structmd,'mask') & isnumeric(md.mask)),md.mask=mask(); end
778
779 %Field name change
780 if isfield(structmd,'drag'), md.friction.coefficient=structmd.drag; end
781 if isfield(structmd,'p'), md.friction.p=structmd.p; end
782 if isfield(structmd,'q'), md.friction.q=structmd.p; end
783 if isfield(structmd,'melting'), md.basalforcings.melting_rate=structmd.melting; end
784 if isfield(structmd,'melting_rate'), md.basalforcings.melting_rate=structmd.melting_rate; end
785 if isfield(structmd,'accumulation'), md.surfaceforcings.mass_balance=structmd.accumulation; end
786 if isfield(structmd,'numberofgrids'), md.mesh.numberofvertices=structmd.numberofgrids; end
787 if isfield(structmd,'numberofgrids2d'), md.mesh.numberofvertices2d=structmd.numberofgrids2d; end
788 if isfield(structmd,'uppergrids'), md.mesh.uppervertex=structmd.uppergrids; end
789 if isfield(structmd,'lowergrids'), md.mesh.lowervertex=structmd.lowergrids; end
790 if isfield(structmd,'gridonbed'), md.mesh.vertexonbed=structmd.gridonbed; end
791 if isfield(structmd,'gridonsurface'), md.mesh.vertexonsurface=structmd.gridonsurface; end
792 if isfield(structmd,'extractedgrids'), md.mesh.extractedvertices=structmd.extractedgrids; end
793 if isfield(structmd,'gridonboundary'), md.mesh.vertexonboundary=structmd.gridonboundary; end
794 if isfield(structmd,'petscoptions') & ~isempty(structmd.petscoptions), md.toolkits=structmd.petscoptions; end
795 if isfield(structmd,'g'), md.constants.g=structmd.g; end
796 if isfield(structmd,'yts'), md.constants.yts=structmd.yts; end
797 if isfield(structmd,'surface_mass_balance'), md.surfaceforcings.mass_balance=structmd.surface_mass_balance; end
798 if isfield(structmd,'basal_melting_rate'), md.basalforcings.melting_rate=structmd.basal_melting_rate; end
799 if isfield(structmd,'basal_melting_rate_correction'), md.basalforcings.melting_rate_correction=structmd.basal_melting_rate_correction; end
800 if isfield(structmd,'geothermalflux'), md.basalforcings.geothermalflux=structmd.geothermalflux; end
801 if isfield(structmd,'drag'), md.friction.coefficient=structmd.drag; end
802 if isfield(structmd,'drag_coefficient'), md.friction.coefficient=structmd.drag_coefficient; end
803 if isfield(structmd,'drag_p'), md.friction.p=structmd.drag_p; end
804 if isfield(structmd,'drag_q'), md.friction.q=structmd.drag_q; end
805 if isfield(structmd,'riftproperties'), %old implementation
806 md.rifts=rifts();
807 md.rifts.riftproperties=structmd.riftproperties;
808 md.rifts.riftstruct=structmd.rifts;
809 md.rifts.riftproperties=structmd.riftinfo;
810 end
811 if isfield(structmd,'bamg'), md.private.bamg=structmd.bamg; end
812 if isfield(structmd,'lowmem'), md.settings.lowmem=structmd.lowmem; end
813 if isfield(structmd,'io_gather'), md.settings.io_gather=structmd.io_gather; end
814 if isfield(structmd,'spcwatercolumn'), md.hydrology.spcwatercolumn=structmd.spcwatercolumn; end
815 if isfield(structmd,'hydro_n'), md.hydrology.n=structmd.hydro_n; end
816 if isfield(structmd,'hydro_p'), md.hydrology.p=structmd.hydro_p; end
817 if isfield(structmd,'hydro_q'), md.hydrology.q=structmd.hydro_q; end
818 if isfield(structmd,'hydro_CR'), md.hydrology.CR=structmd.hydro_CR; end
819 if isfield(structmd,'hydro_kn'), md.hydrology.kn=structmd.hydro_kn; end
820 if isfield(structmd,'spctemperature'), md.thermal.spctemperature=structmd.spctemperature; end
821 if isfield(structmd,'min_thermal_constraints'), md.thermal.penalty_threshold=structmd.min_thermal_constraints; end
822 if isfield(structmd,'artificial_diffusivity'), md.thermal.stabilization=structmd.artificial_diffusivity; end
823 if isfield(structmd,'max_nonlinear_iterations'), md.thermal.maxiter=structmd.max_nonlinear_iterations; end
824 if isfield(structmd,'stabilize_constraints'), md.thermal.penalty_lock=structmd.stabilize_constraints; end
825 if isfield(structmd,'penalty_offset'), md.thermal.penalty_factor=structmd.penalty_offset; end
826 if isfield(structmd,'name'), md.miscellaneous.name=structmd.name; end
827 if isfield(structmd,'notes'), md.miscellaneous.notes=structmd.notes; end
828 if isfield(structmd,'dummy'), md.miscellaneous.dummy=structmd.dummy; end
829 if isfield(structmd,'dt'), md.timestepping.time_step=structmd.dt; end
830 if isfield(structmd,'ndt'), md.timestepping.final_time=structmd.ndt; end
831 if isfield(structmd,'time_adapt'), md.timestepping.time_adapt=structmd.time_adapt; end
832 if isfield(structmd,'cfl_coefficient'), md.timestepping.cfl_coefficient=structmd.cfl_coefficient; end
833 if isfield(structmd,'spcthickness'), md.masstransport.spcthickness=structmd.spcthickness; end
834 if isfield(structmd,'artificial_diffusivity'), md.masstransport.stabilization=structmd.artificial_diffusivity; end
835 if isfield(structmd,'hydrostatic_adjustment'), md.masstransport.hydrostatic_adjustment=structmd.hydrostatic_adjustment; end
836 if isfield(structmd,'penalties'), md.masstransport.vertex_pairing=structmd.penalties; end
837 if isfield(structmd,'penalty_offset'), md.masstransport.penalty_factor=structmd.penalty_offset; end
838 if isfield(structmd,'B'), md.materials.rheology_B=structmd.B; end
839 if isfield(structmd,'n'), md.materials.rheology_n=structmd.n; end
840 if isfield(structmd,'rheology_B'), md.materials.rheology_B=structmd.rheology_B; end
841 if isfield(structmd,'rheology_n'), md.materials.rheology_n=structmd.rheology_n; end
842 if isfield(structmd,'rheology_Z'), md.damage.D=(1-structmd.rheology_Z); end
843 if isfield(structmd,'spcthickness'), md.balancethickness.spcthickness=structmd.spcthickness; end
844 if isfield(structmd,'artificial_diffusivity'), md.balancethickness.stabilization=structmd.artificial_diffusivity; end
845 if isfield(structmd,'dhdt'), md.balancethickness.thickening_rate=structmd.dhdt; end
846 if isfield(structmd,'isSIA'), md.flowequation.isSIA=structmd.isSIA; end
847 if isfield(structmd,'isFS'), md.flowequation.isFS=structmd.isFS; end
848 if isfield(structmd,'elements_type'), md.flowequation.element_equation=structmd.elements_type; end
849 if isfield(structmd,'vertices_type'), md.flowequation.vertex_equation=structmd.vertices_type; end
850 if isfield(structmd,'eps_rel'), md.steadystate.reltol=structmd.eps_rel; end
851 if isfield(structmd,'max_steadystate_iterations'), md.steadystate.maxiter=structmd.max_steadystate_iterations; end
852 if isfield(structmd,'isdiagnostic'), md.transient.isstressbalance=structmd.isdiagnostic; end
853 if isfield(structmd,'isprognostic'), md.transient.ismasstransport=structmd.isprognostic; end
854 if isfield(structmd,'isthermal'), md.transient.isthermal=structmd.isthermal; end
855 if isfield(structmd,'control_analysis'), md.inversion.iscontrol=structmd.control_analysis; end
856 if isfield(structmd,'weights'), md.inversion.cost_functions_coefficients=structmd.weights; end
857 if isfield(structmd,'nsteps'), md.inversion.nsteps=structmd.nsteps; end
858 if isfield(structmd,'maxiter_per_step'), md.inversion.maxiter_per_step=structmd.maxiter_per_step; end
859 if isfield(structmd,'cm_min'), md.inversion.min_parameters=structmd.cm_min; end
860 if isfield(structmd,'cm_max'), md.inversion.max_parameters=structmd.cm_max; end
861 if isfield(structmd,'vx_obs'), md.inversion.vx_obs=structmd.vx_obs; end
862 if isfield(structmd,'vy_obs'), md.inversion.vy_obs=structmd.vy_obs; end
863 if isfield(structmd,'vel_obs'), md.inversion.vel_obs=structmd.vel_obs; end
864 if isfield(structmd,'thickness_obs'), md.inversion.thickness_obs=structmd.thickness_obs; end
865 if isfield(structmd,'vx'), md.initialization.vx=structmd.vx; end
866 if isfield(structmd,'vy'), md.initialization.vy=structmd.vy; end
867 if isfield(structmd,'vz'), md.initialization.vz=structmd.vz; end
868 if isfield(structmd,'vel'), md.initialization.vel=structmd.vel; end
869 if isfield(structmd,'pressure'), md.initialization.pressure=structmd.pressure; end
870 if isfield(structmd,'temperature'), md.initialization.temperature=structmd.temperature; end
871 if isfield(structmd,'waterfraction'), md.initialization.waterfraction=structmd.waterfraction; end
872 if isfield(structmd,'watercolumn'), md.initialization.watercolumn=structmd.watercolumn; end
873 if isfield(structmd,'surface'), md.geometry.surface=structmd.surface; end
874 if isfield(structmd,'bed'), md.geometry.bed=structmd.bed; end
875 if isfield(structmd,'thickness'), md.geometry.thickness=structmd.thickness; end
876 if isfield(structmd,'bathymetry'), md.geometry.bathymetry=structmd.bathymetry; end
877 if isfield(structmd,'thickness_coeff'), md.geometry.hydrostatic_ratio=structmd.thickness_coeff; end
878 if isfield(structmd,'connectivity'), md.mesh.average_vertex_connectivity=structmd.connectivity; end
879 if isfield(structmd,'extractednodes'), md.mesh.extractedvertices=structmd.extractednodes; end
880 if isfield(structmd,'extractedelements'), md.mesh.extractedelements=structmd.extractedelements; end
881 if isfield(structmd,'nodeonboundary'), md.mesh.vertexonboundary=structmd.nodeonboundary; end
882 if isfield(structmd,'hemisphere'), md.mesh.hemisphere=structmd.hemisphere; end
883 if isfield(structmd,'lat'), md.mesh.lat=structmd.lat; end
884 if isfield(structmd,'long'), md.mesh.long=structmd.long; end
885 if isfield(structmd,'segments'), md.mesh.segments=structmd.segments; end
886 if isfield(structmd,'segmentmarkers'), md.mesh.segmentmarkers=structmd.segmentmarkers; end
887 if isfield(structmd,'dim'), md.mesh.dimension=structmd.dim; end
888 if isfield(structmd,'numlayers'), md.mesh.numberoflayers=structmd.numlayers; end
889 if isfield(structmd,'numberofelements'), md.mesh.numberofelements=structmd.numberofelements; end
890 if isfield(structmd,'numberofvertices'), md.mesh.numberofvertices=structmd.numberofvertices; end
891 if isfield(structmd,'numberofnodes'), md.mesh.numberofvertices=structmd.numberofnodes; end
892 if isfield(structmd,'numberofedges'), md.mesh.numberofedges=structmd.numberofedges; end
893 if isfield(structmd,'numberofelements2d'), md.mesh.numberofelements2d=structmd.numberofelements2d; end
894 if isfield(structmd,'numberofnodes2d'), md.mesh.numberofvertices2d=structmd.numberofnodes2d; end
895 if isfield(structmd,'nodeconnectivity'), md.mesh.vertexconnectivity=structmd.nodeconnectivity; end
896 if isfield(structmd,'elementconnectivity'), md.mesh.elementconnectivity=structmd.elementconnectivity; end
897 if isfield(structmd,'uppernodes'), md.mesh.uppervertex=structmd.uppernodes; end
898 if isfield(structmd,'lowernodes'), md.mesh.lowervertex=structmd.lowernodes; end
899 if isfield(structmd,'upperelements'), md.mesh.upperelements=structmd.upperelements; end
900 if isfield(structmd,'lowerelements'), md.mesh.lowerelements=structmd.lowerelements; end
901 if isfield(structmd,'elementonbed'), md.mesh.elementonbed=structmd.elementonbed; end
902 if isfield(structmd,'elementonsurface'), md.mesh.elementonsurface=structmd.elementonsurface; end
903 if isfield(structmd,'nodeonsurface'), md.mesh.vertexonsurface=structmd.nodeonsurface; end
904 if isfield(structmd,'nodeonbed'), md.mesh.vertexonbed=structmd.nodeonbed; end
905 if isfield(structmd,'elements2d'), md.mesh.elements2d=structmd.elements2d; end
906 if isfield(structmd,'y2d'), md.mesh.y2d=structmd.y2d; end
907 if isfield(structmd,'x2d'), md.mesh.x2d=structmd.x2d; end
908 if isfield(structmd,'elements'), md.mesh.elements=structmd.elements; end
909 if isfield(structmd,'edges'),
910 md.mesh.edges=structmd.edges;
911 md.mesh.edges(isnan(md.mesh.edges))=-1;
912 end
913 if isfield(structmd,'y'), md.mesh.y=structmd.y; end
914 if isfield(structmd,'x'), md.mesh.x=structmd.x; end
915 if isfield(structmd,'z'), md.mesh.z=structmd.z; end
916 if isfield(structmd,'mask'), md.flaim.criterion=structmd.mask; end
917 if isfield(structmd,'diagnostic_ref'), md.stressbalance.referential=structmd.diagnostic_ref; end
918 if isfield(structmd,'npart'); md.qmu.numberofpartitions=structmd.npart; end
919 if isfield(structmd,'part'); md.qmu.partition=structmd.part; end
920
921 %Field changes
922 if (isfield(structmd,'type') & ischar(structmd.type)),
923 if strcmpi(structmd.type,'2d'), md.mesh.dimension=2; end
924 if strcmpi(structmd.type,'3d'), md.mesh.dimension=3; end
925 end
926 if isnumeric(md.verbose),
927 md.verbose=verbose;
928 end
929
930 if isfield(structmd,'spcvelocity'),
931 md.stressbalance.spcvx=NaN*ones(md.mesh.numberofvertices,1);
932 md.stressbalance.spcvy=NaN*ones(md.mesh.numberofvertices,1);
933 md.stressbalance.spcvz=NaN*ones(md.mesh.numberofvertices,1);
934 pos=find(structmd.spcvelocity(:,1)); md.stressbalance.spcvx(pos)=structmd.spcvelocity(pos,4);
935 pos=find(structmd.spcvelocity(:,2)); md.stressbalance.spcvy(pos)=structmd.spcvelocity(pos,5);
936 pos=find(structmd.spcvelocity(:,3)); md.stressbalance.spcvz(pos)=structmd.spcvelocity(pos,6);
937 end
938 if isfield(structmd,'spcvx'),
939 md.stressbalance.spcvx=NaN*ones(md.mesh.numberofvertices,1);
940 pos=find(~isnan(structmd.spcvx)); md.stressbalance.spcvx(pos)=structmd.spcvx(pos);
941 end
942 if isfield(structmd,'spcvy'),
943 md.stressbalance.spcvy=NaN*ones(md.mesh.numberofvertices,1);
944 pos=find(~isnan(structmd.spcvy)); md.stressbalance.spcvy(pos)=structmd.spcvy(pos);
945 end
946 if isfield(structmd,'spcvz'),
947 md.stressbalance.spcvz=NaN*ones(md.mesh.numberofvertices,1);
948 pos=find(~isnan(structmd.spcvz)); md.stressbalance.spcvz(pos)=structmd.spcvz(pos);
949 end
950 if isfield(structmd,'pressureload'),
951 if ~isempty(structmd.pressureload) & ismember(structmd.pressureload(end,end),[118 119 120]),
952 pos=find(structmd.pressureload(:,end)==120); md.stressbalance.icefront(pos,end)=0;
953 pos=find(structmd.pressureload(:,end)==118); md.stressbalance.icefront(pos,end)=1;
954 pos=find(structmd.pressureload(:,end)==119); md.stressbalance.icefront(pos,end)=2;
955 end
956 end
957 if isfield(structmd,'elements_type') & structmd.elements_type(end,end)>50,
958 pos=find(structmd.elements_type==59); md.flowequation.element_equation(pos,end)=0;
959 pos=find(structmd.elements_type==55); md.flowequation.element_equation(pos,end)=1;
960 pos=find(structmd.elements_type==56); md.flowequation.element_equation(pos,end)=2;
961 pos=find(structmd.elements_type==60); md.flowequation.element_equation(pos,end)=3;
962 pos=find(structmd.elements_type==62); md.flowequation.element_equation(pos,end)=4;
963 pos=find(structmd.elements_type==57); md.flowequation.element_equation(pos,end)=5;
964 pos=find(structmd.elements_type==58); md.flowequation.element_equation(pos,end)=6;
965 pos=find(structmd.elements_type==61); md.flowequation.element_equation(pos,end)=7;
966 end
967 if isfield(structmd,'vertices_type') & structmd.vertices_type(end,end)>50,
968 pos=find(structmd.vertices_type==59); md.flowequation.vertex_equation(pos,end)=0;
969 pos=find(structmd.vertices_type==55); md.flowequation.vertex_equation(pos,end)=1;
970 pos=find(structmd.vertices_type==56); md.flowequation.vertex_equation(pos,end)=2;
971 pos=find(structmd.vertices_type==60); md.flowequation.vertex_equation(pos,end)=3;
972 pos=find(structmd.vertices_type==62); md.flowequation.vertex_equation(pos,end)=4;
973 pos=find(structmd.vertices_type==57); md.flowequation.vertex_equation(pos,end)=5;
974 pos=find(structmd.vertices_type==58); md.flowequation.vertex_equation(pos,end)=6;
975 pos=find(structmd.vertices_type==61); md.flowequation.vertex_equation(pos,end)=7;
976 end
977 if isfield(structmd,'rheology_law') & isnumeric(structmd.rheology_law),
978 if (structmd.rheology_law==272), md.materials.rheology_law='None'; end
979 if (structmd.rheology_law==368), md.materials.rheology_law='Paterson'; end
980 if (structmd.rheology_law==369), md.materials.rheology_law='Arrhenius'; end
981 end
982 if isfield(structmd,'groundingline_migration') & isnumeric(structmd.groundingline_migration),
983 if (structmd.groundingline_migration==272), md.groundingline.migration='None'; end
984 if (structmd.groundingline_migration==273), md.groundingline.migration='AgressiveMigration'; end
985 if (structmd.groundingline_migration==274), md.groundingline.migration='SoftMigration'; end
986 end
987 if isfield(structmd,'control_type') & isnumeric(structmd.control_type),
988 if (structmd.control_type==143), md.inversion.control_parameters={'FrictionCoefficient'}; end
989 if (structmd.control_type==190), md.inversion.control_parameters={'RheologyBbar'}; end
990 if (structmd.control_type==147), md.inversion.control_parameters={'Thickeningrate'}; end
991 end
992 if isfield(structmd,'cm_responses') & ismember(structmd.cm_responses(end,end),[165:170 383 388 389]),
993 pos=find(structmd.cm_responses==166); md.inversion.cost_functions(pos)=101;
994 pos=find(structmd.cm_responses==167); md.inversion.cost_functions(pos)=102;
995 pos=find(structmd.cm_responses==168); md.inversion.cost_functions(pos)=103;
996 pos=find(structmd.cm_responses==169); md.inversion.cost_functions(pos)=104;
997 pos=find(structmd.cm_responses==170); md.inversion.cost_functions(pos)=105;
998 pos=find(structmd.cm_responses==165); md.inversion.cost_functions(pos)=201;
999 pos=find(structmd.cm_responses==389); md.inversion.cost_functions(pos)=501;
1000 pos=find(structmd.cm_responses==388); md.inversion.cost_functions(pos)=502;
1001 pos=find(structmd.cm_responses==382); md.inversion.cost_functions(pos)=503;
1002 end
1003
1004 if isfield(structmd,'artificial_diffusivity') & structmd.artificial_diffusivity==2,
1005 md.thermal.stabilization=2;
1006 md.masstransport.stabilization=1;
1007 md.balancethickness.stabilization=1;
1008 end
1009 if isnumeric(md.masstransport.hydrostatic_adjustment)
1010 if md.masstransport.hydrostatic_adjustment==269,
1011 md.masstransport.hydrostatic_adjustment='Incremental';
1012 else
1013 md.masstransport.hydrostatic_adjustment='Absolute';
1014 end
1015 end
1016
1017 %New fields
1018 if ~isfield(structmd,'upperelements');
1019 md.mesh.upperelements=transpose(1:md.mesh.numberofelements)+md.mesh.numberofelements2d;
1020 md.mesh.upperelements(end-md.mesh.numberofelements2d+1:end)=NaN;
1021 end
1022 if ~isfield(structmd,'lowerelements');
1023 md.mesh.lowerelements=transpose(1:md.mesh.numberofelements)-md.mesh.numberofelements2d;
1024 md.mesh.lowerelements(1:md.mesh.numberofelements2d)=NaN;
1025 end
1026 if ~isfield(structmd,'diagnostic_ref');
1027 md.stressbalance.referential=NaN*ones(md.mesh.numberofvertices,6);
1028 end
1029 if ~isfield(structmd,'loadingforce');
1030 md.stressbalance.loadingforce=0*ones(md.mesh.numberofvertices,3);
1031 end
1032
1033 %2013 August 9
1034 if isfield(structmd,'prognostic') & isa(structmd.prognostic,'prognostic'),
1035 disp('Recovering old prognostic class');
1036 md.masstransport=masstransport(structmd.prognostic);
1037 end
1038 %2013 August 9
1039 if isfield(structmd,'diagnostic') & (isa(structmd.diagnostic,'diagnostic') || isa(structmd.diagnostic,'stressbalance')),
1040 disp('Recovering old diagnostic class');
1041 md.stressbalance=stressbalance(structmd.diagnostic);
1042 end
1043 end% }}}
1044 function md = setdefaultparameters(md) % {{{
1045
1046 %initialize subclasses
1047 md.mesh = mesh();
1048 md.mask = mask();
1049 md.constants = constants();
1050 md.geometry = geometry();
1051 md.initialization = initialization();
1052 md.surfaceforcings = surfaceforcings();
1053 md.basalforcings = basalforcings();
1054 md.friction = friction();
1055 md.rifts = rifts();
1056 md.timestepping = timestepping();
1057 md.groundingline = groundingline();
1058 md.materials = matice();
1059 md.damage = damage();
1060 md.flowequation = flowequation();
1061 md.debug = debug();
1062 md.verbose = verbose();
1063 md.settings = settings();
1064 md.toolkits = toolkits();
1065 md.cluster = generic();
1066 md.balancethickness = balancethickness();
1067 md.stressbalance = stressbalance();
1068 md.hydrology = hydrologyshreve();
1069 md.masstransport = masstransport();
1070 md.thermal = thermal();
1071 md.steadystate = steadystate();
1072 md.transient = transient();
1073 md.gia = gia();
1074 md.autodiff = autodiff();
1075 md.flaim = flaim();
1076 md.inversion = inversion();
1077 md.qmu = qmu();
1078 md.radaroverlay = radaroverlay();
1079 md.results = struct();
1080 md.miscellaneous = miscellaneous();
1081 md.private = private();
1082 end
1083 %}}}
1084 function disp(obj) % {{{
1085 disp(sprintf('%19s: %-22s -- %s','mesh' ,['[1x1 ' class(obj.mesh) ']'],'mesh properties'));
1086 disp(sprintf('%19s: %-22s -- %s','mask' ,['[1x1 ' class(obj.mask) ']'],'defines grounded and floating elements'));
1087 disp(sprintf('%19s: %-22s -- %s','geometry' ,['[1x1 ' class(obj.geometry) ']'],'surface elevation, bedrock topography, ice thickness,...'));
1088 disp(sprintf('%19s: %-22s -- %s','constants' ,['[1x1 ' class(obj.constants) ']'],'physical constants'));
1089 disp(sprintf('%19s: %-22s -- %s','surfaceforcings' ,['[1x1 ' class(obj.surfaceforcings) ']'],'surface forcings'));
1090 disp(sprintf('%19s: %-22s -- %s','basalforcings' ,['[1x1 ' class(obj.basalforcings) ']'],'bed forcings'));
1091 disp(sprintf('%19s: %-22s -- %s','materials' ,['[1x1 ' class(obj.materials) ']'],'material properties'));
1092 disp(sprintf('%19s: %-22s -- %s','damage' ,['[1x1 ' class(obj.damage) ']'],'damage propagation laws'));
1093 disp(sprintf('%19s: %-22s -- %s','friction' ,['[1x1 ' class(obj.friction) ']'],'basal friction/drag properties'));
1094 disp(sprintf('%19s: %-22s -- %s','flowequation' ,['[1x1 ' class(obj.flowequation) ']'],'flow equations'));
1095 disp(sprintf('%19s: %-22s -- %s','timestepping' ,['[1x1 ' class(obj.timestepping) ']'],'time stepping for transient models'));
1096 disp(sprintf('%19s: %-22s -- %s','initialization' ,['[1x1 ' class(obj.initialization) ']'],'initial guess/state'));
1097 disp(sprintf('%19s: %-22s -- %s','rifts' ,['[1x1 ' class(obj.rifts) ']'],'rifts properties'));
1098 disp(sprintf('%19s: %-22s -- %s','debug' ,['[1x1 ' class(obj.debug) ']'],'debugging tools (valgrind, gprof)'));
1099 disp(sprintf('%19s: %-22s -- %s','verbose' ,['[1x1 ' class(obj.verbose) ']'],'verbosity level in solve'));
1100 disp(sprintf('%19s: %-22s -- %s','settings' ,['[1x1 ' class(obj.settings) ']'],'settings properties'));
1101 disp(sprintf('%19s: %-22s -- %s','toolkits' ,['[1x1 ' class(obj.toolkits) ']'],'PETSc options for each solution'));
1102 disp(sprintf('%19s: %-22s -- %s','cluster' ,['[1x1 ' class(obj.cluster) ']'],'cluster parameters (number of cpus...)'));
1103 disp(sprintf('%19s: %-22s -- %s','balancethickness',['[1x1 ' class(obj.balancethickness) ']'],'parameters for balancethickness solution'));
1104 disp(sprintf('%19s: %-22s -- %s','stressbalance' ,['[1x1 ' class(obj.stressbalance) ']'],'parameters for stressbalance solution'));
1105 disp(sprintf('%19s: %-22s -- %s','groundingline' ,['[1x1 ' class(obj.groundingline) ']'],'parameters for groundingline solution'));
1106 disp(sprintf('%19s: %-22s -- %s','hydrology' ,['[1x1 ' class(obj.hydrology) ']'],'parameters for hydrology solution'));
1107 disp(sprintf('%19s: %-22s -- %s','masstransport' ,['[1x1 ' class(obj.masstransport) ']'],'parameters for masstransport solution'));
1108 disp(sprintf('%19s: %-22s -- %s','thermal' ,['[1x1 ' class(obj.thermal) ']'],'parameters for thermal solution'));
1109 disp(sprintf('%19s: %-22s -- %s','steadystate' ,['[1x1 ' class(obj.steadystate) ']'],'parameters for steadystate solution'));
1110 disp(sprintf('%19s: %-22s -- %s','transient' ,['[1x1 ' class(obj.transient) ']'],'parameters for transient solution'));
1111 disp(sprintf('%19s: %-22s -- %s','gia' ,['[1x1 ' class(obj.gia) ']'],'parameters for gia solution'));
1112 disp(sprintf('%19s: %-22s -- %s','autodiff' ,['[1x1 ' class(obj.autodiff) ']'],'automatic differentiation parameters'));
1113 disp(sprintf('%19s: %-22s -- %s','flaim' ,['[1x1 ' class(obj.flaim) ']'],'flaim parameters'));
1114 disp(sprintf('%19s: %-22s -- %s','inversion' ,['[1x1 ' class(obj.inversion) ']'],'parameters for inverse methods'));
1115 disp(sprintf('%19s: %-22s -- %s','qmu' ,['[1x1 ' class(obj.qmu) ']'],'dakota properties'));
1116 disp(sprintf('%19s: %-22s -- %s','results' ,['[1x1 ' class(obj.results) ']'],'model results'));
1117 disp(sprintf('%19s: %-22s -- %s','radaroverlay' ,['[1x1 ' class(obj.radaroverlay) ']'],'radar image for plot overlay'));
1118 disp(sprintf('%19s: %-22s -- %s','miscellaneous' ,['[1x1 ' class(obj.miscellaneous) ']'],'miscellaneous fields'));
1119 end % }}}
1120 function memory(obj) % {{{
1121
1122 disp(sprintf('\nMemory imprint:\n'));
1123
1124 fields=properties('model');
1125 mem=0;
1126
1127 for i=1:length(fields),
1128 field=obj.(fields{i});
1129 s=whos('field');
1130 mem=mem+s.bytes/1e6;
1131 disp(sprintf('%19s: %6.2f Mb',fields{i},s.bytes/1e6));
1132 end
1133 disp(sprintf('%19s--%10s','--------------','--------------'));
1134 disp(sprintf('%19s: %g Mb','Total',mem));
1135 end % }}}
1136 function netcdf(obj,filename) % {{{
1137 %NETCDF - save model as netcdf
1138 %
1139 % Usage:
1140 % netcdf(md,filename)
1141 %
1142 % Example:
1143 % netcdf(md,'model.nc');
1144
1145 disp('Saving model as NetCDF');
1146 %1. Create NetCDF file
1147 ncid=netcdf.create(filename,'CLOBBER');
1148 netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'),'Conventions','CF-1.4');
1149 netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'),'Title',['ISSM model (' obj.miscellaneous.name ')']);
1150 netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'),'Author',getenv('USER'));
1151 netcdf.putAtt(ncid,netcdf.getConstant('NC_GLOBAL'),'Date',datestr(now));
1152
1153 %Preallocate variable id, needed to write variables in netcdf file
1154 var_id=zeros(1000,1);%preallocate
1155
1156 for step=1:2,
1157 counter=0;
1158 [var_id,counter]=structtonc(ncid,'md',obj,0,var_id,counter,step);
1159 if step==1, netcdf.endDef(ncid); end
1160 end
1161
1162 if counter>1000,
1163 warning(['preallocation of var_id need to be updated from ' num2str(1000) ' to ' num2str(counter)]);
1164 end
1165
1166 netcdf.close(ncid)
1167 end % }}}
1168 function xylim(obj) % {{{
1169
1170 xlim([min(obj.mesh.x) max(obj.mesh.x)]);
1171 ylim([min(obj.mesh.y) max(obj.mesh.y)])
1172 end % }}}
1173 function md=upload(md) % {{{
1174 %the goal of this routine is to upload the model onto a server, and to empty it.
1175 %So first, save the model with a unique name and upload the file to the server:
1176 random_part=fix(rand(1)*10000);
1177 id=[md.miscellaneous.name '-' regexprep(datestr(now),'[^\w'']','') '-' num2str(random_part) '-' getenv('USER') '-' oshostname() '.upload'];
1178 eval(['save ' id ' md']);
1179
1180 %Now, upload the file:
1181 issmscpout(md.settings.upload_server,md.settings.upload_path,md.settings.upload_login,md.settings.upload_port,{id},1);
1182
1183 %Now, empty this model of everything except settings, and record name of file we just uploaded!
1184 settings_back=md.settings;
1185 md=model();
1186 md.settings=settings_back;
1187 md.settings.upload_filename=id;
1188
1189 %get locally rid of file that was uploaded
1190 eval(['delete ' id]);
1191
1192 end % }}}
1193 function md=download(md) % {{{
1194
1195 %the goal of this routine is to download the internals of the current model from a server, because
1196 %this model is empty, except for the settings which tell us where to go and find this model!
1197
1198 %Download the file:
1199 issmscpin(md.settings.upload_server, md.settings.upload_login, md.settings.upload_port, md.settings.upload_path, {md.settings.upload_filename});
1200
1201 name=md.settings.upload_filename;
1202
1203 %Now, load this model:
1204 md=loadmodel(md.settings.upload_filename);
1205
1206 %get locally rid of file that was downloaded
1207 eval(['delete ' name]);
1208
1209 end % }}}
1210 end
1211 end
Note: See TracBrowser for help on using the repository browser.