Skip to content
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

simplify Matlab interoperability (thanks @rwgk for providing the workaround!) #335

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 0 additions & 5 deletions .github/workflows/readme_listings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,6 @@ jobs:
- run: python -c "import pytest_codeblocks; code=pytest_codeblocks.extract_from_file('README.md'); f=open('readme.m', 'w'); f.writelines(block.code for block in code if block.syntax=='Matlab'); f.close()"
- run: cat -n readme.m

# see https://github.com/pybind/cmake_example/pull/164
- run: |
echo "pybind11_type=type" > pybind11_builtins.py
echo "PYTHONPATH=." >> $GITHUB_ENV

- uses: matlab-actions/setup-matlab@v0
with:
release: R2022a
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ print(aero_state.masses()'aero_state.num_concs, "# kg/m3")
notes (see the [PyPartMC Matlab CI workflow](https://github.com/open-atmos/PyPartMC/blob/main/.github/workflows/readme_listings.yml) for an example on how to achieve it on Ubuntu 20):
- Matlab ships with convenience copies of C, C++ and Fortran runtime libraries which are `dlopened()` by default; one way to make PyPartMC OK with it is to [pip-]install by compiling from source using the very same version of GCC that Matlab borrowed these libraries from (e.g., [GCC 9 for Matlab R2022a, etc](https://www.mathworks.com/support/requirements/supported-compilers-linux.html));
- Matlab needs to [use the same Python interpretter/venv](https://www.mathworks.com/support/requirements/python-compatibility.html) as the pip invocation used to install PyPartMC;
- a single-line `pybind11_builtins.py` file with just `pybind11_type=type` inside needs to be placed within Matlab's `PYTHONPATH` to sort out a [Matlab-pybind11 incompatibility](https://github.com/pybind/pybind11/issues/3945).

````Matlab
ppmc = py.importlib.import_module('PyPartMC');
Expand Down
20 changes: 13 additions & 7 deletions src/pypartmc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ PYBIND11_MODULE(_PyPartMC, m) {
//m.def("run_sect", &run_sect, "Do a 1D sectional simulation (Bott 1998 scheme).");
//m.def("run_exact", &run_exact, "Do an exact solution simulation.");

py::class_<AeroData, std::shared_ptr<AeroData>>(m, "AeroData",
py::class_<AeroData, std::shared_ptr<AeroData>>(m, "AeroData", py::metaclass((PyObject *) &PyType_Type),
R"pbdoc(
Aerosol material properties and associated data.

Expand Down Expand Up @@ -110,7 +110,7 @@ PYBIND11_MODULE(_PyPartMC, m) {
"returns list of aerosol species names")
;

py::class_<AeroParticle>(m, "AeroParticle",
py::class_<AeroParticle>(m, "AeroParticle", py::metaclass((PyObject *) &PyType_Type),
R"pbdoc(
Single aerosol particle data structure.

Expand Down Expand Up @@ -188,7 +188,7 @@ PYBIND11_MODULE(_PyPartMC, m) {
"Sets the aerosol particle volumes.")
;

py::class_<AeroState>(m, "AeroState",
py::class_<AeroState>(m, "AeroState", py::metaclass((PyObject *) &PyType_Type),
R"pbdoc(
The current collection of aerosol particles.

Expand Down Expand Up @@ -246,7 +246,7 @@ PYBIND11_MODULE(_PyPartMC, m) {
.def("zero", AeroState::zero, "remove all particles from an AeroState")
;

py::class_<GasData, std::shared_ptr<GasData>>(m, "GasData",
py::class_<GasData, std::shared_ptr<GasData>>(m, "GasData", py::metaclass((PyObject *) &PyType_Type),
R"pbdoc(
Constant gas data.

Expand All @@ -270,6 +270,7 @@ PYBIND11_MODULE(_PyPartMC, m) {

py::class_<EnvState>(m,
"EnvState",
py::metaclass((PyObject *) &PyType_Type),
R"pbdoc(
Current environment state.

Expand Down Expand Up @@ -298,6 +299,7 @@ PYBIND11_MODULE(_PyPartMC, m) {

py::class_<Photolysis>(m,
"Photolysis",
py::metaclass((PyObject *) &PyType_Type),
R"pbdoc(
PartMC interface to a photolysis module
)pbdoc"
Expand All @@ -307,6 +309,7 @@ PYBIND11_MODULE(_PyPartMC, m) {

py::class_<CampCore>(m,
"CampCore",
py::metaclass((PyObject *) &PyType_Type),
R"pbdoc(
An interface between PartMC and the CAMP
)pbdoc"
Expand All @@ -316,6 +319,7 @@ PYBIND11_MODULE(_PyPartMC, m) {

py::class_<Scenario>(m,
"Scenario",
py::metaclass((PyObject *) &PyType_Type),
R"pbdoc(
This is everything needed to drive the scenario being simulated.

Expand Down Expand Up @@ -360,6 +364,7 @@ PYBIND11_MODULE(_PyPartMC, m) {

py::class_<GasState>(m,
"GasState",
py::metaclass((PyObject *) &PyType_Type),
R"pbdoc(
Current state of the gas mixing ratios in the system.

Expand Down Expand Up @@ -393,21 +398,22 @@ PYBIND11_MODULE(_PyPartMC, m) {

py::class_<RunPartOpt>(m,
"RunPartOpt",
py::metaclass((PyObject *) &PyType_Type),
"Options controlling the execution of run_part()."
)
.def(py::init<const nlohmann::json&>())
.def_property_readonly("t_max", RunPartOpt::t_max, "total simulation time")
.def_property_readonly("del_t", RunPartOpt::del_t, "time step")
;

py::class_<BinGrid>(m,"BinGrid")
py::class_<BinGrid>(m, "BinGrid", py::metaclass((PyObject *) &PyType_Type))
.def(py::init<const double, const py::str, const double, const double>())
.def("__len__", BinGrid::__len__, "returns number of bins")
.def_property_readonly("edges", BinGrid::edges, "Bin edges")
.def_property_readonly("centers", BinGrid::centers, "Bin centers")
;

py::class_<AeroMode>(m,"AeroMode")
py::class_<AeroMode>(m, "AeroMode", py::metaclass((PyObject *) &PyType_Type))
.def(py::init<AeroData&, const nlohmann::json&>())
.def_property("num_conc", &AeroMode::get_num_conc, &AeroMode::set_num_conc,
"provides access (read or write) to the total number concentration of a mode")
Expand All @@ -429,7 +435,7 @@ PYBIND11_MODULE(_PyPartMC, m) {
"Mode name, used to track particle sources")
;

py::class_<AeroDist>(m,"AeroDist")
py::class_<AeroDist>(m, "AeroDist", py::metaclass((PyObject *) &PyType_Type))
.def(py::init<std::shared_ptr<AeroData>, const nlohmann::json&>())
.def_property_readonly("n_mode", &AeroDist::get_n_mode,
"Number of aerosol modes")
Expand Down
Loading