Skip to content

Commit 8a181f9

Browse files
jcurtis2slayoo
andauthored
added ability to set GasState values from a JSON object (#204)
Co-authored-by: Sylwester Arabas <[email protected]>
1 parent eca4225 commit 8a181f9

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

src/gas_state.F90

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,12 @@ subroutine f_gas_state_from_json(ptr_c, gas_data_ptr_c) bind(C)
6161
type(gas_state_t), pointer :: ptr_f => null()
6262
type(c_ptr), intent(in) :: ptr_c
6363
type(c_ptr), intent(in) :: gas_data_ptr_c
64-
64+
type(spec_file_t) :: file
6565
type(gas_data_t), pointer :: gas_data_ptr_f => null()
66-
integer :: ncid
6766

6867
call c_f_pointer(ptr_c, ptr_f)
6968
call c_f_pointer(gas_data_ptr_c, gas_data_ptr_f)
70-
call gas_state_input_netcdf(ptr_f, ncid, gas_data_ptr_f)
69+
call spec_file_read_gas_state(file, gas_data_ptr_f, ptr_f)
7170
end subroutine
7271

7372
subroutine f_gas_state_to_json(ptr_c) bind(C)

src/gas_state.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extern "C" void f_gas_state_set_item(const void *ptr, const int *idx, const doub
1818
extern "C" void f_gas_state_get_item(const void *ptr, const int *idx, double *val) noexcept;
1919
extern "C" void f_gas_state_len(const void *ptr, int *len) noexcept;
2020
extern "C" void f_gas_state_to_json(const void *ptr) noexcept;
21-
extern "C" void f_gas_state_from_json(const void *ptr) noexcept;
21+
extern "C" void f_gas_state_from_json(const void *ptr, const void *gasdata_ptr) noexcept;
2222
extern "C" void f_gas_state_set_size(const void *ptr, const void *gasdata_ptr) noexcept;
2323
extern "C" void f_gas_state_mix_rats(const void *ptr, const double *data, const int *len);
2424

@@ -116,4 +116,12 @@ struct GasState {
116116
}
117117
return data;
118118
}
119+
120+
static void set_mix_rats(const GasState &self, const nlohmann::json &json) {
121+
122+
gimmick_ptr() = std::make_unique<InputGimmick>(json);
123+
f_gas_state_from_json(self.ptr.f_arg(),
124+
self.gas_data->ptr.f_arg());
125+
gimmick_ptr().reset(); // TODO #117: guard
126+
}
119127
};

src/pypartmc.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ PYBIND11_MODULE(_PyPartMC, m) {
291291
"sets the GasState to the size of GasData")
292292
.def("mix_rat", GasState::mix_rat,
293293
"returns the mixing ratio of a gas species")
294-
.def_property_readonly("mix_rats", GasState::mix_rats,
295-
"returns array of mixing ratios")
294+
.def_property("mix_rats", &GasState::mix_rats, &GasState::set_mix_rats,
295+
"provides access (read of write) to the array of mixing ratios")
296296
;
297297

298298
py::class_<RunPartOpt>(m,
@@ -314,7 +314,7 @@ PYBIND11_MODULE(_PyPartMC, m) {
314314
py::class_<AeroMode>(m,"AeroMode")
315315
.def(py::init<AeroData&, const nlohmann::json&>())
316316
.def_property("num_conc", &AeroMode::get_num_conc, &AeroMode::set_num_conc,
317-
"returns the total number concentration of a mode")
317+
"provides access (read or write) to the total number concentration of a mode")
318318
.def("num_dist", &AeroMode::num_dist,
319319
"returns the binned number concenration of a mode")
320320
.def_property("vol_frac", &AeroMode::get_vol_frac,

tests/test_gas_state.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
GAS_DATA_MINIMAL = ppmc.GasData(GAS_DATA_CTOR_ARG_MINIMAL)
1616

1717

18+
GAS_STATE_MINIMAL = ({"SO2": [0.1]},)
19+
20+
1821
class TestGasState:
1922
@staticmethod
2023
def test_ctor_valid():
@@ -97,3 +100,33 @@ def test_get_mix_rats():
97100

98101
# assert
99102
assert len(sut.mix_rats) == len(sut)
103+
104+
@staticmethod
105+
def test_set_mix_rats_from_json():
106+
# arrange
107+
gas_data = ppmc.GasData(
108+
(
109+
"SO2",
110+
"NO2",
111+
"NO",
112+
"CO",
113+
)
114+
)
115+
sut = ppmc.GasState(gas_data)
116+
117+
# act
118+
gas_state_init_values = ({"SO2": [0.1]}, {"CO": [0.5]})
119+
sut.mix_rats = gas_state_init_values
120+
121+
# assert
122+
idx_set = []
123+
for item in gas_state_init_values:
124+
keys = item.keys()
125+
assert len(keys) == 1
126+
key = tuple(keys)[0]
127+
val = tuple(item.values())[0][0]
128+
idx_set.append(gas_data.spec_by_name(key))
129+
assert sut[gas_data.spec_by_name(key)] == val
130+
for i_spec in range(gas_data.n_spec):
131+
if not i_spec in idx_set:
132+
assert sut[i_spec] == 0

0 commit comments

Comments
 (0)