Skip to content

Commit

Permalink
added ability to set GasState values from a JSON object (#204)
Browse files Browse the repository at this point in the history
Co-authored-by: Sylwester Arabas <[email protected]>
  • Loading branch information
jcurtis2 and slayoo authored Apr 11, 2023
1 parent eca4225 commit 8a181f9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
5 changes: 2 additions & 3 deletions src/gas_state.F90
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ subroutine f_gas_state_from_json(ptr_c, gas_data_ptr_c) bind(C)
type(gas_state_t), pointer :: ptr_f => null()
type(c_ptr), intent(in) :: ptr_c
type(c_ptr), intent(in) :: gas_data_ptr_c

type(spec_file_t) :: file
type(gas_data_t), pointer :: gas_data_ptr_f => null()
integer :: ncid

call c_f_pointer(ptr_c, ptr_f)
call c_f_pointer(gas_data_ptr_c, gas_data_ptr_f)
call gas_state_input_netcdf(ptr_f, ncid, gas_data_ptr_f)
call spec_file_read_gas_state(file, gas_data_ptr_f, ptr_f)
end subroutine

subroutine f_gas_state_to_json(ptr_c) bind(C)
Expand Down
10 changes: 9 additions & 1 deletion src/gas_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extern "C" void f_gas_state_set_item(const void *ptr, const int *idx, const doub
extern "C" void f_gas_state_get_item(const void *ptr, const int *idx, double *val) noexcept;
extern "C" void f_gas_state_len(const void *ptr, int *len) noexcept;
extern "C" void f_gas_state_to_json(const void *ptr) noexcept;
extern "C" void f_gas_state_from_json(const void *ptr) noexcept;
extern "C" void f_gas_state_from_json(const void *ptr, const void *gasdata_ptr) noexcept;
extern "C" void f_gas_state_set_size(const void *ptr, const void *gasdata_ptr) noexcept;
extern "C" void f_gas_state_mix_rats(const void *ptr, const double *data, const int *len);

Expand Down Expand Up @@ -116,4 +116,12 @@ struct GasState {
}
return data;
}

static void set_mix_rats(const GasState &self, const nlohmann::json &json) {

gimmick_ptr() = std::make_unique<InputGimmick>(json);
f_gas_state_from_json(self.ptr.f_arg(),
self.gas_data->ptr.f_arg());
gimmick_ptr().reset(); // TODO #117: guard
}
};
6 changes: 3 additions & 3 deletions src/pypartmc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ PYBIND11_MODULE(_PyPartMC, m) {
"sets the GasState to the size of GasData")
.def("mix_rat", GasState::mix_rat,
"returns the mixing ratio of a gas species")
.def_property_readonly("mix_rats", GasState::mix_rats,
"returns array of mixing ratios")
.def_property("mix_rats", &GasState::mix_rats, &GasState::set_mix_rats,
"provides access (read of write) to the array of mixing ratios")
;

py::class_<RunPartOpt>(m,
Expand All @@ -314,7 +314,7 @@ PYBIND11_MODULE(_PyPartMC, m) {
py::class_<AeroMode>(m,"AeroMode")
.def(py::init<AeroData&, const nlohmann::json&>())
.def_property("num_conc", &AeroMode::get_num_conc, &AeroMode::set_num_conc,
"returns the total number concentration of a mode")
"provides access (read or write) to the total number concentration of a mode")
.def("num_dist", &AeroMode::num_dist,
"returns the binned number concenration of a mode")
.def_property("vol_frac", &AeroMode::get_vol_frac,
Expand Down
33 changes: 33 additions & 0 deletions tests/test_gas_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
GAS_DATA_MINIMAL = ppmc.GasData(GAS_DATA_CTOR_ARG_MINIMAL)


GAS_STATE_MINIMAL = ({"SO2": [0.1]},)


class TestGasState:
@staticmethod
def test_ctor_valid():
Expand Down Expand Up @@ -97,3 +100,33 @@ def test_get_mix_rats():

# assert
assert len(sut.mix_rats) == len(sut)

@staticmethod
def test_set_mix_rats_from_json():
# arrange
gas_data = ppmc.GasData(
(
"SO2",
"NO2",
"NO",
"CO",
)
)
sut = ppmc.GasState(gas_data)

# act
gas_state_init_values = ({"SO2": [0.1]}, {"CO": [0.5]})
sut.mix_rats = gas_state_init_values

# assert
idx_set = []
for item in gas_state_init_values:
keys = item.keys()
assert len(keys) == 1
key = tuple(keys)[0]
val = tuple(item.values())[0][0]
idx_set.append(gas_data.spec_by_name(key))
assert sut[gas_data.spec_by_name(key)] == val
for i_spec in range(gas_data.n_spec):
if not i_spec in idx_set:
assert sut[i_spec] == 0

0 comments on commit 8a181f9

Please sign in to comment.