-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
282 lines (233 loc) · 7.31 KB
/
main.cc
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
#include <PCU.h>
#include <pumi.h>
#include <apf.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <cmath>
using namespace std;
#include "reorder.cc"
#include "element_routines.cc"
#include "matrix_assembly.cc"
#include "postprocessing_routines.cc"
#include <Eigen/Dense>
using namespace Eigen;
int main(int argc, char** argv)
{
bool isproblemquadratic = true;
// Check if files have been passed properly
if (argc != 3) {
printf("usage: %s dmg_file.dmg smb_file_without_0.smb\n", argv[0]);
return 0;
}
// Load the files passed
MPI_Init(&argc,&argv);
pumi_start();
pGeom geom = pumi_geom_load(argv[1],"mesh");
pMesh mesh = pumi_mesh_load(geom,argv[2],1);
if (isproblemquadratic){
// Convert some mesh to Lagrange or Serendipity
if(!strcmp (argv[1], "reorder_a.dmg")) {
pumi_mesh_setShape(mesh,pumi_shape_getSerendipity());
}
else{
pumi_mesh_setShape(mesh,pumi_shape_getLagrange(2));
}
}
printf("\nMesh and Geometry Successfully loaded\n");
// Reorder mesh
pNumbering numbering;
numbering = reorder_mesh(mesh,geom);
printf("Completed reordering\n");
// Get all the boundary edges
std::vector<boundary_struct> boundary_edges;
get_all_boundary_edges(geom,mesh, boundary_edges);
// Print all the faces the boundary edges are on
for (std::vector<boundary_struct>::iterator it = boundary_edges.begin(); it!= boundary_edges.end(); ++it){
boundary_struct this_edge = *it;
//printf("Edge is on face %d \n", this_edge.boundary);
}
// Get all the boundary vertices
std::vector<boundary_struct> boundary_verts;
get_all_boundary_nodes(mesh, boundary_edges, boundary_verts, numbering);
// Print all the faces the boundary vertices are on
for (std::vector<boundary_struct>::iterator it = boundary_verts.begin(); it!= boundary_verts.end(); ++it){
boundary_struct this_vert = *it;
printf("Vertex %d is on face %d \n", pumi_node_getNumber(numbering, this_vert.e), this_vert.boundary);
}
// Try to find which boundary an entity is on (pass the appropriate list)
std::vector<int> list;
int tofind = 0;
pMeshEnt ment = pumi_mesh_findEnt(mesh,0,tofind);
get_bound_num(ment, boundary_verts, list);
for (int i = 0; i< list.size(); i++){
//printf("The vertex %d is on %d\n",tofind,list[i]);
}
// Check if all the vertices are being returned in clockwise order
// Return error if violated
pMeshIter it;
pMeshEnt e;
it = mesh->begin(2);
while ((e = mesh->iterate(it))){
Adjacent adjacent;
pumi_ment_getAdjacent(e,0,adjacent);
if(reorder_verts(adjacent)){
printf(" Vertices need reordering \n");
return 0;
}
}
mesh->end(it);
printf("Vertices do not need reordering\n");
// Generate all the elemental stiffness matrices
std::vector<contribution> all_contributions;
// Generate the region contributions
it = mesh->begin(2);
while ((e = mesh->iterate(it))){
region_routine(mesh, e, numbering, all_contributions);
}
mesh->end(it);
printf("Generated region contributions\n");
// Generate boundary contributions
for (int i = 0; i < boundary_edges.size(); i++){
edge_routine(mesh, boundary_edges[i], numbering, all_contributions);
}
printf("Generated edge contributions \n");
// Generate the global stiffness matrix and the b vector
// Generate the vector of knowns
int m = pumi_numbering_getNumNode(numbering);
printf("Total number of nodes is %d \n", m);
MatrixXf A = MatrixXf::Zero(m, m);
VectorXf b = VectorXf::Zero(m);
VectorXf u = VectorXf::Zero(m);
// Assemble the Global stiffness matrix and the vector of knowns
for (int i = 0; i < all_contributions.size(); i++){
A(all_contributions[i].row,all_contributions[i].column) += all_contributions[i].coefficient;
//printf("the coefficient is %f \n", all_contributions[i].coefficient);
b(all_contributions[i].row) += all_contributions[i].known;
}
// Check the symmetry of the system
for (int i = 0; i < m; i++){
for (int j = 0; j < m; j++){
//printf(" %.10f \n", A[i][j]-A[j][i]);
if (A(i,j)-A(j,i) > 0.00000001){
printf(" Global Stiffnesss matrix is NOT symmetrical \n");
printf("row %d column %d \n", i,j);
return 0;
}
A(i,j) = A(j,i);
}
//printf("\n");
}
// Check the known vector
for (int i = 0; i < m; i++){
//printf("row %d known %f \n", i, b[i]);
}
// If a mixed boundary condition has not been specified,
// essential boundary conditions may need to be enforced
// Enforce the dirichlet boundary conditions
for (std::vector<boundary_struct>::iterator it = boundary_verts.begin(); it!= boundary_verts.end(); ++it){
boundary_struct this_vert = *it;
//printf("Vertex %d is on face %d \n", pumi_node_getNumber(numbering, this_vert.e), this_vert.boundary);
BC essential = essential_BC(this_vert.boundary, this_vert.e, numbering);
if(essential.first){
int rcn = pumi_node_getNumber (numbering, this_vert.e);
A(rcn,rcn) = 100000000000000;
b(rcn) = essential.second*100000000000000;
}
}
// Solve the system
u = A.partialPivLu().solve(b);
// Apply the solution to the field nodes
pField myfield = pumi_field_create(mesh,"Primary Solution",1);
// Apply to vertex nodes
it = mesh->begin(0);
while ((e = mesh->iterate(it))){
double myvar[1] = {u(pumi_node_getNumber(numbering,e))};
apf::setScalar(myfield, e, 0, myvar[0]);
}
mesh->end(it);
// Apply to edge nodes
it = mesh->begin(1);
while ((e = mesh->iterate(it))){
if (hasNode(mesh, e)){
double myvar[1] = {u(pumi_node_getNumber(numbering,e))};
apf::setScalar(myfield, e, 0, myvar[0]);
}
}
mesh->end(it);
// Freeze field and write out mesh
pumi_field_freeze (myfield);
pumi_mesh_write(mesh,argv[2],"vtk");
printf("Mesh file written to file\n");
pumi_finalize();
MPI_Finalize();
printf("End of Program\n");
}
// Simple definitions of boundaries using faces.
// Define the essential boundary condition using the face it is classified on
BC essential_BC(int boundary_number, pMeshEnt e, pNumbering numbering){
BC BCe;
BCe.first = 1;
// Use this to enforce essential boundary conditions on a face
switch (boundary_number) {
case 0:
BCe.second = 1;
return BCe;
case 2:
BCe.second = -1;
return BCe;
case 6:
BCe.second = 1;
return BCe;
case 16:
BCe.second = -1;
return BCe;
default:
BCe.first = 0;
BCe.second = 0;
return BCe;
}
// Use this to enforce essential boundary conditions on specific nodes
/*
int number = pumi_node_getNumber (numbering, e);
switch (number) {
case 0:
BCe.first = 0;
BCe.second = 0;
return BCe;
default:
BCe.first = 0;
BCe.second = 0;
return BCe;
}
*/
}
// Define natural boundary condition using the face it is classified on
BC natural_BC(int boundary_number){
// push back alpha and then h (coefficeient and then known)
BC BCn;
switch (boundary_number) {
/*
case 0:
BCn.first = 10;
BCn.second = 100;
return BCn;
case 2:
BCn.first = 10;
BCn.second = -100;
return BCn;
case 6:
BCn.first = 10;
BCn.second = 100;
return BCn;
case 16:
BCn.first = 10;
BCn.second = -100;
return BCn;
*/
default:
BCn.first = 0;
BCn.second = 0;
return BCn;
}
}