-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmixed_nonlinear_diffusion.cpp
2438 lines (1972 loc) · 81.1 KB
/
mixed_nonlinear_diffusion.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) 2013-2024, Lawrence Livermore National Security, LLC
* and other libROM project developers. See the top-level COPYRIGHT
* file for details.
*
* SPDX-License-Identifier: (Apache-2.0 OR MIT)
*
*****************************************************************************/
// libROM MFEM Example: parametric ROM for nonlinear diffusion problem
//
// Compile with: ./scripts/compile.sh -m
//
// Description: This example solves a time dependent nonlinear diffusion equation
// dp/dt + div(v) = f, grad p = -a(p) v. After discretization by mixed FEM,
// M(p) v + B^T p = 0
// B v - C p_t = -f
// where
// M(p) = \int_\Omega a(p) w_h \cdot v_h d\Omega w_h, v_h \in R_h
// B = -\int_\Omega \div w_h q_h d\Omega w_h \in R_h, q_h \in W_h
// C = \int_\Omega q_h p_h d\Omega p_h \in W_h, q_h \in W_h
// Here, R_h is a Raviart-Thomas finite element subspace of H(div),
// and W_h is a finite element subspace of L2.
// The first equation allows the substitution v = -M(p)^{-1} B^T p, so
// C p_t + B M(p)^{-1} B^T p = f
// For the purpose of using an ODE solver, this can be expressed as
// p_t = C^{-1} (f - B M(p)^{-1} B^T p) = F(p)
// Sample runs:
// Analytic test (reproductive)
// mpirun -n 1 ./mixed_nonlinear_diffusion -offline
// mpirun -n 1 ./mixed_nonlinear_diffusion -merge -ns 1
// mpirun -n 1 ./mixed_nonlinear_diffusion -online -rrdim 8 -rwdim 8 -nldim 20 -nsdim 20 -hrtype deim
// mpirun -n 1 ./mixed_nonlinear_diffusion -online -rrdim 8 -rwdim 8 -nldim 20 -nsdim 20 -hrtype qdeim
// mpirun -n 1 ./mixed_nonlinear_diffusion -online -rrdim 8 -rwdim 8 -nldim 20 -nsdim 20 -hrtype sopt
// mpirun -n 1 ./mixed_nonlinear_diffusion -online -rrdim 8 -rwdim 8 -nldim 20 -nsdim 20 -ns 1 -eqp
//
// Relative l2 error of ROM solution at final timestep using DEIM sampling: 1.096776797994166e-08
// Elapsed time for time integration loop using DEIM sampling: 0.5686423310000001
// Relative l2 error of ROM solution at final timestep using QDEIM sampling: 1.227055339859034e-08
// Elapsed time for time integration loop using QDEIM sampling: 0.575228571
// Relative l2 error of ROM solution at final timestep using S_OPT sampling: 1.01945081122054e-08
// Elapsed time for time integration loop using S_OPT sampling: 0.5311964850000001
// Relative l2 error of ROM solution at final timestep using EQP: 1.052559143494963e-08
// Elapsed time for time integration loop using EQP: 0.346165296
//
// Note that the timing of the time integration loop does not include setup,
// which can be greater for S_OPT and EQP than for DEIM.
//
// Initial step test (reproductive)
// mpirun -n 1 ./mixed_nonlinear_diffusion -offline -p 1
// mpirun -n 1 ./mixed_nonlinear_diffusion -merge -ns 1 -p 1
// mpirun -n 1 ./mixed_nonlinear_diffusion -online -rrdim 8 -rwdim 8 -nldim 20 -p 1 -hrtype deim
// mpirun -n 1 ./mixed_nonlinear_diffusion -online -rrdim 8 -rwdim 8 -nldim 20 -p 1 -hrtype sopt
// mpirun -n 1 ./mixed_nonlinear_diffusion -online -rrdim 8 -rwdim 8 -nldim 20 -p 1 -ns 1 -eqp
//
// Relative l2 error of ROM solution at final timestep using DEIM sampling: 0.0003712362376412496
// Elapsed time for time integration loop using DEIM sampling: 0.339288686
// Relative l2 error of ROM solution at final timestep using QDEIM sampling: 0.000375066186385267
// Elapsed time for time integration loop using QDEIM sampling: 0.32320409
// Relative l2 error of ROM solution at final timestep using S_OPT sampling: 0.0003797338657417907
// Elapsed time for time integration loop using S_OPT sampling: 0.32422266
// Relative l2 error of ROM solution at final timestep using EQP sampling: 0.0003709977143117392
// Elapsed time for time integration loop using EQP sampling: 0.457011311
//
// Initial step parametric test (predictive)
// mpirun -n 1 ./mixed_nonlinear_diffusion -p 1 -offline -id 0 -sh 0.25
// mpirun -n 1 ./mixed_nonlinear_diffusion -p 1 -offline -id 1 -sh 0.15
// mpirun -n 1 ./mixed_nonlinear_diffusion -p 1 -offline -id 2 -sh 0.35
// mpirun -n 1 ./mixed_nonlinear_diffusion -p 1 -merge -ns 3
// mpirun -n 1 ./mixed_nonlinear_diffusion -p 1 -offline -id 3 -sh 0.3
// mpirun -n 1 ./mixed_nonlinear_diffusion -p 1 -online -rrdim 8 -rwdim 8 -nldim 20 -sh 0.3 -id 3 -hrtype deim
// mpirun -n 1 ./mixed_nonlinear_diffusion -p 1 -online -rrdim 8 -rwdim 8 -nldim 20 -sh 0.3 -id 3 -hrtype sopt
// mpirun -n 1 ./mixed_nonlinear_diffusion -p 1 -online -rrdim 8 -rwdim 8 -nldim 20 -sh 0.3 -id 3 -ns 3 -eqp -maxnnls 30
//
// Relative l2 error of ROM solution at final timestep using DEIM sampling: 0.002681387312231006
// Elapsed time for time integration loop using DEIM sampling: 0.359206509
// Relative l2 error of ROM solution at final timestep using QDEIM sampling: 0.00266355670204476
// Elapsed time for time integration loop using QDEIM sampling: 0.309432016
// Relative l2 error of ROM solution at final timestep using S_OPT sampling: 0.002701713369494112
// Elapsed time for time integration loop using S_OPT sampling: 0.41139788
// Relative l2 error of ROM solution at final timestep using EQP: 0.002659978000520714
// Elapsed time for time integration loop using EQP sampling: 0.164961644
//
// Pointwise snapshots for DMD input
// mpirun -n 1 ./mixed_nonlinear_diffusion -pwsnap -pwx 101 -pwy 101
//
// This example runs in parallel with MPI, by using the same number of MPI ranks
// in all phases (offline, merge, online).
#include "mfem.hpp"
#include <fstream>
#include <iostream>
#include <algorithm>
#include "linalg/BasisGenerator.h"
#include "linalg/BasisReader.h"
#include "linalg/NNLS.h"
#include "hyperreduction/Hyperreduction.h"
#include "mfem/SampleMesh.hpp"
#include "mfem/PointwiseSnapshot.hpp"
using namespace mfem;
using namespace std;
double InitialTemperature(const Vector &x);
double SourceFunction(const Vector &x, const double t);
double ExactSolution(const Vector &x, const double t);
double NonlinearCoefficient(const double p);
double NonlinearCoefficientDerivative(const double p);
#include "mixed_nonlinear_diffusion_eqp.hpp"
typedef enum {ANALYTIC, INIT_STEP} PROBLEM;
typedef enum {RSPACE, WSPACE} FESPACE;
static bool nonlinear_problem;
static int problem;
static double diffusion_c, step_half;
class NonlinearDiffusionOperator;
class RomOperator;
class NonlinearDiffusionGradientOperator : public Operator
{
private:
friend class NonlinearDiffusionOperator;
void SetParameters(Operator *C_solver_, Operator *M_solver_,
Operator *M_prime_, Operator *B_, const double dt_);
Operator *C_solver;
Operator *M_solver;
Operator *M_prime;
Operator *B;
double dt;
mutable Vector zR; // auxiliary vector
mutable Vector yR; // auxiliary vector
mutable Vector xR; // auxiliary vector
mutable Vector zW; // auxiliary vector
public:
NonlinearDiffusionGradientOperator(const int sizeR, const int sizeW);
void Mult(const Vector &x, Vector &y) const;
};
NonlinearDiffusionGradientOperator::NonlinearDiffusionGradientOperator(
const int sizeR,
const int sizeW)
: Operator(sizeW), zW(sizeW), zR(sizeR), yR(sizeR), xR(sizeR),
C_solver(NULL), M_solver(NULL), M_prime(NULL), B(NULL), dt(0.0)
{
}
void NonlinearDiffusionGradientOperator::SetParameters(Operator *C_solver_,
Operator *M_solver_,
Operator *M_prime_, Operator *B_, const double dt_)
{
C_solver = C_solver_;
M_solver = M_solver_;
M_prime = M_prime_;
B = B_;
dt = dt_;
}
void NonlinearDiffusionGradientOperator::Mult(const Vector &x, Vector &y) const
{
// Gradient is I + dt C^{-1} B M(p)^{-1} B^T - dt C^{-1} B M(p)^{-1} M(a'(p)) M(p)^{-1} B^T
// = I + dt C^{-1} B (I - M(p)^{-1} M(a'(p))) M(p)^{-1} B^T
// Apply M(p)^{-1} B^T
B->MultTranspose(x, zR);
M_solver->Mult(zR, yR);
// Apply (I - M(p)^{-1} M(a'(p))) to yR
M_prime->Mult(yR, zR);
M_solver->Mult(zR, xR);
yR.Add(-1.0, xR);
// Apply C^{-1} B
B->Mult(yR, zW);
C_solver->Mult(zW, y);
zW = y;
y = x;
y.Add(dt, zW);
}
class NonlinearDiffusionOperator : public TimeDependentOperator
{
protected:
friend class RomOperator;
Array<int> ess_Rdof_list; // this list remains empty for pure essential b.c.
mutable ParBilinearForm *M;
mutable HypreParMatrix *Cmat, *Mmat;
mutable ParBilinearForm *Mprime;
ParBilinearForm *C;
HypreParMatrix *Bmat;
HypreParMatrix *BTmat;
mutable HypreParMatrix Mprimemat;
mutable BlockOperator *fullOp;
mutable BlockOperator *fullGradient;
mutable BlockDiagonalPreconditioner *fullPrec;
Array<int> block_trueOffsets;
double current_dt;
mutable CGSolver
*M_solver; // Krylov solver for inverting the R mass matrix M
mutable HypreSmoother M_prec; // Preconditioner for the R mass matrix M
mutable CGSolver C_solver; // Krylov solver for inverting the W mass matrix C
HypreSmoother C_prec; // Preconditioner for the W mass matrix C
GMRESSolver *J_gmres;
NewtonSolver newton_solver;
NonlinearDiffusionGradientOperator *gradient;
double linear_solver_rel_tol;
Vector p0;
Vector dpdt_prev;
mutable Vector zR; // auxiliary vector
mutable Vector yR; // auxiliary vector
mutable Vector zW; // auxiliary vector
public:
NonlinearDiffusionOperator(ParFiniteElementSpace &fR, ParFiniteElementSpace &fW,
const double rel_tol, const double abs_tol,
const int iter, const Vector &p);
virtual void Mult(const Vector &p, Vector &dp_dt) const;
void Mult_Mmat(const Vector &p, Vector &Mp) const
{
Mmat->Mult(p, Mp);
}
void Mult_FullSystem(const Vector &p, Vector &dp_dt) const;
void SetBTV(const CAROM::Matrix *V, CAROM::Matrix *BTV) const;
void GetSource(Vector& s) const;
/** Solve the Backward-Euler equation: k = f(p + dt*k, t), for the unknown k.
This is the only requirement for high-order SDIRK implicit integration.*/
virtual void ImplicitSolve(const double dt, const Vector &p, Vector &dp_dt);
virtual Operator &GetGradient(const Vector &p) const;
/// Update the diffusion BilinearForm K using the given true-dof vector `p`.
void SetParameters(const Vector &p) const;
void CopyDpDt(Vector &dpdt) const
{
dpdt = dpdt_prev;
}
void CopyDpDt_W(Vector &dpdt) const
{
Vector dpdt_W(dpdt_prev.GetData() + zR.Size(), zW.Size());
dpdt = dpdt_W;
}
virtual ~NonlinearDiffusionOperator();
ParFiniteElementSpace &fespace_R; // RT finite element space
ParFiniteElementSpace &fespace_W; // L2 scalar finite element space
bool newtonFailure;
};
class RomOperator : public TimeDependentOperator
{
private:
int rrdim, rwdim, nldim;
int nsamp_R, nsamp_S;
double current_dt;
bool oversampling;
NewtonSolver newton_solver;
GMRESSolver *J_gmres;
CAROM::Matrix *BRsp, *BWsp;
CAROM::Vector *psp_librom, *psp_R_librom, *psp_W_librom;
Vector *psp;
Vector *psp_R;
Vector *psp_W;
mutable Vector zR;
mutable CAROM::Vector zY;
mutable CAROM::Vector zN;
const CAROM::Matrix *Vsinv;
// Data for source function
const CAROM::Matrix *Ssinv;
mutable CAROM::Vector zS;
mutable CAROM::Vector zT;
const CAROM::Matrix *S;
mutable DenseMatrix J;
bool hyperreduce, hyperreduce_source;
bool sourceFOM;
mutable ParGridFunction p_gf, rhs_gf;
GridFunctionCoefficient p_coeff;
mutable TransformedCoefficient a_coeff;
TransformedCoefficient aprime_coeff;
mutable SumCoefficient a_plus_aprime_coeff;
CAROM::Vector *pfom_librom, *pfom_R_librom, *pfom_W_librom;
Vector *pfom;
Vector *pfom_R;
Vector *pfom_W;
mutable Vector zfomR;
mutable Vector zfomW;
CAROM::Vector *zfomR_librom;
mutable CAROM::Vector VtzR;
CAROM::SampleMeshManager *smm;
void PrintFDJacobian(const Vector &p) const;
// Data for EQP
bool eqp;
const IntegrationRule *ir_eqp;
std::vector<double> eqp_rw;
std::vector<int> eqp_qp;
std::vector<double> eqp_rw_S;
std::vector<int> eqp_qp_S;
Vector eqp_coef, eqp_coef_S;
const bool fastIntegration = true;
CAROM::Matrix eqp_lifting;
std::vector<int> eqp_liftDOFs;
mutable CAROM::Vector eqp_lifted;
int rank;
protected:
CAROM::Matrix* BR;
CAROM::Matrix* CR;
const CAROM::Matrix* U_R;
Vector y0;
Vector dydt_prev;
NonlinearDiffusionOperator *fom;
NonlinearDiffusionOperator *fomSp;
public:
RomOperator(NonlinearDiffusionOperator *fom_,
NonlinearDiffusionOperator *fomSp_,
const int rrdim_, const int rwdim_, const int nldim_,
CAROM::SampleMeshManager *smm_,
const CAROM::Matrix* V_R_, const CAROM::Matrix* U_R_, const CAROM::Matrix* V_W_,
const CAROM::Matrix *Bsinv,
const double newton_rel_tol, const double newton_abs_tol, const int newton_iter,
const CAROM::Matrix* S_, const CAROM::Matrix *Ssinv_,
const int myid, const bool hyperreduce_source_, const bool oversampling_,
const bool use_eqp, CAROM::Vector *eqpSol,
CAROM::Vector *eqpSol_S, const IntegrationRule *ir_eqp_);
virtual void Mult(const Vector &y, Vector &dy_dt) const;
void Mult_Hyperreduced(const Vector &y, Vector &dy_dt) const;
void Mult_FullOrder(const Vector &y, Vector &dy_dt) const;
/** Solve the Backward-Euler equation: k = f(p + dt*k, t), for the unknown k.
This is the only requirement for high-order SDIRK implicit integration.*/
virtual void ImplicitSolve(const double dt, const Vector &y, Vector &dy_dt);
virtual Operator &GetGradient(const Vector &y) const;
CAROM::Matrix V_W, V_R, VTU_R;
CAROM::Matrix VTCS_W;
virtual ~RomOperator();
};
// TODO: remove this by making online computation serial?
void BroadcastUndistributedRomVector(CAROM::Vector* v)
{
const int N = v->dim();
MFEM_VERIFY(N > 0, "");
double *d = new double[N];
MFEM_VERIFY(d != 0, "");
for (int i=0; i<N; ++i)
d[i] = (*v)(i);
MPI_Bcast(d, N, MPI_DOUBLE, 0, MPI_COMM_WORLD);
for (int i=0; i<N; ++i)
(*v)(i) = d[i];
delete [] d;
}
// TODO: move this to the library?
CAROM::Matrix* GetFirstColumns(const int N, const CAROM::Matrix* A)
{
CAROM::Matrix* S = new CAROM::Matrix(A->numRows(), std::min(N, A->numColumns()),
A->distributed());
for (int i=0; i<S->numRows(); ++i)
{
for (int j=0; j<S->numColumns(); ++j)
(*S)(i,j) = (*A)(i,j);
}
// delete A; // TODO: find a good solution for this.
return S;
}
// TODO: move this to the library?
void MergeBasis(const int dimFOM, const int nparam, const int max_num_snapshots,
std::string name)
{
MFEM_VERIFY(nparam > 0, "Must specify a positive number of parameter sets");
bool update_right_SV = false;
bool isIncremental = false;
CAROM::Options options(dimFOM, nparam * max_num_snapshots, 1, update_right_SV);
CAROM::BasisGenerator generator(options, isIncremental, "basis" + name);
for (int paramID=0; paramID<nparam; ++paramID)
{
std::string snapshot_filename = "basis" + std::to_string(
paramID) + "_" + name + "_snapshot";
generator.loadSamples(snapshot_filename,"snapshot");
}
generator.endSamples(); // save the merged basis file
int cutoff = 0;
generator.finalSummary(1e-4, cutoff, "mergedSV_" + name);
}
// TODO: move this to the library?
const CAROM::Matrix* GetSnapshotMatrix(const int dimFOM, const int nparam,
const int max_num_snapshots, std::string name)
{
MFEM_VERIFY(nparam > 0, "Must specify a positive number of parameter sets");
bool update_right_SV = false;
bool isIncremental = false;
CAROM::Options options(dimFOM, nparam * max_num_snapshots, 1, update_right_SV);
CAROM::BasisGenerator generator(options, isIncremental, "basis" + name);
for (int paramID=0; paramID<nparam; ++paramID)
{
std::string snapshot_filename = "basis" + std::to_string(
paramID) + "_" + name + "_snapshot";
generator.loadSamples(snapshot_filename,"snapshot");
}
// TODO: this deep copy is inefficient, just to get around generator owning the matrix.
CAROM::Matrix *s = new CAROM::Matrix(*generator.getSnapshotMatrix());
return s;
//return generator.getSnapshotMatrix(); // BUG: the matrix is deleted when generator goes out of scope.
}
int main(int argc, char *argv[])
{
// 1. Initialize MPI.
int num_procs, myid;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
// 2. Parse command-line options.
nonlinear_problem = true;
problem = ANALYTIC;
diffusion_c = 2.0;
step_half = 0.25;
const char *mesh_file = "../data/inline-quad.mesh";
int ser_ref_levels = 2;
int par_ref_levels = 1;
int order = 0;
double t_final = 1.0e-1;
double maxdt = 1.0e-3;
double dt = maxdt;
bool visualization = false;
bool visit = false;
int vis_steps = 5;
double newton_rel_tol = 1e-4;
double newton_abs_tol = 1e-10;
int newton_iter = 10;
// ROM parameters
bool offline = false;
bool merge = false;
bool online = false;
const char *samplingType = "deim";
bool use_eqp = false;
bool writeSampleMesh = false;
int num_samples_req = -1;
bool pointwiseSnapshots = false;
int pwx = 0;
int pwy = 0;
int pwz = 0;
int nsets = 0;
int id_param = 0;
// Number of basis vectors to use
int rrdim = -1;
int rwdim = -1;
int nldim = -1;
int nsdim = -1;
bool preconditionNNLS = false;
double tolNNLS = 1.0e-14;
int maxNNLSnnz = 0;
int precision = 16;
cout.precision(precision);
OptionsParser args(argc, argv);
args.AddOption(&mesh_file, "-m", "--mesh",
"Mesh file to use.");
args.AddOption(&ser_ref_levels, "-rs", "--refine-serial",
"Number of times to refine the mesh uniformly in serial.");
args.AddOption(&par_ref_levels, "-rp", "--refine-parallel",
"Number of times to refine the mesh uniformly in parallel.");
args.AddOption(&order, "-o", "--order",
"Order (degree) of the finite elements.");
args.AddOption(&id_param, "-id", "--id", "Parametric index");
args.AddOption(&problem, "-p", "--problem", "Problem setup to use");
args.AddOption(&step_half, "-sh", "--stephalf",
"Initial step function half-width");
args.AddOption(&diffusion_c, "-dc", "--diffusion-constant",
"Diffusion coefficient constant term");
args.AddOption(&rrdim, "-rrdim", "--rrdim",
"Basis dimension for H(div) vector finite element space.");
args.AddOption(&rwdim, "-rwdim", "--rwdim",
"Basis dimension for L2 scalar finite element space.");
args.AddOption(&nldim, "-nldim", "--nldim",
"Basis dimension for the nonlinear term.");
args.AddOption(&nsdim, "-nsdim", "--nsdim",
"Basis dimension for the source term.");
args.AddOption(&t_final, "-tf", "--t-final",
"Final time; start time is 0.");
args.AddOption(&dt, "-dt", "--time-step",
"Time step.");
args.AddOption(&visualization, "-vis", "--visualization", "-no-vis",
"--no-visualization",
"Enable or disable GLVis visualization.");
args.AddOption(&visit, "-visit", "--visit-datafiles", "-no-visit",
"--no-visit-datafiles",
"Save data files for VisIt (visit.llnl.gov) visualization.");
args.AddOption(&vis_steps, "-vs", "--visualization-steps",
"Visualize every n-th timestep.");
args.AddOption(&nsets, "-ns", "--nset", "Number of parametric snapshot sets");
args.AddOption(&offline, "-offline", "--offline", "-no-offline", "--no-offline",
"Enable or disable the offline phase.");
args.AddOption(&online, "-online", "--online", "-no-online", "--no-online",
"Enable or disable the online phase.");
args.AddOption(&merge, "-merge", "--merge", "-no-merge", "--no-merge",
"Enable or disable the merge phase.");
args.AddOption(&samplingType, "-hrtype", "--hrsamplingtype",
"Sampling type for hyperreduction.");
args.AddOption(&num_samples_req, "-nsr", "--nsr",
"Number of samples for the sampling algorithm to select.");
args.AddOption(&use_eqp, "-eqp", "--eqp", "-no-eqp", "--no-eqp",
"Use EQP instead of DEIM for the hyperreduction.");
args.AddOption(&writeSampleMesh, "-smesh", "--sample-mesh", "-no-smesh",
"--no-sample-mesh", "Write the sample mesh to file.");
args.AddOption(&preconditionNNLS, "-preceqp", "--preceqp", "-no-preceqp",
"--no-preceqp", "Precondition the NNLS system for EQP.");
args.AddOption(&tolNNLS, "-tolnnls", "--tol-nnls",
"Tolerance for NNLS solver.");
args.AddOption(&maxNNLSnnz, "-maxnnls", "--max-nnls",
"Maximum nnz for NNLS");
args.AddOption(&pointwiseSnapshots, "-pwsnap", "--pw-snap", "-no-pwsnap",
"--no-pw-snap", "Enable or disable writing pointwise snapshots.");
args.AddOption(&pwx, "-pwx", "--pwx", "Number of snapshot points in x");
args.AddOption(&pwy, "-pwy", "--pwy", "Number of snapshot points in y");
args.AddOption(&pwz, "-pwz", "--pwz", "Number of snapshot points in z");
args.Parse();
if (!args.Good())
{
args.PrintUsage(cout);
MPI_Finalize();
return 1;
}
if (myid == 0)
{
args.PrintOptions(cout);
}
const int check = (int) pointwiseSnapshots + (int) offline + (int) merge +
(int) online;
MFEM_VERIFY(check == 1,
"Only one of offline, merge, online, or pwsnap must be true!");
const bool hyperreduce_source = (problem != INIT_STEP);
StopWatch solveTimer, totalTimer;
totalTimer.Start();
// 3. Read the serial mesh from the given mesh file on all processors. We can
// handle triangular, quadrilateral, tetrahedral and hexahedral meshes
// with the same code.
Mesh *mesh = new Mesh(mesh_file, 1, 1);
const int dim = mesh->Dimension();
// 4. Define the ODE solver used for time integration. For this example,
// only backward Euler is currently supported.
BackwardEulerSolver ode_solver;
// 5. Refine the mesh in serial to increase the resolution. In this example
// we do 'ser_ref_levels' of uniform refinement, where 'ser_ref_levels' is
// a command-line parameter.
for (int lev = 0; lev < ser_ref_levels; lev++)
{
mesh->UniformRefinement();
}
// 6. Define a parallel mesh by a partitioning of the serial mesh. Refine
// this mesh further in parallel to increase the resolution. Once the
// parallel mesh is defined, the serial mesh can be deleted.
ParMesh *pmesh = new ParMesh(MPI_COMM_WORLD, *mesh);
delete mesh;
for (int lev = 0; lev < par_ref_levels; lev++)
{
pmesh->UniformRefinement();
}
#ifndef MFEM_USE_GSLIB
if (pointwiseSnapshots) {
cout << "To use pointwise snapshots, compile with -mg option" << endl;
MFEM_ABORT("Pointwise snapshots aren't available, since the "
"compilation is done without the -mg option");
}
#else
CAROM::PointwiseSnapshot *pws = nullptr;
Vector pwsnap;
CAROM::Vector *pwsnap_CAROM = nullptr;
if (pointwiseSnapshots)
{
pmesh->EnsureNodes();
const int dmdDim[3] = {pwx, pwy, pwz};
pws = new CAROM::PointwiseSnapshot(dim, dmdDim);
pws->SetMesh(pmesh);
int snapshotSize = dmdDim[0];
for (int i=1; i<dim; ++i)
snapshotSize *= dmdDim[i];
pwsnap.SetSize(snapshotSize);
if (myid == 0)
pwsnap_CAROM = new CAROM::Vector(pwsnap.GetData(), pwsnap.Size(),
true, false);
}
#endif
// 7. Define the mixed finite element spaces.
RT_FECollection hdiv_coll(order, dim);
L2_FECollection l2_coll(order, dim);
ParFiniteElementSpace R_space(pmesh, &hdiv_coll);
ParFiniteElementSpace W_space(pmesh, &l2_coll);
HYPRE_Int dimW = W_space.GlobalTrueVSize();
HYPRE_Int dimR = R_space.GlobalTrueVSize();
if (myid == 0)
{
cout << "Global number of L2 unknowns: " << dimW << endl;
cout << "Global number of RT unknowns: " << dimR << endl;
}
bool update_right_SV = false;
bool isIncremental = false;
const std::string basisFileName = "basis" + std::to_string(id_param);
int max_num_snapshots = int(t_final/dt) + 2;
// The merge phase
if (merge)
{
totalTimer.Clear();
totalTimer.Start();
MergeBasis(R_space.GetTrueVSize(), nsets, max_num_snapshots, "R");
MergeBasis(R_space.GetTrueVSize(), nsets, max_num_snapshots, "FR");
MergeBasis(W_space.GetTrueVSize(), nsets, max_num_snapshots, "W");
if (hyperreduce_source)
{
MergeBasis(W_space.GetTrueVSize(), nsets, max_num_snapshots, "S");
}
totalTimer.Stop();
if (myid == 0)
{
printf("Elapsed time for merging and building ROM basis: %e second\n",
totalTimer.RealTime());
}
MPI_Finalize();
return 0;
}
ParGridFunction p_gf(&W_space);
// 8. Set the initial conditions for p.
FunctionCoefficient p_0(InitialTemperature);
p_gf.ProjectCoefficient(p_0);
Vector p, pprev, dpdt, source;
Vector *p_W = &p;
ParGridFunction* sp_p_gf = 0;
Vector sp_p;
Vector *sp_p_W = &sp_p;
Vector *wMFEM = 0;
CAROM::Vector *p_librom = 0;
CAROM::Vector *p_W_librom = 0;
CAROM::Vector* w_W = 0;
CAROM::Vector* w = 0;
const int N1 = R_space.GetTrueVSize();
const int N2 = W_space.GetTrueVSize();
const int fdim = N1 + N2;
cout << myid << ": Local number of L2 unknowns: " << N2 << endl;
cout << myid << ": Local number of RT unknowns: " << N1 << endl;
{
p_librom = new CAROM::Vector(fdim, true);
p.SetDataAndSize(&((*p_librom)(0)), fdim);
p_W_librom = new CAROM::Vector(&((*p_librom)(N1)), N2, true, false);
p = 0.0;
p_W = new Vector(p.GetData() + N1, N2);
p_gf.GetTrueDofs(*p_W);
source.SetSize(N2);
}
// 9. Initialize the diffusion operator and the VisIt visualization.
NonlinearDiffusionOperator oper(R_space, W_space, newton_rel_tol,
newton_abs_tol, newton_iter, p); // FOM operator
NonlinearDiffusionOperator *soper = 0; // Sample mesh operator
if (offline)
{
ostringstream mesh_name, sol_name;
mesh_name << "nldiff-mesh." << setfill('0') << setw(6) << myid;
sol_name << "nldiff-init" << id_param << "." << setfill('0') << setw(6) << myid;
ofstream omesh(mesh_name.str().c_str());
omesh.precision(precision);
pmesh->Print(omesh);
ofstream osol(sol_name.str().c_str());
osol.precision(precision);
p_gf.Save(osol);
}
VisItDataCollection * visit_dc = NULL;
if (visit)
{
if (offline)
visit_dc = new VisItDataCollection("nldiff-fom", pmesh);
else
visit_dc = new VisItDataCollection("nldiff-rom", pmesh);
visit_dc->RegisterField("temperature", &p_gf);
visit_dc->SetCycle(0);
visit_dc->SetTime(0.0);
visit_dc->Save();
}
socketstream sout;
if (visualization)
{
char vishost[] = "localhost";
int visport = 19916;
sout.open(vishost, visport);
sout << "parallel " << num_procs << " " << myid << endl;
int good = sout.good(), all_good;
MPI_Allreduce(&good, &all_good, 1, MPI_INT, MPI_MIN, pmesh->GetComm());
if (!all_good)
{
sout.close();
visualization = false;
if (myid == 0)
{
cout << "Unable to connect to GLVis server at "
<< vishost << ':' << visport << endl;
cout << "GLVis visualization disabled.\n";
}
}
else
{
sout.precision(precision);
sout << "solution\n" << *pmesh << p_gf;
sout << "pause\n";
sout << flush;
if (myid == 0)
{
cout << "GLVis visualization paused."
<< " Press space (in the GLVis window) to resume it.\n";
}
}
}
CAROM::BasisGenerator *basis_generator_R = 0; // For H(div) space
CAROM::BasisGenerator *basis_generator_W = 0; // For scalar L2 space
CAROM::BasisGenerator *basis_generator_FR = 0; // For nonlinear term M(p)v
CAROM::BasisGenerator *basis_generator_S = 0; // For the source function
if (offline) {
CAROM::Options options_R(R_space.GetTrueVSize(), max_num_snapshots, 1,
update_right_SV);
CAROM::Options options_W(W_space.GetTrueVSize(), max_num_snapshots, 1,
update_right_SV);
if (hyperreduce_source)
basis_generator_S = new CAROM::BasisGenerator(options_W, isIncremental,
basisFileName + "_S");
basis_generator_R = new CAROM::BasisGenerator(options_R, isIncremental,
basisFileName + "_R");
basis_generator_W = new CAROM::BasisGenerator(options_W, isIncremental,
basisFileName + "_W");
basis_generator_FR = new CAROM::BasisGenerator(options_R, isIncremental,
basisFileName + "_FR");
}
RomOperator *romop = 0;
const CAROM::Matrix* B_librom = 0;
const CAROM::Matrix* BR_librom = 0;
const CAROM::Matrix* FR_librom = 0;
const CAROM::Matrix* BW_librom = 0;
const CAROM::Matrix* S_librom = 0;
int nsamp_R = -1;
int nsamp_S = -1;
CAROM::SampleMeshManager *smm = nullptr;
CAROM::Vector *eqpSol = nullptr;
CAROM::Vector *eqpSol_S = nullptr;
if (online)
{
CAROM::BasisReader readerR("basisR");
BR_librom = readerR.getSpatialBasis();
if (rrdim == -1)
rrdim = BR_librom->numColumns();
else
BR_librom = GetFirstColumns(rrdim,
BR_librom); // TODO: reduce rrdim if too large
MFEM_VERIFY(BR_librom->numRows() == N1, "");
if (myid == 0)
printf("reduced R dim = %d\n",rrdim);
CAROM::BasisReader readerW("basisW");
BW_librom = readerW.getSpatialBasis();
if (rwdim == -1)
rwdim = BW_librom->numColumns();
else
BW_librom = GetFirstColumns(rwdim, BW_librom);
MFEM_VERIFY(BW_librom->numRows() == N2, "");
if (myid == 0)
printf("reduced W dim = %d\n",rwdim);
// TODO: To get the basis U_R, considering the relation M(p) v + B^T p = 0 in the FOM, we can just use
// U_R = B^T V_W. Note that V_W and V_R may have different numbers of columns, which is fine.
// TODO: maybe we need POD applied to B^T multiplied by W-snapshots, or just a basis generator for
// snapshots of M(p) v. This could be different from B^T multiplied by POD results for the W solutions.
/*
FR_librom = new CAROM::Matrix(N1, rwdim, true);
oper.SetBTV(BW_librom, FR_librom);
*/
CAROM::BasisReader readerFR("basisFR");
FR_librom = readerFR.getSpatialBasis();
if (nldim == -1)
{
nldim = FR_librom->numColumns();
}
MFEM_VERIFY(FR_librom->numRows() == N1 && FR_librom->numColumns() >= nldim, "");
if (FR_librom->numColumns() > nldim)
FR_librom = GetFirstColumns(nldim, FR_librom);
if (myid == 0)
printf("reduced FR dim = %d\n",nldim);
// Setup hyperreduction, using either EQP or sampled DOFs and a sample mesh.
CAROM::BasisReader *readerS = NULL;
ParFiniteElementSpace *sp_R_space, *sp_W_space;
CAROM::Matrix *Bsinv = NULL;
CAROM::Matrix *Ssinv = NULL;
const IntegrationRule *ir0 = NULL;
if (ir0 == NULL)
{
// int order = 2 * el.GetOrder();
const FiniteElement &fe = *R_space.GetFE(0);
ElementTransformation *eltrans = R_space.GetElementTransformation(0);
int order = eltrans->OrderW() + 2 * fe.GetOrder();
ir0 = &IntRules.Get(fe.GetGeomType(), order);
}
if (use_eqp)
{
// EQP setup
eqpSol = new CAROM::Vector(ir0->GetNPoints() * R_space.GetNE(), true);
SetupEQP_snapshots(ir0, myid, &R_space, &W_space, nsets, BR_librom,
GetSnapshotMatrix(R_space.GetTrueVSize(), nsets, max_num_snapshots, "R"),
GetSnapshotMatrix(W_space.GetTrueVSize(), nsets, max_num_snapshots, "W"),
preconditionNNLS, tolNNLS, maxNNLSnnz, *eqpSol);
if (writeSampleMesh) WriteMeshEQP(pmesh, myid, ir0->GetNPoints(), *eqpSol);
if (problem == ANALYTIC)
{
eqpSol_S = new CAROM::Vector(ir0->GetNPoints() * W_space.GetNE(), true);
SetupEQP_S_snapshots(ir0, myid, &W_space, BW_librom,
GetSnapshotMatrix(W_space.GetTrueVSize(), nsets, max_num_snapshots, "S"),
preconditionNNLS, tolNNLS, maxNNLSnnz, *eqpSol_S);
}
}
else
{
// Setup hyperreduction using DEIM, GNAT, or S-OPT
CAROM::Hyperreduction hr(samplingType);
vector<int> num_sample_dofs_per_proc(num_procs);
if (num_samples_req != -1)
{
nsamp_R = num_samples_req;
}
else
{
nsamp_R = nldim;
}
// Now execute the chosen sampling algorithm to get the sampling information.
Bsinv = new CAROM::Matrix(nsamp_R, nldim, false);
vector<int> sample_dofs(nsamp_R); // Indices of the sampled rows
hr.ComputeSamples(FR_librom,
nldim,
sample_dofs,
num_sample_dofs_per_proc,
*Bsinv,
myid,
num_procs,
nsamp_R);
vector<int> sample_dofs_S; // Indices of the sampled rows
vector<int> num_sample_dofs_per_proc_S(num_procs);
vector<int> sample_dofs_withS; // Indices of the sampled rows
vector<int> num_sample_dofs_per_proc_withS;
if (hyperreduce_source)
{
readerS = new CAROM::BasisReader("basisS");
S_librom = readerS->getSpatialBasis();
if (nsdim == -1)
{