-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchef_phasta_loop_aro.cc
267 lines (255 loc) · 9.38 KB
/
chef_phasta_loop_aro.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
#include "chefPhasta.h"
#include "samSz.h"
#include <apf.h>
#include <PCU.h>
#include <chef.h>
#include <phasta.h>
#include "phIO.h"
#include <phstream.h>
#include <sam.h>
#include <apfMDS.h>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
/** \file chef_phasta_loop_aro.cc
\brief In-memory driver for adaptive loops to support the ARO project
\remark Runs Chef and then PHASTA until the user-specified maximum
PHASTA time step is reached.
*/
#ifndef WRITE_VTK
#define WRITE_VTK
#endif
namespace {
void freeMesh(apf::Mesh* m) {
m->destroyNative();
apf::destroyMesh(m);
}
apf::Field* multipleSF(apf::Mesh* m, apf::Field* sf, double factor) {
apf::Field* sz = createFieldOn(m, "multipliedSize", apf::SCALAR);
apf::MeshEntity* vtx;
apf::MeshIterator* itr = m->begin(0);
while( (vtx = m->iterate(itr)) ) {
double h = apf::getScalar(sf,vtx,0);
apf::setScalar(sz,vtx,0,h*factor);
}
m->end(itr);
return sz;
}
static FILE* openstream_read(ph::Input& in, const char* path) {
std::string fname(path);
std::string restartStr("restart");
FILE* f = NULL;
if( fname.find(restartStr) != std::string::npos )
f = openRStreamRead(in.rs);
else {
fprintf(stderr,
"ERROR %s type of stream %s is unknown... exiting\n",
__func__, fname.c_str());
exit(1);
}
return f;
}
void setupChef(ph::Input& ctrl, int step) {
//don't split or tetrahedronize
ctrl.splitFactor = 1;
ctrl.tetrahedronize = 0;
ctrl.timeStepNumber = step;
ctrl.solutionMigration = 1;
if(step>1) {
if(!PCU_Comm_Self()) {
fprintf(stderr, "STATUS error based adapt %d\n", step);
fprintf(stderr, "STATUS ctrl.attributeFileName %s step %d\n",
ctrl.attributeFileName.c_str(), step);
}
ctrl.adaptStrategy = 1; //error field adapt
ctrl.adaptFlag = 1;
}
}
bool overwriteMeshCoord(apf::Mesh2* m) {
apf::Field* f = m->findField("motion_coords");
assert(f);
double* vals = new double[apf::countComponents(f)];
assert(apf::countComponents(f) == 3);
apf::MeshEntity* vtx;
apf::Vector3 points;
apf::MeshIterator* itr = m->begin(0);
while( (vtx = m->iterate(itr)) ) {
apf::getComponents(f, vtx, 0, vals);
for ( int i = 0; i < 3; i++ ) points[i] = vals[i];
m->setPoint(vtx, 0, points);
}
m->end(itr);
delete [] vals;
return true;
}
bool isMeshqGood(apf::Mesh* m, double crtn) {
apf::Field* meshq = m->findField("meshQ");
if (!meshq) {
fprintf(stderr, "Not find meshQ field.");
return true;
}
apf::MeshEntity* elm;
apf::MeshIterator* itr = m->begin(m->getDimension());
while( (elm = m->iterate(itr)) ) {
if (apf::getScalar(meshq, elm, 0) < crtn) {
apf::destroyField(meshq);
return false;
}
}
m->end(itr);
apf::destroyField(meshq);
return true;
}
void writeSequence (apf::Mesh2* m, int step, const char* filename) {
std::ostringstream oss;
oss << filename << step;
const std::string tmp = oss.str();
#ifdef WRITE_VTK
apf::writeVtkFiles(tmp.c_str(),m);
#endif
}
void writePHTfiles (int step, int nstep, int nproc) {
std::ostringstream oss;
oss << "solution_" << step << ".pht";
const std::string tp = oss.str();
const char* filename = tp.c_str();
FILE* sFile = fopen (filename, "w");
fprintf (sFile, "<?xml version=\"1.0\" ?>\n");
fprintf (sFile, "<PhastaMetaFile number_of_pieces=\"%d\">\n", nproc);
fprintf (sFile, " <GeometryFileNamePattern pattern=\"%d/%d-procs_case/geombc.%d.%%d\"\n",step,nproc,step);
fprintf (sFile, " has_piece_entry=\"1\"\n");
fprintf (sFile, " has_time_entry=\"0\"/>\n");
fprintf (sFile, " <FieldFileNamePattern pattern=\"%d-procs_case/restart.%%d.%%d\"\n",nproc);
fprintf (sFile, " has_piece_entry=\"1\"\n");
fprintf (sFile, " has_time_entry=\"1\"/>\n");
fprintf (sFile, " <TimeSteps number_of_steps=\"%d\"\n", nstep);
fprintf (sFile, " auto_generate_indices=\"1\"\n");
fprintf (sFile, " start_index=\"%d\"\n", step+1);
fprintf (sFile, " increment_index_by=\"1\"\n");
fprintf (sFile, " start_value=\"0.0\"\n");
fprintf (sFile, " increment_value_by=\"1.0e-6\">\n");
fprintf (sFile, " </TimeSteps>\n");
fprintf (sFile, " <Fields number_of_fields=\"7\">\n");
fprintf (sFile, " <Field paraview_field_tag=\"pressure\"\n");
fprintf (sFile, " phasta_field_tag=\"solution\"\n");
fprintf (sFile, " start_index_in_phasta_array=\"0\"\n");
fprintf (sFile, " number_of_components=\"1\"/>\n");
fprintf (sFile, " <Field paraview_field_tag=\"velocity\"\n");
fprintf (sFile, " phasta_field_tag=\"solution\"\n");
fprintf (sFile, " start_index_in_phasta_array=\"1\"\n");
fprintf (sFile, " number_of_components=\"3\"\n");
fprintf (sFile, " data_dependency=\"0\"\n");
fprintf (sFile, " data_type=\"double\"/>\n");
fprintf (sFile, " <Field paraview_field_tag=\"temperature\"\n");
fprintf (sFile, " phasta_field_tag=\"solution\"\n");
fprintf (sFile, " start_index_in_phasta_array=\"4\"\n");
fprintf (sFile, " number_of_components=\"1\"/>\n");
fprintf (sFile, " <Field paraview_field_tag=\"motion_coords\"\n");
fprintf (sFile, " phasta_field_tag=\"motion_coords\"\n");
fprintf (sFile, " start_index_in_phasta_array=\"0\"\n");
fprintf (sFile, " number_of_components=\"3\"\n");
fprintf (sFile, " data_dependency=\"0\"\n");
fprintf (sFile, " data_type=\"double\"/>\n");
fprintf (sFile, " <Field paraview_field_tag=\"mesh_vel\"\n");
fprintf (sFile, " phasta_field_tag=\"mesh_vel\"\n");
fprintf (sFile, " start_index_in_phasta_array=\"0\"\n");
fprintf (sFile, " number_of_components=\"3\"\n");
fprintf (sFile, " data_dependency=\"0\"\n");
fprintf (sFile, " data_type=\"double\"/>\n");
fprintf (sFile, " <Field paraview_field_tag=\"material_type\"\n");
fprintf (sFile, " phasta_field_tag=\"material_type\"\n");
fprintf (sFile, " start_index_in_phasta_array=\"0\"\n");
fprintf (sFile, " number_of_components=\"1\"\n");
fprintf (sFile, " data_dependency=\"1\"/>\n");
fprintf (sFile, " <Field paraview_field_tag=\"meshQ\"\n");
fprintf (sFile, " phasta_field_tag=\"meshQ\"\n");
fprintf (sFile, " start_index_in_phasta_array=\"0\"\n");
fprintf (sFile, " number_of_components=\"1\"\n");
fprintf (sFile, " data_dependency=\"1\"/>\n");
fprintf (sFile, " </Fields>\n");
fprintf (sFile, "</PhastaMetaFile>\n");
fclose (sFile);
}
}
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
PCU_Comm_Init();
PCU_Protect();
if( argc != 2 ) {
if(!PCU_Comm_Self())
fprintf(stderr, "Usage: %s <maxTimeStep>\n",argv[0]);
exit(EXIT_FAILURE);
}
int maxStep = atoi(argv[1]);
chefPhasta::initModelers();
grstream grs = makeGRStream();
ph::Input ctrl;
ctrl.load("samAdaptLoop.inp");
/* setup file reading */
// ctrl.openfile_read = openfile_read;
/* load the model and mesh */
apf::Mesh2* m = apf::loadMdsMesh(
ctrl.modelFileName.c_str(),ctrl.meshFileName.c_str());
chef::preprocess(m,ctrl);
chef::preprocess(m,ctrl,grs);
rstream rs = makeRStream();
/* setup stream reading */
ctrl.openfile_read = openstream_read;
ctrl.rs = rs;
phSolver::Input inp("solver.inp", "input.config");
int step = 0; int phtStep = 0;
int loop = 0;
int seq = 0;
writeSequence(m,seq,"test_"); seq++;
do {
m->verify();
/* take the initial mesh as size field */
apf::Field* isoSF = samSz::isoSize(m);
apf::Field* szFld = multipleSF(m, isoSF, 1.1);
step = phasta(inp,grs,rs);
ctrl.rs = rs;
clearGRStream(grs);
if(!PCU_Comm_Self())
fprintf(stderr, "STATUS ran to step %d\n", step);
setupChef(ctrl,step);
chef::readAndAttachFields(ctrl,m);
m->verify();
overwriteMeshCoord(m);
m->verify();
bool doAdaptation = !isMeshqGood(m, ctrl.meshqCrtn);
m->verify();
// make the adaptaion run anyway
// doAdaptation = false;
doAdaptation = true;
// delele above when finish debug
apf::destroyField(m->findField("material_type"));
m->verify();
if ( doAdaptation ) {
writePHTfiles(phtStep, step-phtStep, PCU_Comm_Peers()); phtStep = step;
writeSequence(m,seq,"test_"); seq++;
}
/* Or obtain size field based on a certain field*/
// apf::Field* szFld = getField(m);
apf::synchronize(szFld);
apf::synchronize(m->getCoordinateField());
assert(szFld);
if ( doAdaptation ) {
chef::adapt(m,szFld,ctrl);
writeSequence(m,seq,"test_"); seq++;
m->verify();
}
apf::destroyField(szFld);
chef::balanceAndReorder(ctrl,m);
chef::preprocess(m,ctrl,grs);
clearRStream(rs);
loop++;
} while( loop < maxStep );
destroyGRStream(grs);
destroyRStream(rs);
freeMesh(m);
chefPhasta::finalizeModelers();
PCU_Comm_Free();
MPI_Finalize();
}