Skip to content

Commit b17a724

Browse files
authored
adding basic AeroDist wrapper (#186)
1 parent fc0dfde commit b17a724

File tree

8 files changed

+117
-21
lines changed

8 files changed

+117
-21
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ set(PyPartMC_sources
3939
pypartmc.cpp gimmicks.cpp fake_netcdf.cpp fake_spec_file.cpp sys.cpp
4040
run_part.F90 run_part_opt.F90 util.F90 aero_data.F90 aero_state.F90 env_state.F90 gas_data.F90
4141
gas_state.F90 scenario.F90 condense.F90 aero_particle.F90 bin_grid.F90
42-
camp_core.F90 photolysis.F90 aero_mode.F90
42+
camp_core.F90 photolysis.F90 aero_mode.F90 aero_dist.F90
4343
)
4444
add_prefix(src/ PyPartMC_sources)
4545

src/aero_dist.F90

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
!###################################################################################################
2+
! This file is a part of PyPartMC licensed under the GNU General Public License v3 (LICENSE file) #
3+
! Copyright (C) 2022 University of Illinois Urbana-Champaign #
4+
! Authors: https://github.com/open-atmos/PyPartMC/graphs/contributors #
5+
!###################################################################################################
6+
7+
module PyPartMC_aero_dist
8+
use iso_c_binding
9+
use pmc_aero_dist
10+
implicit none
11+
12+
contains
13+
14+
subroutine f_aero_dist_ctor(ptr_c) bind(C)
15+
type(aero_dist_t), pointer :: ptr_f => null()
16+
type(c_ptr), intent(out) :: ptr_c
17+
18+
allocate(ptr_f)
19+
20+
ptr_c = c_loc(ptr_f)
21+
end subroutine
22+
23+
subroutine f_aero_dist_dtor(ptr_c) bind(C)
24+
type(aero_dist_t), pointer :: ptr_f => null()
25+
type(c_ptr), intent(in) :: ptr_c
26+
27+
call c_f_pointer(ptr_c, ptr_f)
28+
29+
deallocate(ptr_f)
30+
end subroutine
31+
32+
subroutine f_aero_dist_from_json(ptr_c, aero_data_ptr_c) bind(C)
33+
type(aero_dist_t), pointer :: aero_dist => null()
34+
type(aero_data_t), pointer :: aero_data_ptr_f => null()
35+
type(c_ptr), intent(inout) :: ptr_c, aero_data_ptr_c
36+
type(spec_file_t) :: file
37+
38+
call c_f_pointer(ptr_c, aero_dist)
39+
call c_f_pointer(aero_data_ptr_c, aero_data_ptr_f)
40+
41+
call spec_file_read_aero_dist(file, aero_data_ptr_f, aero_dist)
42+
end subroutine
43+
end module

src/aero_dist.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*##################################################################################################
2+
# This file is a part of PyPartMC licensed under the GNU General Public License v3 (LICENSE file) #
3+
# Copyright (C) 2022 University of Illinois Urbana-Champaign #
4+
# Authors: https://github.com/open-atmos/PyPartMC/graphs/contributors #
5+
##################################################################################################*/
6+
7+
#pragma once
8+
9+
#include "pmc_resource.hpp"
10+
11+
extern "C" void f_aero_dist_ctor(
12+
void *ptr
13+
) noexcept;
14+
15+
extern "C" void f_aero_dist_dtor(
16+
void *ptr
17+
) noexcept;
18+
19+
extern "C" void f_aero_dist_from_json(
20+
void *ptr,
21+
void *aero_data_ptr
22+
) noexcept;
23+
24+
struct AeroDist {
25+
PMCResource ptr;
26+
27+
AeroDist(AeroData &aero_data, const nlohmann::json &json):
28+
ptr(f_aero_dist_ctor, f_aero_dist_dtor)
29+
{
30+
gimmick_ptr() = std::make_unique<InputGimmick>(json, "", "mode_name", 1);
31+
f_aero_dist_from_json(ptr.f_arg_non_const(), aero_data.ptr.f_arg_non_const());
32+
gimmick_ptr().reset();
33+
}
34+
};

src/aero_mode.F90

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,6 @@ subroutine f_aero_mode_dtor(ptr_c) bind(C)
2929
deallocate(ptr_f)
3030
end subroutine
3131

32-
subroutine f_aero_mode_init(ptr_c, aero_data_ptr_c) bind(C)
33-
type(c_ptr), intent(inout) :: ptr_c
34-
type(c_ptr), intent(in) :: aero_data_ptr_c
35-
type(aero_mode_t), pointer :: aero_mode => null()
36-
type(aero_data_t), pointer :: aero_data => null()
37-
38-
call c_f_pointer(ptr_c, aero_mode)
39-
call c_f_pointer(aero_data_ptr_c, aero_data)
40-
end subroutine
41-
4232
subroutine f_aero_mode_set_num_conc(ptr_c, val) bind(C)
4333
type(c_ptr), intent(in) :: ptr_c
4434
type(aero_mode_t), pointer :: aero_mode => null()

src/aero_mode.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ extern "C" void f_aero_mode_dtor(
1717
void *ptr
1818
) noexcept;
1919

20-
extern "C" void f_aero_mode_init(
21-
void *ptr,
22-
const void *aero_data_ptr
23-
) noexcept;
24-
2520
extern "C" void f_aero_mode_get_num_conc(
2621
const void *ptr,
2722
double *val
@@ -117,8 +112,6 @@ struct AeroMode {
117112
AeroMode(AeroData &aero_data, const nlohmann::json &json) :
118113
ptr(f_aero_mode_ctor, f_aero_mode_dtor)
119114
{
120-
f_aero_mode_init(ptr.f_arg_non_const(), aero_data.ptr.f_arg());
121-
122115
gimmick_ptr() = std::make_unique<InputGimmick>(json, "", "mode_name");
123116
f_aero_mode_from_json(ptr.f_arg_non_const(), aero_data.ptr.f_arg_non_const());
124117
gimmick_ptr().reset();

src/gimmicks.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,15 @@ struct InputGimmick: Gimmick {
203203
private:
204204
std::string key_cond, key_name;
205205
std::string last_read_line_key = "";
206+
std::size_t max_zoom_level;
206207

207208
public:
208209
InputGimmick(
209210
const nlohmann::json &json,
210211
const std::string key_cond = "",
211-
const std::string key_name = ""
212-
) : Gimmick(json), key_cond(key_cond), key_name(key_name)
212+
const std::string key_name = "",
213+
const std::size_t max_zoom_level = 3
214+
) : Gimmick(json), key_cond(key_cond), key_name(key_name), max_zoom_level(max_zoom_level)
213215
{}
214216

215217
std::string str() const {
@@ -219,7 +221,7 @@ struct InputGimmick: Gimmick {
219221
bool read_line(std::string &name, std::string &data) {
220222
bool eof = this->is_empty();
221223

222-
if (this->zoom_level() == 3) { // TODO #112
224+
if (this->zoom_level() == this->max_zoom_level) { // TODO #112
223225
eof = true;
224226
this->zoom_out();
225227
}

src/pypartmc.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "run_part.hpp"
1313
#include "run_part_opt.hpp"
1414
#include "aero_data.hpp"
15+
#include "aero_dist.hpp"
1516
#include "aero_mode.hpp"
1617
#include "aero_state.hpp"
1718
#include "env_state.hpp"
@@ -312,6 +313,10 @@ PYBIND11_MODULE(_PyPartMC, m) {
312313
.def_property("type", &AeroMode::get_type, &AeroMode::set_type)
313314
;
314315

316+
py::class_<AeroDist>(m,"AeroDist")
317+
.def(py::init<AeroData&, const nlohmann::json&>())
318+
;
319+
315320
m.def(
316321
"histogram_1d", &histogram_1d, py::return_value_policy::copy,
317322
"Return a 1D histogram with of the given weighted data, scaled by the bin sizes."
@@ -363,6 +368,7 @@ PYBIND11_MODULE(_PyPartMC, m) {
363368
m.attr("__all__") = py::make_tuple(
364369
"__version__",
365370
"AeroData",
371+
"AeroDist",
366372
"AeroMode",
367373
"AeroState",
368374
"AeroParticle",

tests/test_aero_dist.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
####################################################################################################
2+
# This file is a part of PyPartMC licensed under the GNU General Public License v3 (LICENSE file) #
3+
# Copyright (C) 2022 University of Illinois Urbana-Champaign #
4+
# Authors: https://github.com/open-atmos/PyPartMC/graphs/contributors #
5+
####################################################################################################
6+
7+
import PyPartMC as ppmc
8+
9+
from .test_aero_data import AERO_DATA_CTOR_ARG_MINIMAL
10+
from .test_aero_mode import AERO_MODE_CTOR_LOG_NORMAL
11+
12+
AERO_DIST_CTOR_ARG_MINIMAL = [
13+
AERO_MODE_CTOR_LOG_NORMAL,
14+
]
15+
16+
17+
# pylint: disable=too-few-public-methods
18+
class TestAeroDist:
19+
@staticmethod
20+
def test_ctor():
21+
# arrange
22+
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)
23+
24+
# act
25+
sut = ppmc.AeroDist(aero_data, AERO_DIST_CTOR_ARG_MINIMAL)
26+
27+
# assert
28+
assert sut is not None

0 commit comments

Comments
 (0)