added: ability to pass command line parameters from python

This commit is contained in:
Arne Morten Kvarving 2024-11-25 10:41:28 +01:00
parent d0bcf54b00
commit 4d5a67fa82
4 changed files with 40 additions and 5 deletions

View File

@ -40,7 +40,8 @@ private:
using Simulator = Opm::GetPropType<TypeTag, Opm::Properties::Simulator>;
public:
PyBlackOilSimulator( const std::string& deckFilename);
PyBlackOilSimulator(const std::string& deckFilename,
const std::vector<std::string>& args);
PyBlackOilSimulator(
std::shared_ptr<Opm::Deck> deck,
std::shared_ptr<Opm::EclipseState> state,
@ -92,6 +93,7 @@ private:
std::shared_ptr<Opm::EclipseState> eclipse_state_;
std::shared_ptr<Opm::Schedule> schedule_;
std::shared_ptr<Opm::SummaryConfig> summary_config_;
std::vector<std::string> args_;
};
} // namespace Opm::Pybind

View File

@ -52,8 +52,10 @@ flowBlackoilTpfaMainInit(int argc, char** argv, bool outputCout, bool outputFile
namespace py = pybind11;
namespace Opm::Pybind {
PyBlackOilSimulator::PyBlackOilSimulator( const std::string &deck_filename)
PyBlackOilSimulator::PyBlackOilSimulator(const std::string& deck_filename,
const std::vector<std::string>& args)
: deck_filename_{deck_filename}
, args_{args}
{
}
@ -231,6 +233,7 @@ int PyBlackOilSimulator::stepInit()
this->mpi_finalize_
);
}
this->main_->setArguments(args_);
int exit_code = EXIT_SUCCESS;
this->flow_main_ = this->main_->initFlowBlackoil(exit_code);
if (this->flow_main_) {
@ -292,8 +295,10 @@ void export_PyBlackOilSimulator(py::module& m)
using namespace Opm::Pybind::DocStrings;
py::class_<PyBlackOilSimulator>(m, "BlackOilSimulator")
.def(py::init<const std::string&>(),
PyBlackOilSimulator_filename_constructor_docstring)
.def(py::init<const std::string&,
const std::vector<std::string>&>(),
PyBlackOilSimulator_filename_constructor_docstring,
py::arg("filename"), py::arg("args") = std::vector<std::string>{})
.def(py::init<
std::shared_ptr<Opm::Deck>,
std::shared_ptr<Opm::EclipseState>,

View File

@ -34,6 +34,31 @@ class PyMain : public Main
public:
using Main::Main;
void setArguments(const std::vector<std::string>& args)
{
if (args.empty()) {
return;
}
// We have the two arguments previously setup
// (binary name and input case name) by the main
// class plus whichever args are in the parameter
// that was passed from the python side.
this->argc_ = 2 + args.size();
// Setup our vector of char*'s
argv_python_.resize(2 + args.size());
argv_python_[0] = argv_[0];
argv_python_[1] = argv_[1];
for (std::size_t i = 0; i < args.size(); ++i) {
argv_python_[i+2] = const_cast<char*>(args[i].c_str());
}
// Finally set the main class' argv pointer to
// the combined parameter list.
this->argv_ = argv_python_.data();
}
using FlowMainType = FlowMain<Properties::TTag::FlowProblemTPFA>;
// To be called from the Python interface code. Only do the
// initialization and then return a pointer to the FlowMain
@ -53,6 +78,9 @@ public:
return std::unique_ptr<FlowMainType>(); // nullptr
}
}
private:
std::vector<char*> argv_python_{};
};
} // namespace Opm

View File

@ -50,7 +50,7 @@ class TestBasic(unittest.TestCase):
def test_all(self):
with pushd(self.data_dir):
sim = BlackOilSimulator("SPE1CASE1.DATA")
sim = BlackOilSimulator(args=['--linear-solver=ilu0'], filename="SPE1CASE1.DATA")
sim.step_init()
sim.step()
dt = sim.get_dt()