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

Refactor Thermodynamics and Inversion #137

Merged
merged 28 commits into from
Mar 29, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
wip
chengcli committed Mar 25, 2024
commit 6a1ead9ca944b5a50fd28e146ca06f4253e7006e
9 changes: 2 additions & 7 deletions examples/2024-FDing-jupiter-rt/run_disort_jup.py
Original file line number Diff line number Diff line change
@@ -4,9 +4,8 @@
sys.path.append("../python")
sys.path.append(".")

from canoe import index_map
from canoe import def_species, load_configure
from canoe.harp import radiation_band
from canoe.utilities import load_configure
from numpy import linspace, ones, exp
from netCDF4 import Dataset
from pylab import *
@@ -36,11 +35,7 @@ def create_atmosphere(nlyr: int) -> dict:


if __name__ == "__main__":
index_map.from_dict(
{
"vapor": ["H2O", "NH3"],
}
)
def_species(vapors=["H2O", "NH3"])

config = load_configure("jupiter_rt.yaml")
band = radiation_band("B1", config, load_opacity=True)
11 changes: 3 additions & 8 deletions examples/2024-FDing-jupiter-rt/run_ktable_jup.py
Original file line number Diff line number Diff line change
@@ -4,9 +4,8 @@
sys.path.append("../python")
sys.path.append(".")

from canoe import index_map
from canoe import def_species, load_configure, find_resource
from canoe.harp import radiation_band
from canoe.utilities import load_configure, find_resource
from netCDF4 import Dataset
from numpy import *
from rfmlib import *
@@ -41,16 +40,12 @@ def create_rfm_atmosphere(nlyr: int) -> dict:

if __name__ == "__main__":
hitran_file = find_resource("HITRAN2020.par")
index_map.from_dict(
{
"vapor": ["H2O", "NH3"],
}
)
def_species(vapors=["H2O", "NH3"])

config = load_configure("jupiter_rt.yaml")
band = radiation_band("B1", config)

nspec = band.get_num_spec_grids()
nspec = band.get_num_specgrids()
wmin, wmax = band.get_range()
wres = (wmax - wmin) / (nspec - 1)

39 changes: 35 additions & 4 deletions python/pycanoe.cpp
Original file line number Diff line number Diff line change
@@ -3,6 +3,13 @@
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

// application
#include <application/application.hpp>
#include <application/exceptions.hpp>

// yaml-cpp
#include <yaml-cpp/yaml.h>

// athena
#include <athena/parameter_input.hpp>

@@ -17,15 +24,41 @@ namespace py = pybind11;
void init_athena(py::module &);
void init_harp(py::module &);
void init_snap(py::module &);
void init_utilities(py::module &);
void init_utils(py::module &);

void def_species(std::vector<std::string> vapors,
std::vector<std::string> clouds,
std::vector<std::string> tracers) {
IndexMap::InitFromNames(vapors, clouds, tracers);
}

std::string find_resource(const std::string &filename) {
auto app = Application::GetInstance();
try {
auto full_path = app->FindResource(filename);
return full_path;
} catch (NotFoundError &e) {
throw std::runtime_error(e.what());
}
}

auto cleanup = []() { IndexMap::Destroy(); };

PYBIND11_MODULE(canoe, m) {
m.attr("__name__") = "canoe";
m.doc() = "Python bindings for canoe";
m.add_object("_cleanup", py::capsule(cleanup));

m.def("def_species", &def_species, py::arg("vapors"),
py::arg("clouds") = std::vector<std::string>(),
py::arg("tracers") = std::vector<std::string>());

m.def("load_configure", &YAML::LoadFile, "");
m.def("find_resource", &find_resource);

init_athena(m);
init_harp(m);
init_utilities(m);
init_utils(m);
// init_snap(m);
//
// Constants
@@ -44,8 +77,6 @@ PYBIND11_MODULE(canoe, m) {
// IndexMap
py::class_<IndexMap>(m, "index_map")
.def_static("get", &IndexMap::GetInstance)
.def_static("from_file", &IndexMap::InitFromAthenaInput)
.def_static("from_dict", &IndexMap::InitFromSpeciesMap)

.def("get_vapor_id", &IndexMap::GetVaporId)
.def("get_cloud_id", &IndexMap::GetCloudId)
2 changes: 1 addition & 1 deletion python/pyharp.cpp
Original file line number Diff line number Diff line change
@@ -53,7 +53,7 @@ void init_harp(py::module &parent) {

.def("resize", &RadiationBand::Resize, py::arg("nc1"), py::arg("nc2") = 1,
py::arg("nc3") = 1, py::arg("nstr") = 4)
.def("get_num_spec_grids", &RadiationBand::GetNumSpecGrids)
.def("get_num_specgrids", &RadiationBand::GetNumSpecGrids)
.def("get_num_absorbers", &RadiationBand::GetNumAbsorbers)
.def("absorbers", &RadiationBand::Absorbers)
.def("get_absorber", &RadiationBand::GetAbsorber)
18 changes: 0 additions & 18 deletions python/pyutils.cpp
Original file line number Diff line number Diff line change
@@ -6,27 +6,11 @@
// yaml-cpp
#include <yaml-cpp/yaml.h>

// application
#include <application/application.hpp>
#include <application/exceptions.hpp>

namespace py = pybind11;

std::string find_resource(const std::string& filename) {
auto app = Application::GetInstance();
try {
auto full_path = app->FindResource(filename);
return full_path;
} catch (NotFoundError& e) {
throw std::runtime_error(e.what());
}
}

void init_utils(py::module& parent) {
auto m = parent.def_submodule("utils", "External utilities");

m.def("find_resource", &find_resource);

// yaml-cpp
py::enum_<YAML::NodeType::value>(m, "NodeType")
.value("Undefined", YAML::NodeType::Undefined)
@@ -66,6 +50,4 @@ void init_utils(py::module& parent) {
.def("first", [](YAML::detail::iterator_value& val) { return val.first; })
.def("second",
[](YAML::detail::iterator_value& val) { return val.second; });

m.def("load_configure", &YAML::LoadFile, "");
}
51 changes: 16 additions & 35 deletions src/index_map.cpp
Original file line number Diff line number Diff line change
@@ -37,8 +37,10 @@ IndexMap const* IndexMap::GetInstance() {
return myindex_map_;
}

IndexMap const* IndexMap::InitFromSpeciesMap(
std::map<std::string, std::vector<std::string>> const& smap) {
IndexMap const* IndexMap::InitFromNames(
std::vector<std::string> const& vapors,
std::vector<std::string> const& clouds,
std::vector<std::string> const& tracers) {
if (myindex_map_ != nullptr) {
throw RuntimeError("IndexMap", "IndexMap has been initialized");
}
@@ -48,44 +50,23 @@ IndexMap const* IndexMap::InitFromSpeciesMap(
Application::Logger app("canoe");
app->Log("Initialize IndexMap");

std::vector<std::string> names;

// vapor id
if (smap.find("vapor") != smap.end()) {
names = smap.at("vapor");
if (names.size() > NVAPOR)
throw ValueError("IndexMap", "Number of vapors", NVAPOR, names.size());
for (size_t i = 0; i < names.size(); ++i)
myindex_map_->vapor_index_map_[names[i]] = 1 + i;
}
if (vapors.size() > NVAPOR)
throw ValueError("IndexMap", "Number of vapors", NVAPOR, vapors.size());
for (size_t i = 0; i < vapors.size(); ++i)
myindex_map_->vapor_index_map_[vapors[i]] = 1 + i;

// cloud id
if (smap.find("cloud") != smap.end()) {
names = smap.at("cloud");
if (names.size() > NCLOUD)
throw ValueError("IndexMap", "Number of clouds", NCLOUD, names.size());
for (size_t i = 0; i < names.size(); ++i)
myindex_map_->cloud_index_map_[names[i]] = i;
}

// chemistry id
if (smap.find("chemistry") != smap.end()) {
names = smap.at("chemistry");
if (names.size() > NCHEMISTRY)
throw ValueError("IndexMap", "Number of chemistry", NCHEMISTRY,
names.size());
for (size_t i = 0; i < names.size(); ++i)
myindex_map_->chemistry_index_map_[names[i]] = i;
}
if (clouds.size() > NCLOUD)
throw ValueError("IndexMap", "Number of clouds", NCLOUD, clouds.size());
for (size_t i = 0; i < clouds.size(); ++i)
myindex_map_->cloud_index_map_[clouds[i]] = i;

// tracer id
if (smap.find("tracer") != smap.end()) {
names = smap.at("tracer");
if (names.size() > NTRACER)
throw ValueError("IndexMap", "Number of tracers", NTRACER, names.size());
for (size_t i = 0; i < names.size(); ++i)
myindex_map_->tracer_index_map_[names[i]] = i;
}
if (tracers.size() > NTRACER)
throw ValueError("IndexMap", "Number of tracers", NTRACER, tracers.size());
for (size_t i = 0; i < tracers.size(); ++i)
myindex_map_->tracer_index_map_[tracers[i]] = i;

//! \todo add particle
return myindex_map_;
5 changes: 3 additions & 2 deletions src/index_map.hpp
Original file line number Diff line number Diff line change
@@ -19,8 +19,9 @@ class IndexMap {

static IndexMap const* InitFromAthenaInput(ParameterInput* pin);

static IndexMap const* InitFromSpeciesMap(
std::map<std::string, std::vector<std::string>> const&);
static IndexMap const* InitFromNames(std::vector<std::string> const& vapors,
std::vector<std::string> const& clouds,
std::vector<std::string> const& tracers);

static void Destroy();