forked from open-atmos/PyPartMC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscenario.hpp
251 lines (220 loc) · 7.34 KB
/
scenario.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*##################################################################################################
# This file is a part of PyPartMC licensed under the GNU General Public License v3 (LICENSE file) #
# Copyright (C) 2022 University of Illinois Urbana-Champaign #
# Authors: https://github.com/open-atmos/PyPartMC/graphs/contributors #
##################################################################################################*/
#pragma once
#include "json_resource.hpp"
#include "pmc_resource.hpp"
#include "aero_data.hpp"
#include "aero_dist.hpp"
#include "env_state.hpp"
#include "gas_data.hpp"
extern "C" void f_scenario_ctor(void *ptr) noexcept;
extern "C" void f_scenario_dtor(void *ptr) noexcept;
extern "C" void f_scenario_from_json(
const void *gas_data,
const void *aero_data,
const void *scenario
)
#ifndef __linux__
noexcept
#endif
;
extern "C" void f_scenario_loss_rate(
const void *scenario,
const double *vol,
const double *density,
const void *aero_data,
const void *env_state,
double *rate
) noexcept;
extern "C" void f_scenario_loss_rate_dry_dep(
const double *vol,
const double *density,
const void *aero_data,
const void *env_state,
double *rate
) noexcept;
extern "C" void f_scenario_init_env_state(
const void *scenario,
void *env_state,
const double *time
) noexcept;
extern "C" void f_scenario_aero_dist_emission(
const void *scenario,
const void *aero_dist,
const int *idx
) noexcept;
extern "C" void f_scenario_aero_emission_n_times(
const void *scenario,
int *n_times
) noexcept;
extern "C" void f_scenario_emission_rates(
const void *scenario,
double *rates,
const int *len
) noexcept;
extern "C" void f_scenario_emission_time(
const void *scenario,
double *times,
const int *len
) noexcept;
extern "C" void f_scenario_aero_dist_background(
const void *scenario,
const void *aero_dist,
const int *idx
) noexcept;
extern "C" void f_scenario_aero_background_n_times(
const void *scenario,
int *n_times
) noexcept;
extern "C" void f_scenario_aero_background_rate_scale(
const void *scenario,
double *rates,
const int *len
) noexcept;
extern "C" void f_scenario_aero_background_time(
const void *scenario,
double *times,
const int *len
) noexcept;
struct Scenario {
PMCResource ptr;
const nlohmann::json json;
Scenario(
const GasData &gas_data,
const AeroData &aero_data,
const nlohmann::json &json
) :
ptr(f_scenario_ctor, f_scenario_dtor),
json(json)
{
// TODO #317 - repeat analogous checks for rates
for (auto &prof: std::set<std::string>({"height", "temp", "pressure"})) {
auto prof_key = prof + "_profile";
if (json.find(prof_key) != json.end()) {
if (!json[prof_key].is_array() || json[prof_key].size() != 2)
throw std::runtime_error(prof_key + " expected to be a 2-element list (of single-element dictionaries)");
for (auto i=0; i<2; ++i)
if (!json[prof_key][i].is_object() || json[prof_key][i].size() != 1)
throw std::runtime_error(prof_key + " expected to contain only single-element dicts");
{
auto elem = json[prof_key][0];
if (elem.find("time") == elem.end())
throw std::runtime_error(prof_key + " first element is expeced to be a single-element dict with 'time' key");
}
{
auto elem = json[prof_key][1];
if (elem.find(prof) == elem.end())
throw std::runtime_error(prof_key + " second element is expeced to be a single-element dict with '" + prof + "' key");
}
if (
!json[prof_key][0]["time"].is_array() ||
!json[prof_key][1][prof].is_array() ||
json[prof_key][0]["time"].size() != json[prof_key][1][prof].size()
)
throw std::runtime_error(prof_key + " 'time' and '" + prof + "' arrays do not have matching size");
}
}
JSONResourceGuard<InputJSONResource> guard(json, "dist", "mode_name");
f_scenario_from_json(
gas_data.ptr.f_arg(),
aero_data.ptr.f_arg(),
this->ptr.f_arg()
);
}
static auto __str__(const Scenario &self) {
return self.json.dump();
}
static void init_env_state(
const Scenario &self,
EnvState &env_state,
const double time
) {
f_scenario_init_env_state(
self.ptr.f_arg(),
env_state.ptr.f_arg_non_const(),
&time
);
}
static AeroDist* get_dist(const Scenario &self, const AeroData &aero_data, const int &idx) {
// if (idx < 0 || idx >= AeroDist::get_n_mode(self))
// throw std::out_of_range("Index out of range");
AeroDist *ptr = new AeroDist();
f_scenario_aero_dist_emission(self.ptr.f_arg(), ptr, &idx);
return ptr;
}
static auto get_emissions_n_times(const Scenario &self) {
int len;
f_scenario_aero_emission_n_times(self.ptr.f_arg(), &len);
return len;
}
static auto emission_rate_scale(const Scenario &self) {
int len;
f_scenario_aero_emission_n_times(self.ptr.f_arg(), &len);
std::valarray<double> rates(len);
f_scenario_emission_rates(
self.ptr.f_arg(),
begin(rates),
&len
);
return rates;
}
static auto emission_time(const Scenario &self) {
int len;
f_scenario_aero_emission_n_times(self.ptr.f_arg(), &len);
std::valarray<double> times(len);
f_scenario_emission_time(
self.ptr.f_arg(),
begin(times),
&len
);
return times;
}
static AeroDist* get_aero_background_dist(const Scenario &self, const AeroData &aero_data, const int &idx) {
AeroDist *ptr = new AeroDist();
f_scenario_aero_dist_background(self.ptr.f_arg(), ptr, &idx);
return ptr;
}
static auto get_aero_dilution_n_times(const Scenario &self) {
int len;
f_scenario_aero_background_n_times(self.ptr.f_arg(), &len);
return len;
}
static auto aero_dilution_rate(const Scenario &self) {
int len;
f_scenario_aero_background_n_times(self.ptr.f_arg(), &len);
std::valarray<double> rates(len);
f_scenario_aero_background_rate_scale(
self.ptr.f_arg(),
begin(rates),
&len
);
return rates;
}
static auto aero_dilution_time(const Scenario &self) {
int len;
f_scenario_aero_background_n_times(self.ptr.f_arg(), &len);
std::valarray<double> times(len);
f_scenario_aero_background_time(
self.ptr.f_arg(),
begin(times),
&len
);
return times;
}
};
double loss_rate(
const Scenario &scenario,
const double vol,
const double density,
const AeroData &aero_data,
const EnvState &env_state
);
double loss_rate_dry_dep(
const double vol,
const double density,
const AeroData &aero_data,
const EnvState &env_state
);