forked from tan2/DynEarthSol-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfields.cxx
478 lines (395 loc) · 13.4 KB
/
fields.cxx
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#include <iostream>
#include "constants.hpp"
#include "parameters.hpp"
#include "bc.hpp"
#include "matprops.hpp"
#include "utils.hpp"
#include "fields.hpp"
void allocate_variables(const Param ¶m, Variables& var)
{
const int n = var.nnode;
const int e = var.nelem;
var.volume = new double_vec(e);
var.volume_old = new double_vec(e);
var.volume_n = new double_vec(n);
var.mass = new double_vec(n);
var.tmass = new double_vec(n);
var.edvoldt = new double_vec(e);
{
// these fields are reallocated during remeshing interpolation
var.temperature = new double_vec(n);
var.coord0 = new array_t(n);
var.plstrain = new double_vec(e);
var.delta_plstrain = new double_vec(e);
var.vel = new array_t(n, 0);
var.strain = new tensor_t(e, 0);
var.stress = new tensor_t(e, 0);
var.stressyy = new double_vec(e, 0);
}
var.ntmp= new double_vec(n);
var.dpressure= new double_vec(e);
var.force = new array_t(n, 0);
var.strain_rate = new tensor_t(e, 0);
var.shpdx = new shapefn(e);
if (NDIMS == 3) var.shpdy = new shapefn(e);
var.shpdz = new shapefn(e);
var.mat = new MatProps(param, var);
}
void reallocate_variables(const Param& param, Variables& var)
{
const int n = var.nnode;
const int e = var.nelem;
delete var.volume;
delete var.volume_old;
delete var.volume_n;
var.volume = new double_vec(e);
var.volume_old = new double_vec(e);
var.volume_n = new double_vec(n);
delete var.mass;
delete var.tmass;
var.mass = new double_vec(n);
var.tmass = new double_vec(n);
delete var.edvoldt;
var.edvoldt = new double_vec(e);
delete var.ntmp;
var.ntmp = new double_vec(n);
delete var.dpressure;
var.dpressure = new double_vec(e);
delete var.force;
var.force = new array_t(n, 0);
delete var.strain_rate;
var.strain_rate = new tensor_t(e, 0);
delete var.shpdx;
delete var.shpdz;
var.shpdx = new shapefn(e);
if (NDIMS == 3) {
delete var.shpdy;
var.shpdy = new shapefn(e);
}
var.shpdz = new shapefn(e);
delete var.mat;
var.mat = new MatProps(param, var);
}
void update_temperature(const Param ¶m, const Variables &var,
double_vec &temperature, double_vec &tdot)
{
tdot.assign(var.nnode, 0);
class ElemFunc_temperature : public ElemFunc
{
private:
const Variables &var;
const double_vec &temperature;
double_vec ⃛
public:
ElemFunc_temperature(const Variables &var, const double_vec &temperature, double_vec &tdot) :
var(var), temperature(temperature), tdot(tdot) {};
void operator()(int e)
{
// diffusion matrix
double D[NODES_PER_ELEM][NODES_PER_ELEM];
const int *conn = (*var.connectivity)[e];
double kv = var.mat->k(e) * (*var.volume)[e]; // thermal conductivity * volumn
const double *shpdx = (*var.shpdx)[e];
#ifdef THREED
const double *shpdy = (*var.shpdy)[e];
#endif
const double *shpdz = (*var.shpdz)[e];
for (int i=0; i<NODES_PER_ELEM; ++i) {
for (int j=0; j<NODES_PER_ELEM; ++j) {
#ifdef THREED
D[i][j] = (shpdx[i] * shpdx[j] +
shpdy[i] * shpdy[j] +
shpdz[i] * shpdz[j]);
#else
D[i][j] = (shpdx[i] * shpdx[j] +
shpdz[i] * shpdz[j]);
#endif
}
}
for (int i=0; i<NODES_PER_ELEM; ++i) {
double diffusion = 0;
for (int j=0; j<NODES_PER_ELEM; ++j)
diffusion += D[i][j] * temperature[conn[j]];
tdot[conn[i]] += diffusion * kv;
}
}
} elemf(var, temperature, tdot);
loop_all_elem(var.egroups, elemf);
// Combining temperature update and bc in the same loop for efficiency,
// since only the top boundary has Dirichlet bc, and all the other boundaries
// have no heat flux bc.
#pragma omp parallel for default(none) \
shared(var, param, tdot, temperature)
for (int n=0; n<var.nnode; ++n) {
if ((*var.bcflag)[n] & BOUNDZ1)
temperature[n] = param.bc.surface_temperature;
else
temperature[n] -= tdot[n] * var.dt / (*var.tmass)[n];
}
}
void update_strain_rate(const Variables& var, tensor_t& strain_rate)
{
double *v[NODES_PER_ELEM];
#pragma omp parallel for default(none) \
shared(var, strain_rate) private(v)
for (int e=0; e<var.nelem; ++e) {
const int *conn = (*var.connectivity)[e];
const double *shpdx = (*var.shpdx)[e];
const double *shpdz = (*var.shpdz)[e];
double *s = (*var.strain_rate)[e];
for (int i=0; i<NODES_PER_ELEM; ++i)
v[i] = (*var.vel)[conn[i]];
// XX component
int n = 0;
s[n] = 0;
for (int i=0; i<NODES_PER_ELEM; ++i)
s[n] += v[i][0] * shpdx[i];
#ifdef THREED
const double *shpdy = (*var.shpdy)[e];
// YY component
n = 1;
s[n] = 0;
for (int i=0; i<NODES_PER_ELEM; ++i)
s[n] += v[i][1] * shpdy[i];
#endif
// ZZ component
#ifdef THREED
n = 2;
#else
n = 1;
#endif
s[n] = 0;
for (int i=0; i<NODES_PER_ELEM; ++i)
s[n] += v[i][NDIMS-1] * shpdz[i];
#ifdef THREED
// XY component
n = 3;
s[n] = 0;
for (int i=0; i<NODES_PER_ELEM; ++i)
s[n] += 0.5 * (v[i][0] * shpdy[i] + v[i][1] * shpdx[i]);
#endif
// XZ component
#ifdef THREED
n = 4;
#else
n = 2;
#endif
s[n] = 0;
for (int i=0; i<NODES_PER_ELEM; ++i)
s[n] += 0.5 * (v[i][0] * shpdz[i] + v[i][NDIMS-1] * shpdx[i]);
#ifdef THREED
// YZ component
n = 5;
s[n] = 0;
for (int i=0; i<NODES_PER_ELEM; ++i)
s[n] += 0.5 * (v[i][1] * shpdz[i] + v[i][2] * shpdy[i]);
#endif
}
}
static void apply_damping(const Param& param, const Variables& var, array_t& force)
{
// flatten 2d arrays to simplify indexing
double* ff = force.data();
const double* v = var.vel->data();
const double small_vel = 1e-13;
switch (param.control.damping_option) {
case 0:
// no damping, stress field can become very noisy
break;
case 1:
// damping when force and velocity are parallel
// acclerating when force and velocity are anti-parallel
#pragma omp parallel for default(none) \
shared(var, param, ff, v)
for (int i=0; i<var.nnode*NDIMS; ++i) {
if (std::fabs(v[i]) > small_vel) {
ff[i] -= param.control.damping_factor * std::copysign(ff[i], v[i]);
}
}
break;
case 2:
// damping prop. to force
#pragma omp parallel for default(none) \
shared(var, param, ff, v)
for (int i=0; i<var.nnode*NDIMS; ++i) {
ff[i] -= param.control.damping_factor * ff[i];
}
break;
case 3:
// damping when force and velocity are parallel
// weakly acclerating when force and velocity are anti-parallel
#pragma omp parallel for default(none) \
shared(var, param, ff, v)
for (int i=0; i<var.nnode*NDIMS; ++i) {
if ((ff[i]<0) == (v[i]<0)) {
// strong damping
ff[i] -= param.control.damping_factor * ff[i], v[i];
}
else {
// weak acceleration
ff[i] += (1 - param.control.damping_factor) * ff[i];
}
}
break;
case 4:
// rayleigh damping
break;
default:
std::cerr << "Error: unknown damping_option: " << param.control.damping_option << '\n';
std::exit(1);
}
}
void update_force(const Param& param, const Variables& var, array_t& force)
{
std::fill_n(force.data(), var.nnode*NDIMS, 0);
class ElemFunc_force : public ElemFunc
{
private:
const Variables &var;
array_t &force;
const double gravity;
public:
ElemFunc_force(const Variables &var, array_t &force, double gravity) :
var(var), force(force), gravity(gravity) {};
void operator()(int e)
{
const int *conn = (*var.connectivity)[e];
const double *shpdx = (*var.shpdx)[e];
#ifdef THREED
const double *shpdy = (*var.shpdy)[e];
#endif
const double *shpdz = (*var.shpdz)[e];
double *s = (*var.stress)[e];
double vol = (*var.volume)[e];
double buoy = 0;
if (gravity != 0)
buoy = var.mat->rho(e) * gravity / NODES_PER_ELEM;
for (int i=0; i<NODES_PER_ELEM; ++i) {
double *f = force[conn[i]];
#ifdef THREED
f[0] -= (s[0]*shpdx[i] + s[3]*shpdy[i] + s[4]*shpdz[i]) * vol;
f[1] -= (s[3]*shpdx[i] + s[1]*shpdy[i] + s[5]*shpdz[i]) * vol;
f[2] -= (s[4]*shpdx[i] + s[5]*shpdy[i] + s[2]*shpdz[i] + buoy) * vol;
#else
f[0] -= (s[0]*shpdx[i] + s[2]*shpdz[i]) * vol;
f[1] -= (s[2]*shpdx[i] + s[1]*shpdz[i] + buoy) * vol;
#endif
}
}
} elemf(var, force, param.control.gravity);
loop_all_elem(var.egroups, elemf);
apply_stress_bcs(param, var, force);
if (param.control.is_quasi_static) {
apply_damping(param, var, force);
}
}
void update_velocity(const Variables& var, array_t& vel)
{
const double* m = &(*var.mass)[0];
// flatten 2d arrays to simplify indexing
const double* f = var.force->data();
double* v = vel.data();
#pragma omp parallel for default(none) \
shared(var, m, f, v)
for (int i=0; i<var.nnode*NDIMS; ++i) {
int n = i / NDIMS;
v[i] += var.dt * f[i] / m[n];
}
}
void update_coordinate(const Variables& var, array_t& coord)
{
double* x = var.coord->data();
const double* v = var.vel->data();
#pragma omp parallel for default(none) \
shared(var, x, v)
for (int i=0; i<var.nnode*NDIMS; ++i) {
x[i] += v[i] * var.dt;
}
}
namespace {
#ifdef THREED
void jaumann_rate_3d(double *s, double dt, double w3, double w4, double w5)
{
double s_inc[NSTR];
s_inc[0] =-2.0 * s[3] * w3 - 2.0 * s[4] * w4;
s_inc[1] = 2.0 * s[3] * w3 - 2.0 * s[5] * w5;
s_inc[2] = 2.0 * s[4] * w4 + 2.0 * s[5] * w5;
s_inc[3] = s[0] * w3 - s[1] * w3 - s[4] * w5 - s[5] * w4;
s_inc[4] = s[0] * w4 - s[2] * w4 + s[3] * w5 - s[5] * w3;
s_inc[5] = s[1] * w5 - s[2] * w5 + s[3] * w4 + s[4] * w3;
for(int i=0; i<NSTR; ++i) {
s[i] += dt * s_inc[i];
}
}
#else
void jaumann_rate_2d(double *s, double dt, double w2)
{
double s_inc[NSTR];
s_inc[0] =-2.0 * s[2] * w2;
s_inc[1] = 2.0 * s[2] * w2;
s_inc[2] = s[0] * w2 - s[1] * w2;
for(int i=0; i<NSTR; ++i) {
s[i] += dt * s_inc[i];
}
}
#endif
}
void rotate_stress(const Variables &var, tensor_t &stress, tensor_t &strain)
{
// The spin rate tensor, W, and the Cauchy stress tensor, S, are
// [ 0 w3 w4] [s0 s3 s4]
// W = [-w3 0 w5], S=[s3 s1 s5].
// [-w4 -w5 0] [s4 s5 s2]
//
// Stress (and strain) increment based on the Jaumann rate is
// dt*(S*W-W*S). S*W-W*S is also symmetric.
// So, following the indexing of stress tensor,
// we get
// sj[0] = dt * ( -2 * s3 * w3 - 2 * s4 * w4)
// sj[1] = dt * ( 2 * s3 * w3 - 2 * s5 * w5)
// sj[2] = dt * ( 2 * s4 * w4 + 2 * s5 * w5)
// sj[3] = dt * ( s0 * w3 - s1 * w3 - s4 * w5 - s5 * w4)
// sj[4] = dt * ( s0 * w4 - s2 * w4 + s3 * w5 - s5 * w3)
// sj[5] = dt * ( s1 * w5 - s2 * w5 + s3 * w4 + s4 * w3)
#pragma omp parallel for default(none) \
shared(var, stress, strain)
for (int e=0; e<var.nelem; ++e) {
const int *conn = (*var.connectivity)[e];
#ifdef THREED
double w3, w4, w5;
{
const double *shpdx = (*var.shpdx)[e];
const double *shpdy = (*var.shpdy)[e];
const double *shpdz = (*var.shpdz)[e];
double *v[NODES_PER_ELEM];
for (int i=0; i<NODES_PER_ELEM; ++i)
v[i] = (*var.vel)[conn[i]];
w3 = 0;
for (int i=0; i<NODES_PER_ELEM; ++i)
w3 += 0.5 * (v[i][0] * shpdy[i] - v[i][1] * shpdx[i]);
w4 = 0;
for (int i=0; i<NODES_PER_ELEM; ++i)
w4 += 0.5 * (v[i][0] * shpdz[i] - v[i][NDIMS-1] * shpdx[i]);
w5 = 0;
for (int i=0; i<NODES_PER_ELEM; ++i)
w5 += 0.5 * (v[i][1] * shpdz[i] - v[i][NDIMS-1] * shpdy[i]);
}
jaumann_rate_3d(stress[e], var.dt, w3, w4, w5);
jaumann_rate_3d(strain[e], var.dt, w3, w4, w5);
#else
double w2;
{
const double *shpdx = (*var.shpdx)[e];
const double *shpdz = (*var.shpdz)[e];
double *v[NODES_PER_ELEM];
for (int i=0; i<NODES_PER_ELEM; ++i)
v[i] = (*var.vel)[conn[i]];
w2 = 0;
for (int i=0; i<NODES_PER_ELEM; ++i)
w2 += 0.5 * (v[i][NDIMS-1] * shpdx[i] - v[i][0] * shpdz[i]);
}
jaumann_rate_2d(stress[e], var.dt, w2);
jaumann_rate_2d(strain[e], var.dt, w2);
#endif
}
}