Skip to content

Commit 22ca79b

Browse files
authored
make all C++ headers self contained and add a CMake check to ensure it remains this way. closes #25 (#200)
1 parent b275068 commit 22ca79b

File tree

14 files changed

+327
-176
lines changed

14 files changed

+327
-176
lines changed

CMakeLists.txt

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ set(PyPartMC_sources
4444
pypartmc.cpp gimmicks.cpp fake_netcdf.cpp fake_spec_file.cpp sys.cpp
4545
run_part.F90 run_part_opt.F90 util.F90 aero_data.F90 aero_state.F90 env_state.F90 gas_data.F90
4646
gas_state.F90 scenario.F90 condense.F90 aero_particle.F90 bin_grid.F90
47-
camp_core.F90 photolysis.F90 aero_mode.F90 aero_dist.F90
47+
camp_core.F90 photolysis.F90 aero_mode.F90 aero_dist.F90 bin_grid.cpp condense.cpp run_part.cpp
48+
scenario.cpp util.cpp
4849
)
4950
add_prefix(src/ PyPartMC_sources)
5051

@@ -291,12 +292,13 @@ target_link_libraries(partmclib PRIVATE klulib)
291292
add_subdirectory(gitmodules/pybind11)
292293
pybind11_add_module(_PyPartMC ${PyPartMC_sources})
293294
add_dependencies(_PyPartMC partmclib)
294-
target_include_directories(_PyPartMC PRIVATE
295-
${CMAKE_SOURCE_DIR}/gitmodules/json/include
296-
${CMAKE_SOURCE_DIR}/gitmodules/pybind11_json/include
297-
${CMAKE_SOURCE_DIR}/gitmodules/span/include
298-
${CMAKE_SOURCE_DIR}/gitmodules/string_view-standalone/include
295+
set(PYPARTMC_INCLUDE_DIRS
296+
"${CMAKE_SOURCE_DIR}/gitmodules/json/include;"
297+
"${CMAKE_SOURCE_DIR}/gitmodules/pybind11_json/include;"
298+
"${CMAKE_SOURCE_DIR}/gitmodules/span/include;"
299+
"${CMAKE_SOURCE_DIR}/gitmodules/string_view-standalone/include;"
299300
)
301+
target_include_directories(_PyPartMC PRIVATE ${PYPARTMC_INCLUDE_DIRS})
300302
target_compile_definitions(_PyPartMC PRIVATE VERSION_INFO=${VERSION_INFO})
301303
target_link_libraries(_PyPartMC PRIVATE partmclib)
302304
if (APPLE)
@@ -319,3 +321,24 @@ foreach(target _PyPartMC)
319321
)
320322
endforeach()
321323

324+
include(CheckCXXSourceCompiles)
325+
file(GLOB PyPartMC_headers ${CMAKE_SOURCE_DIR}/src/*.hpp)
326+
if (NOT "${CMAKE_REQUIRED_INCLUDES}" STREQUAL "")
327+
message("CMAKE_REQUIRED_INCLUDES not empty! (${CMAKE_REQUIRED_INCLUDES})")
328+
endif()
329+
foreach(file ${PyPartMC_headers})
330+
set(CMAKE_REQUIRED_INCLUDES "${PYPARTMC_INCLUDE_DIRS};${pybind11_INCLUDE_DIRS}")
331+
check_cxx_source_compiles("
332+
// https://github.com/nlohmann/json/issues/1408
333+
#define HAVE_SNPRINTF
334+
#include \"${file}\"
335+
int main() { return 0;}
336+
"
337+
_header_self_contained_${file}
338+
)
339+
unset(CMAKE_REQUIRED_INCLUDES)
340+
if (NOT _header_self_contained_${file})
341+
message(SEND_ERROR "non-self-contained header: ${file}")
342+
endif()
343+
endforeach()
344+

src/aero_mode.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include "pmc_resource.hpp"
1010
#include "pybind11/stl.h"
11+
#include "aero_data.hpp"
12+
#include "bin_grid.hpp"
1113

1214
extern "C" void f_aero_mode_ctor(
1315
void *ptr

src/bin_grid.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 "bin_grid.hpp"
8+
9+
std::valarray<double> histogram_1d(
10+
const BinGrid &bin_grid,
11+
std::valarray<double> values,
12+
std::valarray<double> weights
13+
) {
14+
int len;
15+
f_bin_grid_size(
16+
bin_grid.ptr.f_arg(),
17+
&len
18+
);
19+
int data_size = values.size();
20+
std::valarray<double> data(len);
21+
f_bin_grid_histogram_1d(
22+
bin_grid.ptr.f_arg(),
23+
begin(values),
24+
begin(weights),
25+
&data_size,
26+
begin(data),
27+
&len
28+
);
29+
30+
return data;
31+
}
32+
33+
std::vector<std::vector<double>> histogram_2d(
34+
const BinGrid &x_bin_grid,
35+
std::valarray<double> x_values,
36+
const BinGrid &y_bin_grid,
37+
std::valarray<double> y_values,
38+
std::valarray<double> weights
39+
) {
40+
int x_len;
41+
f_bin_grid_size(x_bin_grid.ptr.f_arg(), &x_len);
42+
43+
int y_len;
44+
f_bin_grid_size(y_bin_grid.ptr.f_arg(), &y_len);
45+
46+
const int data_size = x_values.size();
47+
48+
std::vector<std::vector<double>> data(
49+
x_len,
50+
std::vector<double>(y_len, 0)
51+
);
52+
std::valarray<double> data_flat(x_len * y_len);
53+
f_bin_grid_histogram_2d(
54+
x_bin_grid.ptr.f_arg(),
55+
begin(x_values),
56+
y_bin_grid.ptr.f_arg(),
57+
begin(y_values),
58+
begin(weights),
59+
&data_size,
60+
begin(data_flat),
61+
&x_len,
62+
&y_len
63+
);
64+
65+
for(int i = 0; i < x_len; i++) {
66+
for(int j = 0; j < y_len; j++) {
67+
data[i][j] = data_flat[i*y_len + j];
68+
}
69+
}
70+
71+
return data;
72+
}

src/bin_grid.hpp

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -117,67 +117,16 @@ struct BinGrid {
117117
}
118118
};
119119

120-
auto histogram_1d(
120+
std::valarray<double> histogram_1d(
121121
const BinGrid &bin_grid,
122122
std::valarray<double> values,
123123
std::valarray<double> weights
124-
) {
125-
int len;
126-
f_bin_grid_size(
127-
bin_grid.ptr.f_arg(),
128-
&len
129-
);
130-
int data_size = values.size();
131-
std::valarray<double> data(len);
132-
f_bin_grid_histogram_1d(
133-
bin_grid.ptr.f_arg(),
134-
begin(values),
135-
begin(weights),
136-
&data_size,
137-
begin(data),
138-
&len
139-
);
140-
141-
return data;
142-
}
143-
144-
auto histogram_2d(
124+
);
125+
126+
std::vector<std::vector<double>> histogram_2d(
145127
const BinGrid &x_bin_grid,
146128
std::valarray<double> x_values,
147129
const BinGrid &y_bin_grid,
148130
std::valarray<double> y_values,
149131
std::valarray<double> weights
150-
) {
151-
int x_len;
152-
f_bin_grid_size(x_bin_grid.ptr.f_arg(), &x_len);
153-
154-
int y_len;
155-
f_bin_grid_size(y_bin_grid.ptr.f_arg(), &y_len);
156-
157-
const int data_size = x_values.size();
158-
159-
std::vector<std::vector<double>> data(
160-
x_len,
161-
std::vector<double>(y_len, 0)
162-
);
163-
std::valarray<double> data_flat(x_len * y_len);
164-
f_bin_grid_histogram_2d(
165-
x_bin_grid.ptr.f_arg(),
166-
begin(x_values),
167-
y_bin_grid.ptr.f_arg(),
168-
begin(y_values),
169-
begin(weights),
170-
&data_size,
171-
begin(data_flat),
172-
&x_len,
173-
&y_len
174-
);
175-
176-
for(int i = 0; i < x_len; i++) {
177-
for(int j = 0; j < y_len; j++) {
178-
data[i][j] = data_flat[i*y_len + j];
179-
}
180-
}
181-
182-
return data;
183-
}
132+
);

src/condense.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 "condense.hpp"
8+
9+
void condense_equilib_particle(
10+
const EnvState &env_state,
11+
const AeroData &aero_data,
12+
const AeroParticle &aero_particle
13+
) {
14+
f_condense_equilib_particle(
15+
env_state.ptr.f_arg(),
16+
aero_data.ptr.f_arg(),
17+
aero_particle.ptr.f_arg()
18+
);
19+
}
20+
21+
void condense_equilib_particles(
22+
const EnvState &env_state,
23+
const AeroData &aero_data,
24+
const AeroState &aero_state
25+
) {
26+
f_condense_equilib_particles(
27+
env_state.ptr.f_arg(),
28+
aero_data.ptr.f_arg(),
29+
aero_state.ptr.f_arg()
30+
);
31+
}

src/condense.hpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,10 @@ void condense_equilib_particle(
2626
const EnvState &env_state,
2727
const AeroData &aero_data,
2828
const AeroParticle &aero_particle
29-
) {
30-
f_condense_equilib_particle(
31-
env_state.ptr.f_arg(),
32-
aero_data.ptr.f_arg(),
33-
aero_particle.ptr.f_arg()
34-
);
35-
}
29+
);
3630

3731
void condense_equilib_particles(
3832
const EnvState &env_state,
3933
const AeroData &aero_data,
4034
const AeroState &aero_state
41-
) {
42-
f_condense_equilib_particles(
43-
env_state.ptr.f_arg(),
44-
aero_data.ptr.f_arg(),
45-
aero_state.ptr.f_arg()
46-
);
47-
}
48-
35+
);

src/gas_data.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include "gimmicks.hpp" // TODO #119: rename to something like json_resource.hpp?
1010
#include "pmc_resource.hpp"
11+
#include "pybind11/stl.h"
12+
#include "pybind11_json/pybind11_json.hpp"
1113

1214
extern "C" void f_gas_data_ctor(void *ptr) noexcept;
1315
extern "C" void f_gas_data_dtor(void *ptr) noexcept;
@@ -21,7 +23,7 @@ struct GasData {
2123
PMCResource ptr;
2224
const nlohmann::json json;
2325

24-
GasData(const py::tuple &tpl) :
26+
GasData(const pybind11::tuple &tpl) :
2527
ptr(f_gas_data_ctor, f_gas_data_dtor),
2628
json(tpl)
2729
{

src/run_part.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 "run_part.hpp"
8+
9+
void run_part(
10+
const Scenario &scenario,
11+
EnvState &env_state,
12+
const AeroData &aero_data,
13+
AeroState &aero_state,
14+
const GasData &gas_data,
15+
GasState &gas_state,
16+
const RunPartOpt &run_part_opt,
17+
const CampCore &camp_core,
18+
const Photolysis &photolysis
19+
) {
20+
f_run_part(
21+
scenario.ptr.f_arg(),
22+
env_state.ptr.f_arg_non_const(),
23+
aero_data.ptr.f_arg(),
24+
aero_state.ptr.f_arg_non_const(),
25+
gas_data.ptr.f_arg(),
26+
gas_state.ptr.f_arg_non_const(),
27+
run_part_opt.ptr.f_arg(),
28+
camp_core.ptr.f_arg(),
29+
photolysis.ptr.f_arg()
30+
);
31+
}
32+
33+
void run_part_timestep(
34+
const Scenario &scenario,
35+
EnvState &env_state,
36+
const AeroData &aero_data,
37+
AeroState &aero_state,
38+
const GasData &gas_data,
39+
GasState &gas_state,
40+
const RunPartOpt &run_part_opt,
41+
const CampCore &camp_core,
42+
const Photolysis &photolysis,
43+
const int &i_time,
44+
const double &t_start
45+
) {
46+
f_run_part_timestep(
47+
scenario.ptr.f_arg(),
48+
env_state.ptr.f_arg_non_const(),
49+
aero_data.ptr.f_arg(),
50+
aero_state.ptr.f_arg_non_const(),
51+
gas_data.ptr.f_arg(),
52+
gas_state.ptr.f_arg_non_const(),
53+
run_part_opt.ptr.f_arg(),
54+
camp_core.ptr.f_arg(),
55+
photolysis.ptr.f_arg(),
56+
&i_time,
57+
&t_start
58+
);
59+
}
60+
61+
void run_part_timeblock(
62+
const Scenario &scenario,
63+
EnvState &env_state,
64+
const AeroData &aero_data,
65+
AeroState &aero_state,
66+
const GasData &gas_data,
67+
GasState &gas_state,
68+
const RunPartOpt &run_part_opt,
69+
const CampCore &camp_core,
70+
const Photolysis &photolysis,
71+
const int &i_time,
72+
const int &i_next,
73+
const double &t_start
74+
) {
75+
f_run_part_timeblock(
76+
scenario.ptr.f_arg(),
77+
env_state.ptr.f_arg_non_const(),
78+
aero_data.ptr.f_arg(),
79+
aero_state.ptr.f_arg_non_const(),
80+
gas_data.ptr.f_arg(),
81+
gas_state.ptr.f_arg_non_const(),
82+
run_part_opt.ptr.f_arg(),
83+
camp_core.ptr.f_arg(),
84+
photolysis.ptr.f_arg(),
85+
&i_time,
86+
&i_next,
87+
&t_start
88+
);
89+
}

0 commit comments

Comments
 (0)