forked from CEED/Laghos
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlaghost_solver.cpp
2386 lines (2146 loc) · 86 KB
/
laghost_solver.cpp
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
// the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
// reserved. See files LICENSE and NOTICE for details.
//
// This file is part of CEED, a collection of benchmarks, miniapps, software
// libraries and APIs for efficient high-order finite element and spectral
// element discretizations for exascale applications. For more information and
// source code availability see http://github.com/ceed.
//
// The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
// a collaborative effort of two U.S. Department of Energy organizations (Office
// of Science and the National Nuclear Security Administration) responsible for
// the planning and preparation of a capable exascale ecosystem, including
// software, applications, hardware, advanced system engineering and early
// testbed platforms, in support of the nation's exascale computing imperative.
#include "general/forall.hpp"
#include "laghost_solver.hpp"
#include "linalg/kernels.hpp"
#include <unordered_map>
#include <cmath>
#ifdef MFEM_USE_MPI
namespace mfem
{
namespace geodynamics
{
void VisualizeField(socketstream &sock, const char *vishost, int visport,
ParGridFunction &gf, const char *title,
int x, int y, int w, int h, bool vec)
{
gf.HostRead();
ParMesh &pmesh = *gf.ParFESpace()->GetParMesh();
MPI_Comm comm = pmesh.GetComm();
int num_procs, myid;
MPI_Comm_size(comm, &num_procs);
MPI_Comm_rank(comm, &myid);
bool newly_opened = false;
int connection_failed;
do
{
if (myid == 0)
{
if (!sock.is_open() || !sock)
{
sock.open(vishost, visport);
sock.precision(8);
newly_opened = true;
}
sock << "solution\n";
}
pmesh.PrintAsOne(sock);
gf.SaveAsOne(sock);
if (myid == 0 && newly_opened)
{
const char* keys = (gf.FESpace()->GetMesh()->Dimension() == 2)
? "mAcRjl" : "mmaaAcl";
sock << "window_title '" << title << "'\n"
<< "window_geometry "
<< x << " " << y << " " << w << " " << h << "\n"
<< "keys " << keys;
if ( vec ) { sock << "vvv"; }
sock << std::endl;
}
if (myid == 0)
{
connection_failed = !sock && !newly_opened;
}
MPI_Bcast(&connection_failed, 1, MPI_INT, 0, comm);
}
while (connection_failed);
}
static void Rho0DetJ0Vol(const int dim, const int NE,
const IntegrationRule &ir,
ParMesh *pmesh,
ParFiniteElementSpace &L2,
const ParGridFunction &rho0,
QuadratureData &qdata,
double &volume);
LagrangianGeoOperator::LagrangianGeoOperator(const int size,
ParFiniteElementSpace &h1,
ParFiniteElementSpace &l2,
ParFiniteElementSpace &l2_stress,
const Array<int> &ess_tdofs,
// Coefficient &_rho0_coeff,
// Coefficient &_scale_rho0_coeff,
ParGridFunction &rho0_gf,
ParGridFunction &fictitious_rho0_gf,
ParGridFunction &gamma_gf,
const int source,
const double cfl,
const bool visc,
const bool vort,
const bool p_assembly,
const double cgt,
const int cgiter,
double ftz,
const int oq,
ParGridFunction &lambda_gf, ParGridFunction &mu_gf, double mscale, const double gravity, const double _thickness,
const bool winkler_foundation, const double _winkler_rho, const bool dyn_damping, const double _dyn_factor, Vector _bc_id_pa, const double _vbc_max_val) : // -0-
TimeDependentOperator(size),
H1(h1), L2(l2), L2_stress(l2_stress), H1c(H1.GetParMesh(), H1.FEColl(), 1),
pmesh(H1.GetParMesh()),
H1Vsize(H1.GetVSize()),
H1TVSize(H1.TrueVSize()),
H1GTVSize(H1.GlobalTrueVSize()),
L2Vsize(L2.GetVSize()),
L2TVSize(L2.TrueVSize()),
L2GTVSize(L2.GlobalTrueVSize()),
block_offsets(5),
x_gf(&H1),
ess_tdofs(ess_tdofs),
dim(pmesh->Dimension()),
NE(pmesh->GetNE()),
l2dofs_cnt(L2.GetFE(0)->GetDof()),
l2_stress_dofs_cnt(L2_stress.GetFE(0)->GetDof()),
h1dofs_cnt(H1.GetFE(0)->GetDof()),
source_type(source), cfl(cfl),
mass_scale(mscale), grav_mag(gravity), thickness(_thickness), winkler_rho(_winkler_rho), dyn_factor(_dyn_factor), vbc_max_val(_vbc_max_val),
use_viscosity(visc),
use_vorticity(vort),
p_assembly(p_assembly),
winkler_foundation(winkler_foundation),
dyn_damping(dyn_damping),
cg_rel_tol(cgt), cg_max_iter(cgiter),ftz_tol(ftz),
rho0_gf(rho0_gf),
fictitious_rho0_gf(fictitious_rho0_gf),
gamma_gf(gamma_gf),
lambda_gf(lambda_gf),
mu_gf(mu_gf),
bc_id_pa(_bc_id_pa),
// tension_cutoff(_tension_cutoff.Size()),
// cohesion(_cohesion.Size()),
// friction_angle(_friction_angle.Size()),
// dilation_angle(_dilation_angle.Size()),
Mv(&H1), Mv_spmat_copy(),
fic_Mv(&H1), fic_Mv_spmat_copy(),
Me(l2dofs_cnt, l2dofs_cnt, NE),
Me_inv(l2dofs_cnt, l2dofs_cnt, NE),
rho0_coeff(&rho0_gf),
scale_rho0_coeff(&fictitious_rho0_gf),
ir(IntRules.Get(pmesh->GetElementBaseGeometry(0),
(oq > 0) ? oq : 3 * H1.GetOrder(0) + L2.GetOrder(0) - 1)),
Q1D(int(floor(0.7 + pow(ir.GetNPoints(), 1.0 / dim)))),
qdata(dim, NE, ir.GetNPoints()),
qdata_is_current(false),
forcemat_is_assembled(false),
gmat_is_assembled(false),
Force(&l2, &h1),
// Force(&L2, &H1),
// Body_Force(nullptr),
Body_Force(&H1),
ForcePA(nullptr), VMassPA(nullptr), EMassPA(nullptr), StressPA(nullptr),
VMassPA_Jprec(nullptr),
CG_VMass(H1.GetParMesh()->GetComm()),
CG_EMass(L2.GetParMesh()->GetComm()),
timer(p_assembly ? L2TVSize : 1),
qupdate(nullptr),
X(H1c.GetTrueVSize()),
B(H1c.GetTrueVSize()),
one(L2Vsize),
rhs(H1Vsize),
e_rhs(L2Vsize),
s_rhs(L2Vsize), // rhs for stress vector
rhs_c_gf(&H1c),
dvc_gf(&H1c)
{
// If you add block vector, you should add offset
block_offsets[0] = 0;
block_offsets[1] = block_offsets[0] + H1.GetVSize();
block_offsets[2] = block_offsets[1] + H1.GetVSize();
block_offsets[3] = block_offsets[2] + L2.GetVSize();
block_offsets[4] = block_offsets[3] + L2.GetVSize()*3*(dim-1);
// block_offsets[5] = block_offsets[4] + H1.GetVSize();
one.UseDevice(true);
one = 1.0;
qdata.mscale = mscale;
qdata.vbc_max_val = vbc_max_val;
qdata.gravity = gravity;
qdata.h_est = 1e+38;
// for (int i = 0; i < pmesh->attributes.Max(); i++)
// {
// if(_tension_cutoff[i] == 0)
// {
// tension_cutoff[i] = _cohesion[i]/tan(M_PI*_friction_angle[i]/180.0); // Tension limit
// }
// else
// {
// tension_cutoff[i] = _tension_cutoff[i];
// }
// cohesion[i] = _cohesion[i];
// friction_angle[i] = M_PI*_friction_angle[i]/180.0;
// dilation_angle[i] = M_PI*_dilation_angle[i]/180.0;
// }
if (p_assembly)
{
qupdate = new QUpdate(dim, NE, Q1D, visc, vort, cfl, &timer, gamma_gf, lambda_gf, mu_gf, ir, H1, L2, L2_stress); // -1-
// qupdate = new QUpdate(dim, NE, Q1D, visc, vort, cfl,
// &timer, gamma_gf, ir, H1, L2);
ForcePA = new ForcePAOperator(qdata, H1, L2, ir);
StressPA = new StressPAOperator(qdata, H1, L2, ir); // stress rate operator, slee
VMassPA = new MassPAOperator(H1c, ir, rho0_coeff);
// VMassPA = new MassPAOperator(H1c, ir, scale_rho0_coeff);
EMassPA = new MassPAOperator(L2, ir, rho0_coeff);
// Inside the above constructors for mass, there is reordering of the mesh
// nodes which is performed on the host. Since the mesh nodes are a
// subvector, so we need to sync with the rest of the base vector (which
// is assumed to be in the memory space used by the mfem::Device).
H1.GetParMesh()->GetNodes()->ReadWrite();
// Attributes 1/2/3 correspond to fixed-x/y/z boundaries, i.e.,
// we must enforce v_x/y/z = 0 for the velocity components.
const int bdr_attr_max = H1.GetMesh()->bdr_attributes.Max();
Array<int> ess_bdr(bdr_attr_max);
for (int c = 0; c < dim; c++)
{
ess_bdr = 0;
if(dim == 2)
{
if(c == 0)
{
for (int i = 0; i < bc_id_pa.Size(); ++i) {if(bc_id_pa[i]==1 || bc_id_pa[i]==3){ess_bdr[i] = 1;}}
H1c.GetEssentialTrueDofs(ess_bdr, c_tdofs[c]);
c_tdofs[c].Read();
}
else if(c == 1)
{
for (int i = 0; i < bc_id_pa.Size(); ++i) {if(bc_id_pa[i]==2 || bc_id_pa[i]==3){ess_bdr[i] = 1;}}
H1c.GetEssentialTrueDofs(ess_bdr, c_tdofs[c]);
c_tdofs[c].Read();
}
}
else
{
if(c == 0)
{
for (int i = 0; i < bc_id_pa.Size(); ++i) {if(bc_id_pa[i]==1 || bc_id_pa[i]==4 || bc_id_pa[i]==5 || bc_id_pa[i]==6){ess_bdr[i] = 1;}}
H1c.GetEssentialTrueDofs(ess_bdr, c_tdofs[c]);
c_tdofs[c].Read();
}
else if(c == 1)
{
for (int i = 0; i < bc_id_pa.Size(); ++i) {if(bc_id_pa[i]==2 || bc_id_pa[i]==4 || bc_id_pa[i]==5 || bc_id_pa[i]==7){ess_bdr[i] = 1;}}
H1c.GetEssentialTrueDofs(ess_bdr, c_tdofs[c]);
c_tdofs[c].Read();
}
else
{
for (int i = 0; i < bc_id_pa.Size(); ++i) {if(bc_id_pa[i]==3 || bc_id_pa[i]==4 || bc_id_pa[i]==6 || bc_id_pa[i]==7){ess_bdr[i] = 1;}}
H1c.GetEssentialTrueDofs(ess_bdr, c_tdofs[c]);
c_tdofs[c].Read();
}
}
}
X.UseDevice(true);
B.UseDevice(true);
rhs.UseDevice(true);
e_rhs.UseDevice(true);
s_rhs.UseDevice(true); // for gpu calculation, slee
// Standard local (full) assembly and inversion for energy mass matrices.
// 'Me' is used in the computation of stress
// std::cout << "Standard local assembly and inversion for energy mass matrices" << std::endl;
MassIntegrator mi(rho0_coeff, &ir);
for (int e = 0; e < NE; e++)
{
DenseMatrixInverse inv(&Me(e));
const FiniteElement &fe = *L2.GetFE(e);
ElementTransformation &Tr = *L2.GetElementTransformation(e);
mi.AssembleElementMatrix(fe, Tr, Me(e));
inv.Factor();
inv.GetInverseMatrix(Me_inv(e));
}
}
else
{
// Standard local assembly and inversion for energy mass matrices.
// 'Me' is used in the computation of the internal energy
// which is used twice: once at the start and once at the end of the run.
MassIntegrator mi(rho0_coeff, &ir);
for (int e = 0; e < NE; e++)
{
DenseMatrixInverse inv(&Me(e));
const FiniteElement &fe = *L2.GetFE(e);
ElementTransformation &Tr = *L2.GetElementTransformation(e);
mi.AssembleElementMatrix(fe, Tr, Me(e));
inv.Factor();
inv.GetInverseMatrix(Me_inv(e));
}
// Standard assembly for the velocity mass matrix.
VectorMassIntegrator *vmi = new VectorMassIntegrator(rho0_coeff, &ir);
Mv.AddDomainIntegrator(vmi);
Mv.Assemble();
Mv_spmat_copy = Mv.SpMat();
VectorMassIntegrator *vmi_scale = new VectorMassIntegrator(scale_rho0_coeff, &ir);
fic_Mv.AddDomainIntegrator(vmi_scale);
fic_Mv.Assemble();
fic_Mv_spmat_copy = Mv.SpMat();
}
// Values of rho0DetJ0 and Jac0inv at all quadrature points.
// Initial local mesh size (assumes all mesh elements are the same).
int Ne, ne = NE;
double Volume, vol = 0.0;
if (dim > 1 && p_assembly)
{
Rho0DetJ0Vol(dim, NE, ir, pmesh, L2, rho0_gf, qdata, vol);
}
else
{
const int NQ = ir.GetNPoints();
Vector rho_vals(NQ);
for (int e = 0; e < NE; e++)
{
rho0_gf.GetValues(e, ir, rho_vals);
ElementTransformation &Tr = *H1.GetElementTransformation(e);
for (int q = 0; q < NQ; q++)
{
const IntegrationPoint &ip = ir.IntPoint(q);
Tr.SetIntPoint(&ip);
DenseMatrixInverse Jinv(Tr.Jacobian());
Jinv.GetInverseMatrix(qdata.Jac0inv(e*NQ + q));
const double rho0DetJ0 = Tr.Weight() * rho_vals(q);
qdata.rho0DetJ0w(e*NQ + q) = rho0DetJ0 * ir.IntPoint(q).weight;
}
}
for (int e = 0; e < NE; e++) { vol += pmesh->GetElementVolume(e); }
}
MPI_Allreduce(&vol, &Volume, 1, MPI_DOUBLE, MPI_SUM, pmesh->GetComm());
MPI_Allreduce(&ne, &Ne, 1, MPI_INT, MPI_SUM, pmesh->GetComm());
switch (pmesh->GetElementBaseGeometry(0))
{
case Geometry::SEGMENT: qdata.h0 = Volume / Ne; break;
case Geometry::SQUARE: qdata.h0 = sqrt(Volume / Ne); break;
case Geometry::TRIANGLE: qdata.h0 = sqrt(2.0 * Volume / Ne); break;
case Geometry::CUBE: qdata.h0 = pow(Volume / Ne, 1./3.); break;
case Geometry::TETRAHEDRON: qdata.h0 = pow(6.0 * Volume / Ne, 1./3.); break;
default: MFEM_ABORT("Unknown zone type!");
}
qdata.h0 /= (double) H1.GetOrder(0);
if (p_assembly)
{
// Setup the preconditioner of the velocity mass operator.
// BC are handled by the VMassPA, so ess_tdofs here can be empty.
Array<int> empty_tdofs;
VMassPA_Jprec = new OperatorJacobiSmoother(VMassPA->GetBF(), empty_tdofs);
CG_VMass.SetPreconditioner(*VMassPA_Jprec);
CG_VMass.SetOperator(*VMassPA);
CG_VMass.SetRelTol(cg_rel_tol);
CG_VMass.SetAbsTol(0.0);
CG_VMass.SetMaxIter(cg_max_iter);
CG_VMass.SetPrintLevel(-1);
CG_EMass.SetOperator(*EMassPA);
CG_EMass.iterative_mode = false;
CG_EMass.SetRelTol(cg_rel_tol);
CG_EMass.SetAbsTol(0.0);
CG_EMass.SetMaxIter(cg_max_iter);
CG_EMass.SetPrintLevel(-1);
}
else
{
ForceIntegrator *fi = new ForceIntegrator(qdata);
fi->SetIntRule(&ir);
Force.AddDomainIntegrator(fi);
// Make a dummy assembly to figure out the sparsity.
Force.Assemble(0);
Force.Finalize(0);
// Standarad fully aseembly for body force integarator
BodyForceIntegrator *bi = new BodyForceIntegrator(qdata);
bi->SetIntRule(&ir);
Body_Force.AddDomainIntegrator(bi);
Body_Force.Assemble();
}
}
LagrangianGeoOperator::~LagrangianGeoOperator()
{
delete qupdate;
if (p_assembly)
{
delete EMassPA;
delete VMassPA;
delete VMassPA_Jprec;
delete ForcePA;
delete StressPA;
// delete BodyFPA;
}
}
void LagrangianGeoOperator::Mult(const Vector &S, Vector &dS_dt) const
{
// Make sure that the mesh positions correspond to the ones in S. This is
// needed only because some mfem time integrators don't update the solution
// vector at every intermediate stage (hence they don't change the mesh).
UpdateMesh(S); // comment out for testing pseudo transient loop
// The monolithic BlockVector stores the unknown fields as follows:
// (Position, Velocity, Specific Internal Energy).
Vector* sptr = const_cast<Vector*>(&S);
ParGridFunction v;
const int VsizeH1 = H1.GetVSize();
v.MakeRef(&H1, *sptr, VsizeH1);
// Set dx_dt = v (explicit).
ParGridFunction dx; // comment out for testing pseudo transient loop
dx.MakeRef(&H1, dS_dt, 0); // comment out for testing pseudo transient loop
dx = v; // comment out for testing pseudo transient loop
SolveVelocity(S, dS_dt);
SolveEnergy(S, v, dS_dt);
SolveStress(S, dS_dt);
qdata_is_current = false;
}
void LagrangianGeoOperator::SolveVelocity(const Vector &S, Vector &dS_dt) const
{
UpdateQuadratureData(S);
AssembleForceMatrix();
Vector* sptr = const_cast<Vector*>(&S);
ParGridFunction v;
v.MakeRef(&H1, *sptr, H1.GetVSize());
Vector vel_mag(H1.GetVSize()/dim);
for (int i = 0; i < H1.GetVSize()/dim; i++)
{
if(dim == 2){vel_mag[i] = sqrt(pow(v[i], 2) + pow(v[i+H1.GetVSize()/dim], 2));}
else{vel_mag[i] = sqrt(pow(v[i], 2) + pow(v[i+H1.GetVSize()/dim], 2) + pow(v[i+2*H1.GetVSize()/dim], 2));}
}
double bulkm = lambda_gf.Max() + 2 * mu_gf.Max();
double denm = rho0_gf.Max();
// double pseudo_speed = global_max_vel * mass_scale;
double pseudo_speed = vbc_max_val * mass_scale;
double mfactor = (pseudo_speed * pseudo_speed) / bulkm;
if(mfactor > 1.0){mfactor = 1.0;} // if mass scaling is greater than unity, mass scaling is off.
if(mass_scale == 1.0){mfactor = 1.0;} // no mass scaling
mfactor = mfactor * denm; // p_assembly requires small cfl < 0.5 (emperically)
// The monolithic BlockVector stores the unknown fields as follows:
// (Position, Velocity, Specific Internal Energy).
ParGridFunction dv;
dv.MakeRef(&H1, dS_dt, H1.GetVSize());
dv = 0.0;
// AMR requires resize of vectors
// Solve for velocity.
one.SetSize(L2.GetVSize());
rhs.SetSize(H1.GetVSize());
// Vector B, X; In AMR B and X should be redefined.
one = 1.0; rhs = 0.0;
B.HostRead();
// Body Force vector (F = 1 * g)
ParGridFunction accel_src_gf;
accel_src_gf.SetSpace(&H1);
GTCoefficient accel_coeff(dim);
accel_src_gf.ProjectCoefficient(accel_coeff);
accel_src_gf.Read();
accel_src_gf *= grav_mag;
if (p_assembly)
{
// Damping vector (F = -1 * sign(v))
ParGridFunction sign_gf;
sign_gf.SetSpace(&H1);
sign_gf = 0.0; sign_gf += v;
timer.sw_force.Start();
ForcePA->Mult(one, rhs); // F*1
timer.sw_force.Stop();
rhs.Neg(); // -F
if(winkler_foundation)
{
// ParGridFunction x;
// x.MakeRef(&H1, *sptr, H1.GetVSize()*0);
// Vector WC(H1.GetVSize()); WC.UseDevice(true);
// Vector WD(H1.GetVSize()); WD.UseDevice(true);
// Vector WK(H1.GetVSize()); WK.UseDevice(true);
// WC = x; WD = thickness; WC -= WD; WC.Neg();
Array<int> nbc_bdr(pmesh->bdr_attributes.Max());
nbc_bdr = 0; nbc_bdr[2] = 1; // bottome boundary
LinearForm winkler(&H1);
VectorArrayCoefficient winkler_load(dim);
for (int i = 0; i < dim-1; i++)
{
winkler_load.Set(i, new ConstantCoefficient(0.0));
}
Vector pull_force(pmesh->bdr_attributes.Max());
pull_force = 0.0;
pull_force(2) = 1.0;
winkler_load.Set(dim-1, new PWConstCoefficient(pull_force));
winkler.AddBoundaryIntegrator(new VectorBoundaryLFIntegrator(winkler_load), nbc_bdr);
winkler.Assemble();
// Applying winkler
Winkler(S, winkler, thickness);
rhs.Add(winkler_rho*grav_mag, winkler);
// Multvv(WC, winkler, WK);
// rhs.Add(winkler_rho*grav_mag, WK);
}
// Partial assembly solve for each velocity component
const int size = H1c.GetVSize();
const Operator *Pconf = H1c.GetProlongationMatrix();
for (int c = 0; c < dim; c++)
{
dvc_gf.MakeRef(&H1c, dS_dt, H1Vsize + c*size);
rhs_c_gf.MakeRef(&H1c, rhs, c*size);
if (Pconf) { Pconf->MultTranspose(rhs_c_gf, B); }
else { B = rhs_c_gf; }
if(c == dim -1)
{
ParGridFunction accel_comp;
accel_comp.MakeRef(&H1c, accel_src_gf, c*size);
Vector AC;
accel_comp.GetTrueDofs(AC);
Vector BA(AC.Size());
VMassPA->MultFull(AC, BA);
B += BA; // -(F + rho*g)
}
// // Applying damping for all forces such internal, external, and body
// if(dyn_damping)
// {
// ParGridFunction sign_comp;
// sign_comp.MakeRef(&H1c, sign_gf, c*size);
// Vector AC; sign_comp.GetTrueDofs(AC);
// AC.Signcopy(); AC.Neg();
// Vector damping(B.Size()); Vector sdamping(B.Size());
// damping.UseDevice(true); sdamping.UseDevice(true);
// damping = 0.0; damping += B; sdamping = 0.0;
// damping.Abs(); damping *=dyn_factor;
// Multvv(AC, damping, sdamping);
// damping.HostReadWrite(); sdamping.HostReadWrite();
// B += sdamping;
// // Getdamping_comp(S, c, damping);
// // B += damping;
// }
// Applying damping for all forces such internal, external, and body
if(dyn_damping)
{
Vector damping(B.Size());
damping=0.0;
damping.Add(1.0, B);
Getdamping_comp(S, c, damping);
B.Add(dyn_factor, damping);
}
H1c.GetRestrictionMatrix()->Mult(dvc_gf, X);
VMassPA->SetEssentialTrueDofs(c_tdofs[c]);
VMassPA->EliminateRHS(B);
timer.sw_cgH1.Start();
CG_VMass.Mult(B, X);
timer.sw_cgH1.Stop();
timer.H1iter += CG_VMass.GetNumIterations();
X*=mfactor;
if (Pconf) { Pconf->Mult(X, dvc_gf); }
else { dvc_gf = X; }
// We need to sync the subvector 'dvc_gf' with its base vector
// because it may have been moved to a different memory space.
dvc_gf.GetMemory().SyncAlias(dS_dt.GetMemory(), dvc_gf.Size());
}
}
else // full assembly
{
timer.sw_force.Start();
Force.Mult(one, rhs);
timer.sw_force.Stop();
rhs.Neg();
// rhs += *v_source;
// // Applying body force
// Body_Force.Update();
// Body_Force.Assemble();
// rhs.Add(1.0, Body_Force);
Vector rhs_accel(rhs.Size());
Mv_spmat_copy.Mult(accel_src_gf, rhs_accel);
rhs += rhs_accel;
if(winkler_foundation)
{
Array<int> nbc_bdr(pmesh->bdr_attributes.Max());
nbc_bdr = 0; nbc_bdr[2] = 1; // bottome boundary
LinearForm winkler(&H1);
VectorArrayCoefficient winkler_load(dim);
for (int i = 0; i < dim-1; i++)
{
winkler_load.Set(i, new ConstantCoefficient(0.0));
}
Vector pull_force(pmesh->bdr_attributes.Max());
pull_force = 0.0;
pull_force(2) = 1.0;
winkler_load.Set(dim-1, new PWConstCoefficient(pull_force));
winkler.AddBoundaryIntegrator(new VectorBoundaryLFIntegrator(winkler_load), nbc_bdr);
winkler.Assemble();
// Applying winkler
Winkler(S, winkler, thickness);
rhs.Add(winkler_rho*grav_mag, winkler);
}
HypreParMatrix A;
// Mv.FormLinearSystem(ess_tdofs, dv, rhs, A, X, B);
fic_Mv.FormLinearSystem(ess_tdofs, dv, rhs, A, X, B);
// Applying damping for all forces such internal, external, and body
if(dyn_damping)
{
Vector damping(B.Size());
damping=0.0;
damping.Add(1.0, B);
Getdamping(S, damping);
B.Add(dyn_factor, damping);
//
}
CGSolver cg(H1.GetParMesh()->GetComm());
HypreSmoother prec;
// prec.SetType(HypreSmoother::Jacobi, 1); // l1-scaled Jacobi
prec.SetType(HypreSmoother::Chebyshev); // Chebyshev
cg.SetPreconditioner(prec);
cg.SetOperator(A);
cg.SetRelTol(cg_rel_tol);
cg.SetAbsTol(0.0);
cg.SetMaxIter(cg_max_iter);
cg.SetPrintLevel(-1);
timer.sw_cgH1.Start();
cg.Mult(B, X);
timer.sw_cgH1.Stop();
// X*=mfactor;
timer.H1iter += cg.GetNumIterations();
// Mv.RecoverFEMSolution(X, rhs, dv);
fic_Mv.RecoverFEMSolution(X, rhs, dv);
}
}
void LagrangianGeoOperator::SolveEnergy(const Vector &S, const Vector &v, Vector &dS_dt) const
{
// UpdateQuadratureData(S, dt);
UpdateQuadratureData(S);
AssembleForceMatrix();
// The monolithic BlockVector stores the unknown fields as follows:
// (Position, Velocity, Specific Internal Energy).
ParGridFunction de;
de.MakeRef(&L2, dS_dt, H1.GetVSize()*2);
de = 0.0;
// resize for e_rhs
/*
AMR requires resize of vectors
e_rhs.SetSize(L2.GetVSize());
e_rhs = 0.0;
*/
// Solve for energy, assemble the energy source if such exists.
LinearForm *e_source = nullptr;
if (source_type == 1) // 2D Taylor-Green.
{
// Needed since the Assemble() defaults to PA.
L2.GetMesh()->DeleteGeometricFactors();
e_source = new LinearForm(&L2);
TaylorCoefficient coeff;
DomainLFIntegrator *d = new DomainLFIntegrator(coeff, &ir);
e_source->AddDomainIntegrator(d);
e_source->Assemble();
}
// TODO
// MassIntegrator mi(&ir);
// Me.SetSize(l2dofs_cnt, l2dofs_cnt, NE);
// Me_inv.SetSize(l2dofs_cnt, l2dofs_cnt, NE);
// MassIntegrator mi(rho0_coeff, &ir);
// for (int e = 0; e < NE; e++)
// {
// DenseMatrixInverse inv(&Me(e));
// const FiniteElement &fe = *L2.GetFE(e);
// ElementTransformation &Tr = *L2.GetElementTransformation(e);
// mi.AssembleElementMatrix(fe, Tr, Me(e));
// inv.Factor();
// inv.GetInverseMatrix(Me_inv(e));
// }
Array<int> l2dofs;
if (p_assembly)
{
timer.sw_force.Start();
ForcePA->MultTranspose(v, e_rhs);
timer.sw_force.Stop();
if (e_source) { e_rhs += *e_source; }
timer.sw_cgL2.Start();
CG_EMass.Mult(e_rhs, de);
timer.sw_cgL2.Stop();
const HYPRE_Int cg_num_iter = CG_EMass.GetNumIterations();
timer.L2iter += (cg_num_iter==0) ? 1 : cg_num_iter;
// Move the memory location of the subvector 'de' to the memory
// location of the base vector 'dS_dt'.
de.GetMemory().SyncAlias(dS_dt.GetMemory(), de.Size());
}
else // not p_assembly
{
timer.sw_force.Start();
Force.MultTranspose(v, e_rhs);
timer.sw_force.Stop();
if (e_source) { e_rhs += *e_source; }
Vector loc_rhs(l2dofs_cnt), loc_de(l2dofs_cnt);
for (int e = 0; e < NE; e++)
{
L2.GetElementDofs(e, l2dofs);
e_rhs.GetSubVector(l2dofs, loc_rhs);
timer.sw_cgL2.Start();
Me_inv(e).Mult(loc_rhs, loc_de);
timer.sw_cgL2.Stop();
timer.L2iter += 1;
de.SetSubVector(l2dofs, loc_de);
}
}
delete e_source;
}
void LagrangianGeoOperator::SolveStress(const Vector &S, Vector &dS_dt) const
{
UpdateQuadratureData(S);
if (p_assembly)
{
ParGridFunction v(&H1);
v = 1.0; // dummy velocity for partial assembly
// Stress rate devided by mass matrix
for (int i = 0; i < 3*(dim - 1); i++)
{
// Partial assembly solve for each stress component
timer.sw_force.Start();
const int comp = i;
StressPA->MultTranspose(v, s_rhs, comp);
timer.sw_force.Stop();
ParGridFunction ds;
ds.MakeRef(&L2, dS_dt, H1Vsize*2 + L2Vsize + L2Vsize*i);
ds = 0.0;
timer.sw_cgL2.Start();
CG_EMass.Mult(s_rhs, ds);
timer.sw_cgL2.Stop();
const HYPRE_Int cg_num_iter = CG_EMass.GetNumIterations();
timer.L2iter += (cg_num_iter==0) ? 1 : cg_num_iter;
// Move the memory location of the subvector 'ds' to the memory
// location of the base vector 'dS_dt'.
ds.GetMemory().SyncAlias(dS_dt.GetMemory(), ds.Size());
}
}
else
{
ParGridFunction dsig;
dsig.MakeRef(&L2_stress, dS_dt, H1.GetVSize()*2 + L2.GetVSize());
int NED = NE*l2_stress_dofs_cnt;
int dim2 = dim*dim;
if(dim == 2)
{
Vector sub_rhs1(l2_stress_dofs_cnt), sub_rhs2(l2_stress_dofs_cnt), sub_rhs3(l2_stress_dofs_cnt);
Vector loc_dsig1(l2_stress_dofs_cnt), loc_dsig2(l2_stress_dofs_cnt), loc_dsig3(l2_stress_dofs_cnt);
loc_dsig1=0.0; loc_dsig2=0.0; loc_dsig3=0.0;
Array<int> offset(4);
offset[0] = 0;
offset[1] = offset[0] + l2_stress_dofs_cnt;
offset[2] = offset[1] + l2_stress_dofs_cnt;
offset[3] = offset[2] + l2_stress_dofs_cnt;
BlockVector loc_rhs(offset, Device::GetMemoryType());
sub_rhs1.MakeRef(loc_rhs, 0*l2_stress_dofs_cnt);
sub_rhs2.MakeRef(loc_rhs, 1*l2_stress_dofs_cnt);
sub_rhs3.MakeRef(loc_rhs, 2*l2_stress_dofs_cnt);
loc_rhs = 0.0;
SigmaIntegrator gi(qdata);
gi.SetIntRule(&ir);
Array<int> dof_loc1(l2_stress_dofs_cnt);
Array<int> dof_loc2(l2_stress_dofs_cnt);
Array<int> dof_loc3(l2_stress_dofs_cnt);
for (int e = 0; e < NE; e++)
{
L2_stress.GetElementDofs(e, dof_loc1);
L2_stress.GetElementDofs(e, dof_loc2);
L2_stress.GetElementDofs(e, dof_loc3);
const FiniteElement &fe = *L2_stress.GetFE(e);
ElementTransformation &eltr = *L2_stress.GetElementTransformation(e);
gi.AssembleRHSElementVect(fe, eltr, loc_rhs);
Me_inv(e).Mult(sub_rhs1, loc_dsig1);
Me_inv(e).Mult(sub_rhs2, loc_dsig2);
Me_inv(e).Mult(sub_rhs3, loc_dsig3);
for (int i = 0; i < dof_loc1.Size(); i++)
{
dof_loc1[i] = i + (e+0*NE)*dof_loc1.Size();
dof_loc2[i] = i + (e+1*NE)*dof_loc1.Size();
dof_loc3[i] = i + (e+2*NE)*dof_loc1.Size();
}
dsig.SetSubVector(dof_loc1, loc_dsig1);
dsig.SetSubVector(dof_loc2, loc_dsig2);
dsig.SetSubVector(dof_loc3, loc_dsig3);
}
}
else if(dim == 3)
{
Vector sub_rhs1(l2_stress_dofs_cnt), sub_rhs2(l2_stress_dofs_cnt), sub_rhs3(l2_stress_dofs_cnt);
Vector sub_rhs4(l2_stress_dofs_cnt), sub_rhs5(l2_stress_dofs_cnt), sub_rhs6(l2_stress_dofs_cnt);
Vector loc_dsig1(l2_stress_dofs_cnt), loc_dsig2(l2_stress_dofs_cnt), loc_dsig3(l2_stress_dofs_cnt);
Vector loc_dsig4(l2_stress_dofs_cnt), loc_dsig5(l2_stress_dofs_cnt), loc_dsig6(l2_stress_dofs_cnt);
loc_dsig1=0.0; loc_dsig2=0.0; loc_dsig3=0.0; loc_dsig4=0.0; loc_dsig5=0.0; loc_dsig6=0.0;
Array<int> offset(7);
offset[0] = 0;
offset[1] = offset[0] + l2_stress_dofs_cnt;
offset[2] = offset[1] + l2_stress_dofs_cnt;
offset[3] = offset[2] + l2_stress_dofs_cnt;
offset[4] = offset[3] + l2_stress_dofs_cnt;
offset[5] = offset[4] + l2_stress_dofs_cnt;
offset[6] = offset[5] + l2_stress_dofs_cnt;
BlockVector loc_rhs(offset, Device::GetMemoryType());
sub_rhs1.MakeRef(loc_rhs, 0*l2_stress_dofs_cnt);
sub_rhs2.MakeRef(loc_rhs, 1*l2_stress_dofs_cnt);
sub_rhs3.MakeRef(loc_rhs, 2*l2_stress_dofs_cnt);
sub_rhs4.MakeRef(loc_rhs, 3*l2_stress_dofs_cnt);
sub_rhs5.MakeRef(loc_rhs, 4*l2_stress_dofs_cnt);
sub_rhs6.MakeRef(loc_rhs, 5*l2_stress_dofs_cnt);
loc_rhs = 0.0;
SigmaIntegrator gi(qdata);
gi.SetIntRule(&ir);
Array<int> dof_loc1(l2_stress_dofs_cnt);
Array<int> dof_loc2(l2_stress_dofs_cnt);
Array<int> dof_loc3(l2_stress_dofs_cnt);
Array<int> dof_loc4(l2_stress_dofs_cnt);
Array<int> dof_loc5(l2_stress_dofs_cnt);
Array<int> dof_loc6(l2_stress_dofs_cnt);
for (int e = 0; e < NE; e++)
{
L2_stress.GetElementDofs(e, dof_loc1);
L2_stress.GetElementDofs(e, dof_loc2);
L2_stress.GetElementDofs(e, dof_loc3);
L2_stress.GetElementDofs(e, dof_loc4);
L2_stress.GetElementDofs(e, dof_loc5);
L2_stress.GetElementDofs(e, dof_loc6);
const FiniteElement &fe = *L2_stress.GetFE(e);
ElementTransformation &eltr = *L2_stress.GetElementTransformation(e);
gi.AssembleRHSElementVect(fe, eltr, loc_rhs);
Me_inv(e).Mult(sub_rhs1, loc_dsig1);
Me_inv(e).Mult(sub_rhs2, loc_dsig2);
Me_inv(e).Mult(sub_rhs3, loc_dsig3);
Me_inv(e).Mult(sub_rhs4, loc_dsig4);
Me_inv(e).Mult(sub_rhs5, loc_dsig5);
Me_inv(e).Mult(sub_rhs6, loc_dsig6);
for (int i = 0; i < dof_loc1.Size(); i++)
{
dof_loc1[i] = i + (e+0*NE)*dof_loc1.Size();
dof_loc2[i] = i + (e+1*NE)*dof_loc1.Size();
dof_loc3[i] = i + (e+2*NE)*dof_loc1.Size();
dof_loc4[i] = i + (e+3*NE)*dof_loc1.Size();
dof_loc5[i] = i + (e+4*NE)*dof_loc1.Size();
dof_loc6[i] = i + (e+5*NE)*dof_loc1.Size();
}
dsig.SetSubVector(dof_loc1, loc_dsig1);
dsig.SetSubVector(dof_loc2, loc_dsig2);
dsig.SetSubVector(dof_loc3, loc_dsig3);
dsig.SetSubVector(dof_loc4, loc_dsig4);
dsig.SetSubVector(dof_loc5, loc_dsig5);
dsig.SetSubVector(dof_loc6, loc_dsig6);
}
}
}
}
void LagrangianGeoOperator::UpdateMesh(const Vector &S) const
{
Vector* sptr = const_cast<Vector*>(&S);
x_gf.MakeRef(&H1, *sptr, 0);
H1.GetParMesh()->NewNodes(x_gf, false);
}
void LagrangianGeoOperator::Getdamping(const Vector &S, Vector &_v_damping) const
{
Vector* sptr = const_cast<Vector*>(&S);
ParGridFunction v;
v.MakeRef(&H1, *sptr, H1.GetVSize());
v.SetTrueVector();
Vector vt = v.GetTrueVector();
for( int i = 0; i < _v_damping.Size(); i++ )
{
_v_damping[i]=-1*copysignl(1.0, vt[i])*fabs(_v_damping[i]); //
}
}
void LagrangianGeoOperator::Getdamping_comp(const Vector &S, const int &comp, Vector &_v_damping) const
{
// damping by each component in p_assembly
Vector* sptr = const_cast<Vector*>(&S);
ParGridFunction v;
v.MakeRef(&H1, *sptr, H1.GetVSize());
v.SetTrueVector();
Vector vt = v.GetTrueVector();
for( int i = 0; i < _v_damping.Size(); i++ )
{
_v_damping[i]=-1*copysignl(1.0, vt[i+_v_damping.Size()*comp])*fabs(_v_damping[i]); //
}
}
void LagrangianGeoOperator::Winkler(const Vector &S, Vector &_winkler, double &_thickness) const
{
Vector* sptr = const_cast<Vector*>(&S);
ParGridFunction x;
// ParGridFunction x, x_ini;
x.MakeRef(&H1, *sptr, H1.GetVSize()*0);
double bottom = 0.0;
// x_ini.MakeRef(&H1, *sptr, H1.GetVSize()*2 + L2.GetVSize() + L2.GetVSize()*3*(dim-1));
for( int i = 0; i < _winkler.Size(); i++ )
{
// _winkler[i]=-1.0*(x[i]-(bottom + _thickness))*_winkler[i]; //
_winkler[i]= (_thickness - x[i])*_winkler[i];
// p = var.compensation_pressure -
// (var.mat->rho(e) + param.bc.winkler_delta_rho) *
// param.control.gravity * (zcenter + param.mesh.zlength);
}
}
double LagrangianGeoOperator::GetTimeStepEstimate(const Vector &S) const
{
UpdateMesh(S);
UpdateQuadratureData(S);
double glob_dt_est;
const MPI_Comm comm = H1.GetParMesh()->GetComm();
MPI_Allreduce(&qdata.dt_est, &glob_dt_est, 1, MPI_DOUBLE, MPI_MIN, comm);
return glob_dt_est;
}
double LagrangianGeoOperator::GetLengthEstimate(const Vector &S) const
{
UpdateMesh(S);
UpdateQuadratureData(S);