-
Notifications
You must be signed in to change notification settings - Fork 5
/
chef_phasta_sam_adaptLoop.cc
137 lines (128 loc) · 3.85 KB
/
chef_phasta_sam_adaptLoop.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
#include "chefPhasta.h"
#include <PCU.h>
#include <chef.h>
#include <phasta.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_sam_adaptLoop.cc
\brief Example in-memory driver for adaptive loops
\remark Runs Chef and then PHASTA until the user-specified maximum
PHASTA time step is reached. Size fields to drive adaptation
are defined using SAM from
<a href=https://github.com/SCOREC/core>core</a>.
This example also demonstrates the use of the fine grained
chef.h and phasta.h APIs.
*/
namespace {
void printElapsedTime(const char* key, double s) {
if( !PCU_Comm_Self() )
fprintf(stderr, "%s elapsed time %.3f\n", key, PCU_Time()-s);
}
void freeMesh(apf::Mesh* m) {
m->destroyNative();
apf::destroyMesh(m);
}
apf::Field* getField(apf::Mesh* m) {
/* if the value of the fldIdx'th index from the fldName
* field is greater than fldLimit then multiply the current
* isotropic mesh size at the vertex by szFactor */
const unsigned fldIdx = 5;
const double fldLimit = 1e-6;
const double szFactor = 0.5;
const char* fldName = "errors";
return sam::errorThreshold(m,fldName,fldIdx,fldLimit,szFactor);
}
static FILE* openfile_read(ph::Input&, const char* path) {
return fopen(path, "r");
}
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;
}
}
}
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]);
double start = PCU_Time();
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,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;
do {
double stepStart = PCU_Time();
step = phasta(inp,grs,rs);
clearGRStream(grs);
if(!PCU_Comm_Self())
fprintf(stderr, "STATUS ran to step %d\n", step);
if( step >= maxStep )
break;
setupChef(ctrl,step);
chef::readAndAttachFields(ctrl,m);
apf::Field* szFld = getField(m);
assert(szFld);
chef::adapt(m,szFld);
apf::destroyField(szFld);
chef::balance(ctrl,m);
chef::preprocess(m,ctrl,grs);
clearRStream(rs);
printElapsedTime("endOfStep", stepStart);
printElapsedTime("total", start);
} while( step < maxStep );
destroyGRStream(grs);
destroyRStream(rs);
freeMesh(m);
chefPhasta::finalizeModelers();
PCU_Comm_Free();
MPI_Finalize();
}