Skip to content

Commit 3ee9dd9

Browse files
jcurtis2slayoo
andauthored
Add access to rand.F90 functions and subroutines (#228)
Co-authored-by: Sylwester Arabas <[email protected]>
1 parent 61154bc commit 3ee9dd9

File tree

8 files changed

+113
-2
lines changed

8 files changed

+113
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ set(PyPartMC_sources
4747
run_part.F90 run_part_opt.F90 util.F90 aero_data.F90 aero_state.F90 env_state.F90 gas_data.F90
4848
gas_state.F90 scenario.F90 condense.F90 aero_particle.F90 bin_grid.F90
4949
camp_core.F90 photolysis.F90 aero_mode.F90 aero_dist.F90 bin_grid.cpp condense.cpp run_part.cpp
50-
scenario.cpp util.cpp
50+
scenario.cpp util.cpp rand.cpp rand.F90
5151
)
5252
add_prefix(src/ PyPartMC_sources)
5353

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def build_extension(self, ext): # pylint: disable=too-many-branches
150150
extras_require={
151151
"tests": [
152152
"pytest",
153+
"pytest-order",
153154
"fastcore!=1.5.8", # https://github.com/fastai/fastcore/issues/439
154155
"ghapi",
155156
]

src/pypartmc.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "pybind11_json/pybind11_json.hpp"
1010

1111
#include "util.hpp"
12+
#include "rand.hpp"
1213
#include "run_part.hpp"
1314
#include "run_part_opt.hpp"
1415
#include "aero_data.hpp"
@@ -416,6 +417,14 @@ PYBIND11_MODULE(_PyPartMC, m) {
416417
"Evaluate a loss rate function."
417418
);
418419

420+
m.def(
421+
"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"
422+
);
423+
424+
m.def(
425+
"rand_normal", &rand_normal, "Generates a normally distributed random number with the given mean and standard deviation"
426+
);
427+
419428
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
420429

421430
m.attr("__all__") = py::make_tuple(
@@ -446,6 +455,8 @@ PYBIND11_MODULE(_PyPartMC, m) {
446455
"sphere_rad2vol",
447456
"diam2rad",
448457
"loss_rate_dry_dep",
449-
"loss_rate"
458+
"loss_rate",
459+
"rand_init",
460+
"rand_normal"
450461
);
451462
}

src/rand.F90

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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_rand
8+
9+
use iso_c_binding
10+
use pmc_rand
11+
12+
implicit none
13+
14+
contains
15+
16+
subroutine f_pmc_srand(seed, offset) bind(C)
17+
integer(c_int) :: seed
18+
integer(c_int) :: offset
19+
20+
call pmc_srand(seed, offset)
21+
22+
end subroutine
23+
24+
subroutine f_rand_normal(mean, stddev, val) bind(C)
25+
real(c_double) :: mean
26+
real(c_double) :: stddev
27+
real(c_double) :: val
28+
29+
val = rand_normal(mean, stddev)
30+
31+
end subroutine
32+
33+
end module

src/rand.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
#include "rand.hpp"
8+
9+
void rand_init(int seed, int offset) {
10+
f_pmc_srand(&seed, &offset);
11+
}
12+
13+
double rand_normal(double mean, double stddev) {
14+
double val;
15+
16+
f_rand_normal(&mean, &stddev, &val);
17+
18+
return val;
19+
}

src/rand.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
extern "C" void f_pmc_srand(const int*, const int*);
10+
extern "C" void f_rand_normal(const double*, const double*, double*);
11+
void rand_init(int seed, int offset);
12+
double rand_normal(double mean, double stddev);

tests/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import PyPartMC as ppmc
2+
3+
ppmc.rand_init(44, 0)

tests/test_rand.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 pytest
8+
9+
import PyPartMC as ppmc
10+
11+
12+
@pytest.mark.order(-1)
13+
@pytest.mark.parametrize(
14+
"init_args",
15+
(
16+
((44, 1), (44, 1)),
17+
pytest.param(((44, 0), (44, 1)), marks=pytest.mark.xfail(strict=True)),
18+
),
19+
)
20+
def test_set_rand_seed(init_args):
21+
# arrange
22+
rand_arg = (0, 1)
23+
values = []
24+
25+
# act
26+
for init_arg in init_args:
27+
ppmc.rand_init(*init_arg)
28+
values.append(ppmc.rand_normal(*rand_arg))
29+
30+
# assert
31+
for value in values[1:]:
32+
assert value == values[0]

0 commit comments

Comments
 (0)