-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsns_solve_eqs.c
265 lines (222 loc) · 6.26 KB
/
nsns_solve_eqs.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
/*
// Alireza Rashti
// January 2023
*/
/* solving physics equations (elliptic solve) */
#include "nsns_solve_eqs.h"
/* setup and issue physics solve */
void nsns_solve_equation(Physics_T *const phys)
{
if (!phys) return;
FUNC_TIC
/* prepare dF/du if asked */
if (!Pcmps(P_"dF/du_prepare","none"))
prepare_dFdu(phys);
/* initialize */
physics(phys,EQ_SET_PARAMS);
physics(phys,EQ_ADD_FIELDS);
/* setup external eq functions
// NOTE: it must be after EQ_SET_PARAMS */
eq_field_update = field_update;
eq_source_update = source_update;
eq_stop_criteria = stop_criteria;
eq_analyze_solution = nsns_analyze;
/* solve */
physics(phys,EQ_SOLVE);
FUNC_TOC
}
/* stop criteria for solver, namely, if some conditions satisfied,
// it stops. one can give specific criteria according to the given field's name.
// ->return value: 0 means stop, 1 means continue to solve */
static int stop_criteria(Grid_T *const grid,const char *const name)
{
int stop = 1;
int stop_max = 1;
int stop_res = 0;
int stop_backtrack = 1;
int stop_abnormal = 1;
const double res_d = Pgetd("solve_residual");/* desired residual */
const int max_step = Pgeti("solve_max_Newton_step");
const double res_fac = Pgetd("solve_residual_factor");
const Uint npatch = grid->np;
Uint p;
/* if no step should be taken */
if (max_step == 0)
{
printf("%s equation:\n"
Pretty0"Newton solver reached maximum step number so existing ...\n",name);
fflush(stdout);
return 0;
}
/* NOTE: due to the break command, the order of ifs are important */
for (p = 0; p < npatch; ++p)
{
Patch_T *patch = grid->patch[p];
double res = patch->solving_man->Frms;/* current residual */
int solver_step = patch->solving_man->settings->solver_step;/* iteration number */
/* if nan or inf */
if (!isfinite(res))
{
stop_abnormal = 0;
break;
}
/* if this is the very first step, don't check the following */
if (solver_step == 0)
continue;
/* note: all patches have same solver_step */
if (solver_step >= max_step)
{
stop_max = 0;
break;
}
}
if (!stop_abnormal)
{
printf("%s equation:\n"
Pretty0"Newton solver got abnormal residual so exit ...\n",name);
fflush(stdout);
return stop_abnormal;
}
if (!stop_backtrack)
{
printf("%s equation:\n"
Pretty0"Newton solver increased the residual so backtrack and exist ...\n",name);
fflush(stdout);
backtrack_solutions(grid,name);
return stop_backtrack;
}
if (!stop_max)
{
printf("%s equation:\n"
Pretty0"Newton solver reached maximum step number so existing ...\n",name);
fflush(stdout);
return stop_max;
}
for (p = 0; p < npatch; ++p)
{
Patch_T *patch = grid->patch[p];
double res = patch->solving_man->Frms;/* current residual */
double res_i = patch->solving_man->settings->Frms_i;/* initial residual */
/* since one of them is enough to continue */
if (res > res_d && res > res_fac*res_i)
{
stop_res = 1;
break;
}
}
if (!stop_res)
{
printf("%s equation:\n"
Pretty0"Newton solver satisfies demanding residual so existing ...\n",name);
fflush(stdout);
return stop_res;
}
return stop;
}
/* how update sourc */
static void source_update(Grid_T *const grid,const char *const name)
{
Physics_T *const nsns = init_physics(0,NSNS);
nsns->grid = grid;
physics(nsns,ADM_UPDATE_AConfIJ);
nsns->grid = 0;/* don't free grid */
free_physics(nsns);
UNUSED(name);
}
/* how update field */
static void field_update(Patch_T *const patch,const char *const name)
{
if (!strcmp(name,"psi"))
{
partial_derivative_regex(patch,"^dpsi_D.$,^ddpsi_D.D.$");
}
else if (!strcmp(name,"alphaPsi"))
{
partial_derivative_regex(patch,"^dalphaPsi_D.$,^ddalphaPsi_D.D.$");
}
else if (!strcmp(name,"B0_U0"))
{
partial_derivative_regex(patch,"^dB0_U0D.$,^ddB0_U0D.D.$");
adm_update_beta_U0(patch);
}
else if (!strcmp(name,"B0_U1"))
{
partial_derivative_regex(patch,"^dB0_U1D.$,^ddB0_U1D.D.$");
adm_update_beta_U1(patch);
}
else if (!strcmp(name,"B0_U2"))
{
partial_derivative_regex(patch,"^dB0_U2D.$,^ddB0_U2D.D.$");
adm_update_beta_U2(patch);
}
else if (!strcmp(name,"phi1"))
{
partial_derivative_regex(patch,"^dphi_D.$,^ddphi_D.D.$");
}
else if (!strcmp(name,"phi2"))
{
partial_derivative_regex(patch,"^dphi_D.$,^ddphi_D.D.$");
}
else
Error0(NO_OPTION);
}
/* restore fields to the last solution */
static void backtrack_solutions(Grid_T *const grid,const char *const name)
{
const Uint npatch = grid->np;
Uint p;
OpenMP_Patch_Pragma(omp parallel for)
for (p = 0; p < npatch; ++p)
{
Patch_T *patch = grid->patch[p];
Field_T *f = patch->fields[Ind(name)];
double *v = f->v;
const double *last_sol = patch->solving_man->settings->last_sol;
Uint ijk;
free_coeffs(f);
for(ijk = 0; ijk < patch->nn; ++ijk)
v[ijk] = last_sol[ijk];
eq_field_update(patch,name);
}
}
/* prepare required dF/du's to speed up */
static void prepare_dFdu(Physics_T *const phys)
{
FUNC_TIC
/* get types */
const char *const param = Pgets(P_"dF/du_prepare");
char **dFdu = read_separated_items_in_string(param,',');
printf(Pretty0"compute '%s'\n",param);
/* collect all patches don't have Jacobian already */
Grid_T *grid = mygrid(phys,".*");
Patch_T **patches = 0;
Uint Np = 0;/* number of patches */
FOR_ALL_p(grid->np)
{
Patch_T *patch = grid->patch[p];
if (!patch->solving_man || !patch->solving_man->jacobian)
{
patches = realloc(patches,(Np+2)*sizeof(*patches));
IsNull(patches);
patches[Np] = patch;
patches[Np+1] = 0;
++Np;
if (!patch->solving_man)
{
patch->solving_man = calloc(1,sizeof(*patch->solving_man));
IsNull(patch->solving_man);
}
}
}
/* compute jacobian in each patch (very expensive) */
OpenMP_Patch_Pragma(omp parallel for)
FOR_ALL_p(Np)
{
Patch_T *patch = patches[p];
prepare_Js_jacobian_eq(patch,(const char * const *)dFdu);
}
/* free */
free_2d(dFdu);
Free(patches);
FUNC_TOC
}