Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
examples/tmp*.pdf
examples/tmp*.svg
examples/tmp*.gif
**/__pycache__/
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@
shallow = true
[submodule "nanobind_json"]
path = gitmodules/nanobind_json
url = https://github.com/Griger5/nanobind_json
url = https://github.com/Griger5/nanobind_json.git
shallow = true
2 changes: 1 addition & 1 deletion src/aero_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct AeroData {
f_aero_data_from_camp(this->ptr.f_arg(), camp_core.ptr.f_arg());
}

AeroData(const nlohmann::json &json) :
AeroData(const nlohmann::ordered_json &json) :
ptr(f_aero_data_ctor, f_aero_data_dtor)
{
if (!InputJSONResource::unique_keys(json))
Expand Down
2 changes: 1 addition & 1 deletion src/aero_dist.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct AeroDist {

AeroDist(
std::shared_ptr<AeroData> aero_data,
const nlohmann::json &json
const nlohmann::ordered_json &json
):
ptr(f_aero_dist_ctor, f_aero_dist_dtor),
aero_data(aero_data)
Expand Down
4 changes: 2 additions & 2 deletions src/aero_mode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ struct AeroMode {
ptr(f_aero_mode_ctor, f_aero_mode_dtor)
{}

AeroMode(AeroData &aero_data, const nlohmann::json &json) :
AeroMode(AeroData &aero_data, const nlohmann::ordered_json &json) :
ptr(f_aero_mode_ctor, f_aero_mode_dtor)
{
if (json.size() != 1 || !json.is_object() || !json.begin().value().is_object())
Expand All @@ -149,7 +149,7 @@ struct AeroMode {
guard.check_parameters();
}

static void check_mode_json(const nlohmann::json &mode) {
static void check_mode_json(const nlohmann::ordered_json &mode) {
for (auto key : std::set<std::string>({"mass_frac", "mode_type"})) // TODO #320: more...
if (mode.find(key) == mode.end())
throw std::runtime_error("mode parameters dict must include key '" + key + "'");
Expand Down
2 changes: 1 addition & 1 deletion src/env_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extern "C" void f_env_state_air_dens(const void *ptr, double *air_density) noexc
struct EnvState {
PMCResource ptr;

EnvState(const nlohmann::json &json) :
EnvState(const nlohmann::ordered_json &json) :
ptr(f_env_state_ctor, f_env_state_dtor)
{
JSONResourceGuard<InputJSONResource> guard(json);
Expand Down
10 changes: 5 additions & 5 deletions src/gas_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "gas_data_parameters.hpp"
#include "camp_core.hpp"
#include "nanobind/nanobind.h"
#include "nanobind_json/nanobind_json.hpp"
#include "nanobind_json/nanobind_json.h"

extern "C" void f_gas_data_ctor(void *ptr) noexcept;
extern "C" void f_gas_data_dtor(void *ptr) noexcept;
Expand All @@ -26,7 +26,7 @@ extern "C" void f_gas_data_spec_name_by_index(const void *ptr, const int *i_spec

struct GasData {
PMCResource ptr;
const nlohmann::json json;
const nlohmann::ordered_json json;

GasData(const CampCore &CampCore) :
ptr(f_gas_data_ctor, f_gas_data_dtor)
Expand All @@ -38,11 +38,11 @@ struct GasData {
ptr(f_gas_data_ctor, f_gas_data_dtor),
json(tpl)
{
auto json_array = nlohmann::json::array();
auto json_array = nlohmann::ordered_json::array();
for (const auto item : tpl)
json_array.push_back(nlohmann::json::object({{
json_array.push_back(nlohmann::ordered_json::object({{
nanobind::cast<std::string>(item),
nlohmann::json::array()
nlohmann::ordered_json::array()
}}));

JSONResourceGuard<InputJSONResource> guard(json_array);
Expand Down
2 changes: 1 addition & 1 deletion src/gas_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ struct GasState {
return data;
}

static void set_mix_rats(const GasState &self, const nlohmann::json &json) {
static void set_mix_rats(const GasState &self, const nlohmann::ordered_json &json) {
if (json.size() == 0)
throw std::runtime_error("Non-empty sequence of mixing ratios expected");

Expand Down
6 changes: 3 additions & 3 deletions src/input_guard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

struct InputGuard {
public:
InputGuard(const nlohmann::json &j) {
InputGuard(const nlohmann::ordered_json &j) {
process_json(j);

this->dict_key_present = false;
Expand Down Expand Up @@ -71,8 +71,8 @@ struct InputGuard {

bool dict_key_present;

void process_json(const nlohmann::json &j) {
nlohmann::json flat = j.flatten();
void process_json(const nlohmann::ordered_json &j) {
nlohmann::ordered_json flat = j.flatten();

// JSON Pointer, as in a string syntax for identifying a specific value in JSON
std::vector<std::string> json_pointers;
Expand Down
30 changes: 15 additions & 15 deletions src/json_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
struct JSONResource {
private:
std::set<std::string> vars;
const nlohmann::json *json;
std::stack<const nlohmann::json*> json_parent;
const nlohmann::ordered_json *json;
std::stack<const nlohmann::ordered_json*> json_parent;

std::unique_ptr<InputGuard> input_guard_ptr;

Expand All @@ -34,7 +34,7 @@ struct JSONResource {

JSONResource() {}

JSONResource(const nlohmann::json &json) {
JSONResource(const nlohmann::ordered_json &json) {
this->set_current_json_ptr(&json);
for (auto &entry : this->json->items()) {
this->vars.insert(entry.key());
Expand All @@ -43,7 +43,7 @@ struct JSONResource {
input_guard_ptr = std::make_unique<InputGuard>(json);
};

void set_current_json_ptr(const nlohmann::json *ptr) {
void set_current_json_ptr(const nlohmann::ordered_json *ptr) {
this->json = ptr;
}

Expand Down Expand Up @@ -86,8 +86,8 @@ struct JSONResource {

void zoom_in(const bpstd::string_view &sub) noexcept {
auto it = this->json->is_array()
? this->json->at(this->json->size()-1).find(sub)
: this->json->find(sub);
? this->json->at(this->json->size()-1).find(sub.to_string())
: this->json->find(sub.to_string());

assert(*it != NULL);
this->json_parent.push(this->json);
Expand Down Expand Up @@ -157,15 +157,15 @@ struct JSONResource {
const bpstd::string_view name,
T *var
) {
*var = this->find(name)->get<T>();
*var = this->find(name.to_string())->get<T>();
}

void read_str(
const bpstd::string_view &name,
char* var_data,
int* var_size
) noexcept {
auto it = this->find(name);
auto it = this->find(name.to_string());
if (it == this->json->end())
{
assert(false);
Expand Down Expand Up @@ -240,7 +240,7 @@ struct InputJSONResource: JSONResource {

public:
InputJSONResource(
const nlohmann::json &json,
const nlohmann::ordered_json &json,
const std::string key_cond = "",
const std::string key_name = "",
const std::size_t max_zoom_level = 3
Expand Down Expand Up @@ -290,7 +290,7 @@ struct InputJSONResource: JSONResource {
return 1;
}

static bool unique_keys(const nlohmann::json &json) {
static bool unique_keys(const nlohmann::ordered_json &json) {
std::set<std::string> keys;
for (auto i=0u; i<json.size(); ++i) {
for (auto &entry : json.at(i).items()) {
Expand All @@ -304,9 +304,9 @@ struct InputJSONResource: JSONResource {
};

struct OutputJSONResource: JSONResource {
std::unique_ptr<nlohmann::json> guard;
std::unique_ptr<nlohmann::ordered_json> guard;

OutputJSONResource() : guard(std::make_unique<nlohmann::json>()) {
OutputJSONResource() : guard(std::make_unique<nlohmann::ordered_json>()) {
this->set_current_json_ptr(this->guard.get());
}

Expand All @@ -331,20 +331,20 @@ struct JSONResourceGuard {
json_resource_ptr() = std::make_unique<T>();
}

JSONResourceGuard(const nlohmann::json & json) {
JSONResourceGuard(const nlohmann::ordered_json & json) {
json_resource_ptr() = std::make_unique<T>(json);
}

JSONResourceGuard(
const nlohmann::json & json,
const nlohmann::ordered_json & json,
const std::string key_cond,
const std::string key_name
) {
json_resource_ptr() = std::make_unique<T>(json, key_cond, key_name);
}

JSONResourceGuard(
const nlohmann::json & json,
const nlohmann::ordered_json & json,
const std::string key_cond,
const std::string key_name,
const int max_zoom_level
Expand Down
21 changes: 10 additions & 11 deletions src/pypartmc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "nanobind/ndarray.h"
#undef snprintf // required to fix an issue with std::snprintf in nlohmann::json
#include "nlohmann/json.hpp"
#include "nanobind_json/nanobind_json.hpp"
#include "nanobind_json/nanobind_json.h"
#include "sundials/sundials_config.h"
#include "camp/version.h"
#include "tl/optional.hpp"
Expand Down Expand Up @@ -174,7 +174,7 @@ NB_MODULE(_PyPartMC, m) {
)pbdoc"
)
.def(nb::init<const CampCore&>())
.def(nb::init<const nlohmann::json&>())
.def(nb::init<const nlohmann::ordered_json&>())
.def("spec_by_name", AeroData::spec_by_name,
"Return the number of the species in AeroData with the given name.")
.def("__len__", AeroData::__len__, "Return number of aerosol species.")
Expand Down Expand Up @@ -404,7 +404,7 @@ NB_MODULE(_PyPartMC, m) {
scenario_t.
)pbdoc"
)
.def(nb::init<const nlohmann::json&>())
.def(nb::init<const nlohmann::ordered_json&>())
.def("set_temperature", EnvState::set_temperature,
"Set the temperature of the environment state.")
.def_prop_ro("temp", EnvState::temp,
Expand Down Expand Up @@ -463,7 +463,7 @@ NB_MODULE(_PyPartMC, m) {
nb::init<
const GasData&,
const AeroData&,
const nlohmann::json&
const nlohmann::ordered_json&
>(),
"instantiates and initializes from a JSON object"
)
Expand Down Expand Up @@ -525,7 +525,7 @@ NB_MODULE(_PyPartMC, m) {
"RunPartOpt",
"Options controlling the execution of run_part()."
)
.def(nb::init<const nlohmann::json&>())
.def(nb::init<const nlohmann::ordered_json&>())
.def_prop_ro("t_max", RunPartOpt::t_max, "Total simulation time.")
.def_prop_ro("del_t", RunPartOpt::del_t, "Time step.")
;
Expand All @@ -534,7 +534,7 @@ NB_MODULE(_PyPartMC, m) {
"RunSectOpt",
"Options controlling the execution of run_sect()."
)
.def(nb::init<const nlohmann::json&, EnvState&>())
.def(nb::init<const nlohmann::ordered_json&, EnvState&>())
.def_prop_ro("t_max", RunSectOpt::t_max, "Total simulation time.")
.def_prop_ro("del_t", RunSectOpt::del_t, "Time step.")
;
Expand All @@ -543,7 +543,7 @@ NB_MODULE(_PyPartMC, m) {
"RunExactOpt",
"Options controlling the execution of run_exact()."
)
.def(nb::init<const nlohmann::json&, EnvState&>())
.def(nb::init<const nlohmann::ordered_json&, EnvState&>())
.def_prop_ro("t_max", RunExactOpt::t_max, "Total simulation time.")
;

Expand All @@ -556,9 +556,8 @@ NB_MODULE(_PyPartMC, m) {
.def_prop_ro("widths", BinGrid::widths, "Return bin widths.")
;

nb::class_<AeroMode>(m,"AeroMode",
"An aerosol size distribution mode.")
.def(nb::init<AeroData&, const nlohmann::json&>())
nb::class_<AeroMode>(m,"AeroMode")
.def(nb::init<AeroData&, const nlohmann::ordered_json&>())
.def_prop_rw("num_conc", &AeroMode::get_num_conc, &AeroMode::set_num_conc,
"Provide access (read or write) to the total number concentration of a mode.")
.def("num_dist", &AeroMode::num_dist,
Expand All @@ -584,7 +583,7 @@ NB_MODULE(_PyPartMC, m) {
;

nb::class_<AeroDist>(m,"AeroDist")
.def(nb::init<std::shared_ptr<AeroData>, const nlohmann::json&>())
.def(nb::init<std::shared_ptr<AeroData>, const nlohmann::ordered_json&>())
.def_prop_ro("n_mode", &AeroDist::get_n_mode,
"Number of aerosol modes in the distribution.")
.def_prop_ro("num_conc", &AeroDist::get_total_num_conc,
Expand Down
4 changes: 2 additions & 2 deletions src/run_exact_opt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ extern "C" void f_run_exact_opt_t_max(const void *ptr, double *t_max) noexcept;
struct RunExactOpt {
PMCResource ptr;

RunExactOpt(const nlohmann::json &json, EnvState &env_state) :
RunExactOpt(const nlohmann::ordered_json &json, EnvState &env_state) :
ptr(f_run_exact_opt_ctor, f_run_exact_opt_dtor)
{
nlohmann::json json_copy(json);
nlohmann::ordered_json json_copy(json);

for (auto key : std::set<std::string>({
"t_output"
Expand Down
4 changes: 2 additions & 2 deletions src/run_part_opt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ struct RunPartOpt {
PMCResource ptr;
bool allow_halving, allow_doubling;

RunPartOpt(const nlohmann::json &json) :
RunPartOpt(const nlohmann::ordered_json &json) :
ptr(f_run_part_opt_ctor, f_run_part_opt_dtor)
{
nlohmann::json json_copy(json);
nlohmann::ordered_json json_copy(json);

if (json_copy.find("do_parallel") != json_copy.end() && json_copy["do_parallel"])
throw std::runtime_error("setting do_parallel=true not supported in PyPartMC");
Expand Down
4 changes: 2 additions & 2 deletions src/run_sect_opt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ extern "C" void f_run_sect_opt_del_t(const void *ptr, double *del_t) noexcept;
struct RunSectOpt {
PMCResource ptr;

RunSectOpt(const nlohmann::json &json, EnvState &env_state) :
RunSectOpt(const nlohmann::ordered_json &json, EnvState &env_state) :
ptr(f_run_sect_opt_ctor, f_run_sect_opt_dtor)
{
nlohmann::json json_copy(json);
nlohmann::ordered_json json_copy(json);

for (auto key : std::set<std::string>({
"t_output", "t_progress"
Expand Down
4 changes: 2 additions & 2 deletions src/scenario.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ extern "C" void f_scenario_aero_background_time(

struct Scenario {
PMCResource ptr;
const nlohmann::json json;
const nlohmann::ordered_json json;

Scenario(
const GasData &gas_data,
const AeroData &aero_data,
const nlohmann::json &json
const nlohmann::ordered_json &json
) :
ptr(f_scenario_ctor, f_scenario_dtor),
json(json)
Expand Down
Loading
Loading