Skip to content

Commit 9328b4f

Browse files
ENH: simulation: wrap up implementation of steps
ENH: simulation: better const correctness :) Co-authored-by: Amrita Goswami <amrita16thaug646@gmail.com>
1 parent da10e5b commit 9328b4f

3 files changed

Lines changed: 43 additions & 37 deletions

File tree

flowy/include/models/mr_lava_loba.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ inline int compute_n_lobes( int idx_flow, const Config::InputParams & input, std
8888

8989
// Calculate the current thickness of a lobe
9090
inline double compute_current_lobe_thickness(
91-
int idx_lobe, int n_lobes, const Config::InputParams & input, CommonLobeDimensions & lobe_dimensions )
91+
int idx_lobe, int n_lobes, const Config::InputParams & input, const CommonLobeDimensions & lobe_dimensions )
9292
{
9393
const double delta_lobe_thickness
9494
= 2.0 * ( lobe_dimensions.avg_lobe_thickness - lobe_dimensions.thickness_min ) / ( n_lobes - 1.0 );
@@ -157,7 +157,7 @@ compute_initial_lobe_position( int idx_flow, Lobe & lobe, const Config::InputPar
157157
// perturbes the initial azimuthal angle of the lobe, which is
158158
// computed from the terrain slope
159159
inline void compute_lobe_axes(
160-
Lobe & lobe, double slope, const Config::InputParams & input, CommonLobeDimensions & lobe_dimensions )
160+
Lobe & lobe, double slope, const Config::InputParams & input, const CommonLobeDimensions & lobe_dimensions )
161161
{
162162
// Factor for the lobe eccentricity
163163
const double aspect_ratio = std::min( input.max_aspect_ratio, 1.0 + input.aspect_ratio_coeff * slope );
@@ -206,7 +206,7 @@ inline void perturb_lobe_angle( Lobe & lobe, double slope, const Config::InputPa
206206
// Select which lobe amongst the existing lobes will be the parent for the new descendent lobe
207207
inline int select_parent_lobe(
208208
int idx_descendant, std::vector<Lobe> & lobes, const Config::InputParams & input,
209-
CommonLobeDimensions & lobe_dimensions, std::mt19937 & gen )
209+
const CommonLobeDimensions & lobe_dimensions, std::mt19937 & gen )
210210
{
211211
Lobe & lobe_descendent = lobes[idx_descendant];
212212

flowy/include/simulation.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@ namespace Flowy
2323
*/
2424
struct SimulationState
2525
{
26-
int n_lobes_processed = 0;
2726
std::chrono::time_point<std::chrono::system_clock> t_run_start{};
28-
int n_lobes{};
2927
std::vector<Lobe> lobes{};
3028

29+
int n_lobes_processed = 0;
30+
int n_lobes = 0;
3131
int step = 0;
3232
int idx_flow = 0;
3333
int idx_lobe = 0;
3434
int n_lobes_current_flow = 0;
35+
36+
bool beginning_of_new_flow = true;
3537
};
3638

3739
enum class RunStatus

src/simulation.cpp

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ RunStatus Simulation::steps( int n_steps )
407407
simulation_state = SimulationState();
408408
simulation_state->t_run_start = std::chrono::high_resolution_clock::now();
409409
}
410-
auto common_lobe_dimensions = CommonLobeDimensions( input );
410+
const auto common_lobe_dimensions = CommonLobeDimensions( input );
411411

412412
// Define some aliases for convenience
413413
auto & lobes = simulation_state->lobes;
@@ -421,10 +421,10 @@ RunStatus Simulation::steps( int n_steps )
421421

422422
for( int step = step_initial; step < step_initial + n_steps; step++ )
423423
{
424-
bool is_first_step_of_new_flow = {}; // TODO: figure this out
425-
bool is_last_step_of_flow = {};
426-
bool is_an_initial_lobe = {};
427-
bool is_last_step = {};
424+
const bool is_last_step_of_flow = idx_lobe == n_lobes - 1;
425+
const bool is_first_step_of_new_flow = simulation_state->beginning_of_new_flow;
426+
const bool is_an_initial_lobe = idx_lobe < input.n_init;
427+
const bool is_last_step = ( idx_flow == input.n_flows ) && is_last_step_of_flow;
428428

429429
if( is_first_step_of_new_flow )
430430
{
@@ -438,6 +438,9 @@ RunStatus Simulation::steps( int n_steps )
438438

439439
// set idx_lobe to zero, because we are at the beginning of a flow
440440
idx_lobe = 0;
441+
442+
// switch back the flag
443+
simulation_state->beginning_of_new_flow = false;
441444
}
442445

443446
if( is_an_initial_lobe )
@@ -451,7 +454,7 @@ RunStatus Simulation::steps( int n_steps )
451454
lobe_cur.thickness
452455
= MrLavaLoba::compute_current_lobe_thickness( idx_lobe, n_lobes, input, common_lobe_dimensions );
453456

454-
auto [height_lobe_center, slope] = topography.height_and_slope( lobe_cur.center );
457+
const auto [height_lobe_center, slope] = topography.height_and_slope( lobe_cur.center );
455458

456459
if( height_lobe_center == topography.no_data_value )
457460
{
@@ -479,8 +482,8 @@ RunStatus Simulation::steps( int n_steps )
479482

480483
// Select which of the previously created lobes is the parent lobe
481484
// from which the new descendent lobe will bud
482-
auto idx_parent = MrLavaLoba::select_parent_lobe( idx_lobe, lobes, input, common_lobe_dimensions, gen );
483-
Lobe & lobe_parent = lobes[idx_parent];
485+
const auto idx_parent = MrLavaLoba::select_parent_lobe( idx_lobe, lobes, input, common_lobe_dimensions, gen );
486+
const Lobe & lobe_parent = lobes[idx_parent];
484487

485488
// stopping condition (parent lobe close the domain boundary or at a not defined z value)
486489
if( stop_condition( lobe_parent.center, lobe_parent.semi_axes[0] ) )
@@ -492,12 +495,12 @@ RunStatus Simulation::steps( int n_steps )
492495

493496
// Find the preliminary budding point on the perimeter of the parent lobe (npoints is the number of raster
494497
// points on the ellipse)
495-
Flowy::Vector2 budding_point = topography.find_preliminary_budding_point( lobe_parent, input.npoints );
498+
const Flowy::Vector2 budding_point = topography.find_preliminary_budding_point( lobe_parent, input.npoints );
496499

497-
auto [height_lobe_center, slope_parent] = topography.height_and_slope( lobe_parent.center );
498-
auto [height_bp, slope_bp] = topography.height_and_slope( budding_point );
500+
const auto [height_lobe_center, slope_parent] = topography.height_and_slope( lobe_parent.center );
501+
const auto [height_bp, slope_bp] = topography.height_and_slope( budding_point );
499502

500-
Vector2 diff = ( budding_point - lobe_parent.center );
503+
const Vector2 diff = ( budding_point - lobe_parent.center );
501504

502505
// Perturb the angle and set it (not on the parent anymore)
503506
lobe_cur.set_azimuthal_angle( std::atan2( diff[1], diff[0] ) ); // Sets the angle prior to perturbation
@@ -509,8 +512,8 @@ RunStatus Simulation::steps( int n_steps )
509512

510513
// Compute the final budding point
511514
// It is defined by the point on the perimeter of the parent lobe closest to the center of the new lobe
512-
auto angle_diff = lobe_parent.get_azimuthal_angle() - lobe_cur.get_azimuthal_angle();
513-
Vector2 final_budding_point = lobe_parent.point_at_angle( -angle_diff );
515+
const auto angle_diff = lobe_parent.get_azimuthal_angle() - lobe_cur.get_azimuthal_angle();
516+
const Vector2 final_budding_point = lobe_parent.point_at_angle( -angle_diff );
514517

515518
// final_budding_point = budding_point;
516519
if( stop_condition( final_budding_point, lobe_parent.semi_axes[0] ) )
@@ -520,7 +523,7 @@ RunStatus Simulation::steps( int n_steps )
520523
break;
521524
}
522525
// Get the slope at the final budding point
523-
double slope_budding_point = topography.slope_between_points( lobe_parent.center, final_budding_point );
526+
const double slope_budding_point = topography.slope_between_points( lobe_parent.center, final_budding_point );
524527

525528
// compute the new lobe axes
526529
MrLavaLoba::compute_lobe_axes( lobe_cur, slope_budding_point, input, common_lobe_dimensions );
@@ -549,6 +552,7 @@ RunStatus Simulation::steps( int n_steps )
549552
{
550553
// Increment idx_flow
551554
idx_flow++;
555+
simulation_state->beginning_of_new_flow = true;
552556

553557
if( input.save_hazard_data )
554558
{
@@ -563,8 +567,8 @@ RunStatus Simulation::steps( int n_steps )
563567

564568
if( input.print_remaining_time )
565569
{
566-
auto t_cur = std::chrono::high_resolution_clock::now();
567-
auto remaining_time = std::chrono::duration_cast<std::chrono::milliseconds>(
570+
const auto t_cur = std::chrono::high_resolution_clock::now();
571+
const auto remaining_time = std::chrono::duration_cast<std::chrono::milliseconds>(
568572
( input.n_flows - idx_flow - 1 ) * ( t_cur - simulation_state->t_run_start ) / ( idx_flow + 1 ) );
569573
fmt::print( " remaining_time = {:%Hh %Mm %Ss}\n", remaining_time );
570574
}
@@ -592,7 +596,7 @@ void Simulation::run()
592596
int n_lobes_processed = 0;
593597

594598
auto t_run_start = std::chrono::high_resolution_clock::now();
595-
auto common_lobe_dimensions = CommonLobeDimensions( input );
599+
const auto common_lobe_dimensions = CommonLobeDimensions( input );
596600

597601
for( int idx_flow = 0; idx_flow < input.n_flows; idx_flow++ )
598602
{
@@ -617,7 +621,7 @@ void Simulation::run()
617621
lobe_cur.thickness
618622
= MrLavaLoba::compute_current_lobe_thickness( idx_lobe, n_lobes, input, common_lobe_dimensions );
619623

620-
auto [height_lobe_center, slope] = topography.height_and_slope( lobe_cur.center );
624+
const auto [height_lobe_center, slope] = topography.height_and_slope( lobe_cur.center );
621625

622626
if( height_lobe_center == topography.no_data_value )
623627
{
@@ -648,8 +652,8 @@ void Simulation::run()
648652

649653
// Select which of the previously created lobes is the parent lobe
650654
// from which the new descendent lobe will bud
651-
auto idx_parent = MrLavaLoba::select_parent_lobe( idx_lobe, lobes, input, common_lobe_dimensions, gen );
652-
Lobe & lobe_parent = lobes[idx_parent];
655+
const auto idx_parent = MrLavaLoba::select_parent_lobe( idx_lobe, lobes, input, common_lobe_dimensions, gen );
656+
const Lobe & lobe_parent = lobes[idx_parent];
653657

654658
// stopping condition (parent lobe close the domain boundary or at a not defined z value)
655659
if( stop_condition( lobe_parent.center, lobe_parent.semi_axes[0] ) )
@@ -662,10 +666,10 @@ void Simulation::run()
662666
// points on the ellipse)
663667
Flowy::Vector2 budding_point = topography.find_preliminary_budding_point( lobe_parent, input.npoints );
664668

665-
auto [height_lobe_center, slope_parent] = topography.height_and_slope( lobe_parent.center );
666-
auto [height_bp, slope_bp] = topography.height_and_slope( budding_point );
669+
const auto [height_lobe_center, slope_parent] = topography.height_and_slope( lobe_parent.center );
670+
const auto [height_bp, slope_bp] = topography.height_and_slope( budding_point );
667671

668-
Vector2 diff = ( budding_point - lobe_parent.center );
672+
const Vector2 diff = ( budding_point - lobe_parent.center );
669673

670674
// Perturb the angle and set it (not on the parent anymore)
671675
lobe_cur.set_azimuthal_angle( std::atan2( diff[1], diff[0] ) ); // Sets the angle prior to perturbation
@@ -677,8 +681,8 @@ void Simulation::run()
677681

678682
// Compute the final budding point
679683
// It is defined by the point on the perimeter of the parent lobe closest to the center of the new lobe
680-
auto angle_diff = lobe_parent.get_azimuthal_angle() - lobe_cur.get_azimuthal_angle();
681-
Vector2 final_budding_point = lobe_parent.point_at_angle( -angle_diff );
684+
const auto angle_diff = lobe_parent.get_azimuthal_angle() - lobe_cur.get_azimuthal_angle();
685+
const Vector2 final_budding_point = lobe_parent.point_at_angle( -angle_diff );
682686

683687
// final_budding_point = budding_point;
684688
if( stop_condition( final_budding_point, lobe_parent.semi_axes[0] ) )
@@ -687,7 +691,7 @@ void Simulation::run()
687691
break;
688692
}
689693
// Get the slope at the final budding point
690-
double slope_budding_point = topography.slope_between_points( lobe_parent.center, final_budding_point );
694+
const double slope_budding_point = topography.slope_between_points( lobe_parent.center, final_budding_point );
691695

692696
// compute the new lobe axes
693697
MrLavaLoba::compute_lobe_axes( lobe_cur, slope_budding_point, input, common_lobe_dimensions );
@@ -724,22 +728,22 @@ void Simulation::run()
724728

725729
if( input.print_remaining_time )
726730
{
727-
auto t_cur = std::chrono::high_resolution_clock::now();
728-
auto remaining_time = std::chrono::duration_cast<std::chrono::milliseconds>(
731+
const auto t_cur = std::chrono::high_resolution_clock::now();
732+
const auto remaining_time = std::chrono::duration_cast<std::chrono::milliseconds>(
729733
( input.n_flows - idx_flow - 1 ) * ( t_cur - t_run_start ) / ( idx_flow + 1 ) );
730734
fmt::print( " remaining_time = {:%Hh %Mm %Ss}\n", remaining_time );
731735
}
732736
}
733737

734-
auto t_cur = std::chrono::high_resolution_clock::now();
735-
auto total_time = std::chrono::duration_cast<std::chrono::milliseconds>( ( t_cur - t_run_start ) );
738+
const auto t_cur = std::chrono::high_resolution_clock::now();
739+
const auto total_time = std::chrono::duration_cast<std::chrono::milliseconds>( ( t_cur - t_run_start ) );
736740
fmt::print( "total_time = {:%Hh %Mm %Ss}\n", total_time );
737741

738742
fmt::print( "Total number of processed lobes = {}\n", n_lobes_processed );
739743

740744
if( total_time.count() > 0 )
741745
{
742-
auto lobes_per_ms = n_lobes_processed / total_time.count();
746+
const auto lobes_per_ms = n_lobes_processed / total_time.count();
743747
fmt::print( "n_lobes/ms = {}\n", lobes_per_ms );
744748
}
745749

0 commit comments

Comments
 (0)