Skip to content

Commit

Permalink
adding basic AeroDist wrapper (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
slayoo authored Dec 3, 2022
1 parent fc0dfde commit b17a724
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ set(PyPartMC_sources
pypartmc.cpp gimmicks.cpp fake_netcdf.cpp fake_spec_file.cpp sys.cpp
run_part.F90 run_part_opt.F90 util.F90 aero_data.F90 aero_state.F90 env_state.F90 gas_data.F90
gas_state.F90 scenario.F90 condense.F90 aero_particle.F90 bin_grid.F90
camp_core.F90 photolysis.F90 aero_mode.F90
camp_core.F90 photolysis.F90 aero_mode.F90 aero_dist.F90
)
add_prefix(src/ PyPartMC_sources)

Expand Down
43 changes: 43 additions & 0 deletions src/aero_dist.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
!###################################################################################################
! 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 #
!###################################################################################################

module PyPartMC_aero_dist
use iso_c_binding
use pmc_aero_dist
implicit none

contains

subroutine f_aero_dist_ctor(ptr_c) bind(C)
type(aero_dist_t), pointer :: ptr_f => null()
type(c_ptr), intent(out) :: ptr_c

allocate(ptr_f)

ptr_c = c_loc(ptr_f)
end subroutine

subroutine f_aero_dist_dtor(ptr_c) bind(C)
type(aero_dist_t), pointer :: ptr_f => null()
type(c_ptr), intent(in) :: ptr_c

call c_f_pointer(ptr_c, ptr_f)

deallocate(ptr_f)
end subroutine

subroutine f_aero_dist_from_json(ptr_c, aero_data_ptr_c) bind(C)
type(aero_dist_t), pointer :: aero_dist => null()
type(aero_data_t), pointer :: aero_data_ptr_f => null()
type(c_ptr), intent(inout) :: ptr_c, aero_data_ptr_c
type(spec_file_t) :: file

call c_f_pointer(ptr_c, aero_dist)
call c_f_pointer(aero_data_ptr_c, aero_data_ptr_f)

call spec_file_read_aero_dist(file, aero_data_ptr_f, aero_dist)
end subroutine
end module
34 changes: 34 additions & 0 deletions src/aero_dist.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*##################################################################################################
# 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 "pmc_resource.hpp"

extern "C" void f_aero_dist_ctor(
void *ptr
) noexcept;

extern "C" void f_aero_dist_dtor(
void *ptr
) noexcept;

extern "C" void f_aero_dist_from_json(
void *ptr,
void *aero_data_ptr
) noexcept;

struct AeroDist {
PMCResource ptr;

AeroDist(AeroData &aero_data, const nlohmann::json &json):
ptr(f_aero_dist_ctor, f_aero_dist_dtor)
{
gimmick_ptr() = std::make_unique<InputGimmick>(json, "", "mode_name", 1);
f_aero_dist_from_json(ptr.f_arg_non_const(), aero_data.ptr.f_arg_non_const());
gimmick_ptr().reset();
}
};
10 changes: 0 additions & 10 deletions src/aero_mode.F90
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ subroutine f_aero_mode_dtor(ptr_c) bind(C)
deallocate(ptr_f)
end subroutine

subroutine f_aero_mode_init(ptr_c, aero_data_ptr_c) bind(C)
type(c_ptr), intent(inout) :: ptr_c
type(c_ptr), intent(in) :: aero_data_ptr_c
type(aero_mode_t), pointer :: aero_mode => null()
type(aero_data_t), pointer :: aero_data => null()

call c_f_pointer(ptr_c, aero_mode)
call c_f_pointer(aero_data_ptr_c, aero_data)
end subroutine

subroutine f_aero_mode_set_num_conc(ptr_c, val) bind(C)
type(c_ptr), intent(in) :: ptr_c
type(aero_mode_t), pointer :: aero_mode => null()
Expand Down
7 changes: 0 additions & 7 deletions src/aero_mode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ extern "C" void f_aero_mode_dtor(
void *ptr
) noexcept;

extern "C" void f_aero_mode_init(
void *ptr,
const void *aero_data_ptr
) noexcept;

extern "C" void f_aero_mode_get_num_conc(
const void *ptr,
double *val
Expand Down Expand Up @@ -117,8 +112,6 @@ struct AeroMode {
AeroMode(AeroData &aero_data, const nlohmann::json &json) :
ptr(f_aero_mode_ctor, f_aero_mode_dtor)
{
f_aero_mode_init(ptr.f_arg_non_const(), aero_data.ptr.f_arg());

gimmick_ptr() = std::make_unique<InputGimmick>(json, "", "mode_name");
f_aero_mode_from_json(ptr.f_arg_non_const(), aero_data.ptr.f_arg_non_const());
gimmick_ptr().reset();
Expand Down
8 changes: 5 additions & 3 deletions src/gimmicks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,15 @@ struct InputGimmick: Gimmick {
private:
std::string key_cond, key_name;
std::string last_read_line_key = "";
std::size_t max_zoom_level;

public:
InputGimmick(
const nlohmann::json &json,
const std::string key_cond = "",
const std::string key_name = ""
) : Gimmick(json), key_cond(key_cond), key_name(key_name)
const std::string key_name = "",
const std::size_t max_zoom_level = 3
) : Gimmick(json), key_cond(key_cond), key_name(key_name), max_zoom_level(max_zoom_level)
{}

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

if (this->zoom_level() == 3) { // TODO #112
if (this->zoom_level() == this->max_zoom_level) { // TODO #112
eof = true;
this->zoom_out();
}
Expand Down
6 changes: 6 additions & 0 deletions src/pypartmc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "run_part.hpp"
#include "run_part_opt.hpp"
#include "aero_data.hpp"
#include "aero_dist.hpp"
#include "aero_mode.hpp"
#include "aero_state.hpp"
#include "env_state.hpp"
Expand Down Expand Up @@ -312,6 +313,10 @@ PYBIND11_MODULE(_PyPartMC, m) {
.def_property("type", &AeroMode::get_type, &AeroMode::set_type)
;

py::class_<AeroDist>(m,"AeroDist")
.def(py::init<AeroData&, const nlohmann::json&>())
;

m.def(
"histogram_1d", &histogram_1d, py::return_value_policy::copy,
"Return a 1D histogram with of the given weighted data, scaled by the bin sizes."
Expand Down Expand Up @@ -363,6 +368,7 @@ PYBIND11_MODULE(_PyPartMC, m) {
m.attr("__all__") = py::make_tuple(
"__version__",
"AeroData",
"AeroDist",
"AeroMode",
"AeroState",
"AeroParticle",
Expand Down
28 changes: 28 additions & 0 deletions tests/test_aero_dist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
####################################################################################################
# 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 #
####################################################################################################

import PyPartMC as ppmc

from .test_aero_data import AERO_DATA_CTOR_ARG_MINIMAL
from .test_aero_mode import AERO_MODE_CTOR_LOG_NORMAL

AERO_DIST_CTOR_ARG_MINIMAL = [
AERO_MODE_CTOR_LOG_NORMAL,
]


# pylint: disable=too-few-public-methods
class TestAeroDist:
@staticmethod
def test_ctor():
# arrange
aero_data = ppmc.AeroData(AERO_DATA_CTOR_ARG_MINIMAL)

# act
sut = ppmc.AeroDist(aero_data, AERO_DIST_CTOR_ARG_MINIMAL)

# assert
assert sut is not None

0 comments on commit b17a724

Please sign in to comment.