Skip to content

Add sectional and exact simulations plus aero_binned type #426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 33 commits into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b986bff
add run_sect
jcurtis2 Mar 30, 2025
00662ca
add tests for run_sect
jcurtis2 Mar 30, 2025
722ff16
add run_exact
jcurtis2 Mar 30, 2025
d1f2ef9
start to add input_sectional
jcurtis2 Mar 30, 2025
8c41191
start of aero_binned structure for sectional and exact result
jcurtis2 Apr 1, 2025
a33d819
add AeroBinned and input_sectional
jcurtis2 Apr 1, 2025
a5397a4
fix AeroBinned
jcurtis2 Apr 1, 2025
69c7692
fix tests
jcurtis2 Apr 1, 2025
65793df
more aero_binned code
jcurtis2 Apr 5, 2025
f8a8560
add bin_grid widths
jcurtis2 Apr 6, 2025
3b0ec77
fix order of arguments for add_aero_dist
jcurtis2 Apr 6, 2025
96f17dc
add a test for aero_binned number concentration
jcurtis2 Apr 6, 2025
0c63274
add tests and fix code for pylint
jcurtis2 Apr 6, 2025
de1ec01
finalize run_exact code
jcurtis2 Apr 6, 2025
6e1e659
change progress in test_run_sect
jcurtis2 Apr 7, 2025
eb94655
fix test_run_exact_opt
jcurtis2 Apr 7, 2025
09e5127
fix test_run_exact
jcurtis2 Apr 7, 2025
ac82c7b
fix pre-commit
jcurtis2 Apr 7, 2025
0802f93
add aero_binned.F90 to CMakeLists
jcurtis2 Apr 7, 2025
b9a0469
add some test coverage
jcurtis2 Apr 7, 2025
46e1897
fix pre-commit
jcurtis2 Apr 7, 2025
da9007a
add test for reading in exact netcdf output
jcurtis2 Apr 7, 2025
9ea0e85
add vol_conc to AeroBinned
jcurtis2 Apr 8, 2025
6d4bc73
fix some doc strings
jcurtis2 Apr 8, 2025
6a390f9
fix typo
jcurtis2 Apr 8, 2025
38d6618
remove nonexistent function
jcurtis2 Apr 8, 2025
093e6cc
add env_state to run_sect for additive coagulation
jcurtis2 Apr 10, 2025
362770e
fix broken output test
jcurtis2 Apr 10, 2025
f6f22a1
add comment about calling input_sectional in input_exact code
jcurtis2 Apr 10, 2025
739538b
minor fortran fixes
jcurtis2 Apr 10, 2025
2db9dbd
minor fortran fix
jcurtis2 Apr 10, 2025
b5cf8ee
update copyright dates - no other comments! :)
slayoo Apr 22, 2025
96ecea4
Merge branch 'main' into run_sect
slayoo Apr 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ add_definitions("-DSUNDIALS_INT64_T=1")

set(PyPartMC_sources
pypartmc.cpp json_resource.cpp spec_file_pypartmc.cpp sys.cpp
run_sect.F90 run_sect_opt.F90 run_exact.F90 run_exact_opt.F90 aero_binned.F90
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 output.cpp output.F90 rand.cpp rand.F90
run_sect.cpp run_exact.cpp scenario.cpp util.cpp output.cpp output.F90 rand.cpp rand.F90
)
add_prefix(src/ PyPartMC_sources)

Expand Down
99 changes: 99 additions & 0 deletions src/aero_binned.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
!###################################################################################################
! This file is a part of PyPartMC licensed under the GNU General Public License v3 (LICENSE file) #
! Copyright (C) 2025 University of Illinois Urbana-Champaign #
! Authors: https://github.com/open-atmos/PyPartMC/graphs/contributors #
!###################################################################################################

module PyPartMC_aero_binned
use iso_c_binding
use pmc_aero_binned
implicit none

contains

subroutine f_aero_binned_ctor(ptr_c) bind(C)
type(aero_binned_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_binned_dtor(ptr_c) bind(C)
type(aero_binned_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_binned_num_conc(ptr_c, num_conc, n_bins) bind(C)
type(aero_binned_t), pointer :: ptr_f => null()
type(c_ptr), intent(in) :: ptr_c
integer(c_int), intent(in) :: n_bins
real(c_double), intent(inout) :: num_conc(n_bins)

call c_f_pointer(ptr_c, ptr_f)

num_conc = ptr_f%num_conc

end subroutine

subroutine f_aero_binned_species_vol_conc(ptr_c, vol_conc, n_bins, i_spec) bind(C)
type(aero_binned_t), pointer :: ptr_f => null()
type(c_ptr), intent(in) :: ptr_c
integer(c_int), intent(in) :: n_bins, i_spec
real(c_double), intent(inout) :: vol_conc(n_bins)

call c_f_pointer(ptr_c, ptr_f)

vol_conc = ptr_f%vol_conc(:,i_spec+1)

end subroutine

subroutine f_aero_binned_len(ptr_c, len) bind(C)
type(aero_binned_t), pointer :: ptr_f => null()
type(c_ptr), intent(in) :: ptr_c
integer(c_int), intent(out) :: len

call c_f_pointer(ptr_c, ptr_f)

len = size(ptr_f%num_conc)

end subroutine

subroutine f_aero_binned_add_aero_dist(ptr_c, bin_grid_ptr_c, aero_data_ptr_c, &
aero_dist_ptr_c) bind(C)
type(c_ptr), intent(in) :: ptr_c, bin_grid_ptr_c, aero_data_ptr_c, &
aero_dist_ptr_c
type(aero_binned_t), pointer :: ptr_f => null()
type(aero_data_t), pointer :: aero_data_ptr_f => null()
type(aero_dist_t), pointer :: aero_dist_ptr_f => null()
type(bin_grid_t), pointer :: bin_grid_ptr_f => null()

call c_f_pointer(ptr_c, ptr_f)
call c_f_pointer(bin_grid_ptr_c, bin_grid_ptr_f)
call c_f_pointer(aero_data_ptr_c, aero_data_ptr_f)
call c_f_pointer(aero_dist_ptr_c, aero_dist_ptr_f)

call aero_binned_add_aero_dist(ptr_f, bin_grid_ptr_f, &
aero_data_ptr_f, aero_dist_ptr_f)

end subroutine

subroutine f_aero_binned_set_sizes(ptr_c, aero_data_ptr_c, bin_grid_ptr_c) bind(C)
type(c_ptr), intent(in) :: ptr_c, bin_grid_ptr_c, aero_data_ptr_c
type(aero_binned_t), pointer :: ptr_f => null()
type(aero_data_t), pointer :: aero_data_ptr_f => null()
type(bin_grid_t), pointer :: bin_grid_ptr_f => null()

call c_f_pointer(ptr_c, ptr_f)
call c_f_pointer(bin_grid_ptr_c, bin_grid_ptr_f)
call c_f_pointer(aero_data_ptr_c, aero_data_ptr_f)

call aero_binned_set_sizes(ptr_f, bin_grid_size(bin_grid_ptr_f), &
aero_data_n_spec(aero_data_ptr_f))

end subroutine

end module
119 changes: 119 additions & 0 deletions src/aero_binned.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*##################################################################################################
# This file is a part of PyPartMC licensed under the GNU General Public License v3 (LICENSE file) #
# Copyright (C) 2025 University of Illinois Urbana-Champaign #
# Authors: https://github.com/open-atmos/PyPartMC/graphs/contributors #
##################################################################################################*/

#pragma once

#include "pmc_resource.hpp"
#include "aero_data.hpp"
#include "bin_grid.hpp"
#include "aero_dist.hpp"
#include "aero_data.hpp"
#include "pybind11/stl.h"

extern "C" void f_aero_binned_ctor(void *ptr) noexcept;
extern "C" void f_aero_binned_dtor(void *ptr) noexcept;
extern "C" void f_aero_binned_len(
const void *ptr, int *len
) noexcept;
extern "C" void f_aero_binned_add_aero_dist(
void *ptr,
const void *aero_data,
const void *bin_grid,
const void *aero_dist
) noexcept;
extern "C" void f_aero_binned_set_sizes(
void *ptr,
const void *aero_data,
const void *aero_binned
) noexcept;
extern "C" void f_aero_binned_num_conc(
const void *ptr,
double *num_conc,
const int *len
) noexcept;
extern "C" void f_aero_binned_species_vol_conc(
const void *ptr,
double *data,
const int *n_bins,
const int *i_spec
) noexcept;

struct AeroBinned {
PMCResource ptr;
std::shared_ptr<AeroData> aero_data;
AeroBinned(std::shared_ptr<AeroData> aero_data) :
ptr(f_aero_binned_ctor, f_aero_binned_dtor),
aero_data(aero_data)
{
}

AeroBinned(std::shared_ptr<AeroData> aero_data, const BinGrid &bin_grid) :
ptr(f_aero_binned_ctor, f_aero_binned_dtor),
aero_data(aero_data)
{
f_aero_binned_set_sizes(
ptr.f_arg_non_const(),
aero_data->ptr.f_arg(),
bin_grid.ptr.f_arg()
);
}

static auto num_conc(const AeroBinned &self) {
int len;
f_aero_binned_len(
self.ptr.f_arg(),
&len
);
std::valarray<double> num_conc(len);

f_aero_binned_num_conc(
self.ptr.f_arg(),
begin(num_conc),
&len
);

return num_conc;
}

static auto vol_conc(const AeroBinned &self){

int len;
f_aero_binned_len(
self.ptr.f_arg(),
&len
);

int cols = len;
int rows;
f_aero_data_len(self.aero_data->ptr.f_arg(), &rows);

std::valarray<std::valarray<double>> vol_conc(rows);
std::valarray<double> vol_species(cols);
for (int i = 0; i < rows; i++) {
vol_conc[i] = std::valarray<double>(cols);
f_aero_binned_species_vol_conc(self.ptr.f_arg(), begin(vol_species), &cols, &i);
for (int j = 0; j < cols; ++j) {
vol_conc[i][j] = vol_species[j];
}
}

return vol_conc;

}

static void add_aero_dist(AeroBinned &self,
const BinGrid &bin_grid, const AeroDist &aero_dist)
{
f_aero_binned_add_aero_dist(
self.ptr.f_arg_non_const(),
bin_grid.ptr.f_arg(),
self.aero_data->ptr.f_arg(),
aero_dist.ptr.f_arg()
);
}

};

12 changes: 11 additions & 1 deletion src/bin_grid.F90
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
!###################################################################################################
! 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 #
! Copyright (C) 2022-2025 University of Illinois Urbana-Champaign #
! Authors: https://github.com/open-atmos/PyPartMC/graphs/contributors #
!###################################################################################################

Expand Down Expand Up @@ -70,6 +70,16 @@ subroutine f_bin_grid_centers(ptr_c, arr_data, arr_size) bind(C)
arr_data = bin_grid%centers
end subroutine

subroutine f_bin_grid_widths(ptr_c, arr_data, arr_size) bind(C)
type(c_ptr), intent(in) :: ptr_c
type(bin_grid_t), pointer :: bin_grid => null()
integer(c_int), intent(in) :: arr_size
real(c_double), dimension(arr_size), intent(out) :: arr_data

call c_f_pointer(ptr_c, bin_grid)
arr_data = bin_grid%widths
end subroutine

subroutine f_bin_grid_histogram_1d(x_bin_grid_ptr_c, x_data, weight_data, &
arr_size, output_data, bin_grid_size) bind(C)

Expand Down
30 changes: 29 additions & 1 deletion src/bin_grid.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*##################################################################################################
# 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 #
# Copyright (C) 2022-2025 University of Illinois Urbana-Champaign #
# Authors: https://github.com/open-atmos/PyPartMC/graphs/contributors #
##################################################################################################*/

Expand Down Expand Up @@ -38,6 +38,12 @@ extern "C" void f_bin_grid_centers(
const int *arr_size
) noexcept;

extern "C" void f_bin_grid_widths(
const void *ptr,
void *arr_data,
const int *arr_size
) noexcept;

extern "C" void f_bin_grid_histogram_1d(
const void *x_bin_grid_ptr_c,
const void *x_data,
Expand Down Expand Up @@ -74,6 +80,11 @@ struct BinGrid {
f_bin_grid_init(ptr.f_arg(), &n_bin, &type, &min, &max);
}

BinGrid():
ptr(f_bin_grid_ctor, f_bin_grid_dtor)
{
}

static std::size_t __len__(const BinGrid &self) {
int len;
f_bin_grid_size(
Expand Down Expand Up @@ -115,6 +126,23 @@ struct BinGrid {
);
return data;
}

static auto widths(const BinGrid &self)
{
int len;
f_bin_grid_size(
self.ptr.f_arg(),
&len
);
std::valarray<double> data(len);
f_bin_grid_widths(
self.ptr.f_arg(),
begin(data),
&len
);
return data;
}

};

std::valarray<double> histogram_1d(
Expand Down
Loading
Loading