-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminiapp.cpp
313 lines (245 loc) · 8.17 KB
/
miniapp.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
// Charles Goodman
// 26 October 2021
// Deterministic Transport Solver
// monoenergetic, 1,2,3-D, implicit time, discrete ordinates
#include <mfem.hpp>
#include <fstream>
#include <iostream>
#include <algorithm>
double inflow_function(const mfem::Vector &x);
double u0_function(const mfem::Vector &x);
int main(int argc, char *argv[]) {
// Enable GPU (when executed with option -d cuda)
const char *mesh_file = "1_10x1_10.mesh";
int order = 1;
const char *device_config = "cpu";
const char* amgx_json_file = "FGMRES.json"; // JSON file for AmgX
mfem::OptionsParser args(argc, argv);
args.AddOption(&mesh_file, "-m", "--mesh",
"Mesh file to use.");
args.AddOption(&order, "-o", "--order",
"Finite element order (polynomial degree) or -1 for"
" isoparametric space.");
args.AddOption(&device_config, "-d", "--device",
"Device configuration string, see Device::Configure().");
args.AddOption(&amgx_json_file, "--amgx-file", "--amgx-file",
"AMGX solver config file (overrides --amgx-solver, --amgx-verbose)");
args.Parse();
if (!args.Good())
{
args.PrintUsage(std::cout);
return 1;
}
args.PrintOptions(std::cout);
mfem::Device device(device_config);
device.Print();
// Read Input Files
// Geometry
mfem::Mesh mesh(mesh_file, 1, 1);
int dim = mesh.Dimension();
// Problem Data
std::ifstream matf("B1.mat");
// Boundary Conditions (unused)
int lboundtype;
double lboundval;
int rboundtype;
double rboundval;
matf >> lboundtype;
matf >> lboundval;
matf >> rboundtype;
matf >> rboundval;
//Materials
int Nmats;
matf >> Nmats;
double tmp;
double tmp2;
double tmp3;
mfem::Vector SigmaT(mesh.attributes.Max());
mfem::Vector SigmaS(mesh.attributes.Max());
mfem::Vector Source(mesh.attributes.Max());
for (int i = 0; i < Nmats; i++) {
matf >> tmp;
SigmaT(i) = tmp;
matf >> tmp;
matf >> tmp2;
matf >> tmp3;
SigmaS(i) = (tmp + tmp2*tmp3)/8; //2 to power of quad dim;
matf >> tmp;
Source(i) = tmp/4/3.141592;
}
matf.close();
mfem::PWConstCoefficient sigmaT(SigmaT);
mfem::PWConstCoefficient sigmaS(SigmaS);
mfem::PWConstCoefficient source(Source);
// Quadrature Set
// Write Quadrature set angles from file to a vector of unit vectors
std::ifstream quadf("LS16.quad");
int quadDim;
quadf >> quadDim;
int M;
quadf >> M;
std::vector<double> QuadW;
std::vector<mfem::Vector> Quad;
for (int i = 0; i < M; i++) {
double angle[quadDim];
double tmp;
for (int j = 0; j < quadDim; j++) {
quadf >> angle[j];
}
mfem::Vector Omega;
Omega.NewDataAndSize(angle,quadDim);
Quad.push_back(Omega);
quadf >> tmp;
QuadW.push_back(tmp);
}
quadf.close();
int kend = 5;
int k = 0;
double dt = .02;
double v = 30; //52; // cm/shake for 14 MeV neutron
//Define the discontinous DG finite element space of given polynomial order on the mesh
mfem::DG_FECollection fec(order, dim, mfem::BasisType::GaussLobatto);
mfem::FiniteElementSpace fes(&mesh, &fec);
mfem::FiniteElementSpace vfes(&mesh, &fec, dim);
mfem::Array<int> ess_tdof_list, ess_bdr(mesh.bdr_attributes.Max());
ess_bdr = 0;
ess_bdr[0] = 1;
fes.GetEssentialTrueDofs(ess_bdr, ess_tdof_list);
std::vector<mfem::BilinearForm*> A;
for ( int m = 0; m < M; m++) { //For each discrete ordinate
mfem::VectorConstantCoefficient Omega(Quad[m]);
mfem::BilinearForm *S = new mfem::BilinearForm(&fes);
mfem::ConstantCoefficient timeconst (1.0/v/dt);
S->AddDomainIntegrator(new mfem::MassIntegrator(timeconst));
S->AddDomainIntegrator(new mfem::ConvectionIntegrator(Omega, 1.0));
S->AddDomainIntegrator(new mfem::MassIntegrator(sigmaT));
S->AddInteriorFaceIntegrator(new mfem::TransposeIntegrator(new mfem::DGTraceIntegrator(Omega, -1.0, 0.5))); //criterion of DG interior bounds
S->AddBdrFaceIntegrator(new mfem::TransposeIntegrator(new mfem::DGTraceIntegrator(Omega, -1.0, 0.5))); //criterion for DG external bounds //Not compatible with pa
S->Assemble();
S->Finalize(); //Not for pa?
//A is a vector of Matrix such that Ax = b
A.push_back(S);
}
//Initialize AmgX solver
mfem::AmgXSolver amgx;
amgx.ReadParameters(amgx_json_file, mfem::AmgXSolver::EXTERNAL);
amgx.InitSerial();
//Set initial condition
mfem::FunctionCoefficient u0(u0_function);
std::vector<mfem::GridFunction> u;
std::vector<mfem::GridFunction> uprev;
for (int m = 0; m < M; m++) {
mfem::GridFunction ut(&fes);
ut.ProjectCoefficient(u0);
u.push_back(ut);
ut /= v*dt;
uprev.push_back(ut);
}
mfem::GridFunction scalarflux(&fes);
mfem::GridFunction current(&vfes);
std::vector<mfem::GridFunction*> currentcom (dim);
for (int d = 0; d < dim; d++) {
currentcom[d] = new mfem::GridFunction(&fes);
}
scalarflux = 0.0;
// Time Loop
while (k < kend) {
k++;
//Initialize source from previous time step
int inneriterations = 0;
while (true) {
inneriterations++;
//converge source iterations
mfem::GridFunction oldscalarflux = scalarflux;
mfem::GridFunctionCoefficient oldscalarfluxc(&oldscalarflux);
mfem::ProductCoefficient scatsource(sigmaS, oldscalarfluxc);
scalarflux = 0.0;
current = 0.0;
for (int d = 0; d < dim; d++) {
*currentcom[d] = 0;
}
for (int m = 0; m < M; m++) {
//run mfem over domain for each ordinate
//make these two function coefficient functions of m and t
mfem::VectorConstantCoefficient Omega(Quad[m]);
mfem::FunctionCoefficient inflow(inflow_function);
//previous time step angular flux
mfem::GridFunctionCoefficient tprev(&uprev[m]);
//Here is the iterated source
mfem::LinearForm b(&fes);
//Scattering Source
b.AddDomainIntegrator(new mfem::DomainLFIntegrator(scatsource));
//Time Source
b.AddDomainIntegrator(new mfem::DomainLFIntegrator(tprev));
//External source
b.AddDomainIntegrator(new mfem::DomainLFIntegrator(source));
//Boundary Conditions
b.AddBdrFaceIntegrator(new mfem::BoundaryFlowIntegrator(inflow, Omega, -1.0, -0.5));
b.Assemble();
//Setup and Solve System
mfem::OperatorPtr A1;
mfem::Vector B1, X1;
A[m]->FormLinearSystem(ess_tdof_list, u[m], b, A1, X1, B1);
amgx.SetOperator(*A1.As<mfem::SparseMatrix>());
amgx.Mult(B1,X1);
A[m]->RecoverFEMSolution(X1, b, u[m]);
//need to use quadrature weights
mfem::GridFunction tmpgf = u[m];
tmpgf *= QuadW[m];
//calculate scalar flux
scalarflux += tmpgf;
//calculate current
//can be done componentwise and recombined in vector at end
for (int d = 0; d < dim; d++) {
mfem::GridFunction tmpgf2 = tmpgf;
tmpgf2 *= Quad[m].Elem(d);
*currentcom[d] += tmpgf2;
}
//tries to perform inner product
//current += Quad[m]*u[m];
}
//compute residual in angular flux calc
int order_to_integrate = 4;
const mfem::IntegrationRule *irs[mfem::Geometry::NumGeom];
for (int i=0; i < mfem::Geometry::NumGeom; ++i) {
irs[i] = &(mfem::IntRules.Get(i, order_to_integrate));
}
std::cout << scalarflux.ComputeMaxError(oldscalarfluxc,irs) << std::endl;
//end the loop when flux is converged
if (scalarflux.ComputeMaxError(oldscalarfluxc,irs) < 1E-8) {
std::cout << inneriterations << std::endl;
std::cout << scalarflux.ComputeMaxError(oldscalarfluxc,irs) << std::endl;
break;
}
}
//save previous time step angular flux
for (int m = 0; m < M; m++) {
uprev[m] = u[m];
uprev[m] /= v*dt;
}
//write solution at step k to output file
system(("mkdir -p /gpfs/wolf/gen167/scratch/clemency/output/"+std::to_string(k)).c_str());
std::ofstream osol("/gpfs/wolf/gen167/scratch/clemency/output/"+std::to_string(k)+"/sf.gf");
osol.precision(16);
scalarflux.Save(osol);
osol.close();
for (int d = 0; d < dim; d++) {
std::ofstream osol("/gpfs/wolf/gen167/scratch/clemency/output/"+std::to_string(k)+"/curr"+std::to_string(d)+".gf");
osol.precision(16);
currentcom[d]->Save(osol);
osol.close();
}
}
}
// Inflow boundary condition
double inflow_function(const mfem::Vector &x) {
if (x[0] == 0) {
return 100;
} else {
return 0;
}
}
// Initial Condition
double u0_function(const mfem::Vector &x) {
return .001;
}