-
Notifications
You must be signed in to change notification settings - Fork 239
/
Dambreak.cpp
252 lines (240 loc) · 13.7 KB
/
Dambreak.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
/**
* @file dambreak.cpp
* @brief 2D dambreak example.
* @details This is the one of the basic test cases, also the first case for
* understanding SPH method for free surface flow simulation.
* @author Luhui Han, Chi Zhang and Xiangyu Hu
*/
#include "sphinxsys.h" //SPHinXsys Library.
using namespace SPH; // Namespace cite here.
//----------------------------------------------------------------------
// Basic geometry parameters and numerical setup.
//----------------------------------------------------------------------
Real DL = 5.366; /**< Water tank length. */
Real DH = 5.366; /**< Water tank height. */
Real LL = 2.0; /**< Water column length. */
Real LH = 1.0; /**< Water column height. */
Real particle_spacing_ref = 0.025; /**< Initial reference particle spacing. */
Real BW = particle_spacing_ref * 4; /**< Thickness of tank wall. */
//----------------------------------------------------------------------
// Material parameters.
//----------------------------------------------------------------------
Real rho0_f = 1.0; /**< Reference density of fluid. */
Real gravity_g = 1.0; /**< Gravity. */
Real U_ref = 2.0 * sqrt(gravity_g * LH); /**< Characteristic velocity. */
Real c_f = 10.0 * U_ref; /**< Reference sound speed. */
//----------------------------------------------------------------------
// Geometric shapes used in this case.
//----------------------------------------------------------------------
Vec2d water_block_halfsize = Vec2d(0.5 * LL, 0.5 * LH); // local center at origin
Vec2d water_block_translation = water_block_halfsize; // translation to global coordinates
Vec2d outer_wall_halfsize = Vec2d(0.5 * DL + BW, 0.5 * DH + BW);
Vec2d outer_wall_translation = Vec2d(-BW, -BW) + outer_wall_halfsize;
Vec2d inner_wall_halfsize = Vec2d(0.5 * DL, 0.5 * DH);
Vec2d inner_wall_translation = inner_wall_halfsize;
//----------------------------------------------------------------------
// Complex shape for wall boundary, note that no partial overlap is allowed
// for the shapes in a complex shape.
//----------------------------------------------------------------------
class WallBoundary : public ComplexShape
{
public:
explicit WallBoundary(const std::string &shape_name) : ComplexShape(shape_name)
{
add<TransformShape<GeometricShapeBox>>(Transform(outer_wall_translation), outer_wall_halfsize);
subtract<TransformShape<GeometricShapeBox>>(Transform(inner_wall_translation), inner_wall_halfsize);
}
};
//----------------------------------------------------------------------
// Main program starts here.
//----------------------------------------------------------------------
int main(int ac, char *av[])
{
//----------------------------------------------------------------------
// Build up an SPHSystem and IO environment.
//----------------------------------------------------------------------
BoundingBox system_domain_bounds(Vec2d(-BW, -BW), Vec2d(DL + BW, DH + BW));
SPHSystem sph_system(system_domain_bounds, particle_spacing_ref);
sph_system.handleCommandlineOptions(ac, av)->setIOEnvironment();
//----------------------------------------------------------------------
// Creating bodies with corresponding materials and particles.
//----------------------------------------------------------------------
TransformShape<GeometricShapeBox> initial_water_block(Transform(water_block_translation), water_block_halfsize, "WaterBody");
FluidBody water_block(sph_system, initial_water_block);
water_block.defineMaterial<WeaklyCompressibleFluid>(rho0_f, c_f);
water_block.generateParticles<BaseParticles, Lattice>();
SolidBody wall_boundary(sph_system, makeShared<WallBoundary>("WallBoundary"));
wall_boundary.defineMaterial<Solid>();
wall_boundary.generateParticles<BaseParticles, Lattice>();
ObserverBody fluid_observer(sph_system, "FluidObserver");
StdVec<Vecd> observation_location = {Vecd(DL, 0.2)};
fluid_observer.generateParticles<ObserverParticles>(observation_location);
//----------------------------------------------------------------------
// Define body relation map.
// The contact map gives the topological connections between the bodies.
// Basically the the range of bodies to build neighbor particle lists.
// Generally, we first define all the inner relations, then the contact relations.
//----------------------------------------------------------------------
InnerRelation water_block_inner(water_block);
ContactRelation water_wall_contact(water_block, {&wall_boundary});
ContactRelation fluid_observer_contact(fluid_observer, {&water_block});
//----------------------------------------------------------------------
// Combined relations built from basic relations
// which is only used for update configuration.
//----------------------------------------------------------------------
ComplexRelation water_wall_complex(water_block_inner, water_wall_contact);
//----------------------------------------------------------------------
// Define the numerical methods used in the simulation.
// Note that there may be data dependence on the sequence of constructions.
// Generally, the geometric models or simple objects without data dependencies,
// such as gravity, should be initiated first.
// Then the major physical particle dynamics model should be introduced.
// Finally, the auxiliary models such as time step estimator, initial condition,
// boundary condition and other constraints should be defined.
//----------------------------------------------------------------------
Gravity gravity(Vecd(0.0, -gravity_g));
SimpleDynamics<GravityForce<Gravity>> constant_gravity(water_block, gravity);
SimpleDynamics<NormalDirectionFromBodyShape> wall_boundary_normal_direction(wall_boundary);
Dynamics1Level<fluid_dynamics::Integration1stHalfWithWallRiemann> fluid_pressure_relaxation(water_block_inner, water_wall_contact);
Dynamics1Level<fluid_dynamics::Integration2ndHalfWithWallRiemann> fluid_density_relaxation(water_block_inner, water_wall_contact);
InteractionWithUpdate<fluid_dynamics::DensitySummationComplexFreeSurface> fluid_density_by_summation(water_block_inner, water_wall_contact);
ReduceDynamics<fluid_dynamics::AdvectionViscousTimeStep> fluid_advection_time_step(water_block, U_ref);
ReduceDynamics<fluid_dynamics::AcousticTimeStep> fluid_acoustic_time_step(water_block);
//----------------------------------------------------------------------
// Define the configuration related particles dynamics.
//----------------------------------------------------------------------
ParticleSorting particle_sorting(water_block);
//----------------------------------------------------------------------
// Define the methods for I/O operations, observations
// and regression tests of the simulation.
//----------------------------------------------------------------------
BodyStatesRecordingToVtp body_states_recording(sph_system);
body_states_recording.addToWrite<Vecd>(wall_boundary, "NormalDirection");
RestartIO restart_io(sph_system);
RegressionTestDynamicTimeWarping<ReducedQuantityRecording<TotalMechanicalEnergy>> write_water_mechanical_energy(water_block, gravity);
RegressionTestDynamicTimeWarping<ObservedQuantityRecording<Real>> write_recorded_water_pressure("Pressure", fluid_observer_contact);
//----------------------------------------------------------------------
// Prepare the simulation with cell linked list, configuration
// and case specified initial condition if necessary.
//----------------------------------------------------------------------
sph_system.initializeSystemCellLinkedLists();
sph_system.initializeSystemConfigurations();
wall_boundary_normal_direction.exec();
constant_gravity.exec();
//----------------------------------------------------------------------
// Load restart file if necessary.
//----------------------------------------------------------------------
Real &physical_time = *sph_system.getSystemVariableDataByName<Real>("PhysicalTime");
if (sph_system.RestartStep() != 0)
{
physical_time = restart_io.readRestartFiles(sph_system.RestartStep());
water_block.updateCellLinkedList();
water_wall_complex.updateConfiguration();
fluid_observer_contact.updateConfiguration();
}
//----------------------------------------------------------------------
// Setup for time-stepping control
//----------------------------------------------------------------------
size_t number_of_iterations = sph_system.RestartStep();
int screen_output_interval = 100;
int observation_sample_interval = screen_output_interval * 2;
int restart_output_interval = screen_output_interval * 10;
Real end_time = 20.0;
Real output_interval = 0.1;
//----------------------------------------------------------------------
// Statistics for CPU time
//----------------------------------------------------------------------
TickCount t1 = TickCount::now();
TimeInterval interval;
TimeInterval interval_computing_time_step;
TimeInterval interval_computing_fluid_pressure_relaxation;
TimeInterval interval_updating_configuration;
TickCount time_instance;
//----------------------------------------------------------------------
// First output before the main loop.
//----------------------------------------------------------------------
body_states_recording.writeToFile();
write_water_mechanical_energy.writeToFile(number_of_iterations);
write_recorded_water_pressure.writeToFile(number_of_iterations);
//----------------------------------------------------------------------
// Main loop starts here.
//----------------------------------------------------------------------
while (physical_time < end_time)
{
Real integration_time = 0.0;
/** Integrate time (loop) until the next output time. */
while (integration_time < output_interval)
{
/** outer loop for dual-time criteria time-stepping. */
time_instance = TickCount::now();
Real advection_dt = fluid_advection_time_step.exec();
fluid_density_by_summation.exec();
interval_computing_time_step += TickCount::now() - time_instance;
time_instance = TickCount::now();
Real relaxation_time = 0.0;
Real acoustic_dt = 0.0;
while (relaxation_time < advection_dt)
{
/** inner loop for dual-time criteria time-stepping. */
acoustic_dt = fluid_acoustic_time_step.exec();
fluid_pressure_relaxation.exec(acoustic_dt);
fluid_density_relaxation.exec(acoustic_dt);
relaxation_time += acoustic_dt;
integration_time += acoustic_dt;
physical_time += acoustic_dt;
}
interval_computing_fluid_pressure_relaxation += TickCount::now() - time_instance;
/** screen output, write body observables and restart files */
if (number_of_iterations % screen_output_interval == 0)
{
std::cout << std::fixed << std::setprecision(9) << "N=" << number_of_iterations << " Time = "
<< physical_time
<< " advection_dt = " << advection_dt << " acoustic_dt = " << acoustic_dt << "\n";
if (number_of_iterations % observation_sample_interval == 0 && number_of_iterations != sph_system.RestartStep())
{
write_water_mechanical_energy.writeToFile(number_of_iterations);
write_recorded_water_pressure.writeToFile(number_of_iterations);
}
if (number_of_iterations % restart_output_interval == 0)
restart_io.writeToFile(number_of_iterations);
}
number_of_iterations++;
/** Update cell linked list and configuration. */
time_instance = TickCount::now();
if (number_of_iterations % 100 == 0 && number_of_iterations != 1)
{
particle_sorting.exec();
}
water_block.updateCellLinkedList();
water_wall_complex.updateConfiguration();
fluid_observer_contact.updateConfiguration();
interval_updating_configuration += TickCount::now() - time_instance;
}
body_states_recording.writeToFile();
TickCount t2 = TickCount::now();
TickCount t3 = TickCount::now();
interval += t3 - t2;
}
TickCount t4 = TickCount::now();
TimeInterval tt;
tt = t4 - t1 - interval;
std::cout << "Total wall time for computation: " << tt.seconds()
<< " seconds." << std::endl;
std::cout << std::fixed << std::setprecision(9) << "interval_computing_time_step ="
<< interval_computing_time_step.seconds() << "\n";
std::cout << std::fixed << std::setprecision(9) << "interval_computing_fluid_pressure_relaxation = "
<< interval_computing_fluid_pressure_relaxation.seconds() << "\n";
std::cout << std::fixed << std::setprecision(9) << "interval_updating_configuration = "
<< interval_updating_configuration.seconds() << "\n";
if (sph_system.GenerateRegressionData())
{
write_water_mechanical_energy.generateDataBase(1.0e-3);
write_recorded_water_pressure.generateDataBase(1.0e-3);
}
else if (sph_system.RestartStep() == 0)
{
write_water_mechanical_energy.testResult();
write_recorded_water_pressure.testResult();
}
return 0;
};