Skip to content

Handle potential exceptions from preCICE calls #366

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

Open
wants to merge 4 commits into
base: develop
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
110 changes: 95 additions & 15 deletions Adapter.C
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,29 @@ void preciceAdapter::Adapter::initialize()
DEBUG(adapterInfo("Initializing the preCICE solver interface..."));
SETUP_TIMER();

if (precice_->requiresInitialData())
bool requiresInitialData = false;
try
{
precice_->requiresInitialData();
}
catch (const std::runtime_error& e)
{
std::exit(1);
}
if (requiresInitialData)
{
DEBUG(adapterInfo("Initializing preCICE data..."));
writeCouplingData();
}

DEBUG(adapterInfo("Initializing preCICE data..."));
precice_->initialize();
try
{
precice_->initialize();
}
catch (const std::runtime_error& e)
Comment on lines +548 to +567
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a rather verbose way of handling this, also a bit hard to read. Why not simply wrapping the whole code block into a try catch?

try{
 if (precice_->requiresInitialData())
  precice_->initialize()
  } catch(...)
{}

}

{
std::exit(1);
}
preciceInitialized_ = true;
ACCUMULATE_TIMER(timeInInitialize_);

Expand All @@ -566,7 +584,14 @@ void preciceAdapter::Adapter::finalize()

// Finalize the preCICE solver interface
SETUP_TIMER();
precice_->finalize();
try
{
precice_->finalize();
}
catch (const std::runtime_error& e)
{
std::exit(1);
}
ACCUMULATE_TIMER(timeInFinalize_);

preciceInitialized_ = false;
Expand All @@ -587,7 +612,14 @@ void preciceAdapter::Adapter::advance()
DEBUG(adapterInfo("Advancing preCICE..."));

SETUP_TIMER();
precice_->advance(timestepSolver_);
try
{
precice_->advance(timestepSolver_);
}
catch (const std::runtime_error& e)
{
std::exit(1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::exit(1);
std::exit(EXIT_FAILURE);

}
ACCUMULATE_TIMER(timeInAdvance_);

return;
Expand Down Expand Up @@ -652,7 +684,7 @@ void preciceAdapter::Adapter::adjustSolverTimeStepAndReadData()
the same timestep as the one determined by preCICE.
*/
double tolerance = 1e-14;
if (precice_->getMaxTimeStepSize() - timestepSolverDetermined > tolerance)
if (getMaxTimeStepSize() - timestepSolverDetermined > tolerance)
{
// Add a bool 'subCycling = true' which is checked in the storeMeshPoints() function.
adapterInfo(
Expand All @@ -668,24 +700,24 @@ void preciceAdapter::Adapter::adjustSolverTimeStepAndReadData()
"warning");
}
}
else if (timestepSolverDetermined - precice_->getMaxTimeStepSize() > tolerance)
else if (timestepSolverDetermined - getMaxTimeStepSize() > tolerance)
{
// In the last time-step, we adjust to dt = 0, but we don't need to trigger the warning here
if (precice_->isCouplingOngoing())
if (isCouplingOngoing())
{
adapterInfo(
"The solver's timestep cannot be larger than the coupling timestep."
" Adjusting from "
+ std::to_string(timestepSolverDetermined) + " to " + std::to_string(precice_->getMaxTimeStepSize()),
+ std::to_string(timestepSolverDetermined) + " to " + std::to_string(getMaxTimeStepSize()),
"warning");
}
timestepSolver_ = precice_->getMaxTimeStepSize();
timestepSolver_ = getMaxTimeStepSize();
}
else
{
DEBUG(adapterInfo("The solver's timestep is the same as the "
"coupling timestep."));
timestepSolver_ = precice_->getMaxTimeStepSize();
timestepSolver_ = getMaxTimeStepSize();
}

// Update the solver's timestep (but don't trigger the adjustDeltaT(),
Expand All @@ -702,6 +734,20 @@ void preciceAdapter::Adapter::adjustSolverTimeStepAndReadData()
return;
}

double preciceAdapter::Adapter::getMaxTimeStepSize()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
double preciceAdapter::Adapter::getMaxTimeStepSize()
double preciceAdapter::Adapter::getMaxTimeStepSize() const

?

{
double maxTimeStepSize = 0.0;
try
{
maxTimeStepSize = precice_->getMaxTimeStepSize();
}
catch (const std::runtime_error& e)
{
std::exit(1);
}
return maxTimeStepSize;
}

bool preciceAdapter::Adapter::isCouplingOngoing()
{
bool isCouplingOngoing = false;
Expand All @@ -712,25 +758,59 @@ bool preciceAdapter::Adapter::isCouplingOngoing()
// was not available.
if (NULL != precice_)
{
isCouplingOngoing = precice_->isCouplingOngoing();
try
{
isCouplingOngoing = precice_->isCouplingOngoing();
}
catch (const std::runtime_error& e)
{
std::exit(1);
}
}

return isCouplingOngoing;
}

bool preciceAdapter::Adapter::isCouplingTimeWindowComplete()
{
return precice_->isTimeWindowComplete();
bool complete = false;
try
{
complete = precice_->isTimeWindowComplete();
}
catch (const std::runtime_error& e)
{
std::exit(1);
}
return complete;
}

bool preciceAdapter::Adapter::requiresReadingCheckpoint()
{
return precice_->requiresReadingCheckpoint();
bool requiresReadingCheckpoint = false;
try
{
requiresReadingCheckpoint = precice_->requiresReadingCheckpoint();
}
catch (const std::runtime_error& e)
{
std::exit(1);
}
return requiresReadingCheckpoint;
}

bool preciceAdapter::Adapter::requiresWritingCheckpoint()
{
return precice_->requiresWritingCheckpoint();
bool requiresWritingCheckpoint = false;
try
{
requiresWritingCheckpoint = precice_->requiresWritingCheckpoint();
}
catch (const std::runtime_error& e)
{
std::exit(1);
}
return requiresWritingCheckpoint;
}


Expand Down
3 changes: 3 additions & 0 deletions Adapter.H
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ private:
// and read data associated to the calculated time step length
void adjustSolverTimeStepAndReadData();

//- Get maximum allowed time step size from preCICE
double getMaxTimeStepSize();

//- Determine if the coupling is still happening
bool isCouplingOngoing();

Expand Down
91 changes: 74 additions & 17 deletions Interface.C
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ preciceAdapter::Interface::Interface(
meshConnectivity_(meshConnectivity),
restartFromDeformed_(restartFromDeformed)
{
dim_ = precice_.getMeshDimensions(meshName);
try
{
dim_ = precice_.getMeshDimensions(meshName);
}
catch (const std::runtime_error& e)
{
std::exit(1);
}

if (dim_ == 2 && meshConnectivity_ == true)
{
Expand Down Expand Up @@ -188,7 +195,14 @@ void preciceAdapter::Interface::configureMesh(const fvMesh& mesh, const std::str
}

// Pass the mesh vertices information to preCICE
precice_.setMeshVertices(meshName_, vertices, vertexIDs_);
try
{
precice_.setMeshVertices(meshName_, vertices, vertexIDs_);
}
catch (const std::runtime_error& e)
{
std::exit(1);
}
}
else if (locationType_ == LocationType::faceNodes)
{
Expand Down Expand Up @@ -256,7 +270,14 @@ void preciceAdapter::Interface::configureMesh(const fvMesh& mesh, const std::str
}

// Pass the mesh vertices information to preCICE
precice_.setMeshVertices(meshName_, vertices, vertexIDs_);
try
{
precice_.setMeshVertices(meshName_, vertices, vertexIDs_);
}
catch (const std::runtime_error& e)
{
std::exit(1);
}

if (meshConnectivity_)
{
Expand Down Expand Up @@ -323,7 +344,14 @@ void preciceAdapter::Interface::configureMesh(const fvMesh& mesh, const std::str
DEBUG(adapterInfo("Number of triangles: " + std::to_string(faceField.size() * triaPerQuad)));

//Set Triangles
precice_.setMeshTriangles(meshName_, triVertIDs);
try
{
precice_.setMeshTriangles(meshName_, triVertIDs);
}
catch (const std::runtime_error& e)
{
std::exit(1);
}
}
}
}
Expand Down Expand Up @@ -429,7 +457,14 @@ void preciceAdapter::Interface::configureMesh(const fvMesh& mesh, const std::str
}

// Pass the mesh vertices information to preCICE
precice_.setMeshVertices(meshName_, vertices, vertexIDs_);
try
{
precice_.setMeshVertices(meshName_, vertices, vertexIDs_);
}
catch (const std::runtime_error& e)
{
std::exit(1);
}
}
}

Expand Down Expand Up @@ -542,16 +577,31 @@ void preciceAdapter::Interface::readCouplingData(double relativeReadTime)

// Make preCICE read vector or scalar data
// and fill the adapter's buffer
std::size_t nReadData = vertexIDs_.size() * precice_.getDataDimensions(meshName_, couplingDataReader->dataName());
std::size_t nReadData = 0;
try
{
nReadData = vertexIDs_.size() * precice_.getDataDimensions(meshName_, couplingDataReader->dataName());
}
catch (const std::runtime_error& e)
{
std::exit(1);
}
// We could add a sanity check here
// nReadData == vertexIDs_.size() * (1 + (dim_ - 1) * static_cast<int>(couplingDataReader->hasVectorData()));

precice_.readData(
meshName_,
couplingDataReader->dataName(),
vertexIDs_,
relativeReadTime,
{dataBuffer_.data(), nReadData});
try
{
precice_.readData(
meshName_,
couplingDataReader->dataName(),
vertexIDs_,
relativeReadTime,
{dataBuffer_.data(), nReadData});
}
catch (const std::runtime_error& e)
{
std::exit(1);
}

// Read the received data from the buffer
couplingDataReader->read(dataBuffer_.data(), dim_);
Expand All @@ -575,11 +625,18 @@ void preciceAdapter::Interface::writeCouplingData()
auto nWrittenData = couplingDataWriter->write(dataBuffer_.data(), meshConnectivity_, dim_);

// Make preCICE write vector or scalar data
precice_.writeData(
meshName_,
couplingDataWriter->dataName(),
vertexIDs_,
{dataBuffer_.data(), nWrittenData});
try
{
precice_.writeData(
meshName_,
couplingDataWriter->dataName(),
vertexIDs_,
{dataBuffer_.data(), nWrittenData});
}
catch (const std::runtime_error& e)
{
std::exit(1);
}
}
// }
}
Expand Down
1 change: 1 addition & 0 deletions changelog-entries/366.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Added exception handling for preCICE API calls [#366](https://github.com/precice/openfoam-adapter/pull/366)