-
Notifications
You must be signed in to change notification settings - Fork 0
/
eos_adamswilliamson.c
401 lines (297 loc) · 14.3 KB
/
eos_adamswilliamson.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "eos_adamswilliamson.h"
#include "util.h"
/* Prototypes for local functions used in EOS interface functions */
static PetscErrorCode EOSAdamsWilliamson_GetRho(const data_EOSAdamsWilliamson*,PetscScalar,PetscScalar,PetscScalar*);
//static PetscErrorCode EOSAdamsWilliamson_GetRadiusFromPressure( const data_EOSAdamsWilliamson*, PetscScalar, PetscScalar * );
static PetscErrorCode EOSAdamsWilliamson_GetPressureFromRadius( const data_EOSAdamsWilliamson*, PetscScalar, PetscScalar * );
static PetscErrorCode EOSAdamsWilliamson_GetMassWithinRadius( const data_EOSAdamsWilliamson*, PetscScalar, PetscScalar *);
static PetscErrorCode EOSAdamsWilliamson_GetMassWithinShell( const data_EOSAdamsWilliamson*, PetscScalar, PetscScalar, PetscScalar *);
static PetscErrorCode EOSAdamsWilliamson_GetMassCoordinateAverageRho( const data_EOSAdamsWilliamson*, PetscScalar * );
static PetscErrorCode EOSAdamsWilliamson_GetMassElement( const data_EOSAdamsWilliamson*, PetscScalar, PetscScalar * );
/* EOS interface functions */
static PetscErrorCode EOSEval_AdamsWilliamson(EOS eos, PetscScalar P, PetscScalar S, EOSEvalData *eval)
{
PetscErrorCode ierr;
data_EOSAdamsWilliamson *adams = (data_EOSAdamsWilliamson*) eos->impl_data;
(void) S; //unused
PetscFunctionBegin;
eval->P = P;
ierr = EOSAdamsWilliamson_GetRho( adams, P, S, &eval->rho );CHKERRQ(ierr);
eval->phase_fraction = 1.0; // by definition, since only one phase
PetscFunctionReturn(0);
}
static PetscErrorCode EOSDestroy_AdamsWilliamson(EOS eos)
{
PetscErrorCode ierr;
PetscFunctionBegin;
ierr = PetscFree(eos->impl_data);CHKERRQ(ierr);
eos->impl_data = NULL;
PetscFunctionReturn(0);
}
PetscErrorCode EOSSetUpFromOptions_AdamsWilliamson(EOS eos, const char *prefix, const FundamentalConstants FC, const ScalingConstants SC)
{
PetscErrorCode ierr;
char buf[PETSC_MAX_PATH_LEN]; /* max size */
data_EOSAdamsWilliamson *data = (data_EOSAdamsWilliamson*) eos->impl_data;
PetscFunctionBegin;
(void) FC; // unused
/* radius of planet (m) */
ierr = PetscOptionsGetPositiveScalar("-radius",&data->radius,6371000.0,NULL);CHKERRQ(ierr); // m
data->radius /= SC->RADIUS;
/* core radius relative to physical radius i.e. radius
therefore, scaled (code) core radius is P->coresize * P->radius
and actual physical core radius is P->coresize * P->radius * SC->RADIUS */
ierr = PetscOptionsGetPositiveScalar("-coresize",&data->radius_core,0.55,NULL);CHKERRQ(ierr); // Earth core radius
/* already non-dimensonal, but more convenient to have core radius directly */
data->radius_core *= data->radius;
/* gravity (m/s^2), must be negative */
data->gravity = -10.0;
ierr = PetscOptionsGetScalar(NULL,NULL,"-gravity",&data->gravity,NULL);CHKERRQ(ierr);
data->gravity /= SC->GRAVITY;
/* surface density (kg/m^3) */
ierr = PetscSNPrintf(buf,sizeof(buf),"%s%s%s","-",prefix,"_rhos");CHKERRQ(ierr);
ierr = PetscOptionsGetPositiveScalar(buf,&data->density_surface,4078.95095544,NULL);CHKERRQ(ierr);
data->density_surface /= SC->DENSITY;
/* parameter (1/m) */
ierr = PetscSNPrintf(buf,sizeof(buf),"%s%s%s","-",prefix,"_beta");CHKERRQ(ierr);
ierr = PetscOptionsGetPositiveScalar(buf,&data->beta,1.1115348931000002e-07,NULL);CHKERRQ(ierr);
data->beta *= SC->RADIUS;
/* we need the average density for the mass coordinate to radius mapping */
ierr = EOSAdamsWilliamson_GetMassCoordinateAverageRho( data, &data->density_average );CHKERRQ(ierr);
PetscFunctionReturn(0);
}
/* Creation Function */
PetscErrorCode EOSCreate_AdamsWilliamson(EOS eos) {
PetscErrorCode ierr;
PetscFunctionBeginUser;
ierr = PetscMalloc1(1, (data_EOSAdamsWilliamson**) (&eos->impl_data));CHKERRQ(ierr);
eos->eval = EOSEval_AdamsWilliamson;
eos->destroy = EOSDestroy_AdamsWilliamson;
eos->setupfromoptions = EOSSetUpFromOptions_AdamsWilliamson;
PetscFunctionReturn(0);
}
/* Helper functions */
static PetscErrorCode EOSAdamsWilliamson_GetRho( const data_EOSAdamsWilliamson *adams,PetscScalar P,PetscScalar S, PetscScalar *rho_ptr)
{
/* Adams-Williamson density is a simple function of depth (radius)
Sketch derivation:
dP/dr = dP/drho * drho/dr = -rho g
dP/drho \sim (dP/drho)_s (adiabatic)
drho/dr = -rho g / Si
then integrate to give the form rho(r) = k * exp(-(g*r)/c)
(g is positive)
apply the limit that rho = rhos at r=R
gives:
rho(z) = rhos * exp( beta * z )
where z = R-r
this is arguably the simplest relation to get rho directly from r, but other
EOSs can be envisaged */
PetscScalar rho;
PetscFunctionBeginUser;
(void) S;
/* using pressure, expression is simpler than sketch derivation above */
rho = adams->density_surface - P * adams->beta / adams->gravity;
*rho_ptr = rho;
PetscFunctionReturn(0);
}
/* Mass coordinate mapping and static structure */
static PetscErrorCode EOSAdamsWilliamson_GetPressureFromRadius( const data_EOSAdamsWilliamson *adams, PetscScalar R, PetscScalar *P_ptr )
{
PetscScalar P;
PetscFunctionBeginUser;
P = -adams->density_surface * adams->gravity / adams->beta;
P *= PetscExpScalar( adams->beta*(adams->radius-R) ) - 1.0;
*P_ptr = P;
PetscFunctionReturn(0);
}
PetscErrorCode EOSAdamsWilliamsonGetPressureFromRadius( const EOS eos, PetscScalar R, PetscScalar *P_ptr )
{
/* as above, but global in scope to allow taking an EOS argument. Otherwise, pretty much all functions
local to this EOS have to be converted from static to global which didn't seem like a good idea */
PetscErrorCode ierr;
PetscBool is_adams;
data_EOSAdamsWilliamson *adams = (data_EOSAdamsWilliamson*) eos->impl_data;
PetscFunctionBeginUser;
ierr = EOSCheckType(eos,SPIDER_EOS_ADAMSWILLIAMSON,&is_adams);CHKERRQ(ierr);
if (!is_adams) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_OUTOFRANGE,"Must be called on an Adams-Williamson EOS");
ierr = EOSAdamsWilliamson_GetPressureFromRadius( adams, R, P_ptr );CHKERRQ(ierr);
PetscFunctionReturn(0);
}
PetscErrorCode EOSAdamsWilliamsonGetPressureGradientFromRadius( const EOS eos, PetscScalar R, PetscScalar *dPdr_ptr )
{
PetscErrorCode ierr;
PetscBool is_adams;
PetscScalar P, dPdr, S=0.0;
data_EOSAdamsWilliamson *adams = (data_EOSAdamsWilliamson*) eos->impl_data;
PetscFunctionBeginUser;
ierr = EOSCheckType(eos,SPIDER_EOS_ADAMSWILLIAMSON,&is_adams);CHKERRQ(ierr);
if (!is_adams) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_OUTOFRANGE,"Must be called on an Adams-Williamson EOS");
ierr = EOSAdamsWilliamson_GetPressureFromRadius( adams, R, &P); CHKERRQ(ierr);
ierr = EOSAdamsWilliamson_GetRho( adams, P, S, &dPdr );CHKERRQ(ierr);CHKERRQ(ierr);
dPdr *= adams->gravity;
*dPdr_ptr = dPdr;
PetscFunctionReturn(0);
}
static PetscErrorCode EOSAdamsWilliamson_GetMassWithinRadius( const data_EOSAdamsWilliamson *adams, PetscScalar R, PetscScalar *mass_ptr)
{
/* return integral from 0 to r of r^2 * rho dr */
PetscErrorCode ierr;
PetscScalar mass, P, rho; /* note mass without 4*pi scaling, as convention in SPIDER */
PetscScalar const beta = adams->beta;
PetscScalar S = 0.0; // S not used in this function;
PetscFunctionBeginUser;
mass = -2.0/PetscPowScalar(beta,3) - PetscPowScalar(R,2)/beta -2*R/PetscPowScalar(beta,2);
ierr = EOSAdamsWilliamson_GetPressureFromRadius( adams, R, &P );CHKERRQ(ierr);
ierr = EOSAdamsWilliamson_GetRho( adams, P, S, &rho );CHKERRQ(ierr);
mass *= rho;
*mass_ptr = mass;
PetscFunctionReturn(0);
}
static PetscErrorCode EOSAdamsWilliamson_GetMassWithinShell( const data_EOSAdamsWilliamson *adams, PetscScalar Rout, PetscScalar Rin, PetscScalar *mass_ptr)
{
PetscErrorCode ierr;
PetscScalar massout, massin; /* note mass without 4*pi scaling, as convention in SPIDER */
PetscFunctionBeginUser;
ierr = EOSAdamsWilliamson_GetMassWithinRadius( adams, Rout, &massout );CHKERRQ(ierr);
ierr = EOSAdamsWilliamson_GetMassWithinRadius( adams, Rin, &massin );CHKERRQ(ierr);
*mass_ptr = massout - massin;
PetscFunctionReturn(0);
}
PetscErrorCode EOSAdamsWilliamsonGetMassWithinShell( const EOS eos, PetscScalar Rout, PetscScalar Rin, PetscScalar *mass_ptr)
{
PetscErrorCode ierr;
PetscBool is_adams;
data_EOSAdamsWilliamson *adams = (data_EOSAdamsWilliamson*) eos->impl_data;
PetscFunctionBeginUser;
ierr = EOSCheckType(eos,SPIDER_EOS_ADAMSWILLIAMSON,&is_adams);CHKERRQ(ierr);
if (!is_adams) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_OUTOFRANGE,"Must be called on an Adams-Williamson EOS");
ierr = EOSAdamsWilliamson_GetMassWithinShell( adams, Rout, Rin, mass_ptr );CHKERRQ(ierr);
PetscFunctionReturn(0);
}
static PetscErrorCode EOSAdamsWilliamson_GetMassElement( const data_EOSAdamsWilliamson *adams, PetscScalar R, PetscScalar *mass_ptr )
{
PetscErrorCode ierr;
PetscScalar mass, P, S=0.0; // S unused
PetscFunctionBeginUser;
ierr = EOSAdamsWilliamson_GetPressureFromRadius( adams, R, &P );CHKERRQ(ierr);
ierr = EOSAdamsWilliamson_GetRho( adams, P, S, &mass );CHKERRQ(ierr);
mass *= PetscPowScalar( R, 2.0 );
*mass_ptr = mass;
PetscFunctionReturn(0);
}
static PetscErrorCode EOSAdamsWilliamson_GetMassCoordinateAverageRho( const data_EOSAdamsWilliamson *adams, PetscScalar *rho_ptr )
{
PetscErrorCode ierr;
PetscScalar rho, mass;
PetscFunctionBeginUser;
/* radius_core < r < radius: mass within mantle */
ierr = EOSAdamsWilliamson_GetMassWithinShell( adams, adams->radius, adams->radius_core, &mass ); CHKERRQ(ierr);
/* use the same range applied to construct the mass coordinate mesh
radius_core < xi < radius
hence rho is the actual average density of the mantle */
rho = mass * 3.0 / ( PetscPowScalar( adams->radius, 3.0 ) - PetscPowScalar( adams->radius_core, 3.0) );
*rho_ptr = rho;
PetscFunctionReturn(0);
}
PetscErrorCode EOSAdamsWilliamson_ObjectiveFunctionRadius( SNES snes, Vec x, Vec f, void *ptr )
{
/* uses definition of mass coordinates */
PetscErrorCode ierr;
const PetscScalar *xx, *xi_b, *xi_s;
PetscScalar *ff, xi;
Ctx *E = (Ctx*) ptr;
Parameters const P = E->parameters;
Mesh const *M = &E->mesh;
PetscInt i,numpts_b,numpts_s;
EOS eos = P->eos_mesh;
data_EOSAdamsWilliamson *adams = (data_EOSAdamsWilliamson*) eos->impl_data;
PetscFunctionBeginUser;
/* replace with composite DM? */
/* below is simple loop over all nodes (basic and staggered), but will clearly break for parallel */
ierr = DMDAGetInfo(E->da_b,NULL,&numpts_b,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
ierr = DMDAGetInfo(E->da_s,NULL,&numpts_s,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr); /* initial guess of r */
/* below instead should use composite DM */
ierr = VecGetArrayRead(M->xi_b,&xi_b);CHKERRQ(ierr); /* target mass coordinate */
ierr = VecGetArrayRead(M->xi_s,&xi_s);CHKERRQ(ierr);
ierr = VecGetArray(f,&ff);CHKERRQ(ierr); /* residual function */
for(i=0; i< numpts_b+numpts_s; ++i){
/* mass within radial shell of mantle */
ierr = EOSAdamsWilliamson_GetMassWithinShell( adams, xx[i], adams->radius_core, &ff[i] ); CHKERRQ(ierr);
/* get mass coordinate */
if (i<numpts_b){
xi = xi_b[i]; // basic nodes
}
else{
xi = xi_s[i-numpts_b]; // staggered nodes
}
ff[i] *= 3.0 / adams->density_average;
ff[i] += PetscPowScalar(adams->radius_core,3.0);
ff[i] = PetscPowScalar(ff[i],1.0/3.0);
ff[i] -= xi;
}
ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
ierr = VecRestoreArrayRead(M->xi_b,&xi_b);CHKERRQ(ierr);
ierr = VecRestoreArrayRead(M->xi_s,&xi_s);CHKERRQ(ierr);
ierr = VecRestoreArray(f,&ff);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
PetscErrorCode EOSAdamsWilliamson_JacobianRadius( SNES snes, Vec x, Mat jac, Mat B, void *ptr)
{
PetscErrorCode ierr;
const PetscScalar *xx;
Ctx *E = (Ctx*) ptr;
Parameters const P = E->parameters;
PetscInt i,numpts_b,numpts_s;
Vec diag;
PetscScalar *arr_diag, mass;
EOS eos = P->eos_mesh;
data_EOSAdamsWilliamson *adams = (data_EOSAdamsWilliamson*) eos->impl_data;
PetscFunctionBeginUser;
ierr = DMDAGetInfo(E->da_b,NULL,&numpts_b,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
ierr = DMDAGetInfo(E->da_s,NULL,&numpts_s,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
/* work vector for diagonal of Jacobian */
ierr = VecCreate( PETSC_COMM_WORLD, &diag );CHKERRQ(ierr);
ierr = VecSetSizes( diag, PETSC_DECIDE, numpts_b+numpts_s );CHKERRQ(ierr);
ierr = VecSetFromOptions(diag);CHKERRQ(ierr);
ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr);
ierr = VecGetArray(diag,&arr_diag);CHKERRQ(ierr);
/* set diagonal */
for(i=0; i < numpts_b+numpts_s; ++i){
ierr = EOSAdamsWilliamson_GetMassElement( adams, xx[i], &arr_diag[i] );CHKERRQ(ierr);
arr_diag[i] *= 3.0 / adams->density_average;
ierr = EOSAdamsWilliamson_GetMassWithinShell( adams, xx[i], adams->radius_core, &mass );CHKERRQ(ierr);
mass *= (3.0 / adams->density_average);
mass += PetscPowScalar( adams->radius_core, 3.0);
mass = (1.0/3.0) * PetscPowScalar( mass, -2.0/3.0 );
arr_diag[i] *= mass;
}
ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr);
ierr = VecRestoreArray(diag,&arr_diag);CHKERRQ(ierr);
ierr = MatDiagonalSet( B, diag, INSERT_VALUES );CHKERRQ(ierr);
/* Assemble matrix */
MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
if (jac != B) {
MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);
MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);
}
ierr = VecDestroy(&diag);
PetscFunctionReturn(0);
}
PetscErrorCode EOSAdamsWilliamsonMassCoordinateSpatialDerivative( const EOS eos, PetscScalar R, PetscScalar xi, PetscScalar *dxidr_ptr )
{
PetscErrorCode ierr;
PetscBool is_composite;
PetscScalar dxidr, P, S=0.0; // S unused
data_EOSAdamsWilliamson *adams = (data_EOSAdamsWilliamson*) eos->impl_data;
PetscFunctionBeginUser;
ierr = EOSCheckType(eos,SPIDER_EOS_ADAMSWILLIAMSON,&is_composite);CHKERRQ(ierr);
if (!is_composite) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_OUTOFRANGE,"Must be called on an Adams-Williamson EOS");
ierr = EOSAdamsWilliamson_GetPressureFromRadius( adams, R, &P );CHKERRQ(ierr);
ierr = EOSAdamsWilliamson_GetRho( adams, P, S, &dxidr );CHKERRQ(ierr);
dxidr /= adams->density_average;
dxidr *= (R/xi) * (R/xi);
*dxidr_ptr = dxidr;
PetscFunctionReturn(0);
}