-
Notifications
You must be signed in to change notification settings - Fork 471
/
test_normal_stress.cpp
292 lines (241 loc) · 7.65 KB
/
test_normal_stress.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
// Copyright (c) 2010-2023, Lawrence Livermore National Security, LLC. Produced
// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
// LICENSE and NOTICE for details. LLNL-CODE-806117.
//
// This file is part of the MFEM library. For more information and source code
// availability visit https://mfem.org.
//
// MFEM is free software; you can redistribute it and/or modify it under the
// terms of the BSD-3 license. We welcome feedback and contributions, see file
// CONTRIBUTING.md for details.
#include "fem/fe/fe_base.hpp"
#include "fem/fe_coll.hpp"
#include "fem/pgridfunc.hpp"
#include "general/communication.hpp"
#include "lib/navier_solver.hpp"
#include "kernels/contact_qoi_evaluator.hpp"
#include "linalg/densemat.hpp"
#include "linalg/hypre.hpp"
#include <fstream>
using namespace mfem;
using namespace navier;
void print_matrix(mfem::DenseMatrix m)
{
std::cout << std::scientific;
std::cout << "{";
for (int i = 0; i < m.NumRows(); i++)
{
std::cout << "{";
for (int j = 0; j < m.NumCols(); j++)
{
std::cout << m(i, j);
if (j < m.NumCols() - 1)
{
std::cout << ", ";
}
}
if (i < m.NumRows() - 1)
{
std::cout << "}, ";
}
else
{
std::cout << "}";
}
}
std::cout << "}\n";
std::cout << std::fixed;
}
void print_vector(mfem::Vector v)
{
std::cout << "{";
for (int i = 0; i < v.Size(); i++)
{
std::cout << v(i);
if (i < v.Size() - 1)
{
std::cout << ", ";
}
}
std::cout << "}\n";
}
void analytical_velocity(const Vector &coords, Vector &u)
{
const double x = coords(0);
const double y = coords(1);
u(0) = x * y;
u(1) = 2 * x * y;
}
double analytical_pressure(const Vector &coords)
{
const double x = coords(0);
const double y = coords(1);
return x+y;
}
void analytical_stress(const Vector &coords, Vector &sigma_ne)
{
const double x = coords(0);
const double y = coords(1);
// y - x x + 2y
// x + 2y 3x - y
DenseMatrix sigma(2,2);
sigma(0, 0) = y - x;
sigma(1, 0) = x + 2*y;
sigma(0, 1) = x + 2*y;
sigma(1, 1) = 3*x - y;
Vector ne(2);
ne(0) = 1.0;
ne(1) = 0.0;
sigma_ne.SetSize(2);
sigma.Mult(ne, sigma_ne);
}
DenseMatrix* analytical_stress_mat(const Vector &coords)
{
const double x = coords(0);
const double y = coords(1);
// y - x x + 2y
// x + 2y 3x - y
auto sigma = new DenseMatrix(2, 2);
(*sigma)(0, 0) = y - x;
(*sigma)(1, 0) = x + 2*y;
(*sigma)(0, 1) = x + 2*y;
(*sigma)(1, 1) = 3*x - y;
return sigma;
}
int main(int argc, char *argv[])
{
Mpi::Init(argc, argv);
int num_procs = Mpi::WorldSize();
int myid = Mpi::WorldRank();
Hypre::Init();
int polynomial_order = 2;
int refinements = 0;
int par_refinements = 0;
const char *mesh_file = "two_domain_test.mesh";
OptionsParser args(argc, argv);
args.AddOption(&mesh_file, "-m", "--mesh", "Mesh file to use.");
args.AddOption(&refinements, "-r", "--ref", "");
args.AddOption(&par_refinements, "-pr", "--pref", "");
args.AddOption(&polynomial_order, "-o", "--order", "");
args.ParseCheck(out);
Mesh mesh = Mesh::LoadFromFile(mesh_file);
mesh.EnsureNodes();
const int dim = mesh.Dimension();
const double density = 1.0;
out << "mesh dimension " << dim << "\n";
Array<int> left_domain(mesh.attributes.Max());
left_domain = 0;
left_domain[0] = 1;
Array<int> right_domain(mesh.attributes.Max());
right_domain = 0;
right_domain[1] = 1;
Array<int> partitioning(mesh.GetNE());
partitioning[0] = 1;
partitioning[1] = 0;
partitioning[2] = 0;
partitioning[3] = 1;
ParMesh pmesh(MPI_COMM_WORLD, mesh, partitioning);
for (int i = 0; i < par_refinements; i++)
{
pmesh.UniformRefinement();
}
L2_FECollection l2fec(0, dim);
ParFiniteElementSpace l2fes(&pmesh, &l2fec);
ParGridFunction l2gf(&l2fes);
l2gf = Mpi::WorldRank();
char vishost[] = "localhost";
int visport = 19916;
socketstream sol_sock(vishost, visport);
sol_sock << "parallel " << num_procs << " " << myid << "\n";
sol_sock.precision(8);
sol_sock << "solution\n" << pmesh << l2gf << std::flush;
ParSubMesh fluid_mesh = ParSubMesh::CreateFromDomain(pmesh, left_domain);
out << "rank " << Mpi::WorldRank() << ": " << "fluid_mesh NE: " <<
fluid_mesh.GetNE() << "\n";
NavierSolver navier(&fluid_mesh, polynomial_order, 1.0);
auto ugf = navier.GetCurrentVelocity();
VectorFunctionCoefficient analytical_velocity_coeff(2, analytical_velocity);
ugf->ProjectCoefficient(analytical_velocity_coeff);
auto pgf = navier.GetCurrentPressure();
FunctionCoefficient analytical_pressure_coeff(analytical_pressure);
pgf->ProjectCoefficient(analytical_pressure_coeff);
auto mugf = navier.GetVariableViscosity();
ParSubMesh solid_mesh = ParSubMesh::CreateFromDomain(pmesh, right_domain);
Array<int> interface_marker_solid(solid_mesh.bdr_attributes.Max());
interface_marker_solid = 0;
interface_marker_solid[2] = 1;
auto &primary_mesh = solid_mesh;
auto &secondary_mesh = fluid_mesh;
auto &ir_face = navier.gll_ir_face;
// Compute the requested QoI on the secondary mesh
Vector qoi_mem;
const int qoi_size_on_qp = dim * dim;
auto qoi_func = [&](ElementTransformation &tr, int pt_idx, int num_pts)
{
auto qoi = Reshape(qoi_mem.ReadWrite(), dim, dim, num_pts);
auto A = Reshape(&qoi(0, 0, pt_idx), dim, dim);
const double p = pgf->GetValue(tr);
const double mu = mugf->GetValue(tr);
DenseMatrix dudx(dim, dim);
ugf->GetVectorGradient(tr, dudx);
// (-p * I + nu (grad(u) + grad(u)^T))
for (int i = 0; i < dim; i++)
{
for (int j = 0; j < dim; j++)
{
A(i, j) = -p * (i == j) + density * mu * (dudx(i, j) + dudx(j, i));
}
}
Vector transformed_ip(2);
tr.Transform(tr.GetIntPoint(), transformed_ip);
DenseMatrix B(Reshape(&qoi(0, 0, pt_idx), dim, dim), dim, dim);
// DenseMatrix *C = analytical_stress_mat(transformed_ip);
// out << " el_id: " << tr.ElementNo << "\n";
// out << "transformed ip: ("
// << transformed_ip(0) << ","
// << transformed_ip(1) << ")\n";
// out << "\n";
// out << "computed\n";
out << "rank " << Mpi::WorldRank() << ": ";
print_matrix(B);
// out << "analytical\n";
// print_matrix(*C);
// out << "\n";
// DenseMatrix D(dim, dim);
// Add(1.0, B, -1.0, *C, D);
// if (D.MaxMaxNorm() > 1e-12)
// {
// MFEM_ABORT("ERROR");
// }
// delete C;
};
ContactQoiEvaluator(primary_mesh, secondary_mesh, interface_marker_solid,
ir_face, qoi_func, qoi_mem, qoi_size_on_qp);
const int num_pts = qoi_mem.Size() / qoi_size_on_qp;
auto qoi = Reshape(qoi_mem.ReadWrite(), dim, dim, num_pts);
if (Mpi::WorldRank() == 0)
{
out << "\n\n\n";
}
// The quadrature point data is now on the primary_mesh ranks
int pt_idx = 0;
for (int be = 0; be < primary_mesh.GetNBE(); be++)
{
const int bdr_el_attr = primary_mesh.GetBdrAttribute(be);
if (interface_marker_solid[bdr_el_attr-1] == 0)
{
continue;
}
auto tr = primary_mesh.GetBdrElementTransformation(be);
for (int qp = 0; qp < ir_face.GetNPoints(); qp++)
{
// const IntegrationPoint &ip = ir_face.IntPoint(qp);
// tr->SetIntPoint(&ip);
DenseMatrix B(Reshape(&qoi(0, 0, pt_idx), dim, dim), dim, dim);
pt_idx++;
out << "rank " << Mpi::WorldRank() << ": ";
print_matrix(B);
}
}
return 0;
}