[Feature] Access to solvers routines via Python using Pygeosx
What is the requested feature? This issue is about accessing the different routines in the solvers of GEOSX for more flexibility using Pygeosx.
Is your request related to a specific problem? The problem is that the current implementation of Pygeosx does not allow us to have a full control on the GEOSX execution process (time loop and solverStep execution due to the eventManager for instance).
Describe the solution you'd like Create new routines access in pygeosx and also create/modify access to some class members (such as the eventManager from problemManager) would allow us to bypass the eventManager thread of execution, and have full control over the time loop and execution process from python by calling the routines we want. Example following.
ProblemManager.hpp :
EventManager & getEventManager()
{return *m_eventManager;}
pygeosx.cpp :
PyObject * explicitStep(PyObject * self, PyObject * args) noexcept
{
GEOSX_UNUSED_VAR( self);
double time;
double dt;
bool earlyReturn = false;
if( !PyArg_ParseTuple( args, "dd", &time, &dt ) )
{
return nullptr;
}
geosx::ProblemManager & pb_manager = geosx::g_state->getProblemManager();
geosx::DomainPartition & domain = pb_manager.getDomainPartition();
geosx::EventManager & eventManager = pb_manager.getEventManager();
// Setup event targets
eventManager.forSubGroups( [&]( geosx::EventBase & subEvent )
{
subEvent.getTargetReferences();
} );
geosx::EventBase * subEvent = static_cast( eventManager.getSubGroups()[0]);
earlyReturn = earlyReturn || subEvent->execute(time, dt, 0, 0, 0, domain);
Py_RETURN_NONE;
}
BEGIN_ALLOW_DESIGNATED_INITIALIZERS
/**
*
*/
static PyMethodDef pygeosxFuncs[] = {
{ "initialize", geosx::initialize, METH_VARARGS, geosx::initializeDocString },
{ "reinit", geosx::reinit, METH_VARARGS, geosx::reinitDocString },
{ "apply_initial_conditions", geosx::applyInitialConditions, METH_NOARGS, geosx::applyInitialConditionsDocString },
{ "run", geosx::run, METH_NOARGS, geosx::runDocString },
{ "_finalize", geosx::finalize, METH_NOARGS, geosx::finalizeDocString },
{ "explicitStep", explicitStep, METH_VARARGS, "explicit Step" },
{ nullptr, nullptr, 0, nullptr } /* Sentinel */
};
With these modifications we are able to run a python script calling directly the explicitStep handling the time loop from python. Simple example of python script :
import pygeosx
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
problem = pygeosx.initialize(rank, sys.argv)
pygeosx.apply_initial_conditions()
maxTime = 2.0
time = 0.0
dt = 0.002
while time
We also have a more sophisticated python script, where we can update source, receivers positions and pressure values (acousticSolver) from python, and run the solver for different configurations.
Additional context Ultimately, this would allow us to have full control on different routines to be able to create a Full Waveform Inversion (FWI) process from python using Pygeosx.