-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add access to rand.F90 functions and subroutines (#228)
Co-authored-by: Sylwester Arabas <[email protected]>
- Loading branch information
Showing
8 changed files
with
113 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import PyPartMC as ppmc | ||
|
||
ppmc.rand_init(44, 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |