Skip to content

Commit f24ff3d

Browse files
authored
Merge pull request #327 from pdziekan/delayed_advection
Delayed advection
2 parents 99e8aed + 8a33afa commit f24ff3d

File tree

9 files changed

+243
-9
lines changed

9 files changed

+243
-9
lines changed

bindings/python/lgrngn.hpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,72 @@ namespace libcloudphxx
140140
);
141141
}
142142

143+
//
144+
template <typename real_t>
145+
void sync_in(
146+
lgr::particles_proto_t<real_t> *arg,
147+
const bp_array &th,
148+
const bp_array &rv,
149+
const bp_array &rhod,
150+
const bp_array &Cx,
151+
const bp_array &Cy,
152+
const bp_array &Cz,
153+
bp::dict &ambient_chem
154+
)
155+
{
156+
typedef std::map<enum lgr::chem_species_t, lgr::arrinfo_t<real_t> > map_t;
157+
map_t map;
158+
159+
for (int i = 0; i < len(ambient_chem.keys()); ++i)
160+
map.insert(typename map_t::value_type(
161+
bp::extract<enum lgr::chem_species_t>(ambient_chem.keys()[i]),
162+
np2ai<real_t>(bp::extract<bp_array>(ambient_chem.values()[i]), sz(*arg))
163+
));
164+
165+
lgr::arrinfo_t<real_t>
166+
np2ai_th(np2ai<real_t>(th, sz(*arg))),
167+
np2ai_rv(np2ai<real_t>(rv, sz(*arg)));
168+
arg->sync_in(
169+
np2ai_th,
170+
np2ai_rv,
171+
np2ai<real_t>(rhod, sz(*arg)),
172+
np2ai<real_t>(Cx, sz(*arg)),
173+
np2ai<real_t>(Cy, sz(*arg)),
174+
np2ai<real_t>(Cz, sz(*arg)),
175+
map
176+
);
177+
}
178+
179+
//
180+
template <typename real_t>
181+
void step_cond(
182+
lgr::particles_proto_t<real_t> *arg,
183+
const lgr::opts_t<real_t> &opts,
184+
const bp_array &th,
185+
const bp_array &rv,
186+
bp::dict &ambient_chem
187+
)
188+
{
189+
typedef std::map<enum lgr::chem_species_t, lgr::arrinfo_t<real_t> > map_t;
190+
map_t map;
191+
192+
for (int i = 0; i < len(ambient_chem.keys()); ++i)
193+
map.insert(typename map_t::value_type(
194+
bp::extract<enum lgr::chem_species_t>(ambient_chem.keys()[i]),
195+
np2ai<real_t>(bp::extract<bp_array>(ambient_chem.values()[i]), sz(*arg))
196+
));
197+
198+
lgr::arrinfo_t<real_t>
199+
np2ai_th(np2ai<real_t>(th, sz(*arg))),
200+
np2ai_rv(np2ai<real_t>(rv, sz(*arg)));
201+
arg->step_cond(
202+
opts,
203+
np2ai_th,
204+
np2ai_rv,
205+
map
206+
);
207+
}
208+
143209
template <typename real_t>
144210
bp::dict diag_puddle(lgr::particles_proto_t<real_t> *arg)
145211
{

bindings/python/lib.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,20 @@ BOOST_PYTHON_MODULE(libcloudphxx)
311311
bp::arg("Cz") = BP_ARR_FROM_BP_OBJ,
312312
bp::arg("ambient_chem") = bp::dict()
313313
))
314+
.def("sync_in", &lgrngn::sync_in<real_t>, (
315+
bp::arg("th") = BP_ARR_FROM_BP_OBJ,
316+
bp::arg("rv") = BP_ARR_FROM_BP_OBJ,
317+
bp::arg("rhod")= BP_ARR_FROM_BP_OBJ,
318+
bp::arg("Cx") = BP_ARR_FROM_BP_OBJ,
319+
bp::arg("Cy") = BP_ARR_FROM_BP_OBJ,
320+
bp::arg("Cz") = BP_ARR_FROM_BP_OBJ,
321+
bp::arg("ambient_chem") = bp::dict()
322+
))
323+
.def("step_cond", &lgrngn::step_cond<real_t>, (
324+
bp::arg("th") = BP_ARR_FROM_BP_OBJ,
325+
bp::arg("rv") = BP_ARR_FROM_BP_OBJ,
326+
bp::arg("ambient_chem") = bp::dict()
327+
))
314328
.def("step_async", &lgr::particles_proto_t<real_t>::step_async)
315329
.def("diag_sd_conc", &lgr::particles_proto_t<real_t>::diag_sd_conc)
316330
.def("diag_all", &lgr::particles_proto_t<real_t>::diag_all)

include/libcloudph++/lgrngn/particles.hpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@ namespace libcloudphxx
4444
assert(false);
4545
}
4646

47+
virtual void sync_in(
48+
arrinfo_t<real_t> th,
49+
arrinfo_t<real_t> rv,
50+
const arrinfo_t<real_t> rhod = arrinfo_t<real_t>(),
51+
const arrinfo_t<real_t> courant_x = arrinfo_t<real_t>(),
52+
const arrinfo_t<real_t> courant_y = arrinfo_t<real_t>(),
53+
const arrinfo_t<real_t> courant_z = arrinfo_t<real_t>(),
54+
std::map<enum chem_species_t, arrinfo_t<real_t> > ambient_chem = std::map<enum chem_species_t, arrinfo_t<real_t> >()
55+
) {
56+
assert(false);
57+
}
58+
59+
virtual void step_cond(
60+
const opts_t<real_t> &,
61+
arrinfo_t<real_t> th,
62+
arrinfo_t<real_t> rv,
63+
std::map<enum chem_species_t, arrinfo_t<real_t> > ambient_chem = std::map<enum chem_species_t, arrinfo_t<real_t> >()
64+
) {
65+
assert(false);
66+
}
67+
4768
// returns accumulated rainfall
4869
virtual void step_async(
4970
const opts_t<real_t> &
@@ -109,6 +130,23 @@ namespace libcloudphxx
109130
std::map<enum chem_species_t, arrinfo_t<real_t> > ambient_chem
110131
);
111132

133+
void sync_in(
134+
arrinfo_t<real_t> th,
135+
arrinfo_t<real_t> rv,
136+
const arrinfo_t<real_t> rhod,
137+
const arrinfo_t<real_t> courant_x,
138+
const arrinfo_t<real_t> courant_y,
139+
const arrinfo_t<real_t> courant_z,
140+
std::map<enum chem_species_t, arrinfo_t<real_t> > ambient_chem
141+
);
142+
143+
void step_cond(
144+
const opts_t<real_t> &,
145+
arrinfo_t<real_t> th,
146+
arrinfo_t<real_t> rv,
147+
std::map<enum chem_species_t, arrinfo_t<real_t> > ambient_chem = std::map<enum chem_species_t, arrinfo_t<real_t> >()
148+
);
149+
112150
void step_async(
113151
const opts_t<real_t> &
114152
);
@@ -185,6 +223,24 @@ namespace libcloudphxx
185223
const arrinfo_t<real_t> courant_z,
186224
std::map<enum chem_species_t, arrinfo_t<real_t> > ambient_chem
187225
);
226+
227+
void sync_in(
228+
arrinfo_t<real_t> th,
229+
arrinfo_t<real_t> rv,
230+
const arrinfo_t<real_t> rhod ,
231+
const arrinfo_t<real_t> courant_x,
232+
const arrinfo_t<real_t> courant_y,
233+
const arrinfo_t<real_t> courant_z,
234+
std::map<enum chem_species_t, arrinfo_t<real_t> > ambient_chem
235+
);
236+
237+
void step_cond(
238+
const opts_t<real_t> &,
239+
arrinfo_t<real_t> th,
240+
arrinfo_t<real_t> rv,
241+
std::map<enum chem_species_t, arrinfo_t<real_t> > ambient_chem = std::map<enum chem_species_t, arrinfo_t<real_t> >()
242+
);
243+
188244
void step_async(
189245
const opts_t<real_t> &
190246
);

src/impl/particles_impl.ipp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ namespace libcloudphxx
3434
typedef unsigned long long n_t; // thrust_size_t?
3535

3636
// order of operation flags
37-
bool init_called, should_now_run_async, selected_before_counting;
37+
bool init_called, should_now_run_async, selected_before_counting, should_now_run_cond;
38+
39+
// did density vary in this step
40+
bool var_rho;
3841

3942
// member fields
4043
opts_init_t<real_t> opts_init; // a copy
@@ -236,6 +239,8 @@ namespace libcloudphxx
236239
init_called(false),
237240
should_now_run_async(false),
238241
selected_before_counting(false),
242+
should_now_run_cond(false),
243+
var_rho(false),
239244
opts_init(_opts_init),
240245
n_dims( // 0, 1, 2 or 3
241246
opts_init.nx/m1(opts_init.nx) +
@@ -446,10 +451,10 @@ namespace libcloudphxx
446451

447452
void src(const real_t &dt);
448453

449-
void sstp_step(const int &step, const bool &var_rho);
454+
void sstp_step(const int &step);
450455
void sstp_step_exact(const int &step);
451456
void sstp_save();
452-
void sstp_step_chem(const int &step, const bool &var_rho);
457+
void sstp_step_chem(const int &step);
453458
void sstp_save_chem();
454459

455460
void step_finalize(const opts_t<real_t>&);

src/impl/particles_impl_sstp.ipp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ namespace libcloudphxx
4646

4747
template <typename real_t, backend_t device>
4848
void particles_t<real_t, device>::impl::sstp_step(
49-
const int &step,
50-
const bool &var_rho // if rho varied and need to be updated
49+
const int &step
5150
)
5251
{
5352
if (opts_init.sstp_cond == 1) return;

src/impl/particles_impl_sstp_chem.ipp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ namespace libcloudphxx
5151

5252
template <typename real_t, backend_t device>
5353
void particles_t<real_t, device>::impl::sstp_step_chem(
54-
const int &step,
55-
const bool &var_rho // if rho varied and need to be updated
54+
const int &step
5655
)
5756
{
5857
if (opts_init.sstp_chem == 1) return;

src/particles_multi_gpu_step.ipp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,31 @@ namespace libcloudphxx
2828
pimpl->mcuda_run(&particles_t<real_t, CUDA>::step_sync, opts, th, rv, rhod, courant_1, courant_2, courant_3, ambient_chem);
2929
}
3030

31+
template <typename real_t>
32+
void particles_t<real_t, multi_CUDA>::sync_in(
33+
arrinfo_t<real_t> th,
34+
arrinfo_t<real_t> rv,
35+
const arrinfo_t<real_t> rhod,
36+
const arrinfo_t<real_t> courant_1,
37+
const arrinfo_t<real_t> courant_2,
38+
const arrinfo_t<real_t> courant_3,
39+
std::map<enum chem_species_t, arrinfo_t<real_t> > ambient_chem
40+
)
41+
{
42+
pimpl->mcuda_run(&particles_t<real_t, CUDA>::sync_in, th, rv, rhod, courant_1, courant_2, courant_3, ambient_chem);
43+
}
44+
45+
template <typename real_t>
46+
void particles_t<real_t, multi_CUDA>::step_cond(
47+
const opts_t<real_t> &opts,
48+
arrinfo_t<real_t> th,
49+
arrinfo_t<real_t> rv,
50+
std::map<enum chem_species_t, arrinfo_t<real_t> > ambient_chem
51+
)
52+
{
53+
pimpl->mcuda_run(&particles_t<real_t, CUDA>::step_cond, opts, th, rv, ambient_chem);
54+
}
55+
3156
template <typename real_t>
3257
void particles_t<real_t, multi_CUDA>::step_async(
3358
const opts_t<real_t> &opts

src/particles_step.ipp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ namespace libcloudphxx
2222
const arrinfo_t<real_t> courant_z, // defaults to NULL-NULL pair (e.g. kinematic model)
2323
std::map<enum chem_species_t, arrinfo_t<real_t> > ambient_chem
2424
)
25+
{
26+
sync_in(th, rv, rhod, courant_x, courant_y, courant_z, ambient_chem);
27+
step_cond(opts, th, rv, ambient_chem);
28+
}
29+
30+
template <typename real_t, backend_t device>
31+
void particles_t<real_t, device>::sync_in(
32+
arrinfo_t<real_t> th,
33+
arrinfo_t<real_t> rv,
34+
const arrinfo_t<real_t> rhod, // defaults to NULL-NULL pair (e.g. kinematic or boussinesq model)
35+
const arrinfo_t<real_t> courant_x, // defaults to NULL-NULL pair (e.g. kinematic model)
36+
const arrinfo_t<real_t> courant_y, // defaults to NULL-NULL pair (e.g. kinematic model)
37+
const arrinfo_t<real_t> courant_z, // defaults to NULL-NULL pair (e.g. kinematic model)
38+
std::map<enum chem_species_t, arrinfo_t<real_t> > ambient_chem
39+
)
2540
{
2641
// sanity checks
2742
if (!pimpl->init_called)
@@ -67,6 +82,9 @@ namespace libcloudphxx
6782
if (!courant_z.is_null()) pimpl->init_e2l(courant_z, &pimpl->courant_z, 0, 0, 1, pimpl->n_x_bfr * max(1, pimpl->opts_init.ny) - pimpl->halo_z);
6883
}
6984

85+
// did rhod change
86+
pimpl->var_rho = !rhod.is_null();
87+
7088
// syncing in Eulerian fields (if not null)
7189
pimpl->sync(th, pimpl->th);
7290
pimpl->sync(rv, pimpl->rv);
@@ -98,6 +116,23 @@ namespace libcloudphxx
98116
);
99117
}
100118
}
119+
120+
pimpl->should_now_run_cond = true;
121+
}
122+
123+
template <typename real_t, backend_t device>
124+
void particles_t<real_t, device>::step_cond(
125+
const opts_t<real_t> &opts,
126+
arrinfo_t<real_t> th, // for sync-out
127+
arrinfo_t<real_t> rv, // for sync-out
128+
std::map<enum chem_species_t, arrinfo_t<real_t> > ambient_chem // for sync-out
129+
)
130+
{
131+
//sanity checks
132+
if (!pimpl->should_now_run_cond)
133+
throw std::runtime_error("please call sync_in() before calling step_cond()");
134+
135+
pimpl->should_now_run_cond = false;
101136

102137
// condensation/evaporation
103138
// if const_p == True, pressure is not substepped
@@ -122,7 +157,7 @@ namespace libcloudphxx
122157
{
123158
for (int step = 0; step < pimpl->opts_init.sstp_cond; ++step)
124159
{
125-
pimpl->sstp_step(step, !rhod.is_null());
160+
pimpl->sstp_step(step);
126161
pimpl->hskpng_Tpr();
127162
pimpl->cond(pimpl->opts_init.dt / pimpl->opts_init.sstp_cond, opts.RH_max);
128163
}
@@ -146,7 +181,7 @@ namespace libcloudphxx
146181
if (opts.chem_dsl)
147182
{
148183
//adjust trace gases to substepping
149-
pimpl->sstp_step_chem(step, !rhod.is_null());
184+
pimpl->sstp_step_chem(step);
150185

151186
//dissolving trace gases (Henrys law)
152187
pimpl->chem_henry(pimpl->opts_init.dt / pimpl->opts_init.sstp_chem);

tests/python/unit/api_lgrngn.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,41 @@ def lognormal(lnr):
130130
print frombuffer(prtcls.outbuf())
131131
assert frombuffer(prtcls.outbuf()) == opts_init.sd_conc # parcel set-up
132132

133+
# ----------
134+
# 0D (parcel) with explicit calls to sync_in and step_cond
135+
print "0D"
136+
rhod = arr_t([ 1.])
137+
th = arr_t([300.])
138+
rv = arr_t([ 0.01])
139+
140+
prtcls = lgrngn.factory(backend, opts_init)
141+
prtcls.init(th, rv, rhod)
142+
try:
143+
prtcls.init(th, rv, rhod)
144+
raise Exception("multiple init call not reported!")
145+
except:
146+
pass
147+
try:
148+
prtcls.step_cond(opts, th, rv)
149+
raise Exception("sync_in/cond order mismatch not reported!")
150+
except:
151+
pass
152+
prtcls.sync_in(th, rv, rhod)
153+
prtcls.step_cond(opts, th, rv)
154+
prtcls.step_async(opts)
155+
prtcls.step_sync(opts, th, rv)
156+
prtcls.diag_dry_rng(0.,1.)
157+
prtcls.diag_wet_rng(0.,1.)
158+
prtcls.diag_dry_mom(1)
159+
prtcls.diag_wet_mom(1)
160+
prtcls.diag_kappa_mom(1)
161+
puddle = prtcls.diag_puddle()
162+
print 'puddle: ', puddle
163+
#prtcls.diag_chem(lgrngn.chem_species_t.OH)
164+
prtcls.diag_all()
165+
prtcls.diag_sd_conc()
166+
print frombuffer(prtcls.outbuf())
167+
assert frombuffer(prtcls.outbuf()) == opts_init.sd_conc # parcel set-up
133168

134169

135170
# ----------

0 commit comments

Comments
 (0)