Skip to content

Commit 68e48e1

Browse files
jcurtis2slayoo
andauthored
Add sectional and exact simulations plus aero_binned type (#426)
Co-authored-by: Sylwester Arabas <[email protected]>
1 parent 8ba1e54 commit 68e48e1

28 files changed

+1456
-20
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ add_definitions("-DSUNDIALS_INT64_T=1")
5050

5151
set(PyPartMC_sources
5252
pypartmc.cpp json_resource.cpp spec_file_pypartmc.cpp sys.cpp
53+
run_sect.F90 run_sect_opt.F90 run_exact.F90 run_exact_opt.F90 aero_binned.F90
5354
run_part.F90 run_part_opt.F90 util.F90 aero_data.F90 aero_state.F90 env_state.F90 gas_data.F90
5455
gas_state.F90 scenario.F90 condense.F90 aero_particle.F90 bin_grid.F90
5556
camp_core.F90 photolysis.F90 aero_mode.F90 aero_dist.F90 bin_grid.cpp condense.cpp run_part.cpp
56-
scenario.cpp util.cpp output.cpp output.F90 rand.cpp rand.F90
57+
run_sect.cpp run_exact.cpp scenario.cpp util.cpp output.cpp output.F90 rand.cpp rand.F90
5758
)
5859
add_prefix(src/ PyPartMC_sources)
5960

src/aero_binned.F90

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
!###################################################################################################
2+
! This file is a part of PyPartMC licensed under the GNU General Public License v3 (LICENSE file) #
3+
! Copyright (C) 2025 University of Illinois Urbana-Champaign #
4+
! Authors: https://github.com/open-atmos/PyPartMC/graphs/contributors #
5+
!###################################################################################################
6+
7+
module PyPartMC_aero_binned
8+
use iso_c_binding
9+
use pmc_aero_binned
10+
implicit none
11+
12+
contains
13+
14+
subroutine f_aero_binned_ctor(ptr_c) bind(C)
15+
type(aero_binned_t), pointer :: ptr_f => null()
16+
type(c_ptr), intent(out) :: ptr_c
17+
18+
allocate(ptr_f)
19+
ptr_c = c_loc(ptr_f)
20+
end subroutine
21+
22+
subroutine f_aero_binned_dtor(ptr_c) bind(C)
23+
type(aero_binned_t), pointer :: ptr_f => null()
24+
type(c_ptr), intent(in) :: ptr_c
25+
26+
call c_f_pointer(ptr_c, ptr_f)
27+
deallocate(ptr_f)
28+
end subroutine
29+
30+
subroutine f_aero_binned_num_conc(ptr_c, num_conc, n_bins) bind(C)
31+
type(aero_binned_t), pointer :: ptr_f => null()
32+
type(c_ptr), intent(in) :: ptr_c
33+
integer(c_int), intent(in) :: n_bins
34+
real(c_double), intent(inout) :: num_conc(n_bins)
35+
36+
call c_f_pointer(ptr_c, ptr_f)
37+
38+
num_conc = ptr_f%num_conc
39+
40+
end subroutine
41+
42+
subroutine f_aero_binned_species_vol_conc(ptr_c, vol_conc, n_bins, i_spec) bind(C)
43+
type(aero_binned_t), pointer :: ptr_f => null()
44+
type(c_ptr), intent(in) :: ptr_c
45+
integer(c_int), intent(in) :: n_bins, i_spec
46+
real(c_double), intent(inout) :: vol_conc(n_bins)
47+
48+
call c_f_pointer(ptr_c, ptr_f)
49+
50+
vol_conc = ptr_f%vol_conc(:,i_spec+1)
51+
52+
end subroutine
53+
54+
subroutine f_aero_binned_len(ptr_c, len) bind(C)
55+
type(aero_binned_t), pointer :: ptr_f => null()
56+
type(c_ptr), intent(in) :: ptr_c
57+
integer(c_int), intent(out) :: len
58+
59+
call c_f_pointer(ptr_c, ptr_f)
60+
61+
len = size(ptr_f%num_conc)
62+
63+
end subroutine
64+
65+
subroutine f_aero_binned_add_aero_dist(ptr_c, bin_grid_ptr_c, aero_data_ptr_c, &
66+
aero_dist_ptr_c) bind(C)
67+
type(c_ptr), intent(in) :: ptr_c, bin_grid_ptr_c, aero_data_ptr_c, &
68+
aero_dist_ptr_c
69+
type(aero_binned_t), pointer :: ptr_f => null()
70+
type(aero_data_t), pointer :: aero_data_ptr_f => null()
71+
type(aero_dist_t), pointer :: aero_dist_ptr_f => null()
72+
type(bin_grid_t), pointer :: bin_grid_ptr_f => null()
73+
74+
call c_f_pointer(ptr_c, ptr_f)
75+
call c_f_pointer(bin_grid_ptr_c, bin_grid_ptr_f)
76+
call c_f_pointer(aero_data_ptr_c, aero_data_ptr_f)
77+
call c_f_pointer(aero_dist_ptr_c, aero_dist_ptr_f)
78+
79+
call aero_binned_add_aero_dist(ptr_f, bin_grid_ptr_f, &
80+
aero_data_ptr_f, aero_dist_ptr_f)
81+
82+
end subroutine
83+
84+
subroutine f_aero_binned_set_sizes(ptr_c, aero_data_ptr_c, bin_grid_ptr_c) bind(C)
85+
type(c_ptr), intent(in) :: ptr_c, bin_grid_ptr_c, aero_data_ptr_c
86+
type(aero_binned_t), pointer :: ptr_f => null()
87+
type(aero_data_t), pointer :: aero_data_ptr_f => null()
88+
type(bin_grid_t), pointer :: bin_grid_ptr_f => null()
89+
90+
call c_f_pointer(ptr_c, ptr_f)
91+
call c_f_pointer(bin_grid_ptr_c, bin_grid_ptr_f)
92+
call c_f_pointer(aero_data_ptr_c, aero_data_ptr_f)
93+
94+
call aero_binned_set_sizes(ptr_f, bin_grid_size(bin_grid_ptr_f), &
95+
aero_data_n_spec(aero_data_ptr_f))
96+
97+
end subroutine
98+
99+
end module

src/aero_binned.hpp

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*##################################################################################################
2+
# This file is a part of PyPartMC licensed under the GNU General Public License v3 (LICENSE file) #
3+
# Copyright (C) 2025 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+
#include "aero_data.hpp"
11+
#include "bin_grid.hpp"
12+
#include "aero_dist.hpp"
13+
#include "aero_data.hpp"
14+
#include "pybind11/stl.h"
15+
16+
extern "C" void f_aero_binned_ctor(void *ptr) noexcept;
17+
extern "C" void f_aero_binned_dtor(void *ptr) noexcept;
18+
extern "C" void f_aero_binned_len(
19+
const void *ptr, int *len
20+
) noexcept;
21+
extern "C" void f_aero_binned_add_aero_dist(
22+
void *ptr,
23+
const void *aero_data,
24+
const void *bin_grid,
25+
const void *aero_dist
26+
) noexcept;
27+
extern "C" void f_aero_binned_set_sizes(
28+
void *ptr,
29+
const void *aero_data,
30+
const void *aero_binned
31+
) noexcept;
32+
extern "C" void f_aero_binned_num_conc(
33+
const void *ptr,
34+
double *num_conc,
35+
const int *len
36+
) noexcept;
37+
extern "C" void f_aero_binned_species_vol_conc(
38+
const void *ptr,
39+
double *data,
40+
const int *n_bins,
41+
const int *i_spec
42+
) noexcept;
43+
44+
struct AeroBinned {
45+
PMCResource ptr;
46+
std::shared_ptr<AeroData> aero_data;
47+
AeroBinned(std::shared_ptr<AeroData> aero_data) :
48+
ptr(f_aero_binned_ctor, f_aero_binned_dtor),
49+
aero_data(aero_data)
50+
{
51+
}
52+
53+
AeroBinned(std::shared_ptr<AeroData> aero_data, const BinGrid &bin_grid) :
54+
ptr(f_aero_binned_ctor, f_aero_binned_dtor),
55+
aero_data(aero_data)
56+
{
57+
f_aero_binned_set_sizes(
58+
ptr.f_arg_non_const(),
59+
aero_data->ptr.f_arg(),
60+
bin_grid.ptr.f_arg()
61+
);
62+
}
63+
64+
static auto num_conc(const AeroBinned &self) {
65+
int len;
66+
f_aero_binned_len(
67+
self.ptr.f_arg(),
68+
&len
69+
);
70+
std::valarray<double> num_conc(len);
71+
72+
f_aero_binned_num_conc(
73+
self.ptr.f_arg(),
74+
begin(num_conc),
75+
&len
76+
);
77+
78+
return num_conc;
79+
}
80+
81+
static auto vol_conc(const AeroBinned &self){
82+
83+
int len;
84+
f_aero_binned_len(
85+
self.ptr.f_arg(),
86+
&len
87+
);
88+
89+
int cols = len;
90+
int rows;
91+
f_aero_data_len(self.aero_data->ptr.f_arg(), &rows);
92+
93+
std::valarray<std::valarray<double>> vol_conc(rows);
94+
std::valarray<double> vol_species(cols);
95+
for (int i = 0; i < rows; i++) {
96+
vol_conc[i] = std::valarray<double>(cols);
97+
f_aero_binned_species_vol_conc(self.ptr.f_arg(), begin(vol_species), &cols, &i);
98+
for (int j = 0; j < cols; ++j) {
99+
vol_conc[i][j] = vol_species[j];
100+
}
101+
}
102+
103+
return vol_conc;
104+
105+
}
106+
107+
static void add_aero_dist(AeroBinned &self,
108+
const BinGrid &bin_grid, const AeroDist &aero_dist)
109+
{
110+
f_aero_binned_add_aero_dist(
111+
self.ptr.f_arg_non_const(),
112+
bin_grid.ptr.f_arg(),
113+
self.aero_data->ptr.f_arg(),
114+
aero_dist.ptr.f_arg()
115+
);
116+
}
117+
118+
};
119+

src/bin_grid.F90

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
!###################################################################################################
22
! 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 #
3+
! Copyright (C) 2022-2025 University of Illinois Urbana-Champaign #
44
! Authors: https://github.com/open-atmos/PyPartMC/graphs/contributors #
55
!###################################################################################################
66

@@ -70,6 +70,16 @@ subroutine f_bin_grid_centers(ptr_c, arr_data, arr_size) bind(C)
7070
arr_data = bin_grid%centers
7171
end subroutine
7272

73+
subroutine f_bin_grid_widths(ptr_c, arr_data, arr_size) bind(C)
74+
type(c_ptr), intent(in) :: ptr_c
75+
type(bin_grid_t), pointer :: bin_grid => null()
76+
integer(c_int), intent(in) :: arr_size
77+
real(c_double), dimension(arr_size), intent(out) :: arr_data
78+
79+
call c_f_pointer(ptr_c, bin_grid)
80+
arr_data = bin_grid%widths
81+
end subroutine
82+
7383
subroutine f_bin_grid_histogram_1d(x_bin_grid_ptr_c, x_data, weight_data, &
7484
arr_size, output_data, bin_grid_size) bind(C)
7585

src/bin_grid.hpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*##################################################################################################
22
# 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 #
3+
# Copyright (C) 2022-2025 University of Illinois Urbana-Champaign #
44
# Authors: https://github.com/open-atmos/PyPartMC/graphs/contributors #
55
##################################################################################################*/
66

@@ -38,6 +38,12 @@ extern "C" void f_bin_grid_centers(
3838
const int *arr_size
3939
) noexcept;
4040

41+
extern "C" void f_bin_grid_widths(
42+
const void *ptr,
43+
void *arr_data,
44+
const int *arr_size
45+
) noexcept;
46+
4147
extern "C" void f_bin_grid_histogram_1d(
4248
const void *x_bin_grid_ptr_c,
4349
const void *x_data,
@@ -74,6 +80,11 @@ struct BinGrid {
7480
f_bin_grid_init(ptr.f_arg(), &n_bin, &type, &min, &max);
7581
}
7682

83+
BinGrid():
84+
ptr(f_bin_grid_ctor, f_bin_grid_dtor)
85+
{
86+
}
87+
7788
static std::size_t __len__(const BinGrid &self) {
7889
int len;
7990
f_bin_grid_size(
@@ -115,6 +126,23 @@ struct BinGrid {
115126
);
116127
return data;
117128
}
129+
130+
static auto widths(const BinGrid &self)
131+
{
132+
int len;
133+
f_bin_grid_size(
134+
self.ptr.f_arg(),
135+
&len
136+
);
137+
std::valarray<double> data(len);
138+
f_bin_grid_widths(
139+
self.ptr.f_arg(),
140+
begin(data),
141+
&len
142+
);
143+
return data;
144+
}
145+
118146
};
119147

120148
std::valarray<double> histogram_1d(

0 commit comments

Comments
 (0)