Skip to content

Commit e01f9c9

Browse files
authored
Merge pull request #416 from pdziekan/variable_dt
Variable dt
2 parents 154751f + b52d1d3 commit e01f9c9

20 files changed

+194
-126
lines changed

bindings/python/lib.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ BOOST_PYTHON_MODULE(libcloudphxx)
250250
.def_readwrite("turb_adve", &lgr::opts_t<real_t>::turb_adve)
251251
.def_readwrite("turb_cond", &lgr::opts_t<real_t>::turb_cond)
252252
.def_readwrite("turb_coal", &lgr::opts_t<real_t>::turb_coal)
253+
.def_readwrite("dt", &lgr::opts_t<real_t>::dt)
253254
;
254255
bp::class_<lgr::opts_init_t<real_t>>("opts_init_t")
255256
.add_property("dry_distros", &lgrngn::get_dd<real_t>, &lgrngn::set_dd<real_t>)
@@ -310,6 +311,7 @@ BOOST_PYTHON_MODULE(libcloudphxx)
310311
.add_property("w_LS", &lgrngn::get_w_LS<real_t>, &lgrngn::set_w_LS<real_t>)
311312
.add_property("SGS_mix_len", &lgrngn::get_SGS_mix_len<real_t>, &lgrngn::set_SGS_mix_len<real_t>)
312313
.add_property("kernel_parameters", &lgrngn::get_kp<real_t>, &lgrngn::set_kp<real_t>)
314+
.def_readwrite("variable_dt_switch", &lgr::opts_init_t<real_t>::variable_dt_switch)
313315
;
314316
bp::class_<lgr::particles_proto_t<real_t>/*, boost::noncopyable*/>("particles_proto_t")
315317
.add_property("opts_init", &lgrngn::get_oi<real_t>)

include/libcloudph++/lgrngn/opts.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@ namespace libcloudphxx
2828
// process toggling for chemistry
2929
bool chem_dsl, chem_dsc, chem_rct;
3030

31+
// overriding dt from opts_init
32+
real_t dt;
33+
3134
// ctor with defaults (C++03 compliant) ...
3235
opts_t() :
3336
adve(true), sedi(true), subs(false), cond(true), coal(true), src(false), rcyc(false),
3437
chem_dsl(false), chem_dsc(false), chem_rct(false),
3538
turb_adve(false), turb_cond(false), turb_coal(false),
36-
RH_max(44) // :) (anything greater than 1.1 would be enough
39+
RH_max(44), // :) (anything greater than 1.1 would be enough
40+
dt(-1) // negative means that we do not override dt in this step
3741
{
3842
}
3943
};

include/libcloudph++/lgrngn/opts_init.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ namespace libcloudphxx
6464
// should aerosol concentration init be independent of rhod (assumed to be in cm^{-3} and not at STP)
6565
bool aerosol_independent_of_rhod;
6666

67+
// is it allowed to change dt during simulation through opts.dt
68+
bool variable_dt_switch;
69+
6770
// or, alternatively to sd_conc_mean, multiplicity of all SDs = const
6871
unsigned long long sd_const_multi;
6972

@@ -187,7 +190,8 @@ namespace libcloudphxx
187190
diag_incloud_time(false),
188191
no_ccn_at_init(false),
189192
open_side_walls(false),
190-
periodic_topbot_walls(false)
193+
periodic_topbot_walls(false),
194+
variable_dt_switch(false)
191195
{}
192196

193197
// dtor (just to silence -Winline warnings)

src/impl/particles_impl.ipp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ namespace libcloudphxx
152152
thrust_device::vector<real_t> w_LS; // large-scale subsidence velocity profile
153153
thrust_device::vector<real_t> SGS_mix_len; // SGS mixing length profile
154154

155+
// time steps to be used, considering that opts.dt can override opts_init.dt
156+
real_t dt;
157+
int sstp_cond, sstp_coal, sstp_chem;
158+
155159
// sorting needed only for diagnostics and coalescence
156160
bool sorted;
157161

@@ -163,6 +167,10 @@ namespace libcloudphxx
163167
// is a constant, external pressure profile used? (e.g. anelastic model)
164168
bool const_p;
165169

170+
// is it allowed to do substepping, if not, some memory can be saved
171+
bool allow_sstp_cond,
172+
allow_sstp_chem;
173+
166174
// timestep counter
167175
n_t stp_ctr;
168176

@@ -327,6 +335,8 @@ namespace libcloudphxx
327335
w_LS(_opts_init.w_LS),
328336
SGS_mix_len(_opts_init.SGS_mix_len),
329337
adve_scheme(_opts_init.adve_scheme),
338+
allow_sstp_cond(_opts_init.sstp_cond > 1 || _opts_init.variable_dt_switch),
339+
allow_sstp_chem(_opts_init.sstp_chem > 1 || _opts_init.variable_dt_switch),
330340
pure_const_multi (((_opts_init.sd_conc) == 0) && (_opts_init.sd_const_multi > 0 || _opts_init.dry_sizes.size() > 0)) // coal prob can be greater than one only in sd_conc simulations
331341
{
332342

@@ -383,7 +393,7 @@ namespace libcloudphxx
383393
if (opts_init.ny != 0) distmem_real_vctrs.insert(&y);
384394
if (opts_init.nz != 0) distmem_real_vctrs.insert(&z);
385395

386-
if(opts_init.sstp_cond > 1 && opts_init.exact_sstp_cond)
396+
if(allow_sstp_cond && opts_init.exact_sstp_cond)
387397
{
388398
distmem_real_vctrs.insert(&sstp_tmp_rv);
389399
distmem_real_vctrs.insert(&sstp_tmp_th);
@@ -483,7 +493,7 @@ namespace libcloudphxx
483493
void hskpng_vterm_all();
484494
void hskpng_vterm_invalid();
485495
void hskpng_tke();
486-
void hskpng_turb_vel(const bool only_vertical = false);
496+
void hskpng_turb_vel(const real_t &dt, const bool only_vertical = false);
487497
void hskpng_turb_dot_ss();
488498
void hskpng_remove_n0();
489499
void hskpng_resize_npart();
@@ -522,12 +532,13 @@ namespace libcloudphxx
522532
arrinfo_t<real_t> &
523533
);
524534

535+
void adjust_timesteps(const real_t &dt);
525536
void adve();
526-
void turb_adve();
537+
void turb_adve(const real_t &dt);
527538
template<class adve_t>
528539
void adve_calc(bool, thrust_size_t = 0);
529-
void sedi();
530-
void subs();
540+
void sedi(const real_t &dt);
541+
void subs(const real_t &dt);
531542

532543
void cond_dm3_helper();
533544
void cond(const real_t &dt, const real_t &RH_max, const bool turb_cond);
@@ -537,7 +548,7 @@ namespace libcloudphxx
537548
void update_th_rv(thrust_device::vector<real_t> &);
538549
void update_state(thrust_device::vector<real_t> &, thrust_device::vector<real_t> &);
539550
void update_pstate(thrust_device::vector<real_t> &, thrust_device::vector<real_t> &);
540-
void update_incloud_time();
551+
void update_incloud_time(const real_t &dt);
541552

542553
void coal(const real_t &dt, const bool &turb_coal);
543554

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// vim:filetype=cpp
2+
/** @file
3+
* @copyright University of Warsaw
4+
* @section LICENSE
5+
* GPLv3+ (see the COPYING file or http://www.gnu.org/licenses/)
6+
*/
7+
8+
namespace libcloudphxx
9+
{
10+
namespace lgrngn
11+
{
12+
template <typename real_t, backend_t device>
13+
void particles_t<real_t, device>::impl::adjust_timesteps(const real_t &_dt)
14+
{
15+
if(_dt > 0 && !opts_init.variable_dt_switch) throw std::runtime_error("opts.dt specified, but opts_init.variable_dt_switch is false.");
16+
// dt defined in opts_init can be overriden by dt passed to this function
17+
dt = _dt > 0 ? _dt : opts_init.dt;
18+
// then, number of substeps is adjusted to get desired dt for specific processes
19+
sstp_cond = _dt > 0 ? std::ceil(opts_init.sstp_cond * _dt / opts_init.dt) : opts_init.sstp_cond;
20+
sstp_coal = _dt > 0 ? std::ceil(opts_init.sstp_coal * _dt / opts_init.dt) : opts_init.sstp_coal;
21+
sstp_chem = _dt > 0 ? std::ceil(opts_init.sstp_chem * _dt / opts_init.dt) : opts_init.sstp_chem;
22+
}
23+
};
24+
};

src/impl/particles_impl_hskpng_resize.ipp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ namespace libcloudphxx
4949
dot_ssp.resize(n_part, 0);
5050
}
5151

52-
if(opts_init.chem_switch || opts_init.sstp_cond > 1 || n_dims >= 2)
52+
if(opts_init.chem_switch || allow_sstp_cond || n_dims >= 2)
5353
{
5454
tmp_device_real_part1.resize(n_part);
5555
}
56-
if((opts_init.sstp_cond>1 && opts_init.exact_sstp_cond) || n_dims==3 || opts_init.turb_cond_switch)
56+
if((allow_sstp_cond && opts_init.exact_sstp_cond) || n_dims==3 || opts_init.turb_cond_switch)
5757
{
5858
tmp_device_real_part2.resize(n_part);
5959
}
6060

61-
if(opts_init.sstp_cond>1 && opts_init.exact_sstp_cond)
61+
if(allow_sstp_cond && opts_init.exact_sstp_cond)
6262
{
6363
tmp_device_real_part3.resize(n_part);
6464
tmp_device_real_part4.resize(n_part);

src/impl/particles_impl_hskpng_turb_vel.ipp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace libcloudphxx
4949
};
5050
// calc the SGS turbulent velocity component
5151
template <typename real_t, backend_t device>
52-
void particles_t<real_t, device>::impl::hskpng_turb_vel(const bool only_vertical)
52+
void particles_t<real_t, device>::impl::hskpng_turb_vel(const real_t &dt, const bool only_vertical)
5353
{
5454
namespace arg = thrust::placeholders;
5555

@@ -86,7 +86,7 @@ namespace libcloudphxx
8686
r_normal.begin()
8787
)) + n_part,
8888
vel_turbs_vctrs_a[i]->begin(),
89-
detail::common__turbulence__update_turb_vel<real_t>(opts_init.dt)
89+
detail::common__turbulence__update_turb_vel<real_t>(dt)
9090
);
9191
}
9292
}

src/impl/particles_impl_init_hskpng_ncell.ipp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace libcloudphxx
2929
tmp_device_size_cell.resize(n_cell);
3030
tmp_host_size_cell.resize(n_cell);
3131
tmp_host_real_cell.resize(n_cell);
32-
if(opts_init.sstp_cond > 1 && !opts_init.exact_sstp_cond)
32+
if(allow_sstp_cond && !opts_init.exact_sstp_cond)
3333
{
3434
sstp_tmp_rv.resize(n_cell);
3535
sstp_tmp_th.resize(n_cell);

src/impl/particles_impl_reserve_hskpng_npart.ipp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ namespace libcloudphxx
5656
{
5757
tmp_device_real_part1.reserve(opts_init.n_sd_max);
5858
}
59-
if((opts_init.sstp_cond>1 && opts_init.exact_sstp_cond) || n_dims==3 || opts_init.turb_cond_switch)
59+
if((allow_sstp_cond && opts_init.exact_sstp_cond) || n_dims==3 || opts_init.turb_cond_switch)
6060
{
6161
tmp_device_real_part2.reserve(opts_init.n_sd_max);
6262
}
63-
if(opts_init.sstp_cond>1 && opts_init.exact_sstp_cond)
63+
if(allow_sstp_cond && opts_init.exact_sstp_cond)
6464
{
6565
tmp_device_real_part3.reserve(opts_init.n_sd_max);
6666
tmp_device_real_part4.reserve(opts_init.n_sd_max);
@@ -107,7 +107,7 @@ namespace libcloudphxx
107107
// }
108108
// vt.resize(opts_init.n_sd_max, 0.); // so that it may be safely used in condensation before first update
109109
//
110-
// if(opts_init.sstp_cond>1 && opts_init.exact_sstp_cond)
110+
// if(allow_sstp_cond && opts_init.exact_sstp_cond)
111111
// {
112112
// sstp_tmp_rv.resize(opts_init.n_sd_max);
113113
// sstp_tmp_th.resize(opts_init.n_sd_max);

src/impl/particles_impl_sedi.ipp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace libcloudphxx
1010
namespace lgrngn
1111
{
1212
template <typename real_t, backend_t device>
13-
void particles_t<real_t, device>::impl::sedi()
13+
void particles_t<real_t, device>::impl::sedi(const real_t &dt)
1414
{
1515
namespace arg = thrust::placeholders;
1616

@@ -19,7 +19,7 @@ namespace libcloudphxx
1919
z.begin(), z.end(), // position
2020
vt.begin(), // terminal velocity
2121
z.begin(), // output
22-
arg::_1 - opts_init.dt * arg::_2 // Euler scheme (assuming vt positive!)
22+
arg::_1 - dt * arg::_2 // Euler scheme (assuming vt positive!)
2323
);
2424
}
2525
};

0 commit comments

Comments
 (0)