-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.C
330 lines (292 loc) · 16.1 KB
/
main.C
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Config files
//#include <IBAMR_prefix_config.h>
//#include <IBTK_prefix_config.h>
#include <SAMRAI_config.h>
// Headers for basic PETSc functions
#include <petscsys.h>
// Headers for basic SAMRAI objects
#include <BergerRigoutsos.h>
#include <CartesianGridGeometry.h>
#include <LoadBalancer.h>
#include <StandardTagAndInitialize.h>
// Headers for application-specific algorithm/data structure objects
#include <ibamr/IBExplicitHierarchyIntegrator.h>
#include <ibamr/IBMethod.h>
#include <ibamr/IBStandardForceGen.h>
#include <ibamr/IBStandardInitializer.h>
#include <ibamr/INSStaggeredHierarchyIntegrator.h>
#include <ibamr/app_namespaces.h>
#include <ibtk/AppInitializer.h>
#include <ibtk/muParserCartGridFunction.h>
#include <ibtk/muParserRobinBcCoefs.h>
// Headers for application specific operations.
#include "update_target_point_positions.h"
// Function prototypes
void
output_data(
Pointer<PatchHierarchy<NDIM> > patch_hierarchy,
Pointer<INSHierarchyIntegrator> navier_stokes_integrator,
LDataManager* l_data_manager,
const int iteration_num,
const double loop_time,
const string& data_dump_dirname);
/*******************************************************************************
* For each run, the input filename and restart information (if needed) must *
* be given on the command line. For non-restarted case, command line is: *
* *
* executable <input file name> *
* *
* For restarted run, command line is: *
* *
* executable <input file name> <restart directory> <restart number> *
* *
*******************************************************************************/
int
main(
int argc,
char* argv[])
{
// Initialize PETSc, MPI, and SAMRAI.
PetscInitialize(&argc,&argv,PETSC_NULL,PETSC_NULL);
SAMRAI_MPI::setCommunicator(PETSC_COMM_WORLD);
SAMRAI_MPI::setCallAbortInSerialInsteadOfExit();
SAMRAIManager::startup();
{// cleanup dynamically allocated objects prior to shutdown
// Parse command line options, set some standard options from the input
// file, initialize the restart database (if this is a restarted run),
// and enable file logging.
Pointer<AppInitializer> app_initializer = new AppInitializer(argc, argv, "IB.log");
Pointer<Database> input_db = app_initializer->getInputDatabase();
// Get various standard options set in the input file.
const bool dump_viz_data = app_initializer->dumpVizData();
const int viz_dump_interval = app_initializer->getVizDumpInterval();
const bool uses_visit = dump_viz_data && !app_initializer->getVisItDataWriter().isNull();
const bool is_from_restart = app_initializer->isFromRestart();
const bool dump_restart_data = app_initializer->dumpRestartData();
const int restart_dump_interval = app_initializer->getRestartDumpInterval();
const string restart_dump_dirname = app_initializer->getRestartDumpDirectory();
const bool dump_postproc_data = app_initializer->dumpPostProcessingData();
const int postproc_data_dump_interval = app_initializer->getPostProcessingDataDumpInterval();
const string postproc_data_dump_dirname = app_initializer->getPostProcessingDataDumpDirectory();
if (dump_postproc_data && (postproc_data_dump_interval > 0) && !postproc_data_dump_dirname.empty())
{
Utilities::recursiveMkdir(postproc_data_dump_dirname);
}
const bool dump_timer_data = app_initializer->dumpTimerData();
const int timer_dump_interval = app_initializer->getTimerDumpInterval();
// Create major algorithm and data objects that comprise the
// application. These objects are configured from the input database
// and, if this is a restarted run, from the restart database.
Pointer<IBMethod> ib_method_ops = new IBMethod("IBMethod", app_initializer->getComponentDatabase("IBMethod"));
Pointer<INSHierarchyIntegrator> navier_stokes_integrator = new INSStaggeredHierarchyIntegrator("INSStaggeredHierarchyIntegrator", app_initializer->getComponentDatabase("INSStaggeredHierarchyIntegrator"));
Pointer<IBHierarchyIntegrator> time_integrator = new IBExplicitHierarchyIntegrator("IBHierarchyIntegrator", app_initializer->getComponentDatabase("IBHierarchyIntegrator"), ib_method_ops, navier_stokes_integrator);
Pointer<CartesianGridGeometry<NDIM> > grid_geometry = new CartesianGridGeometry<NDIM>("CartesianGeometry", app_initializer->getComponentDatabase("CartesianGeometry"));
Pointer<PatchHierarchy<NDIM> > patch_hierarchy = new PatchHierarchy<NDIM>("PatchHierarchy", grid_geometry);
Pointer<StandardTagAndInitialize<NDIM> > error_detector = new StandardTagAndInitialize<NDIM>("StandardTagAndInitialize", time_integrator, app_initializer->getComponentDatabase("StandardTagAndInitialize"));
Pointer<BergerRigoutsos<NDIM> > box_generator = new BergerRigoutsos<NDIM>();
Pointer<LoadBalancer<NDIM> > load_balancer = new LoadBalancer<NDIM>("LoadBalancer", app_initializer->getComponentDatabase("LoadBalancer"));
Pointer<GriddingAlgorithm<NDIM> > gridding_algorithm = new GriddingAlgorithm<NDIM>("GriddingAlgorithm", app_initializer->getComponentDatabase("GriddingAlgorithm"), error_detector, box_generator, load_balancer);
// Configure the IB solver.
Pointer<IBStandardInitializer> ib_initializer = new IBStandardInitializer("IBStandardInitializer", app_initializer->getComponentDatabase("IBStandardInitializer"));
ib_method_ops->registerLInitStrategy(ib_initializer);
Pointer<IBStandardForceGen> ib_force_fcn = new IBStandardForceGen();
ib_method_ops->registerIBLagrangianForceFunction(ib_force_fcn);
// Create Eulerian initial condition specification objects.
if (input_db->keyExists("VelocityInitialConditions"))
{
navier_stokes_integrator->registerVelocityInitialConditions(new muParserCartGridFunction("u_init", app_initializer->getComponentDatabase("VelocityInitialConditions"), grid_geometry));
}
if (input_db->keyExists("PressureInitialConditions"))
{
navier_stokes_integrator->registerPressureInitialConditions(new muParserCartGridFunction("p_init", app_initializer->getComponentDatabase("PressureInitialConditions"), grid_geometry));
}
// Create Eulerian boundary condition specification objects (when necessary).
const bool periodic_domain = grid_geometry->getPeriodicShift().min() > 0;
std::vector<RobinBcCoefStrategy<NDIM>*> u_bc_coefs(NDIM,static_cast<RobinBcCoefStrategy<NDIM>*>(NULL));
if (!periodic_domain)
{
for (unsigned int d = 0; d < NDIM; ++d)
{
ostringstream bc_coefs_name_stream;
bc_coefs_name_stream << "u_bc_coefs_" << d;
const string bc_coefs_name = bc_coefs_name_stream.str();
ostringstream bc_coefs_db_name_stream;
bc_coefs_db_name_stream << "VelocityBcCoefs_" << d;
const string bc_coefs_db_name = bc_coefs_db_name_stream.str();
u_bc_coefs[d] = new muParserRobinBcCoefs(bc_coefs_name, app_initializer->getComponentDatabase(bc_coefs_db_name), grid_geometry);
}
navier_stokes_integrator->registerPhysicalBoundaryConditions(u_bc_coefs);
}
// Create Eulerian body force function specification objects.
if (input_db->keyExists("ForcingFunction"))
{
time_integrator->registerBodyForceFunction(new muParserCartGridFunction("f_fcn", app_initializer->getComponentDatabase("ForcingFunction"), grid_geometry));
}
// Set up visualization plot file writers.
Pointer<VisItDataWriter<NDIM> > visit_data_writer = app_initializer->getVisItDataWriter();
Pointer<LSiloDataWriter> silo_data_writer = app_initializer->getLSiloDataWriter();
if (uses_visit)
{
ib_initializer->registerLSiloDataWriter(silo_data_writer);
time_integrator->registerVisItDataWriter(visit_data_writer);
ib_method_ops->registerLSiloDataWriter(silo_data_writer);
}
// Initialize hierarchy configuration and data on all patches.
time_integrator->initializePatchHierarchy(patch_hierarchy, gridding_algorithm);
// Deallocate initialization objects.
ib_method_ops->freeLInitStrategy();
ib_initializer.setNull();
app_initializer.setNull();
// Print the input database contents to the log file.
plog << "Input database:\n";
input_db->printClassData(plog);
// Write restart data before starting main time integration loop.
if (dump_restart_data && !is_from_restart)
{
pout << "\nWriting restart files...\n\n";
RestartManager::getManager()->writeRestartFile(restart_dump_dirname, 0);
}
// Write out initial visualization data.
int iteration_num = time_integrator->getIntegratorStep();
double loop_time = time_integrator->getIntegratorTime();
if (dump_viz_data && uses_visit)
{
pout << "\n\nWriting visualization files...\n\n";
time_integrator->setupPlotData();
visit_data_writer->writePlotData(patch_hierarchy, iteration_num, loop_time);
silo_data_writer->writePlotData(iteration_num, loop_time);
}
// Main time step loop.
double loop_time_end = time_integrator->getEndTime();
double dt = 0.0;
while (!MathUtilities<double>::equalEps(loop_time,loop_time_end) && time_integrator->stepsRemaining())
{
iteration_num = time_integrator->getIntegratorStep();
loop_time = time_integrator->getIntegratorTime();
pout << "\n";
pout << "+++++++++++++++++++++++++++++++++++++++++++++++++++\n";
pout << "At beginning of timestep # " << iteration_num << "\n";
pout << "Simulation time is " << loop_time << "\n";
dt = time_integrator->getMaximumTimeStepSize();
LDataManager* l_data_manager = ib_method_ops->getLDataManager();
update_target_point_positions(patch_hierarchy, l_data_manager, loop_time, dt);
time_integrator->advanceHierarchy(dt);
loop_time += dt;
pout << "\n";
pout << "At end of timestep # " << iteration_num << "\n";
pout << "Simulation time is " << loop_time << "\n";
pout << "+++++++++++++++++++++++++++++++++++++++++++++++++++\n";
pout << "\n";
// At specified intervals, write visualization and restart files,
// print out timer data, and store hierarchy data for post
// processing.
iteration_num += 1;
const bool last_step = !time_integrator->stepsRemaining();
if (dump_viz_data && uses_visit && (iteration_num%viz_dump_interval == 0 || last_step))
{
pout << "\nWriting visualization files...\n\n";
time_integrator->setupPlotData();
visit_data_writer->writePlotData(patch_hierarchy, iteration_num, loop_time);
silo_data_writer->writePlotData(iteration_num, loop_time);
}
if (dump_restart_data && (iteration_num%restart_dump_interval == 0 || last_step))
{
pout << "\nWriting restart files...\n\n";
RestartManager::getManager()->writeRestartFile(restart_dump_dirname, iteration_num);
}
if (dump_timer_data && (iteration_num%timer_dump_interval == 0 || last_step))
{
pout << "\nWriting timer data...\n\n";
TimerManager::getManager()->print(plog);
}
if (dump_postproc_data && (iteration_num%postproc_data_dump_interval == 0 || last_step))
{
output_data(patch_hierarchy, navier_stokes_integrator, l_data_manager, iteration_num, loop_time, postproc_data_dump_dirname);
}
}
// Cleanup Eulerian boundary condition specification objects (when
// necessary).
for (unsigned int d = 0; d < NDIM; ++d) delete u_bc_coefs[d];
}// cleanup dynamically allocated objects prior to shutdown
SAMRAIManager::shutdown();
PetscFinalize();
return 0;
}// main
void
output_data(
Pointer<PatchHierarchy<NDIM> > patch_hierarchy,
Pointer<INSHierarchyIntegrator> navier_stokes_integrator,
LDataManager* l_data_manager,
const int iteration_num,
const double loop_time,
const string& data_dump_dirname)
{
plog << "writing hierarchy data at iteration " << iteration_num << " to disk" << endl;
plog << "simulation time is " << loop_time << endl;
// Write Cartesian data.
string file_name = data_dump_dirname + "/" + "hier_data.";
char temp_buf[128];
// sprintf(temp_buf, "%05d.samrai.%05d", iteration_num, SAMRAI_MPI::getRank());
// file_name += temp_buf;
// Pointer<HDFDatabase> hier_db = new HDFDatabase("hier_db");
// hier_db->create(file_name);
// VariableDatabase<NDIM>* var_db = VariableDatabase<NDIM>::getDatabase();
// ComponentSelector hier_data;
// hier_data.setFlag(var_db->mapVariableAndContextToIndex(navier_stokes_integrator->getVelocityVariable(), navier_stokes_integrator->getCurrentContext()));
// hier_data.setFlag(var_db->mapVariableAndContextToIndex(navier_stokes_integrator->getPressureVariable(), navier_stokes_integrator->getCurrentContext()));
// patch_hierarchy->putToDatabase(hier_db->putDatabase("PatchHierarchy"), hier_data);
// hier_db->putDouble("loop_time", loop_time);
// hier_db->putInteger("iteration_num", iteration_num);
// hier_db->close();
// Write Lagrangian data.
const int finest_hier_level = patch_hierarchy->getFinestLevelNumber();
PetscViewer viewer;
Pointer<LData> X_data = l_data_manager->getLData("X", finest_hier_level);
Vec X_petsc_vec = X_data->getVec();
Vec X_lag_vec;
VecDuplicate(X_petsc_vec, &X_lag_vec);
l_data_manager->scatterPETScToLagrangian(X_petsc_vec, X_lag_vec, finest_hier_level);
file_name = data_dump_dirname + "/" + "X.";
sprintf(temp_buf, "%05d", iteration_num);
file_name += temp_buf;
PetscViewerASCIIOpen(PETSC_COMM_WORLD, file_name.c_str(), &viewer);
VecView(X_lag_vec, viewer);
PetscViewerDestroy(&viewer);
VecDestroy(&X_lag_vec);
Pointer<LData> F_data = l_data_manager->getLData("F", finest_hier_level);
Vec F_petsc_vec = F_data->getVec();
Vec F_lag_vec;
VecDuplicate(F_petsc_vec, &F_lag_vec);
l_data_manager->scatterPETScToLagrangian(F_petsc_vec, F_lag_vec, finest_hier_level);
file_name = data_dump_dirname + "/" + "F.";
sprintf(temp_buf, "%05d", iteration_num);
file_name += temp_buf;
PetscViewerASCIIOpen(PETSC_COMM_WORLD, file_name.c_str(), &viewer);
VecView(F_lag_vec, viewer);
PetscViewerDestroy(&viewer);
VecDestroy(&F_lag_vec);
// Sum up the forces (this is equivalent to computing the discrete
// force density).
double F_sum[NDIM];
for (unsigned int d = 0; d < NDIM; ++d) F_sum[d] = 0.0;
int local_sz;
VecGetLocalSize(F_petsc_vec, &local_sz);
double* F_array;
VecGetArray(F_petsc_vec, &F_array);
for (int k = 0; k < local_sz/NDIM; ++k)
{
for (unsigned int d = 0; d < NDIM; ++d)
{
F_sum[d] += F_array[NDIM*k+d];
}
}
SAMRAI_MPI::sumReduction(&F_sum[0],NDIM);
VecRestoreArray(F_petsc_vec, &F_array);
pout << "integral{F} =";
for (unsigned int d = 0; d < NDIM; ++d)
{
pout << " " << F_sum[d];
}
pout << "\n";
return;
}// output_data