/
2d_flow_stream_around_fish.cpp
280 lines (266 loc) · 16 KB
/
2d_flow_stream_around_fish.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
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
/**
* @file 2d_flow_stream_around_fish.cpp
* @brief fish swimming driven by active muscles
* @author Yaru Ren and Xiangyu Hu
*/
#include "2d_flow_stream_around_fish.h"
#include "sphinxsys.h"
using namespace SPH;
int main(int ac, char *av[])
{
//----------------------------------------------------------------------
// Build up the environment of a SPHSystem.
//----------------------------------------------------------------------
BoundingBox system_domain_bounds(Vec2d(-DL_sponge - BW, -BW), Vec2d(DL + BW, DH + BW));
SPHSystem sph_system(system_domain_bounds, particle_spacing_ref);
/** Tag for run particle relaxation for the initial body fitted distribution. */
sph_system.setRunParticleRelaxation(false);
/** Tag for computation start with relaxed body fitted particles distribution. */
sph_system.setReloadParticles(false);
// handle command line arguments
sph_system.handleCommandlineOptions(ac, av)->setIOEnvironment();
//----------------------------------------------------------------------
// Creating bodies with corresponding materials and particles.
//----------------------------------------------------------------------
FluidBody water_block(sph_system, makeShared<WaterBlock>("WaterBody"));
water_block.defineParticlesAndMaterial<BaseParticles, WeaklyCompressibleFluid>(rho0_f, c_f, mu_f);
ParticleBuffer<ReserveSizeFactor> inlet_particle_buffer(0.5);
water_block.generateParticlesWithReserve<Lattice>(inlet_particle_buffer);
SolidBody fish_body(sph_system, makeShared<FishBody>("FishBody"));
fish_body.defineAdaptationRatios(1.15, 2.0);
fish_body.defineBodyLevelSetShape()->writeLevelSet(sph_system);
fish_body.defineParticlesAndMaterial<ElasticSolidParticles, FishBodyComposite>();
// Using relaxed particle distribution if needed
(!sph_system.RunParticleRelaxation() && sph_system.ReloadParticles())
? fish_body.generateParticles<Reload>(fish_body.getName())
: fish_body.generateParticles<Lattice>();
//----------------------------------------------------------------------
// 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 fish_inner(fish_body);
InnerRelation water_block_inner(water_block);
ContactRelation water_block_contact(water_block, {&fish_body});
ContactRelation fish_contact(fish_body, {&water_block});
//----------------------------------------------------------------------
// Combined relations built from basic relations
// which is only used for update configuration.
//----------------------------------------------------------------------
ComplexRelation water_block_complex(water_block_inner, water_block_contact);
//----------------------------------------------------------------------
// Run particle relaxation for body-fitted distribution if chosen.
//----------------------------------------------------------------------
if (sph_system.RunParticleRelaxation())
{
//----------------------------------------------------------------------
// Methods used for particle relaxation.
//----------------------------------------------------------------------
using namespace relax_dynamics;
SimpleDynamics<RandomizeParticlePosition> random_fish_body_particles(fish_body);
BodyStatesRecordingToVtp write_fish_body(fish_body);
ReloadParticleIO write_particle_reload_files({&fish_body});
RelaxationStepInner relaxation_step_inner(fish_inner);
//----------------------------------------------------------------------
// Particle relaxation starts here.
//----------------------------------------------------------------------
random_fish_body_particles.exec(0.25);
relaxation_step_inner.SurfaceBounding().exec();
write_fish_body.writeToFile();
int ite_p = 0;
while (ite_p < 1000)
{
relaxation_step_inner.exec();
ite_p += 1;
if (ite_p % 200 == 0)
{
std::cout << std::fixed << std::setprecision(9) << "Relaxation steps for the inserted body N = " << ite_p << "\n";
write_fish_body.writeToFile(ite_p);
}
}
std::cout << "The physics relaxation process of inserted body finish !" << std::endl;
write_particle_reload_files.writeToFile();
return 0;
}
//----------------------------------------------------------------------
// Define the main numerical methods used in the simulation.
// Note that there may be data dependence on the constructors of these methods.
//----------------------------------------------------------------------
TimeDependentAcceleration time_dependent_acceleration(Vec2d::Zero());
SimpleDynamics<GravityForce> apply_gravity_force(water_block, time_dependent_acceleration);
BodyAlignedBoxByParticle emitter(water_block, makeShared<AlignedBoxShape>(Transform(Vec2d(emitter_translation)), emitter_halfsize));
SimpleDynamics<fluid_dynamics::EmitterInflowInjection> emitter_inflow_injection(emitter, inlet_particle_buffer, xAxis);
BodyAlignedBoxByCell emitter_buffer(water_block, makeShared<AlignedBoxShape>(Transform(Vec2d(emitter_buffer_translation)), emitter_buffer_halfsize));
SimpleDynamics<fluid_dynamics::InflowVelocityCondition<FreeStreamVelocity>> emitter_buffer_inflow_condition(emitter_buffer);
BodyAlignedBoxByCell disposer(water_block, makeShared<AlignedBoxShape>(Transform(Vec2d(disposer_translation)), disposer_halfsize));
SimpleDynamics<fluid_dynamics::DisposerOutflowDeletion> disposer_outflow_deletion(disposer, 0);
/** Time-space method to detect surface particles. */
InteractionWithUpdate<SpatialTemporalFreeSurfaceIndicationComplex> free_stream_surface_indicator(water_block_inner, water_block_contact);
/** Evaluation of density by freestream approach. */
InteractionWithUpdate<fluid_dynamics::DensitySummationFreeStreamComplex> update_fluid_density(water_block_inner, water_block_contact);
/** We can output a method-specific particle data for debug */
water_block.addBodyStateForRecording<Real>("Pressure");
water_block.addBodyStateForRecording<int>("Indicator");
/** Time step size without considering sound wave speed. */
ReduceDynamics<fluid_dynamics::AdvectionTimeStepSize> get_fluid_advection_time_step_size(water_block, U_f);
/** Time step size with considering sound wave speed. */
ReduceDynamics<fluid_dynamics::AcousticTimeStepSize> get_fluid_time_step_size(water_block);
/** modify the velocity of boundary particles with free-stream velocity. */
SimpleDynamics<fluid_dynamics::FreeStreamVelocityCorrection<FreeStreamVelocity>> velocity_boundary_condition_constraint(water_block);
/** Pressure relaxation using verlet time stepping. */
Dynamics1Level<fluid_dynamics::Integration1stHalfWithWallRiemann> pressure_relaxation(water_block_inner, water_block_contact);
/** Correct the velocity of boundary particles with free-stream velocity through the post process of pressure relaxation. */
pressure_relaxation.post_processes_.push_back(&velocity_boundary_condition_constraint);
Dynamics1Level<fluid_dynamics::Integration2ndHalfWithWallRiemann> density_relaxation(water_block_inner, water_block_contact);
/** Computing viscous acceleration. */
InteractionWithUpdate<fluid_dynamics::ViscousForceWithWall> viscous_force(water_block_inner, water_block_contact);
/** Impose transport velocity formulation. */
InteractionWithUpdate<fluid_dynamics::TransportVelocityCorrectionComplex<BulkParticles>> transport_velocity_correction(water_block_inner, water_block_contact);
/** Computing vorticity in the flow. */
InteractionDynamics<fluid_dynamics::VorticityInner> compute_vorticity(water_block_inner);
//----------------------------------------------------------------------
// Algorithms of FSI.
//----------------------------------------------------------------------
SimpleDynamics<NormalDirectionFromBodyShape> fish_body_normal_direction(fish_body);
/** Corrected configuration for the elastic insert body. */
InteractionWithUpdate<LinearGradientCorrectionMatrixInner> fish_body_corrected_configuration(fish_inner);
/** Compute the force exerted on solid body due to fluid pressure and viscosity. */
InteractionWithUpdate<solid_dynamics::ViscousForceFromFluid> viscous_force_from_fluid(fish_contact);
InteractionWithUpdate<solid_dynamics::PressureForceFromFluid<decltype(density_relaxation)>> pressure_force_from_fluid(fish_contact);
/** Compute the average velocity of the insert body. */
solid_dynamics::AverageVelocityAndAcceleration average_velocity_and_acceleration(fish_body);
//----------------------------------------------------------------------
// Algorithms of solid dynamics.
//----------------------------------------------------------------------
SimpleDynamics<FishMaterialInitialization> composite_material_id(fish_body);
SimpleDynamics<ImposingActiveStrain> imposing_active_strain(fish_body);
ReduceDynamics<solid_dynamics::AcousticTimeStepSize> fish_body_computing_time_step_size(fish_body);
/** Stress relaxation for the inserted body. */
Dynamics1Level<solid_dynamics::Integration1stHalfPK2> fish_body_stress_relaxation_first_half(fish_inner);
Dynamics1Level<solid_dynamics::Integration2ndHalf> fish_body_stress_relaxation_second_half(fish_inner);
/** Update norm .*/
SimpleDynamics<solid_dynamics::UpdateElasticNormalDirection> fish_body_update_normal(fish_body);
fish_body.addBodyStateForRecording<Real>("Density");
fish_body.addBodyStateForRecording<int>("MaterialID");
fish_body.addBodyStateForRecording<Matd>("ActiveStrain");
//----------------------------------------------------------------------
// Define the methods for I/O operations and observations of the simulation.
//----------------------------------------------------------------------
BodyStatesRecordingToVtp write_real_body_states(sph_system.real_bodies_);
RestartIO restart_io(sph_system.real_bodies_);
ReducedQuantityRecording<QuantitySummation<Vecd>> write_total_viscous_force_from_fluid(fish_body, "ViscousForceFromFluid");
ReducedQuantityRecording<QuantitySummation<Vecd>> write_total_pressure_force_from_fluid(fish_body, "PressureForceFromFluid");
//----------------------------------------------------------------------
// Prepare the simulation with cell linked list, configuration
// and case specified initial condition if necessary.
//----------------------------------------------------------------------
/** initialize cell linked lists for all bodies. */
sph_system.initializeSystemCellLinkedLists();
/** initialize configurations for all bodies. */
sph_system.initializeSystemConfigurations();
/** computing surface normal direction for the fish. */
fish_body_normal_direction.exec();
/** computing linear reproducing configuration for the fish. */
fish_body_corrected_configuration.exec();
/** initialize material ids for the fish. */
composite_material_id.exec();
//----------------------------------------------------------------------
// Setup computing and initial conditions.
//----------------------------------------------------------------------
size_t number_of_iterations = 0;
int screen_output_interval = 100;
Real End_Time = 1.7; /**< End time. */
Real D_Time = 0.01; /**< time stamps for output. */
//----------------------------------------------------------------------
// Statistics for CPU time
//----------------------------------------------------------------------
TickCount t1 = TickCount::now();
TimeInterval interval;
//----------------------------------------------------------------------
// First output before the main loop.
//----------------------------------------------------------------------
write_real_body_states.writeToFile();
//----------------------------------------------------------------------
// Main loop starts here.
//----------------------------------------------------------------------
while (GlobalStaticVariables::physical_time_ < End_Time)
{
Real integration_time = 0.0;
/** Integrate time (loop) until the next output time. */
while (integration_time < D_Time)
{
apply_gravity_force.exec();
Real Dt = get_fluid_advection_time_step_size.exec();
free_stream_surface_indicator.exec();
update_fluid_density.exec();
viscous_force.exec();
transport_velocity_correction.exec();
/** FSI for viscous force. */
viscous_force_from_fluid.exec();
/** Update normal direction on elastic body.*/
fish_body_update_normal.exec();
size_t inner_ite_dt = 0;
size_t inner_ite_dt_s = 0;
Real relaxation_time = 0.0;
while (relaxation_time < Dt)
{
Real dt = get_fluid_time_step_size.exec();
/** Fluid pressure relaxation, first half. */
pressure_relaxation.exec(dt);
/** FSI for fluid force on solid body. */
pressure_force_from_fluid.exec();
/** Fluid pressure relaxation, second half. */
density_relaxation.exec(dt);
/** Solid dynamics. */
inner_ite_dt_s = 0;
Real dt_s_sum = 0.0;
average_velocity_and_acceleration.initialize_displacement_.exec();
while (dt_s_sum < dt)
{
Real dt_s = SMIN(fish_body_computing_time_step_size.exec(), dt - dt_s_sum);
imposing_active_strain.exec();
fish_body_stress_relaxation_first_half.exec(dt_s);
fish_body_stress_relaxation_second_half.exec(dt_s);
dt_s_sum += dt_s;
inner_ite_dt_s++;
}
average_velocity_and_acceleration.update_averages_.exec(dt);
relaxation_time += dt;
integration_time += dt;
GlobalStaticVariables::physical_time_ += dt;
emitter_buffer_inflow_condition.exec(dt);
inner_ite_dt++;
}
if (number_of_iterations % screen_output_interval == 0)
{
std::cout << std::fixed << std::setprecision(9) << "N=" << number_of_iterations << " Time = "
<< GlobalStaticVariables::physical_time_
<< " Dt = " << Dt << " Dt / dt = " << inner_ite_dt << " dt / dt_s = " << inner_ite_dt_s << "\n";
write_total_viscous_force_from_fluid.writeToFile(number_of_iterations);
write_total_pressure_force_from_fluid.writeToFile(number_of_iterations);
}
number_of_iterations++;
/** Water block configuration and periodic condition. */
emitter_inflow_injection.exec();
disposer_outflow_deletion.exec();
water_block.updateCellLinkedListWithParticleSort(100);
fish_body.updateCellLinkedList();
/** one need update configuration after periodic condition. */
water_block_complex.updateConfiguration();
/** one need update configuration after periodic condition. */
fish_contact.updateConfiguration();
}
TickCount t2 = TickCount::now();
/** write run-time observation into file */
compute_vorticity.exec();
write_real_body_states.writeToFile();
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;
return 0;
}