source: issm/trunk-jpl/src/c/analyses/EnthalpyAnalysis.cpp@ 19161

Last change on this file since 19161 was 19161, checked in by bdef, 10 years ago

BUG: fix for the friction coupling

File size: 60.1 KB
Line 
1#include "./EnthalpyAnalysis.h"
2#include "../toolkits/toolkits.h"
3#include "../classes/classes.h"
4#include "../shared/shared.h"
5#include "../modules/modules.h"
6#include "../solutionsequences/solutionsequences.h"
7
8/*Model processing*/
9void EnthalpyAnalysis::CreateConstraints(Constraints* constraints,IoModel* iomodel){/*{{{*/
10
11 /*Intermediary*/
12 int count;
13 int M,N;
14 bool spcpresent = false;
15 IssmDouble heatcapacity;
16 IssmDouble referencetemperature;
17
18 /*Output*/
19 IssmDouble *spcvector = NULL;
20 IssmDouble* times=NULL;
21 IssmDouble* values=NULL;
22
23 /*Fetch parameters: */
24 iomodel->Constant(&heatcapacity,MaterialsHeatcapacityEnum);
25 iomodel->Constant(&referencetemperature,ConstantsReferencetemperatureEnum);
26
27 /*return if 2d mesh*/
28 if(iomodel->domaintype==Domain2DhorizontalEnum) return;
29
30 /*Fetch data: */
31 iomodel->FetchData(&spcvector,&M,&N,ThermalSpctemperatureEnum);
32
33 //FIX ME: SHOULD USE IOMODELCREATECONSTRAINTS
34 /*Transient or static?:*/
35 if(M==iomodel->numberofvertices){
36 /*static: just create Constraints objects*/
37 count=0;
38
39 for(int i=0;i<iomodel->numberofvertices;i++){
40 /*keep only this partition's nodes:*/
41 if((iomodel->my_vertices[i])){
42
43 if (!xIsNan<IssmDouble>(spcvector[i])){
44
45 constraints->AddObject(new SpcStatic(iomodel->constraintcounter+count+1,iomodel->nodecounter+i+1,0,heatcapacity*(spcvector[i]-referencetemperature),EnthalpyAnalysisEnum));
46 count++;
47
48 }
49 }
50 }
51 }
52 else if (M==(iomodel->numberofvertices+1)){
53 /*transient: create transient SpcTransient objects. Same logic, except we need to retrieve
54 * various times and values to initialize an SpcTransient object: */
55 count=0;
56
57 /*figure out times: */
58 times=xNew<IssmDouble>(N);
59 for(int j=0;j<N;j++){
60 times[j]=spcvector[(M-1)*N+j];
61 }
62
63 /*Create constraints from x,y,z: */
64 for(int i=0;i<iomodel->numberofvertices;i++){
65
66 /*keep only this partition's nodes:*/
67 if((iomodel->my_vertices[i])){
68
69 /*figure out times and values: */
70 values=xNew<IssmDouble>(N);
71 spcpresent=false;
72 for(int j=0;j<N;j++){
73 values[j]=heatcapacity*(spcvector[i*N+j]-referencetemperature);
74 if(!xIsNan<IssmDouble>(values[j]))spcpresent=true; //NaN means no spc by default
75 }
76
77 if(spcpresent){
78 constraints->AddObject(new SpcTransient(iomodel->constraintcounter+count+1,iomodel->nodecounter+i+1,0,N,times,values,EnthalpyAnalysisEnum));
79 count++;
80 }
81 xDelete<IssmDouble>(values);
82 }
83 }
84 }
85 else{
86 _error_("Size of field " << EnumToStringx(ThermalSpctemperatureEnum) << " not supported");
87 }
88
89 /*Free ressources:*/
90 iomodel->DeleteData(spcvector,ThermalSpctemperatureEnum);
91 xDelete<IssmDouble>(times);
92 xDelete<IssmDouble>(values);
93}/*}}}*/
94void EnthalpyAnalysis::CreateLoads(Loads* loads, IoModel* iomodel){/*{{{*/
95
96 /*No loads */
97}/*}}}*/
98void EnthalpyAnalysis::CreateNodes(Nodes* nodes,IoModel* iomodel){/*{{{*/
99
100 if(iomodel->domaintype==Domain3DEnum) iomodel->FetchData(2,MeshVertexonbaseEnum,MeshVertexonsurfaceEnum);
101 ::CreateNodes(nodes,iomodel,EnthalpyAnalysisEnum,P1Enum);
102 iomodel->DeleteData(2,MeshVertexonbaseEnum,MeshVertexonsurfaceEnum);
103}/*}}}*/
104int EnthalpyAnalysis::DofsPerNode(int** doflist,int domaintype,int approximation){/*{{{*/
105 return 1;
106}/*}}}*/
107void EnthalpyAnalysis::UpdateElements(Elements* elements,IoModel* iomodel,int analysis_counter,int analysis_type){/*{{{*/
108
109 bool dakota_analysis,islevelset,isenthalpy;
110 int frictionlaw;
111 int FrictionCoupling;
112
113 /*Now, is the model 3d? otherwise, do nothing: */
114 if(iomodel->domaintype==Domain2DhorizontalEnum)return;
115
116 /*Is enthalpy requested?*/
117 iomodel->Constant(&isenthalpy,ThermalIsenthalpyEnum);
118 if(!isenthalpy) return;
119
120 /*Fetch data needed: */
121 iomodel->FetchData(3,TemperatureEnum,WaterfractionEnum,PressureEnum);
122
123 /*Update elements: */
124 int counter=0;
125 for(int i=0;i<iomodel->numberofelements;i++){
126 if(iomodel->my_elements[i]){
127 Element* element=(Element*)elements->GetObjectByOffset(counter);
128 element->Update(i,iomodel,analysis_counter,analysis_type,P1Enum);
129 counter++;
130 }
131 }
132
133 iomodel->Constant(&dakota_analysis,QmuIsdakotaEnum);
134 iomodel->Constant(&islevelset,TransientIslevelsetEnum);
135 iomodel->Constant(&frictionlaw,FrictionLawEnum);
136
137 iomodel->FetchDataToInput(elements,ThicknessEnum);
138 iomodel->FetchDataToInput(elements,SurfaceEnum);
139 iomodel->FetchDataToInput(elements,BaseEnum);
140 iomodel->FetchDataToInput(elements,MaskIceLevelsetEnum);
141 iomodel->FetchDataToInput(elements,MaskGroundediceLevelsetEnum);
142 if(iomodel->domaintype!=Domain2DhorizontalEnum){
143 iomodel->FetchDataToInput(elements,MeshVertexonbaseEnum);
144 iomodel->FetchDataToInput(elements,MeshVertexonsurfaceEnum);
145 }
146 iomodel->FetchDataToInput(elements,MaterialsRheologyBEnum);
147 iomodel->FetchDataToInput(elements,MaterialsRheologyNEnum);
148 iomodel->FetchDataToInput(elements,PressureEnum);
149 iomodel->FetchDataToInput(elements,TemperatureEnum);
150 iomodel->FetchDataToInput(elements,WaterfractionEnum);
151 iomodel->FetchDataToInput(elements,EnthalpyEnum);
152 iomodel->FetchDataToInput(elements,BasalforcingsGeothermalfluxEnum);
153 iomodel->FetchDataToInput(elements,WatercolumnEnum);
154 iomodel->FetchDataToInput(elements,BasalforcingsGroundediceMeltingRateEnum);
155 iomodel->FetchDataToInput(elements,VxEnum);
156 iomodel->FetchDataToInput(elements,VyEnum);
157 iomodel->FetchDataToInput(elements,VzEnum);
158 InputUpdateFromConstantx(elements,0.,VxMeshEnum);
159 InputUpdateFromConstantx(elements,0.,VyMeshEnum);
160 InputUpdateFromConstantx(elements,0.,VzMeshEnum);
161 if(islevelset){
162 iomodel->FetchDataToInput(elements,IceMaskNodeActivationEnum);
163 iomodel->FetchDataToInput(elements,MeshVertexonbaseEnum); // required for updating active nodes
164 }
165
166 /*Friction law variables*/
167 switch(frictionlaw){
168 case 1:
169 iomodel->FetchDataToInput(elements,FrictionCoefficientEnum);
170 iomodel->FetchDataToInput(elements,FrictionPEnum);
171 iomodel->FetchDataToInput(elements,FrictionQEnum);
172 break;
173 case 2:
174 iomodel->FetchDataToInput(elements,FrictionCEnum);
175 iomodel->FetchDataToInput(elements,FrictionMEnum);
176 break;
177 case 3:
178 iomodel->Constant(&FrictionCoupling,FrictionCouplingEnum);
179 iomodel->FetchDataToInput(elements,FrictionCEnum);
180 iomodel->FetchDataToInput(elements,FrictionAsEnum);
181 iomodel->FetchDataToInput(elements,FrictionQEnum);
182 if (FrictionCoupling==0){
183 iomodel->FetchDataToInput(elements,FrictionEffectivePressureEnum);
184 }
185 break;
186 case 4:
187 iomodel->FetchDataToInput(elements,FrictionCoefficientEnum);
188 iomodel->FetchDataToInput(elements,FrictionPEnum);
189 iomodel->FetchDataToInput(elements,FrictionQEnum);
190 iomodel->FetchDataToInput(elements,PressureEnum);
191 iomodel->FetchDataToInput(elements,TemperatureEnum);
192 break;
193 case 5:
194 iomodel->FetchDataToInput(elements,FrictionCoefficientEnum);
195 iomodel->FetchDataToInput(elements,FrictionPEnum);
196 iomodel->FetchDataToInput(elements,FrictionQEnum);
197 iomodel->FetchDataToInput(elements,FrictionWaterLayerEnum);
198 break;
199 case 6:
200 iomodel->FetchDataToInput(elements,FrictionCEnum);
201 iomodel->FetchDataToInput(elements,FrictionMEnum);
202 iomodel->FetchDataToInput(elements,PressureEnum);
203 iomodel->FetchDataToInput(elements,TemperatureEnum);
204 break;
205 default:
206 _error_("not supported");
207 }
208 /*Free data: */
209 iomodel->DeleteData(3,TemperatureEnum,WaterfractionEnum,PressureEnum);
210}/*}}}*/
211void EnthalpyAnalysis::UpdateParameters(Parameters* parameters,IoModel* iomodel,int solution_enum,int analysis_enum){/*{{{*/
212
213 int numoutputs;
214 char** requestedoutputs = NULL;
215
216 parameters->AddObject(iomodel->CopyConstantObject(ThermalStabilizationEnum));
217 parameters->AddObject(iomodel->CopyConstantObject(ThermalMaxiterEnum));
218 parameters->AddObject(iomodel->CopyConstantObject(ThermalReltolEnum));
219 parameters->AddObject(iomodel->CopyConstantObject(ThermalIsenthalpyEnum));
220 parameters->AddObject(iomodel->CopyConstantObject(ThermalIsdynamicbasalspcEnum));
221 parameters->AddObject(iomodel->CopyConstantObject(FrictionLawEnum));
222
223 iomodel->FetchData(&requestedoutputs,&numoutputs,ThermalRequestedOutputsEnum);
224 parameters->AddObject(new IntParam(ThermalNumRequestedOutputsEnum,numoutputs));
225 if(numoutputs)parameters->AddObject(new StringArrayParam(ThermalRequestedOutputsEnum,requestedoutputs,numoutputs));
226 iomodel->DeleteData(&requestedoutputs,numoutputs,ThermalRequestedOutputsEnum);
227
228 /*Deal with friction parameters*/
229 int frictionlaw;
230 iomodel->Constant(&frictionlaw,FrictionLawEnum);
231 if(frictionlaw==4 || frictionlaw==6) parameters->AddObject(iomodel->CopyConstantObject(FrictionGammaEnum));
232}/*}}}*/
233
234/*Finite Element Analysis*/
235void EnthalpyAnalysis::ApplyBasalConstraints(IssmDouble* serial_spc,Element* element){/*{{{*/
236
237 /* Check if ice in element */
238 if(!element->IsIceInElement()) return;
239
240 /* Only update Constraints at the base of grounded ice*/
241 if(!(element->IsOnBase()) || element->IsFloating()) return;
242
243 /*Intermediary*/
244 bool isdynamicbasalspc;
245 int numindices;
246 int *indices = NULL;
247 Node* node = NULL;
248 IssmDouble pressure;
249
250 /*Check wether dynamic basal boundary conditions are activated */
251 element->FindParam(&isdynamicbasalspc,ThermalIsdynamicbasalspcEnum);
252 if(!isdynamicbasalspc) return;
253
254 /*Get parameters and inputs: */
255 Input* pressure_input = element->GetInput(PressureEnum); _assert_(pressure_input);
256
257 /*Fetch indices of basal & surface nodes for this finite element*/
258 Penta *penta = (Penta *) element; // TODO: add Basal-/SurfaceNodeIndices to element.h, and change this to Element*
259 penta->BasalNodeIndices(&numindices,&indices,element->GetElementType());
260
261 GaussPenta* gauss=new GaussPenta();
262 for(int i=0;i<numindices;i++){
263 gauss->GaussNode(element->GetElementType(),indices[i]);
264
265 pressure_input->GetInputValue(&pressure,gauss);
266
267 /*apply or release spc*/
268 node=element->GetNode(indices[i]);
269 if(serial_spc[node->Sid()]==1.){
270 pressure_input->GetInputValue(&pressure, gauss);
271 node->ApplyConstraint(0,PureIceEnthalpy(element,pressure));
272 }
273 else
274 node->DofInFSet(0);
275 }
276
277 /*Free ressources:*/
278 xDelete<int>(indices);
279 delete gauss;
280}/*}}}*/
281void EnthalpyAnalysis::ComputeBasalMeltingrate(FemModel* femmodel){/*{{{*/
282 /*Compute basal melting rates: */
283 for(int i=0;i<femmodel->elements->Size();i++){
284 Element* element=xDynamicCast<Element*>(femmodel->elements->GetObjectByOffset(i));
285 ComputeBasalMeltingrate(element);
286 }
287}/*}}}*/
288void EnthalpyAnalysis::ComputeBasalMeltingrate(Element* element){/*{{{*/
289 /*Calculate the basal melt rates of the enthalpy model after Aschwanden 2012*/
290 /* melting rate is positive when melting, negative when refreezing*/
291
292 /* Check if ice in element */
293 if(!element->IsIceInElement()) return;
294
295 /* Only compute melt rates at the base of grounded ice*/
296 if(!element->IsOnBase() || element->IsFloating()) return;
297
298 /* Intermediaries */
299 bool converged;
300 const int dim=3;
301 int i,is,state;
302 int vertexdown,vertexup,numvertices,numsegments;
303 int enthalpy_enum;
304 IssmDouble vec_heatflux[dim],normal_base[dim],d1enthalpy[dim],d1pressure[dim];
305 IssmDouble basalfriction,alpha2,geothermalflux,heatflux;
306 IssmDouble dt,yts;
307 IssmDouble melting_overshoot,lambda;
308 IssmDouble vx,vy,vz;
309 IssmDouble *xyz_list = NULL;
310 IssmDouble *xyz_list_base = NULL;
311 int *pairindices = NULL;
312
313 /*Fetch parameters*/
314 element->GetVerticesCoordinates(&xyz_list);
315 element->GetVerticesCoordinatesBase(&xyz_list_base);
316 element->GetInputValue(&converged,ConvergedEnum);
317 element->FindParam(&dt,TimesteppingTimeStepEnum);
318 element->FindParam(&yts, ConstantsYtsEnum);
319
320 if(dt==0. && !converged) enthalpy_enum=EnthalpyPicardEnum;
321 else enthalpy_enum=EnthalpyEnum;
322
323 IssmDouble latentheat = element->GetMaterialParameter(MaterialsLatentheatEnum);
324 IssmDouble rho_ice = element->GetMaterialParameter(MaterialsRhoIceEnum);
325 IssmDouble rho_water = element->GetMaterialParameter(MaterialsRhoFreshwaterEnum);
326 IssmDouble beta = element->GetMaterialParameter(MaterialsBetaEnum);
327 IssmDouble kappa = EnthalpyDiffusionParameterVolume(element,enthalpy_enum); _assert_(kappa>=0.);
328 IssmDouble kappa_mix;
329
330 /*retrieve inputs*/
331 Input* enthalpy_input = element->GetInput(enthalpy_enum); _assert_(enthalpy_input);
332 Input* pressure_input = element->GetInput(PressureEnum); _assert_(pressure_input);
333 Input* geothermalflux_input = element->GetInput(BasalforcingsGeothermalfluxEnum); _assert_(geothermalflux_input);
334 Input* vx_input = element->GetInput(VxEnum); _assert_(vx_input);
335 Input* vy_input = element->GetInput(VyEnum); _assert_(vy_input);
336 Input* vz_input = element->GetInput(VzEnum); _assert_(vz_input);
337
338 /*Build friction element, needed later: */
339 Friction* friction=new Friction(element,dim);
340
341 /******** MELTING RATES ************************************//*{{{*/
342 element->NormalBase(&normal_base[0],xyz_list_base);
343 element->VerticalSegmentIndices(&pairindices,&numsegments);
344 IssmDouble* meltingrate_enthalpy = xNew<IssmDouble>(numsegments);
345 IssmDouble* heating = xNew<IssmDouble>(numsegments);
346
347 numvertices=element->GetNumberOfVertices();
348 IssmDouble* enthalpies = xNew<IssmDouble>(numvertices);
349 IssmDouble* pressures = xNew<IssmDouble>(numvertices);
350 IssmDouble* watercolumns = xNew<IssmDouble>(numvertices);
351 IssmDouble* basalmeltingrates = xNew<IssmDouble>(numvertices);
352 element->GetInputListOnVertices(enthalpies,enthalpy_enum);
353 element->GetInputListOnVertices(pressures,PressureEnum);
354 element->GetInputListOnVertices(watercolumns,WatercolumnEnum);
355 element->GetInputListOnVertices(basalmeltingrates,BasalforcingsGroundediceMeltingRateEnum);
356
357 Gauss* gauss=element->NewGauss();
358 for(is=0;is<numsegments;is++){
359 vertexdown = pairindices[is*2+0];
360 vertexup = pairindices[is*2+1];
361 gauss->GaussVertex(vertexdown);
362
363 state=GetThermalBasalCondition(element, enthalpies[vertexdown], enthalpies[vertexup], pressures[vertexdown], pressures[vertexup], watercolumns[vertexdown], basalmeltingrates[vertexdown]);
364 switch (state) {
365 case 0:
366 // cold, dry base: apply basal surface forcing
367 for(i=0;i<3;i++) vec_heatflux[i]=0.;
368 break;
369 case 1: case 2: case 3:
370 // case 1 : cold, wet base: keep at pressure melting point
371 // case 2: temperate, thin refreezing base: release spc
372 // case 3: temperate, thin melting base: set spc
373 enthalpy_input->GetInputDerivativeValue(&d1enthalpy[0],xyz_list,gauss);
374 for(i=0;i<3;i++) vec_heatflux[i]=-kappa*d1enthalpy[i];
375 break;
376 case 4:
377 // temperate, thick melting base: set grad H*n=0
378 kappa_mix=GetWetIceConductivity(element, enthalpies[vertexdown], pressures[vertexdown]);
379 pressure_input->GetInputDerivativeValue(&d1pressure[0],xyz_list,gauss);
380 for(i=0;i<3;i++) vec_heatflux[i]=kappa_mix*beta*d1pressure[i];
381 break;
382 default:
383 _printf0_(" unknown thermal basal state found!");
384 }
385 if(state==0) meltingrate_enthalpy[is]=0.;
386 else{
387 /*heat flux along normal*/
388 heatflux=0.;
389 for(i=0;i<3;i++) heatflux+=(vec_heatflux[i])*normal_base[i];
390
391 /*basal friction*/
392 friction->GetAlpha2(&alpha2,gauss);
393 vx_input->GetInputValue(&vx,gauss); vy_input->GetInputValue(&vy,gauss); vz_input->GetInputValue(&vz,gauss);
394 basalfriction=alpha2*(vx*vx + vy*vy + vz*vz);
395 geothermalflux_input->GetInputValue(&geothermalflux,gauss);
396 /* -Mb= Fb-(q-q_geo)/((1-w)*L*rho), and (1-w)*rho=rho_ice, cf Aschwanden 2012, eqs.1, 2, 66*/
397 heating[is]=(heatflux+basalfriction+geothermalflux);
398 meltingrate_enthalpy[is]=heating[is]/(latentheat*rho_ice); // m/s water equivalent
399 }
400 }/*}}}*/
401
402 /******** UPDATE MELTINGRATES AND WATERCOLUMN **************//*{{{*/
403 for(is=0;is<numsegments;is++){
404 vertexdown = pairindices[is*2+0];
405 vertexup = pairindices[is*2+1];
406 if(dt!=0.){
407 if(watercolumns[vertexdown]+meltingrate_enthalpy[is]*dt<0.){ // prevent too much freeze on
408 lambda = -watercolumns[vertexdown]/(dt*meltingrate_enthalpy[is]); _assert_(lambda>=0.); _assert_(lambda<1.);
409 watercolumns[vertexdown]=0.;
410 basalmeltingrates[vertexdown]=lambda*meltingrate_enthalpy[is]; // restrict freeze on only to size of watercolumn
411 enthalpies[vertexdown]+=(1.-lambda)*dt/yts*meltingrate_enthalpy[is]*latentheat*rho_ice; // use rest of energy to cool down base: dE=L*m, m=(1-lambda)*meltingrate*rho_ice
412 }
413 else{
414 basalmeltingrates[vertexdown]=meltingrate_enthalpy[is];
415 watercolumns[vertexdown]+=dt*meltingrate_enthalpy[is];
416 }
417 }
418 else{
419 basalmeltingrates[vertexdown]=meltingrate_enthalpy[is];
420 if(watercolumns[vertexdown]+meltingrate_enthalpy[is]<0.)
421 watercolumns[vertexdown]=0.;
422 else
423 watercolumns[vertexdown]+=meltingrate_enthalpy[is];
424 }
425 basalmeltingrates[vertexdown]*=rho_water/rho_ice; // convert meltingrate from water to ice equivalent
426 _assert_(watercolumns[vertexdown]>=0.);
427 }/*}}}*/
428
429 /*feed updated variables back into model*/
430 if(dt!=0.){
431 element->AddInput(enthalpy_enum,enthalpies,P1Enum); //TODO: distinguis for steadystate and transient run
432 element->AddInput(WatercolumnEnum,watercolumns,P1Enum);
433 }
434 element->AddInput(BasalforcingsGroundediceMeltingRateEnum,basalmeltingrates,P1Enum);
435
436 /*Clean up and return*/
437 delete gauss;
438 delete friction;
439 xDelete<int>(pairindices);
440 xDelete<IssmDouble>(enthalpies);
441 xDelete<IssmDouble>(pressures);
442 xDelete<IssmDouble>(watercolumns);
443 xDelete<IssmDouble>(basalmeltingrates);
444 xDelete<IssmDouble>(meltingrate_enthalpy);
445 xDelete<IssmDouble>(heating);
446 xDelete<IssmDouble>(xyz_list);
447 xDelete<IssmDouble>(xyz_list_base);
448}/*}}}*/
449void EnthalpyAnalysis::Core(FemModel* femmodel){/*{{{*/
450 if(VerboseSolution()) _printf0_(" computing enthalpy\n");
451 femmodel->SetCurrentConfiguration(EnthalpyAnalysisEnum);
452 solutionsequence_thermal_nonlinear(femmodel);
453
454 /*transfer enthalpy to enthalpy picard for the next step: */
455 InputDuplicatex(femmodel,EnthalpyEnum,EnthalpyPicardEnum);
456
457 IssmDouble dt;
458 femmodel->parameters->FindParam(&dt,TimesteppingTimeStepEnum);
459 if(dt==0.) ComputeBasalMeltingrate(femmodel);
460 else PostProcessing(femmodel);
461
462}/*}}}*/
463ElementVector* EnthalpyAnalysis::CreateDVector(Element* element){/*{{{*/
464 /*Default, return NULL*/
465 return NULL;
466}/*}}}*/
467ElementMatrix* EnthalpyAnalysis::CreateJacobianMatrix(Element* element){/*{{{*/
468_error_("Not implemented");
469}/*}}}*/
470ElementMatrix* EnthalpyAnalysis::CreateKMatrix(Element* element){/*{{{*/
471
472 /* Check if ice in element */
473 if(!element->IsIceInElement()) return NULL;
474
475 /*compute all stiffness matrices for this element*/
476 ElementMatrix* Ke1=CreateKMatrixVolume(element);
477 ElementMatrix* Ke2=CreateKMatrixShelf(element);
478 ElementMatrix* Ke =new ElementMatrix(Ke1,Ke2);
479
480 /*clean-up and return*/
481 delete Ke1;
482 delete Ke2;
483 return Ke;
484}/*}}}*/
485ElementMatrix* EnthalpyAnalysis::CreateKMatrixVolume(Element* element){/*{{{*/
486
487 /* Check if ice in element */
488 if(!element->IsIceInElement()) return NULL;
489
490 /*Intermediaries */
491 int stabilization;
492 IssmDouble Jdet,dt,u,v,w,um,vm,wm,vel;
493 IssmDouble h,hx,hy,hz,vx,vy,vz;
494 IssmDouble tau_parameter,diameter;
495 IssmDouble D_scalar;
496 IssmDouble* xyz_list = NULL;
497
498 /*Fetch number of nodes and dof for this finite element*/
499 int numnodes = element->GetNumberOfNodes();
500
501 /*Initialize Element vector and other vectors*/
502 ElementMatrix* Ke = element->NewElementMatrix();
503 IssmDouble* basis = xNew<IssmDouble>(numnodes);
504 IssmDouble* dbasis = xNew<IssmDouble>(3*numnodes);
505 IssmDouble* B = xNew<IssmDouble>(3*numnodes);
506 IssmDouble* Bprime = xNew<IssmDouble>(3*numnodes);
507 IssmDouble D[3][3] = {0.};
508 IssmDouble K[3][3];
509
510 /*Retrieve all inputs and parameters*/
511 element->GetVerticesCoordinates(&xyz_list);
512 element->FindParam(&dt,TimesteppingTimeStepEnum);
513 element->FindParam(&stabilization,ThermalStabilizationEnum);
514 IssmDouble rho_water = element->GetMaterialParameter(MaterialsRhoSeawaterEnum);
515 IssmDouble rho_ice = element->GetMaterialParameter(MaterialsRhoIceEnum);
516 IssmDouble gravity = element->GetMaterialParameter(ConstantsGEnum);
517 IssmDouble heatcapacity = element->GetMaterialParameter(MaterialsHeatcapacityEnum);
518 IssmDouble thermalconductivity = element->GetMaterialParameter(MaterialsThermalconductivityEnum);
519 Input* vx_input = element->GetInput(VxEnum); _assert_(vx_input);
520 Input* vy_input = element->GetInput(VyEnum); _assert_(vy_input);
521 Input* vz_input = element->GetInput(VzEnum); _assert_(vz_input);
522 Input* vxm_input = element->GetInput(VxMeshEnum); _assert_(vxm_input);
523 Input* vym_input = element->GetInput(VyMeshEnum); _assert_(vym_input);
524 Input* vzm_input = element->GetInput(VzMeshEnum); _assert_(vzm_input);
525 if(stabilization==2) diameter=element->MinEdgeLength(xyz_list);
526
527 /*Enthalpy diffusion parameter*/
528 IssmDouble kappa=this->EnthalpyDiffusionParameterVolume(element,EnthalpyPicardEnum); _assert_(kappa>=0.);
529
530 /* Start looping on the number of gaussian points: */
531 Gauss* gauss=element->NewGauss(2);
532 for(int ig=gauss->begin();ig<gauss->end();ig++){
533 gauss->GaussPoint(ig);
534
535 element->JacobianDeterminant(&Jdet,xyz_list,gauss);
536 D_scalar=gauss->weight*Jdet;
537 if(dt!=0.) D_scalar=D_scalar*dt;
538
539 /*Conduction: */
540 GetBConduct(B,element,xyz_list,gauss);
541 D[0][0]=D_scalar*kappa/rho_ice;
542 D[1][1]=D_scalar*kappa/rho_ice;
543 D[2][2]=D_scalar*kappa/rho_ice;
544 TripleMultiply(B,3,numnodes,1,
545 &D[0][0],3,3,0,
546 B,3,numnodes,0,
547 &Ke->values[0],1);
548
549 /*Advection: */
550 GetBAdvec(B,element,xyz_list,gauss);
551 GetBAdvecprime(Bprime,element,xyz_list,gauss);
552 vx_input->GetInputValue(&u,gauss); vxm_input->GetInputValue(&um,gauss); vx=u-um;
553 vy_input->GetInputValue(&v,gauss); vym_input->GetInputValue(&vm,gauss); vy=v-vm;
554 vz_input->GetInputValue(&w,gauss); vzm_input->GetInputValue(&wm,gauss); vz=w-wm;
555 D[0][0]=D_scalar*vx;
556 D[1][1]=D_scalar*vy;
557 D[2][2]=D_scalar*vz;
558 TripleMultiply(B,3,numnodes,1,
559 &D[0][0],3,3,0,
560 Bprime,3,numnodes,0,
561 &Ke->values[0],1);
562
563 /*Transient: */
564 if(dt!=0.){
565 D_scalar=gauss->weight*Jdet;
566 element->NodalFunctions(basis,gauss);
567 TripleMultiply(basis,numnodes,1,0,
568 &D_scalar,1,1,0,
569 basis,1,numnodes,0,
570 &Ke->values[0],1);
571 D_scalar=D_scalar*dt;
572 }
573
574 /*Artifficial diffusivity*/
575 if(stabilization==1){
576 element->ElementSizes(&hx,&hy,&hz);
577 vel=sqrt(vx*vx + vy*vy + vz*vz)+1.e-14;
578 h=sqrt( pow(hx*vx/vel,2) + pow(hy*vy/vel,2) + pow(hz*vz/vel,2));
579 K[0][0]=h/(2.*vel)*fabs(vx*vx); K[0][1]=h/(2.*vel)*fabs(vx*vy); K[0][2]=h/(2.*vel)*fabs(vx*vz);
580 K[1][0]=h/(2.*vel)*fabs(vy*vx); K[1][1]=h/(2.*vel)*fabs(vy*vy); K[1][2]=h/(2.*vel)*fabs(vy*vz);
581 K[2][0]=h/(2.*vel)*fabs(vz*vx); K[2][1]=h/(2.*vel)*fabs(vz*vy); K[2][2]=h/(2.*vel)*fabs(vz*vz);
582 for(int i=0;i<3;i++) for(int j=0;j<3;j++) K[i][j] = D_scalar*K[i][j];
583
584 GetBAdvecprime(Bprime,element,xyz_list,gauss);
585 TripleMultiply(Bprime,3,numnodes,1,
586 &K[0][0],3,3,0,
587 Bprime,3,numnodes,0,
588 &Ke->values[0],1);
589 }
590 else if(stabilization==2){
591 element->NodalFunctionsDerivatives(dbasis,xyz_list,gauss);
592 tau_parameter=element->StabilizationParameter(u-um,v-vm,w-wm,diameter,kappa/rho_ice);
593 for(int i=0;i<numnodes;i++){
594 for(int j=0;j<numnodes;j++){
595 Ke->values[i*numnodes+j]+=tau_parameter*D_scalar*
596 ((u-um)*dbasis[0*numnodes+i]+(v-vm)*dbasis[1*numnodes+i]+(w-wm)*dbasis[2*numnodes+i])*((u-um)*dbasis[0*numnodes+j]+(v-vm)*dbasis[1*numnodes+j]+(w-wm)*dbasis[2*numnodes+j]);
597 }
598 }
599 if(dt!=0.){
600 D_scalar=gauss->weight*Jdet;
601 for(int i=0;i<numnodes;i++){
602 for(int j=0;j<numnodes;j++){
603 Ke->values[i*numnodes+j]+=tau_parameter*D_scalar*basis[j]*((u-um)*dbasis[0*numnodes+i]+(v-vm)*dbasis[1*numnodes+i]+(w-wm)*dbasis[2*numnodes+i]);
604 }
605 }
606 }
607 }
608 }
609
610 /*Clean up and return*/
611 xDelete<IssmDouble>(xyz_list);
612 xDelete<IssmDouble>(basis);
613 xDelete<IssmDouble>(dbasis);
614 xDelete<IssmDouble>(B);
615 xDelete<IssmDouble>(Bprime);
616 delete gauss;
617 return Ke;
618}/*}}}*/
619ElementMatrix* EnthalpyAnalysis::CreateKMatrixShelf(Element* element){/*{{{*/
620
621 /* Check if ice in element */
622 if(!element->IsIceInElement()) return NULL;
623
624 /*Initialize Element matrix and return if necessary*/
625 if(!element->IsOnBase() || !element->IsFloating()) return NULL;
626
627 /*Intermediaries*/
628 IssmDouble dt,Jdet,D;
629 IssmDouble *xyz_list_base = NULL;
630
631 /*Fetch number of nodes for this finite element*/
632 int numnodes = element->GetNumberOfNodes();
633
634 /*Initialize vectors*/
635 ElementMatrix* Ke = element->NewElementMatrix();
636 IssmDouble* basis = xNew<IssmDouble>(numnodes);
637
638 /*Retrieve all inputs and parameters*/
639 element->GetVerticesCoordinatesBase(&xyz_list_base);
640 element->FindParam(&dt,TimesteppingTimeStepEnum);
641 IssmDouble gravity = element->GetMaterialParameter(ConstantsGEnum);
642 IssmDouble rho_water = element->GetMaterialParameter(MaterialsRhoSeawaterEnum);
643 IssmDouble rho_ice = element->GetMaterialParameter(MaterialsRhoIceEnum);
644 IssmDouble heatcapacity = element->GetMaterialParameter(MaterialsHeatcapacityEnum);
645 IssmDouble mixed_layer_capacity= element->GetMaterialParameter(MaterialsMixedLayerCapacityEnum);
646 IssmDouble thermal_exchange_vel= element->GetMaterialParameter(MaterialsThermalExchangeVelocityEnum);
647
648 /* Start looping on the number of gaussian points: */
649 Gauss* gauss=element->NewGaussBase(2);
650 for(int ig=gauss->begin();ig<gauss->end();ig++){
651 gauss->GaussPoint(ig);
652
653 element->JacobianDeterminantBase(&Jdet,xyz_list_base,gauss);
654 element->NodalFunctions(basis,gauss);
655
656 D=gauss->weight*Jdet*rho_water*mixed_layer_capacity*thermal_exchange_vel/(heatcapacity*rho_ice);
657 if(reCast<bool,IssmDouble>(dt)) D=dt*D;
658 TripleMultiply(basis,numnodes,1,0,
659 &D,1,1,0,
660 basis,1,numnodes,0,
661 &Ke->values[0],1);
662
663 }
664
665 /*Clean up and return*/
666 delete gauss;
667 xDelete<IssmDouble>(basis);
668 xDelete<IssmDouble>(xyz_list_base);
669 return Ke;
670}/*}}}*/
671ElementVector* EnthalpyAnalysis::CreatePVector(Element* element){/*{{{*/
672
673 /* Check if ice in element */
674 if(!element->IsIceInElement()) return NULL;
675
676 /*compute all load vectors for this element*/
677 ElementVector* pe1=CreatePVectorVolume(element);
678 ElementVector* pe2=CreatePVectorSheet(element);
679 ElementVector* pe3=CreatePVectorShelf(element);
680 ElementVector* pe =new ElementVector(pe1,pe2,pe3);
681
682 /*clean-up and return*/
683 delete pe1;
684 delete pe2;
685 delete pe3;
686 return pe;
687}/*}}}*/
688ElementVector* EnthalpyAnalysis::CreatePVectorVolume(Element* element){/*{{{*/
689
690 /* Check if ice in element */
691 if(!element->IsIceInElement()) return NULL;
692
693 /*Intermediaries*/
694 int i, stabilization;
695 IssmDouble Jdet,phi,dt;
696 IssmDouble enthalpy, Hpmp;
697 IssmDouble enthalpypicard, d1enthalpypicard[3];
698 IssmDouble pressure, d1pressure[3], d2pressure;
699 IssmDouble waterfractionpicard;
700 IssmDouble kappa,tau_parameter,diameter,kappa_w;
701 IssmDouble u,v,w;
702 IssmDouble scalar_def, scalar_sens ,scalar_transient;
703 IssmDouble* xyz_list = NULL;
704 IssmDouble d1H_d1P, d1P2;
705
706 /*Fetch number of nodes and dof for this finite element*/
707 int numnodes = element->GetNumberOfNodes();
708 int numvertices = element->GetNumberOfVertices();
709
710 /*Initialize Element vector*/
711 ElementVector* pe = element->NewElementVector();
712 IssmDouble* basis = xNew<IssmDouble>(numnodes);
713 IssmDouble* dbasis = xNew<IssmDouble>(3*numnodes);
714
715 /*Retrieve all inputs and parameters*/
716 element->GetVerticesCoordinates(&xyz_list);
717 IssmDouble rho_ice = element->GetMaterialParameter(MaterialsRhoIceEnum);
718 IssmDouble heatcapacity = element->GetMaterialParameter(MaterialsHeatcapacityEnum);
719 IssmDouble thermalconductivity = element->GetMaterialParameter(MaterialsThermalconductivityEnum);
720 IssmDouble temperateiceconductivity = element->GetMaterialParameter(MaterialsTemperateiceconductivityEnum);
721 IssmDouble beta = element->GetMaterialParameter(MaterialsBetaEnum);
722 IssmDouble latentheat = element->GetMaterialParameter(MaterialsLatentheatEnum);
723 element->FindParam(&dt,TimesteppingTimeStepEnum);
724 element->FindParam(&stabilization,ThermalStabilizationEnum);
725 Input* vx_input=element->GetInput(VxEnum); _assert_(vx_input);
726 Input* vy_input=element->GetInput(VyEnum); _assert_(vy_input);
727 Input* vz_input=element->GetInput(VzEnum); _assert_(vz_input);
728 Input* enthalpypicard_input=element->GetInput(EnthalpyPicardEnum); _assert_(enthalpypicard_input);
729 Input* pressure_input=element->GetInput(PressureEnum); _assert_(pressure_input);
730 Input* enthalpy_input=NULL;
731 if(reCast<bool,IssmDouble>(dt)){enthalpy_input = element->GetInput(EnthalpyEnum); _assert_(enthalpy_input);}
732 if(stabilization==2){
733 diameter=element->MinEdgeLength(xyz_list);
734 kappa=this->EnthalpyDiffusionParameterVolume(element,EnthalpyPicardEnum); _assert_(kappa>=0.);
735 }
736
737 /* Start looping on the number of gaussian points: */
738 Gauss* gauss=element->NewGauss(3);
739 for(int ig=gauss->begin();ig<gauss->end();ig++){
740 gauss->GaussPoint(ig);
741
742 element->JacobianDeterminant(&Jdet,xyz_list,gauss);
743 element->NodalFunctions(basis,gauss);
744
745 /*viscous dissipation*/
746 element->ViscousHeating(&phi,xyz_list,gauss,vx_input,vy_input,vz_input);
747
748 scalar_def=phi/rho_ice*Jdet*gauss->weight;
749 if(dt!=0.) scalar_def=scalar_def*dt;
750
751 for(i=0;i<numnodes;i++) pe->values[i]+=scalar_def*basis[i];
752
753 /*sensible heat flux in temperate ice*/
754 enthalpypicard_input->GetInputValue(&enthalpypicard,gauss);
755 pressure_input->GetInputValue(&pressure,gauss);
756 Hpmp=this->PureIceEnthalpy(element, pressure);
757
758 if(enthalpypicard>=Hpmp){
759 enthalpypicard_input->GetInputDerivativeValue(&d1enthalpypicard[0],xyz_list,gauss);
760 pressure_input->GetInputDerivativeValue(&d1pressure[0],xyz_list,gauss);
761 d2pressure=0.; // for linear elements, 2nd derivative is zero
762
763 d1H_d1P=0.;
764 for(i=0;i<3;i++) d1H_d1P+=d1enthalpypicard[i]*d1pressure[i];
765 d1P2=0.;
766 for(i=0;i<3;i++) d1P2+=pow(d1pressure[i],2.);
767
768 scalar_sens=-beta*((temperateiceconductivity - thermalconductivity)/latentheat*(d1H_d1P + beta*heatcapacity*d1P2))/rho_ice;
769 if(dt!=0.) scalar_sens=scalar_sens*dt;
770 for(i=0;i<numnodes;i++) pe->values[i]+=scalar_sens*basis[i];
771 }
772
773 /* Build transient now */
774 if(reCast<bool,IssmDouble>(dt)){
775 enthalpy_input->GetInputValue(&enthalpy, gauss);
776 scalar_transient=enthalpy*Jdet*gauss->weight;
777 for(i=0;i<numnodes;i++) pe->values[i]+=scalar_transient*basis[i];
778 }
779
780 if(stabilization==2){
781 element->NodalFunctionsDerivatives(dbasis,xyz_list,gauss);
782
783 vx_input->GetInputValue(&u,gauss);
784 vy_input->GetInputValue(&v,gauss);
785 vz_input->GetInputValue(&w,gauss);
786 tau_parameter=element->StabilizationParameter(u,v,w,diameter,kappa/rho_ice);
787
788 for(i=0;i<numnodes;i++) pe->values[i]+=tau_parameter*scalar_def*(u*dbasis[0*numnodes+i]+v*dbasis[1*numnodes+i]+w*dbasis[2*numnodes+i]);
789
790 if(dt!=0.){
791 for(i=0;i<numnodes;i++) pe->values[i]+=tau_parameter*scalar_transient*(u*dbasis[0*numnodes+i]+v*dbasis[1*numnodes+i]+w*dbasis[2*numnodes+i]);
792 }
793 }
794 }
795
796 /*Clean up and return*/
797 xDelete<IssmDouble>(basis);
798 xDelete<IssmDouble>(dbasis);
799 xDelete<IssmDouble>(xyz_list);
800 delete gauss;
801 return pe;
802
803}/*}}}*/
804ElementVector* EnthalpyAnalysis::CreatePVectorSheet(Element* element){/*{{{*/
805
806 /* Check if ice in element */
807 if(!element->IsIceInElement()) return NULL;
808
809 /* implementation of the basal condition decision chart of Aschwanden 2012, Fig.5 */
810 if(!element->IsOnBase() || element->IsFloating()) return NULL;
811
812 bool isdynamicbasalspc;
813 int i, state;
814 IssmDouble dt,Jdet,scalar;
815 IssmDouble enthalpy, enthalpyup, pressure, pressureup, watercolumn, meltingrate;
816 IssmDouble vx,vy,vz;
817 IssmDouble alpha2,basalfriction,geothermalflux,heatflux;
818 IssmDouble *xyz_list_base = NULL;
819
820 /*Fetch number of nodes for this finite element*/
821 int numnodes = element->GetNumberOfNodes();
822
823 /*Initialize vectors*/
824 ElementVector* pe = element->NewElementVector();
825 IssmDouble* basis = xNew<IssmDouble>(numnodes);
826
827 /*Retrieve all inputs and parameters*/
828 element->GetVerticesCoordinatesBase(&xyz_list_base);
829 element->FindParam(&dt,TimesteppingTimeStepEnum);
830 element->FindParam(&isdynamicbasalspc,ThermalIsdynamicbasalspcEnum);
831 Input* vx_input = element->GetInput(VxEnum); _assert_(vx_input);
832 Input* vy_input = element->GetInput(VyEnum); _assert_(vy_input);
833 Input* vz_input = element->GetInput(VzEnum); _assert_(vz_input);
834 Input* enthalpy_input = element->GetInput(EnthalpyPicardEnum); _assert_(enthalpy_input);
835 Input* pressure_input = element->GetInput(PressureEnum); _assert_(pressure_input);
836 Input* watercolumn_input = element->GetInput(WatercolumnEnum); _assert_(watercolumn_input);
837 Input* meltingrate_input = element->GetInput(BasalforcingsGroundediceMeltingRateEnum); _assert_(meltingrate_input);
838 Input* geothermalflux_input = element->GetInput(BasalforcingsGeothermalfluxEnum); _assert_(geothermalflux_input);
839 IssmDouble rho_ice = element->GetMaterialParameter(MaterialsRhoIceEnum);
840
841 /*Build friction element, needed later: */
842 Friction* friction=new Friction(element,3);
843
844 /* Start looping on the number of gaussian points: */
845 Gauss* gauss=element->NewGaussBase(2);
846 Gauss* gaussup=element->NewGaussTop(2);
847 for(int ig=gauss->begin();ig<gauss->end();ig++){
848 gauss->GaussPoint(ig);
849 gaussup->GaussPoint(ig);
850
851 element->JacobianDeterminantBase(&Jdet,xyz_list_base,gauss);
852 element->NodalFunctions(basis,gauss);
853
854 if(isdynamicbasalspc){
855 enthalpy_input->GetInputValue(&enthalpy,gauss);
856 enthalpy_input->GetInputValue(&enthalpyup,gaussup);
857 pressure_input->GetInputValue(&pressure,gauss);
858 pressure_input->GetInputValue(&pressureup,gaussup);
859 watercolumn_input->GetInputValue(&watercolumn,gauss);
860 meltingrate_input->GetInputValue(&meltingrate,gauss);
861 state=GetThermalBasalCondition(element, enthalpy, enthalpyup, pressure, pressureup, watercolumn, meltingrate);
862 }
863 else
864 state=0;
865
866 switch (state) {
867 case 0:
868 // cold, dry base: apply basal surface forcing
869 geothermalflux_input->GetInputValue(&geothermalflux,gauss);
870 friction->GetAlpha2(&alpha2,gauss);
871 vx_input->GetInputValue(&vx,gauss);
872 vy_input->GetInputValue(&vy,gauss);
873 vz_input->GetInputValue(&vz,gauss);
874 basalfriction=alpha2*(vx*vx+vy*vy+vz*vz);
875 heatflux=(basalfriction+geothermalflux)/(rho_ice);
876 scalar=gauss->weight*Jdet*heatflux;
877 if(dt!=0.) scalar=dt*scalar;
878 for(i=0;i<numnodes;i++)
879 pe->values[i]+=scalar*basis[i];
880 break;
881 case 1:
882 // cold, wet base: keep at pressure melting point
883 break;
884 case 2:
885 // temperate, thin refreezing base: release spc
886 break;
887 case 3:
888 // temperate, thin melting base: set spc
889 break;
890 case 4:
891 // temperate, thick melting base: set grad H*n=0
892 for(i=0;i<numnodes;i++)
893 pe->values[i]+=0.;
894 break;
895 default:
896 _printf0_(" unknown thermal basal state found!");
897 }
898 }
899
900 /*Clean up and return*/
901 delete gauss;
902 delete gaussup;
903 delete friction;
904 xDelete<IssmDouble>(basis);
905 xDelete<IssmDouble>(xyz_list_base);
906 return pe;
907
908}/*}}}*/
909ElementVector* EnthalpyAnalysis::CreatePVectorShelf(Element* element){/*{{{*/
910
911 /* Check if ice in element */
912 if(!element->IsIceInElement()) return NULL;
913
914 /*Get basal element*/
915 if(!element->IsOnBase() || !element->IsFloating()) return NULL;
916
917 IssmDouble Hpmp,dt,Jdet,scalar_ocean,pressure;
918 IssmDouble *xyz_list_base = NULL;
919
920 /*Fetch number of nodes for this finite element*/
921 int numnodes = element->GetNumberOfNodes();
922
923 /*Initialize vectors*/
924 ElementVector* pe = element->NewElementVector();
925 IssmDouble* basis = xNew<IssmDouble>(numnodes);
926
927 /*Retrieve all inputs and parameters*/
928 element->GetVerticesCoordinatesBase(&xyz_list_base);
929 element->FindParam(&dt,TimesteppingTimeStepEnum);
930 Input* pressure_input=element->GetInput(PressureEnum); _assert_(pressure_input);
931 IssmDouble gravity = element->GetMaterialParameter(ConstantsGEnum);
932 IssmDouble rho_water = element->GetMaterialParameter(MaterialsRhoSeawaterEnum);
933 IssmDouble rho_ice = element->GetMaterialParameter(MaterialsRhoIceEnum);
934 IssmDouble heatcapacity = element->GetMaterialParameter(MaterialsHeatcapacityEnum);
935 IssmDouble mixed_layer_capacity= element->GetMaterialParameter(MaterialsMixedLayerCapacityEnum);
936 IssmDouble thermal_exchange_vel= element->GetMaterialParameter(MaterialsThermalExchangeVelocityEnum);
937
938 /* Start looping on the number of gaussian points: */
939 Gauss* gauss=element->NewGaussBase(2);
940 for(int ig=gauss->begin();ig<gauss->end();ig++){
941 gauss->GaussPoint(ig);
942
943 element->JacobianDeterminantBase(&Jdet,xyz_list_base,gauss);
944 element->NodalFunctions(basis,gauss);
945
946 pressure_input->GetInputValue(&pressure,gauss);
947 Hpmp=element->PureIceEnthalpy(pressure);
948
949 scalar_ocean=gauss->weight*Jdet*rho_water*mixed_layer_capacity*thermal_exchange_vel*Hpmp/(heatcapacity*rho_ice);
950 if(reCast<bool,IssmDouble>(dt)) scalar_ocean=dt*scalar_ocean;
951
952 for(int i=0;i<numnodes;i++) pe->values[i]+=scalar_ocean*basis[i];
953 }
954
955 /*Clean up and return*/
956 delete gauss;
957 xDelete<IssmDouble>(basis);
958 xDelete<IssmDouble>(xyz_list_base);
959 return pe;
960}/*}}}*/
961void EnthalpyAnalysis::DrainWaterfraction(FemModel* femmodel){/*{{{*/
962 /*Drain excess water fraction in ice column: */
963 for(int i=0;i<femmodel->elements->Size();i++){
964 Element* element=xDynamicCast<Element*>(femmodel->elements->GetObjectByOffset(i));
965 DrainWaterfractionIcecolumn(element);
966 }
967}/*}}}*/
968void EnthalpyAnalysis::DrainWaterfraction(Element* element, IssmDouble* pdrainrate_element){/*{{{*/
969
970 /* Check if ice in element */
971 if(!element->IsIceInElement()) return;
972
973 /*Intermediaries*/
974 int iv,is,vertexdown,vertexup,numsegments;
975 IssmDouble dt, height_element;
976 IssmDouble rho_water, rho_ice;
977 int numvertices = element->GetNumberOfVertices();
978
979 IssmDouble* xyz_list = NULL;
980 IssmDouble* enthalpies = xNew<IssmDouble>(numvertices);
981 IssmDouble* pressures = xNew<IssmDouble>(numvertices);
982 IssmDouble* temperatures = xNew<IssmDouble>(numvertices);
983 IssmDouble* waterfractions = xNew<IssmDouble>(numvertices);
984 IssmDouble* deltawaterfractions = xNew<IssmDouble>(numvertices);
985 int *pairindices = NULL;
986
987 rho_ice=element->GetMaterialParameter(MaterialsRhoIceEnum);
988 rho_water=element->GetMaterialParameter(MaterialsRhoSeawaterEnum);
989
990 element->GetVerticesCoordinates(&xyz_list);
991 element->GetInputListOnVertices(enthalpies,EnthalpyEnum);
992 element->GetInputListOnVertices(pressures,PressureEnum);
993
994 element->FindParam(&dt,TimesteppingTimeStepEnum);
995 for(iv=0;iv<numvertices;iv++){
996 element->EnthalpyToThermal(&temperatures[iv],&waterfractions[iv], enthalpies[iv],pressures[iv]);
997 deltawaterfractions[iv]=DrainageFunctionWaterfraction(waterfractions[iv], dt);
998 }
999
1000 /*drain waterfraction, feed updated variables back into model*/
1001 for(iv=0;iv<numvertices;iv++){
1002 if(reCast<bool,IssmDouble>(dt))
1003 waterfractions[iv]-=deltawaterfractions[iv]*dt;
1004 else
1005 waterfractions[iv]-=deltawaterfractions[iv];
1006 element->ThermalToEnthalpy(&enthalpies[iv], temperatures[iv], waterfractions[iv], pressures[iv]);
1007 }
1008 element->AddInput(EnthalpyEnum,enthalpies,P1Enum);
1009 element->AddInput(WaterfractionEnum,waterfractions,P1Enum);
1010
1011 /*return meltwater column equivalent to drained water*/
1012 element->VerticalSegmentIndices(&pairindices,&numsegments);
1013 for(is=0;is<numsegments;is++){
1014 vertexdown = pairindices[is*2+0];
1015 vertexup = pairindices[is*2+1];
1016 height_element=fabs(xyz_list[vertexup*3+2]-xyz_list[vertexdown*3+2]);
1017 pdrainrate_element[is]=(deltawaterfractions[vertexdown]+deltawaterfractions[vertexup])/2.*height_element; // return water equivalent of drainage
1018 _assert_(pdrainrate_element[is]>=0.);
1019 }
1020
1021 /*Clean up and return*/
1022 xDelete<int>(pairindices);
1023 xDelete<IssmDouble>(xyz_list);
1024 xDelete<IssmDouble>(enthalpies);
1025 xDelete<IssmDouble>(pressures);
1026 xDelete<IssmDouble>(temperatures);
1027 xDelete<IssmDouble>(waterfractions);
1028 xDelete<IssmDouble>(deltawaterfractions);
1029}/*}}}*/
1030void EnthalpyAnalysis::DrainWaterfractionIcecolumn(Element* element){/*{{{*/
1031
1032 /* Check if ice in element */
1033 if(!element->IsIceInElement()) return;
1034
1035 /* Only drain waterfraction of ice column from element at base*/
1036 if(!element->IsOnBase()) return; //FIXME: allow freeze on for floating elements
1037
1038 /* Intermediaries*/
1039 int is, numvertices, numsegments;
1040 int *pairindices = NULL;
1041
1042 numvertices=element->GetNumberOfVertices();
1043 element->VerticalSegmentIndices(&pairindices,&numsegments);
1044
1045 IssmDouble* watercolumn = xNew<IssmDouble>(numvertices);
1046 IssmDouble* drainrate_column = xNew<IssmDouble>(numsegments);
1047 IssmDouble* drainrate_element = xNew<IssmDouble>(numsegments);
1048
1049 element->GetInputListOnVertices(watercolumn,WatercolumnEnum);
1050
1051 for(is=0;is<numsegments;is++) drainrate_column[is]=0.;
1052 Element* elementi = element;
1053 for(;;){
1054 for(is=0;is<numsegments;is++) drainrate_element[is]=0.;
1055 DrainWaterfraction(elementi,drainrate_element); // TODO: make sure every vertex is only drained once
1056 for(is=0;is<numsegments;is++) drainrate_column[is]+=drainrate_element[is];
1057
1058 if(elementi->IsOnSurface()) break;
1059 elementi=elementi->GetUpperElement();
1060 }
1061 /* add drained water to water column*/
1062 for(is=0;is<numsegments;is++) watercolumn[is]+=drainrate_column[is];
1063 /* Feed updated water column back into model */
1064 element->AddInput(WatercolumnEnum,watercolumn,P1Enum);
1065
1066 xDelete<int>(pairindices);
1067 xDelete<IssmDouble>(drainrate_column);
1068 xDelete<IssmDouble>(drainrate_element);
1069 xDelete<IssmDouble>(watercolumn);
1070}/*}}}*/
1071IssmDouble EnthalpyAnalysis::EnthalpyDiffusionParameter(Element* element,IssmDouble enthalpy,IssmDouble pressure){/*{{{*/
1072
1073 IssmDouble heatcapacity = element->GetMaterialParameter(MaterialsHeatcapacityEnum);
1074 IssmDouble temperateiceconductivity = element->GetMaterialParameter(MaterialsTemperateiceconductivityEnum);
1075 IssmDouble thermalconductivity = element->GetMaterialParameter(MaterialsThermalconductivityEnum);
1076
1077 if(enthalpy < PureIceEnthalpy(element,pressure)){
1078 return thermalconductivity/heatcapacity;
1079 }
1080 else{
1081 return temperateiceconductivity/heatcapacity;
1082 }
1083}/*}}}*/
1084IssmDouble EnthalpyAnalysis::EnthalpyDiffusionParameterVolume(Element* element,int enthalpy_enum){/*{{{*/
1085
1086 int iv;
1087 IssmDouble lambda; /* fraction of cold ice */
1088 IssmDouble kappa,kappa_c,kappa_t; /* enthalpy conductivities */
1089 IssmDouble Hc,Ht;
1090
1091 /*Get pressures and enthalpies on vertices*/
1092 int numvertices = element->GetNumberOfVertices();
1093 IssmDouble* pressures = xNew<IssmDouble>(numvertices);
1094 IssmDouble* enthalpies = xNew<IssmDouble>(numvertices);
1095 IssmDouble* PIE = xNew<IssmDouble>(numvertices);
1096 IssmDouble* dHpmp = xNew<IssmDouble>(numvertices);
1097 element->GetInputListOnVertices(pressures,PressureEnum);
1098 element->GetInputListOnVertices(enthalpies,enthalpy_enum);
1099 for(iv=0;iv<numvertices;iv++){
1100 PIE[iv] = PureIceEnthalpy(element,pressures[iv]);
1101 dHpmp[iv] = enthalpies[iv]-PIE[iv];
1102 }
1103
1104 bool allequalsign = true;
1105 if(dHpmp[0]<0.){
1106 for(iv=1; iv<numvertices;iv++) allequalsign=(allequalsign && (dHpmp[iv]<0.));
1107 }
1108 else{
1109 for(iv=1; iv<numvertices;iv++) allequalsign=(allequalsign && (dHpmp[iv]>=0.));
1110 }
1111
1112 if(allequalsign){
1113 kappa = EnthalpyDiffusionParameter(element,enthalpies[0],pressures[0]);
1114 }
1115 else{
1116 /* return harmonic mean of thermal conductivities, weighted by fraction of cold/temperate ice,
1117 cf Patankar 1980, pp44 */
1118 kappa_c = EnthalpyDiffusionParameter(element,PureIceEnthalpy(element,0.)-1.,0.);
1119 kappa_t = EnthalpyDiffusionParameter(element,PureIceEnthalpy(element,0.)+1.,0.);
1120 Hc=0.; Ht=0.;
1121 for(iv=0; iv<numvertices;iv++){
1122 if(enthalpies[iv]<PIE[iv])
1123 Hc+=(PIE[iv]-enthalpies[iv]);
1124 else
1125 Ht+=(enthalpies[iv]-PIE[iv]);
1126 }
1127 _assert_((Hc+Ht)>0.);
1128 lambda = Hc/(Hc+Ht);
1129 kappa = kappa_c*kappa_t/(lambda*kappa_t+(1.-lambda)*kappa_c); // ==(lambda/kappa_c + (1.-lambda)/kappa_t)^-1
1130 }
1131
1132 /*Clean up and return*/
1133 xDelete<IssmDouble>(PIE);
1134 xDelete<IssmDouble>(dHpmp);
1135 xDelete<IssmDouble>(pressures);
1136 xDelete<IssmDouble>(enthalpies);
1137 return kappa;
1138}/*}}}*/
1139void EnthalpyAnalysis::GetBAdvec(IssmDouble* B,Element* element,IssmDouble* xyz_list,Gauss* gauss){/*{{{*/
1140 /*Compute B matrix. B=[B1 B2 B3 B4 B5 B6] where Bi is of size 5*NDOF1.
1141 * For node i, Bi' can be expressed in the actual coordinate system
1142 * by:
1143 * Bi_advec =[ h ]
1144 * [ h ]
1145 * [ h ]
1146 * where h is the interpolation function for node i.
1147 *
1148 * We assume B has been allocated already, of size: 3x(NDOF1*NUMNODESP1)
1149 */
1150
1151 /*Fetch number of nodes for this finite element*/
1152 int numnodes = element->GetNumberOfNodes();
1153
1154 /*Get nodal functions*/
1155 IssmDouble* basis=xNew<IssmDouble>(numnodes);
1156 element->NodalFunctions(basis,gauss);
1157
1158 /*Build B: */
1159 for(int i=0;i<numnodes;i++){
1160 B[numnodes*0+i] = basis[i];
1161 B[numnodes*1+i] = basis[i];
1162 B[numnodes*2+i] = basis[i];
1163 }
1164
1165 /*Clean-up*/
1166 xDelete<IssmDouble>(basis);
1167}/*}}}*/
1168void EnthalpyAnalysis::GetBAdvecprime(IssmDouble* B,Element* element,IssmDouble* xyz_list,Gauss* gauss){/*{{{*/
1169 /*Compute B matrix. B=[B1 B2 B3 B4 B5 B6] where Bi is of size 5*NDOF1.
1170 * For node i, Bi' can be expressed in the actual coordinate system
1171 * by:
1172 * Biprime_advec=[ dh/dx ]
1173 * [ dh/dy ]
1174 * [ dh/dz ]
1175 * where h is the interpolation function for node i.
1176 *
1177 * We assume B has been allocated already, of size: 3x(NDOF1*numnodes)
1178 */
1179
1180 /*Fetch number of nodes for this finite element*/
1181 int numnodes = element->GetNumberOfNodes();
1182
1183 /*Get nodal functions derivatives*/
1184 IssmDouble* dbasis=xNew<IssmDouble>(3*numnodes);
1185 element->NodalFunctionsDerivatives(dbasis,xyz_list,gauss);
1186
1187 /*Build B: */
1188 for(int i=0;i<numnodes;i++){
1189 B[numnodes*0+i] = dbasis[0*numnodes+i];
1190 B[numnodes*1+i] = dbasis[1*numnodes+i];
1191 B[numnodes*2+i] = dbasis[2*numnodes+i];
1192 }
1193
1194 /*Clean-up*/
1195 xDelete<IssmDouble>(dbasis);
1196}/*}}}*/
1197void EnthalpyAnalysis::GetBasalConstraints(Vector<IssmDouble>* vec_spc,Element* element){/*{{{*/
1198
1199 /* Check if ice in element */
1200 if(!element->IsIceInElement()) return;
1201
1202 /* Only update Constraints at the base of grounded ice*/
1203 if(!(element->IsOnBase()) || element->IsFloating()) return;
1204
1205 /*Intermediary*/
1206 bool isdynamicbasalspc;
1207 IssmDouble dt;
1208
1209 /*Check wether dynamic basal boundary conditions are activated */
1210 element->FindParam(&isdynamicbasalspc,ThermalIsdynamicbasalspcEnum);
1211 if(!isdynamicbasalspc) return;
1212
1213 element->FindParam(&dt,TimesteppingTimeStepEnum);
1214 if(dt==0.){
1215 GetBasalConstraintsSteadystate(vec_spc,element);
1216 }
1217 else{
1218 GetBasalConstraintsTransient(vec_spc,element);
1219 }
1220}/*}}}*/
1221void EnthalpyAnalysis::GetBasalConstraintsSteadystate(Vector<IssmDouble>* vec_spc,Element* element){/*{{{*/
1222
1223 /* Check if ice in element */
1224 if(!element->IsIceInElement()) return;
1225
1226 /* Only update Constraints at the base of grounded ice*/
1227 if(!(element->IsOnBase()) || element->IsFloating()) return;
1228
1229 /*Intermediary*/
1230 int numindices, numindicesup, state;
1231 int *indices = NULL, *indicesup = NULL;
1232 IssmDouble enthalpy, enthalpyup, pressure, pressureup, watercolumn, meltingrate;
1233
1234 /*Get parameters and inputs: */
1235 Input* enthalpy_input = element->GetInput(EnthalpyPicardEnum); _assert_(enthalpy_input);
1236 Input* pressure_input = element->GetInput(PressureEnum); _assert_(pressure_input);
1237 Input* watercolumn_input = element->GetInput(WatercolumnEnum); _assert_(watercolumn_input);
1238 Input* meltingrate_input = element->GetInput(BasalforcingsGroundediceMeltingRateEnum); _assert_(meltingrate_input);
1239
1240 /*Fetch indices of basal & surface nodes for this finite element*/
1241 Penta *penta = (Penta *) element; // TODO: add Basal-/SurfaceNodeIndices to element.h, and change this to Element*
1242 penta->BasalNodeIndices(&numindices,&indices,element->GetElementType());
1243 penta->SurfaceNodeIndices(&numindicesup,&indicesup,element->GetElementType()); _assert_(numindices==numindicesup);
1244
1245 GaussPenta* gauss=new GaussPenta();
1246 GaussPenta* gaussup=new GaussPenta();
1247 for(int i=0;i<numindices;i++){
1248 gauss->GaussNode(element->GetElementType(),indices[i]);
1249 gaussup->GaussNode(element->GetElementType(),indicesup[i]);
1250
1251 enthalpy_input->GetInputValue(&enthalpy,gauss);
1252 enthalpy_input->GetInputValue(&enthalpyup,gaussup);
1253 pressure_input->GetInputValue(&pressure,gauss);
1254 pressure_input->GetInputValue(&pressureup,gaussup);
1255 watercolumn_input->GetInputValue(&watercolumn,gauss);
1256 meltingrate_input->GetInputValue(&meltingrate,gauss);
1257
1258 state=GetThermalBasalCondition(element, enthalpy, enthalpyup, pressure, pressureup, watercolumn, meltingrate);
1259 switch (state) {
1260 case 0:
1261 // cold, dry base: apply basal surface forcing
1262 vec_spc->SetValue(element->nodes[i]->Sid(),0.,INS_VAL);
1263 break;
1264 case 1:
1265 // cold, wet base: keep at pressure melting point
1266 vec_spc->SetValue(element->nodes[i]->Sid(),1.,INS_VAL);
1267 break;
1268 case 2:
1269 // temperate, thin refreezing base: release spc
1270 vec_spc->SetValue(element->nodes[i]->Sid(),0.,INS_VAL);
1271 break;
1272 case 3:
1273 // temperate, thin melting base: set spc
1274 vec_spc->SetValue(element->nodes[i]->Sid(),1.,INS_VAL);
1275 break;
1276 case 4:
1277 // temperate, thick melting base: s
1278 vec_spc->SetValue(element->nodes[i]->Sid(),1.,INS_VAL);
1279 break;
1280 default:
1281 _printf0_(" unknown thermal basal state found!");
1282 }
1283 }
1284
1285 /*Free ressources:*/
1286 xDelete<int>(indices);
1287 xDelete<int>(indicesup);
1288 delete gauss;
1289 delete gaussup;
1290}/*}}}*/
1291void EnthalpyAnalysis::GetBasalConstraintsTransient(Vector<IssmDouble>* vec_spc,Element* element){/*{{{*/
1292
1293 /* Check if ice in element */
1294 if(!element->IsIceInElement()) return;
1295
1296 /* Only update Constraints at the base of grounded ice*/
1297 if(!(element->IsOnBase()) || element->IsFloating()) return;
1298
1299 /*Intermediary*/
1300 int numindices, numindicesup, state;
1301 int *indices = NULL, *indicesup = NULL;
1302 IssmDouble enthalpy, enthalpyup, pressure, pressureup, watercolumn, meltingrate;
1303
1304 /*Get parameters and inputs: */
1305 Input* enthalpy_input = element->GetInput(EnthalpyEnum); _assert_(enthalpy_input); //TODO: check EnthalpyPicard?
1306 Input* pressure_input = element->GetInput(PressureEnum); _assert_(pressure_input);
1307 Input* watercolumn_input = element->GetInput(WatercolumnEnum); _assert_(watercolumn_input);
1308 Input* meltingrate_input = element->GetInput(BasalforcingsGroundediceMeltingRateEnum); _assert_(meltingrate_input);
1309
1310 /*Fetch indices of basal & surface nodes for this finite element*/
1311 Penta *penta = (Penta *) element; // TODO: add Basal-/SurfaceNodeIndices to element.h, and change this to Element*
1312 penta->BasalNodeIndices(&numindices,&indices,element->GetElementType());
1313 penta->SurfaceNodeIndices(&numindicesup,&indicesup,element->GetElementType()); _assert_(numindices==numindicesup);
1314
1315 GaussPenta* gauss=new GaussPenta();
1316 GaussPenta* gaussup=new GaussPenta();
1317
1318 for(int i=0;i<numindices;i++){
1319 gauss->GaussNode(element->GetElementType(),indices[i]);
1320 gaussup->GaussNode(element->GetElementType(),indicesup[i]);
1321
1322 enthalpy_input->GetInputValue(&enthalpy,gauss);
1323 enthalpy_input->GetInputValue(&enthalpyup,gaussup);
1324 pressure_input->GetInputValue(&pressure,gauss);
1325 pressure_input->GetInputValue(&pressureup,gaussup);
1326 watercolumn_input->GetInputValue(&watercolumn,gauss);
1327 meltingrate_input->GetInputValue(&meltingrate,gauss);
1328
1329 state=GetThermalBasalCondition(element, enthalpy, enthalpyup, pressure, pressureup, watercolumn, meltingrate);
1330
1331 switch (state) {
1332 case 0:
1333 // cold, dry base: apply basal surface forcing
1334 vec_spc->SetValue(element->nodes[i]->Sid(),0.,INS_VAL);
1335 break;
1336 case 1:
1337 // cold, wet base: keep at pressure melting point
1338 vec_spc->SetValue(element->nodes[i]->Sid(),1.,INS_VAL);
1339 break;
1340 case 2:
1341 // temperate, thin refreezing base: release spc
1342 vec_spc->SetValue(element->nodes[i]->Sid(),0.,INS_VAL);
1343 break;
1344 case 3:
1345 // temperate, thin melting base: set spc
1346 vec_spc->SetValue(element->nodes[i]->Sid(),1.,INS_VAL);
1347 break;
1348 case 4:
1349 // temperate, thick melting base: set grad H*n=0
1350 vec_spc->SetValue(element->nodes[i]->Sid(),0.,INS_VAL);
1351 break;
1352 default:
1353 _printf0_(" unknown thermal basal state found!");
1354 }
1355
1356 }
1357
1358 /*Free ressources:*/
1359 xDelete<int>(indices);
1360 xDelete<int>(indicesup);
1361 delete gauss;
1362 delete gaussup;
1363}/*}}}*/
1364void EnthalpyAnalysis::GetBConduct(IssmDouble* B,Element* element,IssmDouble* xyz_list,Gauss* gauss){/*{{{*/
1365 /*Compute B matrix. B=[B1 B2 B3 B4 B5 B6] where Bi is of size 5*NDOF1.
1366 * For node i, Bi' can be expressed in the actual coordinate system
1367 * by:
1368 * Bi_conduct=[ dh/dx ]
1369 * [ dh/dy ]
1370 * [ dh/dz ]
1371 * where h is the interpolation function for node i.
1372 *
1373 * We assume B has been allocated already, of size: 3x(NDOF1*numnodes)
1374 */
1375
1376 /*Fetch number of nodes for this finite element*/
1377 int numnodes = element->GetNumberOfNodes();
1378
1379 /*Get nodal functions derivatives*/
1380 IssmDouble* dbasis=xNew<IssmDouble>(3*numnodes);
1381 element->NodalFunctionsDerivatives(dbasis,xyz_list,gauss);
1382
1383 /*Build B: */
1384 for(int i=0;i<numnodes;i++){
1385 B[numnodes*0+i] = dbasis[0*numnodes+i];
1386 B[numnodes*1+i] = dbasis[1*numnodes+i];
1387 B[numnodes*2+i] = dbasis[2*numnodes+i];
1388 }
1389
1390 /*Clean-up*/
1391 xDelete<IssmDouble>(dbasis);
1392}/*}}}*/
1393void EnthalpyAnalysis::GetSolutionFromInputs(Vector<IssmDouble>* solution,Element* element){/*{{{*/
1394 element->GetSolutionFromInputsOneDof(solution,EnthalpyEnum);
1395}/*}}}*/
1396int EnthalpyAnalysis::GetThermalBasalCondition(Element* element, IssmDouble enthalpy, IssmDouble enthalpyup, IssmDouble pressure, IssmDouble pressureup, IssmDouble watercolumn, IssmDouble meltingrate){/*{{{*/
1397
1398 /* Check if ice in element */
1399 if(!element->IsIceInElement()) return -1;
1400
1401 /* Only update Constraints at the base of grounded ice*/
1402 if(!(element->IsOnBase())) return -1;
1403
1404 /*Intermediary*/
1405 int state=-1;
1406 IssmDouble dt;
1407
1408 /*Get parameters and inputs: */
1409 element->FindParam(&dt,TimesteppingTimeStepEnum);
1410
1411 if(enthalpy<PureIceEnthalpy(element,pressure)){
1412 if(watercolumn<=0.) state=0; // cold, dry base
1413 else state=1; // cold, wet base (refreezing)
1414 }
1415 else{
1416 if(enthalpyup<PureIceEnthalpy(element,pressureup)){
1417 if((dt==0.) && (meltingrate<0.)) state=2; // refreezing temperate base (non-physical, only for steadystate solver)
1418 else state=3; // temperate base, but no temperate layer
1419 }
1420 else state=4; // temperate layer with positive thickness
1421 }
1422
1423 _assert_(state>=0);
1424 return state;
1425}/*}}}*/
1426IssmDouble EnthalpyAnalysis::GetWetIceConductivity(Element* element, IssmDouble enthalpy, IssmDouble pressure){/*{{{*/
1427
1428 IssmDouble temperature, waterfraction;
1429 IssmDouble kappa_w = 0.6; // thermal conductivity of water (in W/m/K)
1430 IssmDouble kappa_i = element->GetMaterialParameter(MaterialsThermalconductivityEnum);
1431 element->EnthalpyToThermal(&temperature, &waterfraction, enthalpy, pressure);
1432
1433 return (1.-waterfraction)*kappa_i + waterfraction*kappa_w;
1434}/*}}}*/
1435void EnthalpyAnalysis::GradientJ(Vector<IssmDouble>* gradient,Element* element,int control_type,int control_index){/*{{{*/
1436 _error_("Not implemented yet");
1437}/*}}}*/
1438void EnthalpyAnalysis::InputUpdateFromSolution(IssmDouble* solution,Element* element){/*{{{*/
1439
1440 bool converged;
1441 int i,rheology_law;
1442 IssmDouble B_average,s_average,T_average=0.,P_average=0.;
1443 int *doflist = NULL;
1444 IssmDouble *xyz_list = NULL;
1445
1446 /*Fetch number of nodes and dof for this finite element*/
1447 int numnodes = element->GetNumberOfNodes();
1448
1449 /*Fetch dof list and allocate solution vector*/
1450 element->GetDofList(&doflist,NoneApproximationEnum,GsetEnum);
1451 IssmDouble* values = xNew<IssmDouble>(numnodes);
1452 IssmDouble* pressure = xNew<IssmDouble>(numnodes);
1453 IssmDouble* surface = xNew<IssmDouble>(numnodes);
1454 IssmDouble* B = xNew<IssmDouble>(numnodes);
1455 IssmDouble* temperature = xNew<IssmDouble>(numnodes);
1456 IssmDouble* waterfraction = xNew<IssmDouble>(numnodes);
1457
1458 /*Use the dof list to index into the solution vector: */
1459 for(i=0;i<numnodes;i++){
1460 values[i]=solution[doflist[i]];
1461
1462 /*Check solution*/
1463 if(xIsNan<IssmDouble>(values[i])) _error_("NaN found in solution vector");
1464 }
1465
1466 /*Get all inputs and parameters*/
1467 element->GetInputValue(&converged,ConvergedEnum);
1468 element->GetInputListOnNodes(&pressure[0],PressureEnum);
1469 if(converged){
1470 for(i=0;i<numnodes;i++){
1471 element->EnthalpyToThermal(&temperature[i],&waterfraction[i],values[i],pressure[i]);
1472 if(waterfraction[i]<0.) _error_("Negative water fraction found in solution vector");
1473 //if(waterfraction[i]>1.) _error_("Water fraction >1 found in solution vector");
1474 }
1475 element->AddInput(EnthalpyEnum,values,element->GetElementType());
1476 element->AddInput(WaterfractionEnum,waterfraction,element->GetElementType());
1477 element->AddInput(TemperatureEnum,temperature,element->GetElementType());
1478
1479 /*Update Rheology only if converged (we must make sure that the temperature is below melting point
1480 * otherwise the rheology could be negative*/
1481 element->FindParam(&rheology_law,MaterialsRheologyLawEnum);
1482 element->GetInputListOnNodes(&surface[0],SurfaceEnum);
1483 switch(rheology_law){
1484 case NoneEnum:
1485 /*Do nothing: B is not temperature dependent*/
1486 break;
1487 case CuffeyEnum:
1488 for(i=0;i<numnodes;i++) B[i]=Cuffey(temperature[i]);
1489 element->AddInput(MaterialsRheologyBEnum,&B[0],element->GetElementType());
1490 break;
1491 case PatersonEnum:
1492 for(i=0;i<numnodes;i++) B[i]=Paterson(temperature[i]);
1493 element->AddInput(MaterialsRheologyBEnum,&B[0],element->GetElementType());
1494 break;
1495 case ArrheniusEnum:
1496 element->GetVerticesCoordinates(&xyz_list);
1497 for(i=0;i<numnodes;i++) B[i]=Arrhenius(temperature[i],surface[i]-xyz_list[i*3+2],element->GetMaterialParameter(MaterialsRheologyNEnum));
1498 element->AddInput(MaterialsRheologyBEnum,&B[0],element->GetElementType());
1499 break;
1500 case LliboutryDuvalEnum:
1501 for(i=0;i<numnodes;i++) B[i]=LliboutryDuval(values[i],pressure[i],element->GetMaterialParameter(MaterialsRheologyNEnum),element->GetMaterialParameter(MaterialsBetaEnum),element->GetMaterialParameter(ConstantsReferencetemperatureEnum),element->GetMaterialParameter(MaterialsHeatcapacityEnum),element->GetMaterialParameter(MaterialsLatentheatEnum));
1502 element->AddInput(MaterialsRheologyBEnum,&B[0],element->GetElementType());
1503 break;
1504 default: _error_("Rheology law " << EnumToStringx(rheology_law) << " not supported yet");
1505 }
1506 }
1507 else{
1508 element->AddInput(EnthalpyPicardEnum,values,element->GetElementType());
1509 }
1510
1511 /*Free ressources:*/
1512 xDelete<IssmDouble>(values);
1513 xDelete<IssmDouble>(pressure);
1514 xDelete<IssmDouble>(surface);
1515 xDelete<IssmDouble>(B);
1516 xDelete<IssmDouble>(temperature);
1517 xDelete<IssmDouble>(waterfraction);
1518 xDelete<IssmDouble>(xyz_list);
1519 xDelete<int>(doflist);
1520}/*}}}*/
1521void EnthalpyAnalysis::PostProcessing(FemModel* femmodel){/*{{{*/
1522
1523 /*Intermediaries*/
1524 bool computebasalmeltingrates=true;
1525 bool drainicecolumn=true;
1526 bool isdynamicbasalspc;
1527 IssmDouble dt;
1528
1529 femmodel->parameters->FindParam(&isdynamicbasalspc,ThermalIsdynamicbasalspcEnum);
1530 femmodel->parameters->FindParam(&dt,TimesteppingTimeStepEnum);
1531
1532 //TODO: use dt to decide what to do
1533 if(drainicecolumn) DrainWaterfraction(femmodel);
1534 if(computebasalmeltingrates) ComputeBasalMeltingrate(femmodel);
1535 if(isdynamicbasalspc) UpdateBasalConstraints(femmodel);
1536
1537}/*}}}*/
1538IssmDouble EnthalpyAnalysis::PureIceEnthalpy(Element* element,IssmDouble pressure){/*{{{*/
1539
1540 IssmDouble heatcapacity = element->GetMaterialParameter(MaterialsHeatcapacityEnum);
1541 IssmDouble referencetemperature = element->GetMaterialParameter(ConstantsReferencetemperatureEnum);
1542
1543 return heatcapacity*(TMeltingPoint(element,pressure)-referencetemperature);
1544}/*}}}*/
1545IssmDouble EnthalpyAnalysis::TMeltingPoint(Element* element,IssmDouble pressure){/*{{{*/
1546
1547 IssmDouble meltingpoint = element->GetMaterialParameter(MaterialsMeltingpointEnum);
1548 IssmDouble beta = element->GetMaterialParameter(MaterialsBetaEnum);
1549
1550 return meltingpoint-beta*pressure;
1551}/*}}}*/
1552void EnthalpyAnalysis::UpdateBasalConstraints(FemModel* femmodel){/*{{{*/
1553
1554 /*Update basal dirichlet BCs for enthalpy: */
1555 Vector<IssmDouble>* spc = NULL;
1556 IssmDouble* serial_spc = NULL;
1557
1558 spc=new Vector<IssmDouble>(femmodel->nodes->NumberOfNodes(EnthalpyAnalysisEnum));
1559 /*First create a vector to figure out what elements should be constrained*/
1560 for(int i=0;i<femmodel->elements->Size();i++){
1561 Element* element=xDynamicCast<Element*>(femmodel->elements->GetObjectByOffset(i));
1562 GetBasalConstraints(spc,element);
1563 }
1564
1565 /*Assemble and serialize*/
1566 spc->Assemble();
1567 serial_spc=spc->ToMPISerial();
1568 delete spc;
1569
1570 /*Then update basal constraints nodes accordingly*/
1571 for(int i=0;i<femmodel->elements->Size();i++){
1572 Element* element=xDynamicCast<Element*>(femmodel->elements->GetObjectByOffset(i));
1573 ApplyBasalConstraints(serial_spc,element);
1574 }
1575
1576 femmodel->UpdateConstraintsx();
1577
1578 /*Delete*/
1579 xDelete<IssmDouble>(serial_spc);
1580}/*}}}*/
1581void EnthalpyAnalysis::UpdateConstraints(FemModel* femmodel){/*{{{*/
1582
1583 bool islevelset;
1584 femmodel->parameters->FindParam(&islevelset,TransientIslevelsetEnum);
1585 if(islevelset){
1586 SetActiveNodesLSMx(femmodel);
1587 }
1588 return;
1589}/*}}}*/
Note: See TracBrowser for help on using the repository browser.