Skip to content

Commit 73feae3

Browse files
committed
EAMxx: simplify interface to call a fcn from py module in atm processes
1 parent 3476f74 commit 73feae3

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

components/eamxx/src/physics/cld_fraction/eamxx_cld_fraction_process_interface.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,7 @@ void CldFraction::initialize_impl (const RunType /* run_type */)
7777
add_postcondition_check<Interval>(get_field_out("cldfrac_tot_for_analysis"),m_grid,0.0,1.0,false);
7878
#ifdef EAMXX_HAS_PYTHON
7979
if (has_py_module()) {
80-
try {
81-
py_module_call("init");
82-
} catch (const pybind11::error_already_set& e) {
83-
std::cout << "[CldFraction::initialize_impl] Error! Something went wrong while calling the python module's function 'init'.\n"
84-
" - module name: " + m_params.get<std::string>("py_module_name") + "\n"
85-
" - pybind11 error: " + std::string(e.what()) + "\n";
86-
throw e;
87-
}
88-
80+
py_module_call("init");
8981
}
9082
#endif
9183
}
@@ -118,14 +110,11 @@ void CldFraction::run_impl (const double /* dt */)
118110
double ice_threshold = m_params.get<double>("ice_cloud_threshold");
119111
double ice_4out_threshold = m_params.get<double>("ice_cloud_for_analysis_threshold");
120112

121-
try {
122-
py_module_call("main",ice_threshold,ice_4out_threshold,py_qi,py_liq_cld_frac,py_ice_cld_frac,py_tot_cld_frac,py_ice_cld_frac_4out,py_tot_cld_frac_4out);
123-
} catch (const pybind11::error_already_set& e) {
124-
std::cout << "[CldFraction::run_impl] Error! Something went wrong while calling the python module's function 'main'.\n"
125-
" - module name: " + m_params.get<std::string>("py_module_name") + "\n"
126-
" - pybind11 error: " + std::string(e.what()) + "\n";
127-
throw e;
128-
}
113+
py_module_call("main",
114+
ice_threshold,ice_4out_threshold,
115+
py_qi,py_liq_cld_frac,
116+
py_ice_cld_frac,py_tot_cld_frac,
117+
py_ice_cld_frac_4out,py_tot_cld_frac_4out);
129118

130119
// Sync outputs to dev
131120
ice_cld_frac.sync_to_dev();

components/eamxx/src/share/atm_process/atmosphere_process_pyhelpers.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ py_module_call (const std::string& name, const Args&... args)
1717
{
1818
const auto& py_module = std::any_cast<const pybind11::module&>(m_py_module);
1919
py_module.attr(name.c_str())(args...);
20+
try {
21+
py_module.attr(name.c_str())(args...);
22+
} catch (const pybind11::error_already_set& e) {
23+
std::cout << "[" << this->name() << "] Error! Something went wrong while calling a python module function.\n"
24+
" - module name: " + m_params.get<std::string>("py_module_name") + "\n"
25+
" - function name: " + name + "\n"
26+
" - pybind11 error: " + std::string(e.what()) + "\n";
27+
throw e;
28+
}
2029
}
2130

2231
inline const pybind11::array& AtmosphereProcess::

0 commit comments

Comments
 (0)