Skip to content

Commit

Permalink
Add access to rand.F90 functions and subroutines (#228)
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 Jun 14, 2023
1 parent 61154bc commit 3ee9dd9
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ set(PyPartMC_sources
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 aero_dist.F90 bin_grid.cpp condense.cpp run_part.cpp
scenario.cpp util.cpp
scenario.cpp util.cpp rand.cpp rand.F90
)
add_prefix(src/ PyPartMC_sources)

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def build_extension(self, ext): # pylint: disable=too-many-branches
extras_require={
"tests": [
"pytest",
"pytest-order",
"fastcore!=1.5.8", # https://github.com/fastai/fastcore/issues/439
"ghapi",
]
Expand Down
13 changes: 12 additions & 1 deletion src/pypartmc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "pybind11_json/pybind11_json.hpp"

#include "util.hpp"
#include "rand.hpp"
#include "run_part.hpp"
#include "run_part_opt.hpp"
#include "aero_data.hpp"
Expand Down Expand Up @@ -416,6 +417,14 @@ PYBIND11_MODULE(_PyPartMC, m) {
"Evaluate a loss rate function."
);

m.def(
"rand_init", &rand_init, "Initializes the random number generator to the state defined by the given seed plus offset. If the seed is 0 then a seed is auto-generated from the current time plus offset"
);

m.def(
"rand_normal", &rand_normal, "Generates a normally distributed random number with the given mean and standard deviation"
);

m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);

m.attr("__all__") = py::make_tuple(
Expand Down Expand Up @@ -446,6 +455,8 @@ PYBIND11_MODULE(_PyPartMC, m) {
"sphere_rad2vol",
"diam2rad",
"loss_rate_dry_dep",
"loss_rate"
"loss_rate",
"rand_init",
"rand_normal"
);
}
33 changes: 33 additions & 0 deletions src/rand.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
!###################################################################################################
! 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_rand

use iso_c_binding
use pmc_rand

implicit none

contains

subroutine f_pmc_srand(seed, offset) bind(C)
integer(c_int) :: seed
integer(c_int) :: offset

call pmc_srand(seed, offset)

end subroutine

subroutine f_rand_normal(mean, stddev, val) bind(C)
real(c_double) :: mean
real(c_double) :: stddev
real(c_double) :: val

val = rand_normal(mean, stddev)

end subroutine

end module
19 changes: 19 additions & 0 deletions src/rand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*##################################################################################################
# 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 #
##################################################################################################*/

#include "rand.hpp"

void rand_init(int seed, int offset) {
f_pmc_srand(&seed, &offset);
}

double rand_normal(double mean, double stddev) {
double val;

f_rand_normal(&mean, &stddev, &val);

return val;
}
12 changes: 12 additions & 0 deletions src/rand.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*##################################################################################################
# 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

extern "C" void f_pmc_srand(const int*, const int*);
extern "C" void f_rand_normal(const double*, const double*, double*);
void rand_init(int seed, int offset);
double rand_normal(double mean, double stddev);
3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import PyPartMC as ppmc

ppmc.rand_init(44, 0)
32 changes: 32 additions & 0 deletions tests/test_rand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
####################################################################################################
# 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 pytest

import PyPartMC as ppmc


@pytest.mark.order(-1)
@pytest.mark.parametrize(
"init_args",
(
((44, 1), (44, 1)),
pytest.param(((44, 0), (44, 1)), marks=pytest.mark.xfail(strict=True)),
),
)
def test_set_rand_seed(init_args):
# arrange
rand_arg = (0, 1)
values = []

# act
for init_arg in init_args:
ppmc.rand_init(*init_arg)
values.append(ppmc.rand_normal(*rand_arg))

# assert
for value in values[1:]:
assert value == values[0]

0 comments on commit 3ee9dd9

Please sign in to comment.