Skip to content

Commit 6b59486

Browse files
authored
Fix netcdf output bug for timestep and timeblock. closes #257 (#333)
1 parent b3e0d83 commit 6b59486

File tree

6 files changed

+6643
-6383
lines changed

6 files changed

+6643
-6383
lines changed

examples/mie_optical.ipynb

Lines changed: 1192 additions & 1020 deletions
Large diffs are not rendered by default.

examples/particle_simulation.ipynb

Lines changed: 5351 additions & 5340 deletions
Large diffs are not rendered by default.

src/run_part.F90

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ subroutine f_run_part_timestep( &
8686
camp_core_ptr_c, &
8787
photolysis_ptr_c, &
8888
i_time, &
89-
t_start &
89+
t_start, &
90+
last_output_time, &
91+
last_progress_time, &
92+
i_output &
9093
) bind(C)
9194

9295
type(c_ptr), intent(in) :: scenario_ptr_c
@@ -117,8 +120,11 @@ subroutine f_run_part_timestep( &
117120
integer(c_int), intent(in) :: i_time
118121
real(c_double), intent(in) :: t_start
119122

120-
real(c_double) :: last_output_time, last_progress_time
121-
integer(c_int) :: i_output, progress_n_samp, progress_n_coag, &
123+
real(c_double), intent(inout) :: last_output_time
124+
real(c_double), intent(inout) :: last_progress_time
125+
integer(c_int), intent(inout) :: i_output
126+
127+
integer(c_int) :: progress_n_samp, progress_n_coag, &
122128
progress_n_emit, progress_n_dil_in, progress_n_dil_out, &
123129
progress_n_nuc
124130

@@ -142,6 +148,13 @@ subroutine f_run_part_timestep( &
142148
if (env_state_ptr_f%elapsed_time < run_part_opt_ptr_f%del_t) then
143149
call mosaic_init(env_state_ptr_f, aero_data_ptr_f, run_part_opt_ptr_f%del_t, &
144150
run_part_opt_ptr_f%do_optical)
151+
if (run_part_opt_ptr_f%t_output > 0) then
152+
call output_state(run_part_opt_ptr_f%output_prefix, &
153+
run_part_opt_ptr_f%output_type, aero_data_ptr_f, aero_state_ptr_f, gas_data_ptr_f, &
154+
gas_state_ptr_f, env_state_ptr_f, 1, .0d0, run_part_opt_ptr_f%del_t, &
155+
run_part_opt_ptr_f%i_repeat, run_part_opt_ptr_f%record_removals, &
156+
run_part_opt_ptr_f%do_optical, run_part_opt_ptr_f%uuid)
157+
end if
145158
end if
146159
call run_part_timestep(scenario_ptr_f, env_state_ptr_f, aero_data_ptr_f, aero_state_ptr_f, &
147160
gas_data_ptr_f, gas_state_ptr_f, run_part_opt_ptr_f, camp_core_ptr_f, photolysis_ptr_f, &
@@ -164,7 +177,10 @@ subroutine f_run_part_timeblock( &
164177
photolysis_ptr_c, &
165178
i_time, &
166179
i_next, &
167-
t_start &
180+
t_start, &
181+
last_output_time, &
182+
last_progress_time, &
183+
i_output &
168184
) bind(C)
169185

170186
type(c_ptr), intent(in) :: scenario_ptr_c
@@ -197,9 +213,11 @@ subroutine f_run_part_timeblock( &
197213
integer(c_int), intent(in) :: i_time
198214
integer(c_int), intent(in) :: i_next
199215
real(c_double), intent(in) :: t_start
216+
real(c_double), intent(inout) :: last_output_time
217+
real(c_double), intent(inout) :: last_progress_time
218+
integer(c_int), intent(inout) :: i_output
200219

201-
real(c_double) :: last_output_time, last_progress_time
202-
integer(c_int) :: i_output, progress_n_samp, progress_n_coag, &
220+
integer(c_int) :: progress_n_samp, progress_n_coag, &
203221
progress_n_emit, progress_n_dil_in, progress_n_dil_out, &
204222
progress_n_nuc
205223

@@ -223,6 +241,13 @@ subroutine f_run_part_timeblock( &
223241
if (env_state_ptr_f%elapsed_time < run_part_opt_ptr_f%del_t) then
224242
call mosaic_init(env_state_ptr_f, aero_data_ptr_f, run_part_opt_ptr_f%del_t, &
225243
run_part_opt_ptr_f%do_optical)
244+
if (run_part_opt_ptr_f%t_output > 0) then
245+
call output_state(run_part_opt_ptr_f%output_prefix, &
246+
run_part_opt_ptr_f%output_type, aero_data_ptr_f, aero_state_ptr_f, gas_data_ptr_f, &
247+
gas_state_ptr_f, env_state_ptr_f, 1, .0d0, run_part_opt_ptr_f%del_t, &
248+
run_part_opt_ptr_f%i_repeat, run_part_opt_ptr_f%record_removals, &
249+
run_part_opt_ptr_f%do_optical, run_part_opt_ptr_f%uuid)
250+
end if
226251
end if
227252

228253
call run_part_timeblock(scenario_ptr_f, env_state_ptr_f, aero_data_ptr_f, aero_state_ptr_f, &

src/run_part.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
##################################################################################################*/
66

77
#include "run_part.hpp"
8+
#include "pybind11/stl.h"
89

910
void run_part(
1011
const Scenario &scenario,
@@ -30,7 +31,7 @@ void run_part(
3031
);
3132
}
3233

33-
void run_part_timestep(
34+
std::tuple<double, double, int> run_part_timestep(
3435
const Scenario &scenario,
3536
EnvState &env_state,
3637
const AeroData &aero_data,
@@ -41,7 +42,10 @@ void run_part_timestep(
4142
const CampCore &camp_core,
4243
const Photolysis &photolysis,
4344
const int &i_time,
44-
const double &t_start
45+
const double &t_start,
46+
double &last_output_time,
47+
double &last_progress_time,
48+
int &i_output
4549
) {
4650
f_run_part_timestep(
4751
scenario.ptr.f_arg(),
@@ -54,11 +58,16 @@ void run_part_timestep(
5458
camp_core.ptr.f_arg(),
5559
photolysis.ptr.f_arg(),
5660
&i_time,
57-
&t_start
61+
&t_start,
62+
&last_output_time,
63+
&last_progress_time,
64+
&i_output
5865
);
66+
67+
return std::make_tuple(last_output_time, last_progress_time, i_output);
5968
}
6069

61-
void run_part_timeblock(
70+
std::tuple<double, double, int> run_part_timeblock(
6271
const Scenario &scenario,
6372
EnvState &env_state,
6473
const AeroData &aero_data,
@@ -70,7 +79,10 @@ void run_part_timeblock(
7079
const Photolysis &photolysis,
7180
const int &i_time,
7281
const int &i_next,
73-
const double &t_start
82+
const double &t_start,
83+
double &last_output_time,
84+
double &last_progress_time,
85+
int &i_output
7486
) {
7587
f_run_part_timeblock(
7688
scenario.ptr.f_arg(),
@@ -84,6 +96,11 @@ void run_part_timeblock(
8496
photolysis.ptr.f_arg(),
8597
&i_time,
8698
&i_next,
87-
&t_start
99+
&t_start,
100+
&last_output_time,
101+
&last_progress_time,
102+
&i_output
88103
);
104+
105+
return std::make_tuple(last_output_time, last_progress_time, i_output);
89106
}

src/run_part.hpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ extern "C" void f_run_part_timestep(
3838
const void*,
3939
const void*,
4040
const int *,
41-
const double *
41+
const double*,
42+
double*,
43+
double*,
44+
int*
4245
) noexcept;
4346

4447
extern "C" void f_run_part_timeblock(
@@ -53,7 +56,10 @@ extern "C" void f_run_part_timeblock(
5356
const void*,
5457
const int *,
5558
const int *,
56-
const double *
59+
const double *,
60+
double*,
61+
double*,
62+
int*
5763
) noexcept;
5864

5965
void run_part(
@@ -68,7 +74,7 @@ void run_part(
6874
const Photolysis &photolysis
6975
);
7076

71-
void run_part_timestep(
77+
std::tuple<double, double, int> run_part_timestep(
7278
const Scenario &scenario,
7379
EnvState &env_state,
7480
const AeroData &aero_data,
@@ -79,10 +85,13 @@ void run_part_timestep(
7985
const CampCore &camp_core,
8086
const Photolysis &photolysis,
8187
const int &i_time,
82-
const double &t_start
88+
const double &t_start,
89+
double &last_output_time,
90+
double &last_progress_time,
91+
int &i_output
8392
);
8493

85-
void run_part_timeblock(
94+
std::tuple<double, double, int> run_part_timeblock(
8695
const Scenario &scenario,
8796
EnvState &env_state,
8897
const AeroData &aero_data,
@@ -94,5 +103,8 @@ void run_part_timeblock(
94103
const Photolysis &photolysis,
95104
const int &i_time,
96105
const int &i_next,
97-
const double &t_start
106+
const double &t_start,
107+
double &last_output_time,
108+
double &last_progress_time,
109+
int &i_output
98110
);

tests/test_run_part.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,30 @@
1212
from .test_aero_state import AERO_STATE_CTOR_ARG_MINIMAL
1313
from .test_env_state import ENV_STATE_CTOR_ARG_MINIMAL
1414
from .test_gas_data import GAS_DATA_CTOR_ARG_MINIMAL
15-
from .test_run_part_opt import RUN_PART_OPT_CTOR_ARG_MINIMAL
15+
from .test_run_part_opt import RUN_PART_OPT_CTOR_ARG_SIMULATION
1616
from .test_scenario import SCENARIO_CTOR_ARG_MINIMAL
1717

1818

1919
@pytest.fixture
20-
def common_args():
20+
def common_args(tmp_path):
2121
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
2222
gas_data = ppmc.GasData(GAS_DATA_CTOR_ARG_MINIMAL)
2323
gas_state = ppmc.GasState(gas_data)
2424
scenario = ppmc.Scenario(gas_data, aero_data, SCENARIO_CTOR_ARG_MINIMAL)
2525
env_state = ppmc.EnvState(ENV_STATE_CTOR_ARG_MINIMAL)
2626
scenario.init_env_state(env_state, 0.0)
27+
filename = tmp_path / "test"
28+
run_part_opt = ppmc.RunPartOpt(
29+
{**RUN_PART_OPT_CTOR_ARG_SIMULATION, "output_prefix": str(filename)}
30+
)
2731
return (
2832
scenario,
2933
env_state,
3034
aero_data,
3135
ppmc.AeroState(aero_data, *AERO_STATE_CTOR_ARG_MINIMAL),
3236
gas_data,
3337
gas_state,
34-
ppmc.RunPartOpt(RUN_PART_OPT_CTOR_ARG_MINIMAL),
38+
run_part_opt,
3539
ppmc.CampCore(),
3640
ppmc.Photolysis(),
3741
)
@@ -42,10 +46,29 @@ class TestRunPart:
4246
def test_run_part(common_args): # pylint: disable=redefined-outer-name
4347
ppmc.run_part(*common_args)
4448

49+
assert common_args[1].elapsed_time == RUN_PART_OPT_CTOR_ARG_SIMULATION["t_max"]
50+
4551
@staticmethod
4652
def test_run_part_timestep(common_args): # pylint: disable=redefined-outer-name
47-
ppmc.run_part_timestep(*common_args, 0, 0)
53+
(last_output_time, last_progress_time, i_output) = ppmc.run_part_timestep(
54+
*common_args, 1, 0, 0, 0, 1
55+
)
56+
57+
assert common_args[1].elapsed_time == RUN_PART_OPT_CTOR_ARG_SIMULATION["del_t"]
58+
assert last_output_time == 0.0
59+
assert last_progress_time == 0.0
60+
assert i_output == 1
4861

4962
@staticmethod
5063
def test_run_part_timeblock(common_args): # pylint: disable=redefined-outer-name
51-
ppmc.run_part_timeblock(*common_args, 0, 0, 0)
64+
65+
num_times = int(
66+
RUN_PART_OPT_CTOR_ARG_SIMULATION["t_output"]
67+
/ RUN_PART_OPT_CTOR_ARG_SIMULATION["del_t"]
68+
)
69+
(last_output_time, last_progress_time, i_output) = ppmc.run_part_timeblock(
70+
*common_args, 1, num_times, 0, 0, 0, 1
71+
)
72+
assert last_output_time == RUN_PART_OPT_CTOR_ARG_SIMULATION["t_output"]
73+
assert last_progress_time == 0.0
74+
assert i_output == 2

0 commit comments

Comments
 (0)