Skip to content

Commit

Permalink
make all C++ headers self contained and add a CMake check to ensure i…
Browse files Browse the repository at this point in the history
…t remains this way. closes #25 (#200)
  • Loading branch information
slayoo authored Mar 28, 2023
1 parent b275068 commit 22ca79b
Show file tree
Hide file tree
Showing 14 changed files with 327 additions and 176 deletions.
35 changes: 29 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ set(PyPartMC_sources
pypartmc.cpp gimmicks.cpp fake_netcdf.cpp fake_spec_file.cpp sys.cpp
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
camp_core.F90 photolysis.F90 aero_mode.F90 aero_dist.F90 bin_grid.cpp condense.cpp run_part.cpp
scenario.cpp util.cpp
)
add_prefix(src/ PyPartMC_sources)

Expand Down Expand Up @@ -291,12 +292,13 @@ target_link_libraries(partmclib PRIVATE klulib)
add_subdirectory(gitmodules/pybind11)
pybind11_add_module(_PyPartMC ${PyPartMC_sources})
add_dependencies(_PyPartMC partmclib)
target_include_directories(_PyPartMC PRIVATE
${CMAKE_SOURCE_DIR}/gitmodules/json/include
${CMAKE_SOURCE_DIR}/gitmodules/pybind11_json/include
${CMAKE_SOURCE_DIR}/gitmodules/span/include
${CMAKE_SOURCE_DIR}/gitmodules/string_view-standalone/include
set(PYPARTMC_INCLUDE_DIRS
"${CMAKE_SOURCE_DIR}/gitmodules/json/include;"
"${CMAKE_SOURCE_DIR}/gitmodules/pybind11_json/include;"
"${CMAKE_SOURCE_DIR}/gitmodules/span/include;"
"${CMAKE_SOURCE_DIR}/gitmodules/string_view-standalone/include;"
)
target_include_directories(_PyPartMC PRIVATE ${PYPARTMC_INCLUDE_DIRS})
target_compile_definitions(_PyPartMC PRIVATE VERSION_INFO=${VERSION_INFO})
target_link_libraries(_PyPartMC PRIVATE partmclib)
if (APPLE)
Expand All @@ -319,3 +321,24 @@ foreach(target _PyPartMC)
)
endforeach()

include(CheckCXXSourceCompiles)
file(GLOB PyPartMC_headers ${CMAKE_SOURCE_DIR}/src/*.hpp)
if (NOT "${CMAKE_REQUIRED_INCLUDES}" STREQUAL "")
message("CMAKE_REQUIRED_INCLUDES not empty! (${CMAKE_REQUIRED_INCLUDES})")
endif()
foreach(file ${PyPartMC_headers})
set(CMAKE_REQUIRED_INCLUDES "${PYPARTMC_INCLUDE_DIRS};${pybind11_INCLUDE_DIRS}")
check_cxx_source_compiles("
// https://github.com/nlohmann/json/issues/1408
#define HAVE_SNPRINTF
#include \"${file}\"
int main() { return 0;}
"
_header_self_contained_${file}
)
unset(CMAKE_REQUIRED_INCLUDES)
if (NOT _header_self_contained_${file})
message(SEND_ERROR "non-self-contained header: ${file}")
endif()
endforeach()

2 changes: 2 additions & 0 deletions src/aero_mode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

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

extern "C" void f_aero_mode_ctor(
void *ptr
Expand Down
72 changes: 72 additions & 0 deletions src/bin_grid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*##################################################################################################
# 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 "bin_grid.hpp"

std::valarray<double> histogram_1d(
const BinGrid &bin_grid,
std::valarray<double> values,
std::valarray<double> weights
) {
int len;
f_bin_grid_size(
bin_grid.ptr.f_arg(),
&len
);
int data_size = values.size();
std::valarray<double> data(len);
f_bin_grid_histogram_1d(
bin_grid.ptr.f_arg(),
begin(values),
begin(weights),
&data_size,
begin(data),
&len
);

return data;
}

std::vector<std::vector<double>> histogram_2d(
const BinGrid &x_bin_grid,
std::valarray<double> x_values,
const BinGrid &y_bin_grid,
std::valarray<double> y_values,
std::valarray<double> weights
) {
int x_len;
f_bin_grid_size(x_bin_grid.ptr.f_arg(), &x_len);

int y_len;
f_bin_grid_size(y_bin_grid.ptr.f_arg(), &y_len);

const int data_size = x_values.size();

std::vector<std::vector<double>> data(
x_len,
std::vector<double>(y_len, 0)
);
std::valarray<double> data_flat(x_len * y_len);
f_bin_grid_histogram_2d(
x_bin_grid.ptr.f_arg(),
begin(x_values),
y_bin_grid.ptr.f_arg(),
begin(y_values),
begin(weights),
&data_size,
begin(data_flat),
&x_len,
&y_len
);

for(int i = 0; i < x_len; i++) {
for(int j = 0; j < y_len; j++) {
data[i][j] = data_flat[i*y_len + j];
}
}

return data;
}
61 changes: 5 additions & 56 deletions src/bin_grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,67 +117,16 @@ struct BinGrid {
}
};

auto histogram_1d(
std::valarray<double> histogram_1d(
const BinGrid &bin_grid,
std::valarray<double> values,
std::valarray<double> weights
) {
int len;
f_bin_grid_size(
bin_grid.ptr.f_arg(),
&len
);
int data_size = values.size();
std::valarray<double> data(len);
f_bin_grid_histogram_1d(
bin_grid.ptr.f_arg(),
begin(values),
begin(weights),
&data_size,
begin(data),
&len
);

return data;
}

auto histogram_2d(
);

std::vector<std::vector<double>> histogram_2d(
const BinGrid &x_bin_grid,
std::valarray<double> x_values,
const BinGrid &y_bin_grid,
std::valarray<double> y_values,
std::valarray<double> weights
) {
int x_len;
f_bin_grid_size(x_bin_grid.ptr.f_arg(), &x_len);

int y_len;
f_bin_grid_size(y_bin_grid.ptr.f_arg(), &y_len);

const int data_size = x_values.size();

std::vector<std::vector<double>> data(
x_len,
std::vector<double>(y_len, 0)
);
std::valarray<double> data_flat(x_len * y_len);
f_bin_grid_histogram_2d(
x_bin_grid.ptr.f_arg(),
begin(x_values),
y_bin_grid.ptr.f_arg(),
begin(y_values),
begin(weights),
&data_size,
begin(data_flat),
&x_len,
&y_len
);

for(int i = 0; i < x_len; i++) {
for(int j = 0; j < y_len; j++) {
data[i][j] = data_flat[i*y_len + j];
}
}

return data;
}
);
31 changes: 31 additions & 0 deletions src/condense.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*##################################################################################################
# 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 "condense.hpp"

void condense_equilib_particle(
const EnvState &env_state,
const AeroData &aero_data,
const AeroParticle &aero_particle
) {
f_condense_equilib_particle(
env_state.ptr.f_arg(),
aero_data.ptr.f_arg(),
aero_particle.ptr.f_arg()
);
}

void condense_equilib_particles(
const EnvState &env_state,
const AeroData &aero_data,
const AeroState &aero_state
) {
f_condense_equilib_particles(
env_state.ptr.f_arg(),
aero_data.ptr.f_arg(),
aero_state.ptr.f_arg()
);
}
17 changes: 2 additions & 15 deletions src/condense.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,10 @@ void condense_equilib_particle(
const EnvState &env_state,
const AeroData &aero_data,
const AeroParticle &aero_particle
) {
f_condense_equilib_particle(
env_state.ptr.f_arg(),
aero_data.ptr.f_arg(),
aero_particle.ptr.f_arg()
);
}
);

void condense_equilib_particles(
const EnvState &env_state,
const AeroData &aero_data,
const AeroState &aero_state
) {
f_condense_equilib_particles(
env_state.ptr.f_arg(),
aero_data.ptr.f_arg(),
aero_state.ptr.f_arg()
);
}

);
4 changes: 3 additions & 1 deletion src/gas_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "gimmicks.hpp" // TODO #119: rename to something like json_resource.hpp?
#include "pmc_resource.hpp"
#include "pybind11/stl.h"
#include "pybind11_json/pybind11_json.hpp"

extern "C" void f_gas_data_ctor(void *ptr) noexcept;
extern "C" void f_gas_data_dtor(void *ptr) noexcept;
Expand All @@ -21,7 +23,7 @@ struct GasData {
PMCResource ptr;
const nlohmann::json json;

GasData(const py::tuple &tpl) :
GasData(const pybind11::tuple &tpl) :
ptr(f_gas_data_ctor, f_gas_data_dtor),
json(tpl)
{
Expand Down
89 changes: 89 additions & 0 deletions src/run_part.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*##################################################################################################
# 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 "run_part.hpp"

void run_part(
const Scenario &scenario,
EnvState &env_state,
const AeroData &aero_data,
AeroState &aero_state,
const GasData &gas_data,
GasState &gas_state,
const RunPartOpt &run_part_opt,
const CampCore &camp_core,
const Photolysis &photolysis
) {
f_run_part(
scenario.ptr.f_arg(),
env_state.ptr.f_arg_non_const(),
aero_data.ptr.f_arg(),
aero_state.ptr.f_arg_non_const(),
gas_data.ptr.f_arg(),
gas_state.ptr.f_arg_non_const(),
run_part_opt.ptr.f_arg(),
camp_core.ptr.f_arg(),
photolysis.ptr.f_arg()
);
}

void run_part_timestep(
const Scenario &scenario,
EnvState &env_state,
const AeroData &aero_data,
AeroState &aero_state,
const GasData &gas_data,
GasState &gas_state,
const RunPartOpt &run_part_opt,
const CampCore &camp_core,
const Photolysis &photolysis,
const int &i_time,
const double &t_start
) {
f_run_part_timestep(
scenario.ptr.f_arg(),
env_state.ptr.f_arg_non_const(),
aero_data.ptr.f_arg(),
aero_state.ptr.f_arg_non_const(),
gas_data.ptr.f_arg(),
gas_state.ptr.f_arg_non_const(),
run_part_opt.ptr.f_arg(),
camp_core.ptr.f_arg(),
photolysis.ptr.f_arg(),
&i_time,
&t_start
);
}

void run_part_timeblock(
const Scenario &scenario,
EnvState &env_state,
const AeroData &aero_data,
AeroState &aero_state,
const GasData &gas_data,
GasState &gas_state,
const RunPartOpt &run_part_opt,
const CampCore &camp_core,
const Photolysis &photolysis,
const int &i_time,
const int &i_next,
const double &t_start
) {
f_run_part_timeblock(
scenario.ptr.f_arg(),
env_state.ptr.f_arg_non_const(),
aero_data.ptr.f_arg(),
aero_state.ptr.f_arg_non_const(),
gas_data.ptr.f_arg(),
gas_state.ptr.f_arg_non_const(),
run_part_opt.ptr.f_arg(),
camp_core.ptr.f_arg(),
photolysis.ptr.f_arg(),
&i_time,
&i_next,
&t_start
);
}
Loading

0 comments on commit 22ca79b

Please sign in to comment.