-
Notifications
You must be signed in to change notification settings - Fork 0
/
twophase.c
239 lines (193 loc) · 9 KB
/
twophase.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
#include "parameters.h"
#include "dimensionalisablefield.h"
#include "twophase.h"
#include "util.h"
#include "eos.h"
#include "rheologicalfront.h"
static PetscErrorCode set_rheological_front_mantle_properties( Ctx *, RheologicalFront *, PetscInt, Vec * );
PetscErrorCode set_Mliq( Ctx *E )
{
PetscErrorCode ierr;
Atmosphere *A = &E->atmosphere;
Solution const *S = &E->solution;
Mesh const *M = &E->mesh;
Vec mass_s;
PetscFunctionBeginUser;
// Mliq = sum[ phi*dm ]
ierr = VecDuplicate( S->phi_s, &mass_s ); CHKERRQ(ierr);
ierr = VecCopy( S->phi_s, mass_s ); CHKERRQ(ierr);
ierr = VecPointwiseMult( mass_s, mass_s, M->mass_s ); CHKERRQ(ierr);
ierr = VecSum( mass_s, &A->Mliq );
VecDestroy( &mass_s );
PetscFunctionReturn(0);
}
PetscErrorCode set_Msol( Ctx *E )
{
PetscErrorCode ierr;
Atmosphere *A = &E->atmosphere;
Solution const *S = &E->solution;
Mesh const *M = &E->mesh;
Vec mass_s;
PetscFunctionBeginUser;
// Msol = sum[ (1-phi)*dm ]
ierr = VecDuplicate( S->phi_s, &mass_s ); CHKERRQ(ierr);
ierr = VecCopy( S->phi_s, mass_s ); CHKERRQ(ierr);
ierr = VecScale( mass_s, -1.0 ); CHKERRQ(ierr);
ierr = VecShift( mass_s, 1.0 ); CHKERRQ(ierr);
ierr = VecPointwiseMult( mass_s, mass_s, M->mass_s ); CHKERRQ(ierr);
ierr = VecSum( mass_s, &A->Msol );
VecDestroy( &mass_s );
PetscFunctionReturn(0);
}
PetscErrorCode set_dMliqdt( Ctx *E )
{
PetscErrorCode ierr;
PetscInt i,ilo_s,ihi_s,w_s;
DM da_s = E->da_s;
Atmosphere *A = &E->atmosphere;
Solution *S = &E->solution;
Mesh *M = &E->mesh;
Parameters P = E->parameters;
Vec result_s;
PetscScalar *arr_result_s;
const PetscScalar *arr_dSdt_s, *arr_phi_s, *arr_mass_s, *arr_pres, *arr_S;
EOSEvalData eos_eval;
PetscFunctionBeginUser;
ierr = DMDAGetCorners(da_s,&ilo_s,0,0,&w_s,0,0);CHKERRQ(ierr);
ihi_s = ilo_s + w_s;
ierr = VecDuplicate( S->dSdt_s, &result_s); CHKERRQ(ierr);
ierr = VecCopy( S->dSdt_s, result_s ); CHKERRQ(ierr);
ierr = DMDAVecGetArrayRead(da_s,M->mass_s,&arr_mass_s);CHKERRQ(ierr);
ierr = DMDAVecGetArrayRead(da_s,M->pressure_s,&arr_pres);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da_s,S->dSdt_s,&arr_dSdt_s);CHKERRQ(ierr);
ierr = DMDAVecGetArrayRead(da_s,S->phi_s,&arr_phi_s);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da_s,result_s,&arr_result_s);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da_s,S->S_s,&arr_S);CHKERRQ(ierr);
#if defined(PETSC_USE_DEBUG)
{
PetscBool is_composite;
ierr = EOSCheckType(P->eos,SPIDER_EOS_COMPOSITE,&is_composite);CHKERRQ(ierr);
if (!is_composite) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_OUTOFRANGE,"Only defined for a composite EOS");
}
#endif
for(i=ilo_s; i<ihi_s; ++i){
ierr = EOSEval( P->eos, arr_pres[i], arr_S[i], &eos_eval );CHKERRQ(ierr);
arr_result_s[i] = arr_dSdt_s[i] * arr_mass_s[i];
arr_result_s[i] /= eos_eval.fusion;
/* with smoothing */
//if (arr_phi_s[i] > 0.5){
// arr_result_s[i] *= ( 1.0 - arr_fwtl_s[i] );
//}
//else if (arr_phi_s[i] <=0.5){
// arr_result_s[i] *= arr_fwts_s[i];
//}
// generally had more luck without smoothing
/* no smoothing approach */
if (arr_phi_s[i] <= 0.0){
arr_result_s[i] = 0.0;
}
else if (arr_phi_s[i] >=1.0){
arr_result_s[i] = 0.0;
}
}
ierr = DMDAVecRestoreArrayRead(da_s,M->mass_s,&arr_mass_s); CHKERRQ(ierr);
ierr = DMDAVecRestoreArray(da_s,S->dSdt_s, &arr_dSdt_s); CHKERRQ(ierr);
ierr = DMDAVecRestoreArrayRead(da_s,S->phi_s,&arr_phi_s); CHKERRQ(ierr);
ierr = DMDAVecRestoreArray(da_s,result_s, &arr_result_s); CHKERRQ(ierr);
ierr = VecSum( result_s, &A->dMliqdt ); CHKERRQ(ierr);
VecDestroy( &result_s );
PetscFunctionReturn(0);
}
/* the next two functions relating to the rheological front
require Ctx, but this is outside of the scope of rheologicalfront.h.
Since ctx.h must know about rheologicalfront.h, to avoid a circular
dependency these two functions live here */
PetscErrorCode set_rheological_front( Ctx *E )
{
PetscErrorCode ierr;
DM da_s = E->da_s;
DM da_b = E->da_b;
Parameters P = E->parameters;
Solution *S = &E->solution;
RheologicalFront *Rfp = &E->rheological_front_phi;
RheologicalFront *Rfd = &E->rheological_front_dynamic;
PetscInt numpts_s, index;
Vec mask_s;
PetscScalar phi_global;
PetscFunctionBeginUser;
ierr = DMDAGetInfo(da_s,NULL,&numpts_s,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
/* critical melt fraction */
index = get_crossover_index( da_s, S->phi_s, P->phi_critical, 0 );
ierr = make_vec_mask( da_s, index, &mask_s );CHKERRQ(ierr);
ierr = set_rheological_front_mantle_properties( E, Rfp, index, &mask_s );CHKERRQ(ierr);
ierr = VecDestroy( &mask_s );CHKERRQ(ierr);
/* inviscid to viscous regime crossover */
index = get_crossover_index( da_b, S->regime, 1.5, 1 );
ierr = make_vec_mask( da_s, index, &mask_s );CHKERRQ(ierr);
ierr = set_rheological_front_mantle_properties( E, Rfd, index, &mask_s );CHKERRQ(ierr);
ierr = VecDestroy( &mask_s );CHKERRQ(ierr);
/* global melt fraction */
ierr = make_vec_mask( da_s, numpts_s, &mask_s );CHKERRQ(ierr);
ierr = average_by_mass_staggered( E, S->phi_s, &mask_s, &phi_global );CHKERRQ(ierr);
/* store to both rheological structs */
Rfp->phi_global = phi_global;
Rfd->phi_global = phi_global;
ierr = VecDestroy( &mask_s );CHKERRQ(ierr);
PetscFunctionReturn(0);
}
static PetscErrorCode set_rheological_front_mantle_properties( Ctx *E, RheologicalFront *Rf, PetscInt index, Vec * mask_ptr_s )
{
PetscErrorCode ierr;
const DM da_s = E->da_s;
const Mesh *M = &E->mesh;
const Parameters P = E->parameters;
const Solution *S = &E->solution;
PetscScalar phi, radius, pressure, temperature;
PetscInt numpts_s, index_above, index_below;
PetscFunctionBeginUser;
ierr = DMDAGetInfo(da_s,NULL,&numpts_s,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
/* rheological front coordinates */
Rf->mesh_index = index;
ierr = VecGetValues(M->radius_b,1,&index,&radius);CHKERRQ(ierr);
Rf->depth = P->radius - radius;
ierr = VecGetValues(M->pressure_b,1,&index,&pressure);CHKERRQ(ierr);
Rf->pressure = pressure;
ierr = VecGetValues(S->temp,1,&index,&temperature);CHKERRQ(ierr);
Rf->temperature = temperature;
/* mantle properties in magma ocean (above rheological front) */
/* middle of layer */
index_above = index/2; /* integer algebra */
ierr = VecGetValues(S->phi,1,&index_above,&phi);CHKERRQ(ierr);
Rf->above_middle.phi = phi;
ierr = VecGetValues(M->radius_b,1,&index_above,&radius);CHKERRQ(ierr);
Rf->above_middle.depth = P->radius - radius;
ierr = VecGetValues(M->pressure_b,1,&index_above,&pressure);CHKERRQ(ierr);
Rf->above_middle.pressure = pressure;
ierr = VecGetValues(S->temp,1,&index_above,&temperature);CHKERRQ(ierr);
Rf->above_middle.temperature = temperature;
/* average by mass */
ierr = average_by_mass_staggered( E, S->phi_s, mask_ptr_s, &Rf->above_mass_avg.phi); CHKERRQ(ierr);
ierr = average_by_mass_staggered( E, M->radius_s, mask_ptr_s, &Rf->above_mass_avg.depth); CHKERRQ(ierr);
Rf->above_mass_avg.depth = P->radius - Rf->above_mass_avg.depth;
ierr = average_by_mass_staggered( E, M->pressure_s, mask_ptr_s, &Rf->above_mass_avg.pressure); CHKERRQ(ierr);
ierr = average_by_mass_staggered( E, S->temp_s, mask_ptr_s, &Rf->above_mass_avg.temperature); CHKERRQ(ierr);
// need to initialise values?
/* middle of layer */
index_below = (numpts_s - index)/2 + index;
ierr = VecGetValues(S->phi,1,&index_below,&phi);CHKERRQ(ierr);
Rf->below_middle.phi = phi;
ierr = VecGetValues(M->radius_b,1,&index_below,&radius);CHKERRQ(ierr);
Rf->below_middle.depth = P->radius - radius;
ierr = VecGetValues(M->pressure_b,1,&index_below,&pressure);CHKERRQ(ierr);
Rf->below_middle.pressure = pressure;
ierr = VecGetValues(S->temp,1,&index_below,&temperature);CHKERRQ(ierr);
Rf->below_middle.temperature = temperature;
/* average by mass */
ierr = invert_vec_mask( mask_ptr_s ); CHKERRQ(ierr);
ierr = average_by_mass_staggered( E, S->phi_s, mask_ptr_s, &Rf->below_mass_avg.phi); CHKERRQ(ierr);
ierr = average_by_mass_staggered( E, M->radius_s, mask_ptr_s, &Rf->below_mass_avg.depth); CHKERRQ(ierr);
Rf->below_mass_avg.depth = P->radius - Rf->below_mass_avg.depth;
ierr = average_by_mass_staggered( E, M->pressure_s, mask_ptr_s, &Rf->below_mass_avg.pressure); CHKERRQ(ierr);
ierr = average_by_mass_staggered( E, S->temp_s, mask_ptr_s, &Rf->below_mass_avg.temperature); CHKERRQ(ierr);
PetscFunctionReturn(0);
}