From 4e12ae1c3e19109bdabb997c5b4a75f92b4202af Mon Sep 17 00:00:00 2001 From: Alexander Jaust <alexander.jaust@ipvs.uni-stuttgart.de> Date: Thu, 25 Apr 2019 13:23:06 +0200 Subject: [PATCH] renamed wrapper to adapter --- .../iterative/CMakeLists.txt | 6 +- .../iterative/main_freeflow.cc | 6 +- .../iterative/main_solidenergy.cc | 6 +- .../iterative/preciceadapter.cc | 355 ++++++++++++++++++ .../iterative/preciceadapter.hh | 155 ++++++++ .../monolithic/problem_freeflow.hh | 6 +- .../monolithic/problem_heat.hh | 6 +- 7 files changed, 525 insertions(+), 15 deletions(-) create mode 100644 appl/conjugateheattransfer/iterative/preciceadapter.cc create mode 100644 appl/conjugateheattransfer/iterative/preciceadapter.hh diff --git a/appl/conjugateheattransfer/iterative/CMakeLists.txt b/appl/conjugateheattransfer/iterative/CMakeLists.txt index 69179bc..3555736 100644 --- a/appl/conjugateheattransfer/iterative/CMakeLists.txt +++ b/appl/conjugateheattransfer/iterative/CMakeLists.txt @@ -1,9 +1,9 @@ add_input_file_links() dune_symlink_to_source_files(FILES "precice-config.xml" "precice-config-serial-implicit.xml") -add_executable(test_freeflow EXCLUDE_FROM_ALL main_freeflow.cc precicewrapper.cc dumuxpreciceindexwrapper.cc) -add_executable(test_solidenergy EXCLUDE_FROM_ALL main_solidenergy.cc precicewrapper.cc dumuxpreciceindexwrapper.cc) -#add_executable(test_precicewrapper EXCLUDE_FROM_ALL precicewrapper.cc) +add_executable(test_freeflow EXCLUDE_FROM_ALL main_freeflow.cc preciceadapter.cc dumuxpreciceindexwrapper.cc) +add_executable(test_solidenergy EXCLUDE_FROM_ALL main_solidenergy.cc preciceadapter.cc dumuxpreciceindexwrapper.cc) +#add_executable(test_preciceadapter EXCLUDE_FROM_ALL preciceadapter.cc) target_compile_definitions(test_freeflow PUBLIC "ENABLEMONOLITHIC=0") target_compile_definitions(test_solidenergy PUBLIC "ENABLEMONOLITHIC=0") diff --git a/appl/conjugateheattransfer/iterative/main_freeflow.cc b/appl/conjugateheattransfer/iterative/main_freeflow.cc index 27532ee..5290c65 100644 --- a/appl/conjugateheattransfer/iterative/main_freeflow.cc +++ b/appl/conjugateheattransfer/iterative/main_freeflow.cc @@ -47,7 +47,7 @@ #include <dumux/nonlinear/newtonsolver.hh> #include "../monolithic/problem_freeflow.hh" -#include "precicewrapper.hh" +#include "preciceadapter.hh" template<class Problem, class GridVariables, class SolutionVector> @@ -60,7 +60,7 @@ void setBoundaryHeatFluxes(const Problem& problem, auto elemVolVars = localView(gridVars.curGridVolVars()); auto elemFaceVars = localView(gridVars.curGridFaceVars()); - auto& couplingInterface = precice_wrapper::PreciceWrapper::getInstance(); + auto& couplingInterface = precice_adapter::PreciceAdapter::getInstance(); for (const auto& element : elements(fvGridGeometry.gridView())) { @@ -125,7 +125,7 @@ int main(int argc, char** argv) try // - Name of solver // - What rank of how many ranks this instance is // Configure preCICE. For now the config file is hardcoded. - auto& couplingInterface = precice_wrapper::PreciceWrapper::getInstance(); + auto& couplingInterface = precice_adapter::PreciceAdapter::getInstance(); couplingInterface.announceSolver( "FreeFlow", mpiHelper.rank(), mpiHelper.size() ); //couplingInterface.createInstance( "FreeFlow", mpiHelper.rank(), mpiHelper.size() ); std::string preciceConfigFilename = "precice-config.xml"; diff --git a/appl/conjugateheattransfer/iterative/main_solidenergy.cc b/appl/conjugateheattransfer/iterative/main_solidenergy.cc index 95194de..28a1543 100644 --- a/appl/conjugateheattransfer/iterative/main_solidenergy.cc +++ b/appl/conjugateheattransfer/iterative/main_solidenergy.cc @@ -45,7 +45,7 @@ #include <dumux/nonlinear/newtonsolver.hh> #include "../monolithic/problem_heat.hh" -#include "precicewrapper.hh" +#include "preciceadapter.hh" template<class ThermalConductivityModel, class Problem, class FVElementGeometry, class ElementVolumeVariables> auto recontructBoundaryTemperature(const Problem& problem, @@ -81,7 +81,7 @@ void setBoundaryTemperatures(const Problem& problem, auto fvGeometry = localView(fvGridGeometry); auto elemVolVars = localView(gridVars.curGridVolVars()); - auto& couplingInterface = precice_wrapper::PreciceWrapper::getInstance(); + auto& couplingInterface = precice_adapter::PreciceAdapter::getInstance(); for (const auto& element : elements(fvGridGeometry.gridView())) { @@ -142,7 +142,7 @@ int main(int argc, char** argv) try // Initialize preCICE.Tell preCICE about: // - Name of solver // - What rank of how many ranks this instance is - auto& couplingInterface = precice_wrapper::PreciceWrapper::getInstance(); + auto& couplingInterface = precice_adapter::PreciceAdapter::getInstance(); couplingInterface.announceSolver( "SolidEnergy", mpiHelper.rank(), mpiHelper.size() ); // Configure preCICE. For now the config file is hardcoded. std::string preciceConfigFilename = "precice-config.xml"; diff --git a/appl/conjugateheattransfer/iterative/preciceadapter.cc b/appl/conjugateheattransfer/iterative/preciceadapter.cc new file mode 100644 index 0000000..0a0ca28 --- /dev/null +++ b/appl/conjugateheattransfer/iterative/preciceadapter.cc @@ -0,0 +1,355 @@ +#include "preciceadapter.hh" + +#include <cassert> + +using namespace precice_adapter; + +PreciceAdapter::PreciceAdapter(): + wasCreated_(false), precice_(nullptr), meshWasCreated_(false), preciceWasInitialized_(false), + meshID_(0), heatFluxID_(0), temperatureID_(0), timeStepSize_(0.) +{ + +} + +PreciceAdapter& PreciceAdapter::getInstance() +{ + static PreciceAdapter instance; + return instance; +} + +void PreciceAdapter::configure( const std::string& configurationFileName ) +{ + precice_->configure( configurationFileName ); +} +/* +void PreciceAdapter::announceHeatFluxToWrite(const HeatFluxType heatFluxType) +{ + writeHeatFluxType_ = heatFluxType; +} + +void PreciceAdapter::announceHeatFluxToRead(const HeatFluxType heatFluxType) +{ + readHeatFluxType_ = heatFluxType; +} +*/ +void PreciceAdapter::announceSolver( const std::string& name, const int rank, const int size ) +{ + assert( precice_ == nullptr ); + precice_ = std::make_unique<precice::SolverInterface>(name, rank, size); + wasCreated_ = true; +} + +int PreciceAdapter::getDimensions() +{ + assert( wasCreated_ ); + return precice_->getDimensions(); +} +/* +void PreciceAdapter::setMeshName(const std::string& meshName) +{ + assert( wasCreated_ ); + meshID_ = precice_->getMeshID(meshName); +} +*/ +void PreciceAdapter::setMesh(const std::string& meshName, + const size_t numPoints, + std::vector<double>& coordinates, + const std::vector<int>& dumuxFaceIDs ) +{ + assert( wasCreated_ ); + assert( numPoints == dumuxFaceIDs.size() ); + meshID_ = precice_->getMeshID(meshName); + vertexIDs_.resize( numPoints ); + precice_->setMeshVertices( meshID_, numPoints, coordinates.data(), vertexIDs_.data() ); + indexMapper_.createMapping( dumuxFaceIDs, vertexIDs_); + meshWasCreated_ = true; +} +/* +int PreciceAdapter::getDataID( const std::string& dataName, const int meshID ) +{ + assert( wasCreated_ ); + return precice_->getDataID( dataName, meshID ); +} +*/ +double PreciceAdapter::initialize() +{ + assert( wasCreated_ ); + assert( meshWasCreated_ ); + + heatFluxID_ = precice_->getDataID( "Heat-Flux", meshID_ ); + temperatureID_ = precice_->getDataID( "Temperature", meshID_ ); + + heatFlux_.resize( getNumberOfVertices() ); + temperature_.resize( getNumberOfVertices() ); + + timeStepSize_ = precice_->initialize(); + assert( timeStepSize_ > 0 ); + + preciceWasInitialized_ = true; + return timeStepSize_; +} + +void PreciceAdapter::initializeData() +{ + assert( preciceWasInitialized_ ); + precice_->initializeData(); +} + +void PreciceAdapter::finalize() +{ + assert( wasCreated_ ); + precice_->finalize(); +} + +/* +void PreciceAdapter::initializeData() +{ + assert( wasCreated_ ); + precice_->initializeData(); +} +*/ + +double PreciceAdapter::advance( const double computedTimeStepLength ) +{ + assert( wasCreated_ ); + return precice_->advance( computedTimeStepLength ); +} + +bool PreciceAdapter::isCouplingOngoing() +{ + assert( wasCreated_ ); + return precice_->isCouplingOngoing(); +} + +size_t PreciceAdapter::getNumberOfVertices() +{ + assert( wasCreated_ ); + return vertexIDs_.size(); +} + +double PreciceAdapter::getHeatFluxOnFace( const int faceID) const +{ + assert( wasCreated_ ); + const auto idx = indexMapper_.getPreciceId( faceID ); + assert(idx < heatFlux_.size() ); + return heatFlux_[idx]; +} + +void PreciceAdapter::writeHeatFluxOnFace(const int faceID, + const double value) +{ + assert( wasCreated_ ); + const auto idx = indexMapper_.getPreciceId( faceID ); + assert(idx < heatFlux_.size() ); + heatFlux_[idx] = value; +} + +double PreciceAdapter::getTemperatureOnFace(const int faceID) const +{ + assert( wasCreated_ ); + const auto idx = indexMapper_.getPreciceId( faceID ); + assert(idx < temperature_.size() ); + return temperature_[idx]; +} + +void PreciceAdapter::writeTemperatureOnFace(const int faceID, const double value) +{ + assert( wasCreated_ ); + const auto idx = indexMapper_.getPreciceId( faceID ); + assert(idx < temperature_.size() ); + temperature_[idx] = value; +} + +void PreciceAdapter::writeHeatFluxToOtherSolver() +{ + assert( wasCreated_ ); + writeBlockScalarDataToPrecice( heatFluxID_, heatFlux_ ); +} + +void PreciceAdapter::readHeatFluxFromOtherSolver() +{ + assert( wasCreated_ ); + readBlockScalarDataFromPrecice( heatFluxID_, heatFlux_ ); +} + +void PreciceAdapter::writeTemperatureToOtherSolver() +{ + assert( wasCreated_ ); + writeBlockScalarDataToPrecice( temperatureID_, temperature_ ); +} + +void PreciceAdapter::readTemperatureFromOtherSolver() +{ + assert( wasCreated_ ); + readBlockScalarDataFromPrecice( temperatureID_, temperature_ ); +} + +bool PreciceAdapter::isCoupledEntity(const int faceID) const +{ + assert( wasCreated_ ); + return indexMapper_.isDumuxIdMapped( faceID ); +} +/* +std::vector<double>& PreciceAdapter::getHeatFluxToWrite() +{ + assert( wasCreated_ ); + assert( writeHeatFluxType_ != HeatFluxType::UNDEFINED); + if ( writeHeatFluxType_ == HeatFluxType::FreeFlow ) + return freeFlowHeatFlux_; + else + return solidHeatFlux_; +} +*/ +//void PreciceAdapter::readScalarQuantitiy(const int dataID, std::vector<double> &data) +//{ +// assert( wasCreated_ ); +// precice_->readBlockScalarData( dataID, vertexIDs_.size(), +// vertexIDs_.data(), data.data() ); +//} +// +//void PreciceAdapter::writeScalarQuantitiy(const int dataID, std::vector<double> &data) +//{ +// assert( wasCreated_ ); +// precice_->writeBlockScalarData( dataID, vertexIDs_.size(), +// vertexIDs_.data(), data.data() ); +//} + +void PreciceAdapter::print(std::ostream& os) +{ + os << indexMapper_; +} + +/* +void PreciceAdapter::writeSolidTemperature(std::vector<double> &temperature) +{ + assert( wasCreated_ ); + precice_->writeBlockScalarData( solidTemperatureID_, vertexIDs_.size(), + vertexIDs_.data(), temperature.data() ); + +} + +void PreciceAdapter::readSolidTemperature(std::vector<double> &temperature) +{ + assert( wasCreated_ ); + precice_->readBlockScalarData( solidTemperatureID_, vertexIDs_.size(), + vertexIDs_.data(), temperature.data() ); +} +*/ + +/* +void PreciceAdapter::readBlockScalarData( const int dataID, + const int size, + int* const valueIndices, + double* const values ) +{ + assert( wasCreated_ ); + precice_->readBlockScalarData( dataID, size, valueIndices, values ); +} + +void PreciceAdapter::writeBlockScalarData( const int dataID, + const int size, + int* const valueIndices, + double* const values ) +{ + assert( wasCreated_ ); + return precice_->writeBlockScalarData( dataID, size, valueIndices, values ); +} +*/ + +bool PreciceAdapter::checkIfActionIsRequired( const std::string& condition ) +{ + assert( wasCreated_ ); + return precice_->isActionRequired( condition ); +} + +void PreciceAdapter::actionIsFulfilled(const std::string& condition) +{ + assert( wasCreated_ ); + precice_->fulfilledAction( condition ); +} + +void PreciceAdapter::readBlockScalarDataFromPrecice(const int dataID, std::vector<double> &data) +{ + assert( wasCreated_ ); + assert( vertexIDs_.size() == data.size() ); + precice_->readBlockScalarData( dataID, vertexIDs_.size(), vertexIDs_.data(), data.data() ); +} + +void PreciceAdapter::writeBlockScalarDataToPrecice(const int dataID, std::vector<double> &data) +{ + assert( wasCreated_ ); + assert( vertexIDs_.size() == data.size() ); + precice_->writeBlockScalarData( dataID, vertexIDs_.size(), vertexIDs_.data(), data.data() ); +} + +bool PreciceAdapter::hasToWriteInitialData() +{ + assert( wasCreated_ ); + return checkIfActionIsRequired(precice::constants::actionWriteInitialData()); +} + +void PreciceAdapter::announceInitialDataWritten() +{ + assert( wasCreated_ ); + writeHeatFluxToOtherSolver(); + precice_->fulfilledAction( precice::constants::actionWriteInitialData() ); +} + +/* +bool PreciceAdapter::isInitialDataAvailable() +{ + assert( wasCreated_ ); + return precice_->isReadDataAvailable(); +} +*/ + +bool PreciceAdapter::hasToReadIterationCheckpoint() +{ + assert( wasCreated_ ); + return checkIfActionIsRequired(precice::constants::actionReadIterationCheckpoint()); +} + +void PreciceAdapter::announceIterationCheckpointRead() +{ + assert( wasCreated_ ); + actionIsFulfilled( precice::constants::actionReadIterationCheckpoint() ); +} + +bool PreciceAdapter::hasToWriteIterationCheckpoint() +{ + assert( wasCreated_ ); + return checkIfActionIsRequired(precice::constants::actionWriteIterationCheckpoint()); +} + +void PreciceAdapter::announceIterationCheckpointWritten() +{ + assert( wasCreated_ ); + actionIsFulfilled( precice::constants::actionWriteIterationCheckpoint() ); +} + +/* +void PreciceAdapter::writeInitialBlockScalarData( const int dataID, + const int size, + int* const valueIndices, + double* const values ) +{ + assert( wasCreated_ ); + if ( hasToWriteInitialData() ) + { + precice_->writeBlockScalarData( dataID, size, valueIndices, values ); + } +} + +void PreciceAdapter::announceAllInitialDataWritten() +{ + assert( wasCreated_ ); + if ( hasToWriteInitialData() ) + { + precice_->fulfilledAction( precice::constants::actionWriteInitialData() ); + } +} +*/ +PreciceAdapter::~PreciceAdapter() +{ +} + diff --git a/appl/conjugateheattransfer/iterative/preciceadapter.hh b/appl/conjugateheattransfer/iterative/preciceadapter.hh new file mode 100644 index 0000000..59ec5d5 --- /dev/null +++ b/appl/conjugateheattransfer/iterative/preciceadapter.hh @@ -0,0 +1,155 @@ +#ifndef PRECICEWRAPPER_HH +#define PRECICEWRAPPER_HH + +#include<string> +#include<ostream> +#include<precice/SolverInterface.hpp> + +#include "dumuxpreciceindexwrapper.hh" + +namespace precice_adapter{ + + /* + enum HeatFluxType + { + UNDEFINED, FreeFlow, Solid + }; + */ + +class PreciceAdapter +{ + +private: + bool wasCreated_; + std::unique_ptr<precice::SolverInterface> precice_; + + PreciceAdapter(); + + bool checkIfActionIsRequired( const std::string& condition ); + void actionIsFulfilled( const std::string& condition ); + + void readBlockScalarDataFromPrecice( const int dataID, std::vector<double>& data ); + void writeBlockScalarDataToPrecice( const int dataID, std::vector<double>& data ); + + bool meshWasCreated_; + bool preciceWasInitialized_; + int meshID_; + int dimension_; + int heatFluxID_; + int temperatureID_; + + double timeStepSize_; + + /* + HeatFluxType writeHeatFluxType_; + HeatFluxType readHeatFluxType_; + */ + + + std::vector<int> vertexIDs_; //should be size_t + std::vector<double> heatFlux_; + std::vector<double> temperature_; + + DumuxPreciceIndexMapper<int> indexMapper_; + + + ~PreciceAdapter(); +public: + PreciceAdapter(const PreciceAdapter&) = delete; + void operator=(const PreciceAdapter&) = delete; + + static PreciceAdapter& getInstance(); + + void announceSolver( const std::string& name, const int rank, const int size ); + void configure( const std::string& configurationFileName ); + + /* + void announceHeatFluxToWrite( const HeatFluxType heatFluxType ); + void announceHeatFluxToRead( const HeatFluxType heatFluxType ); + */ + + int getDimensions(); + // static int getMeshID( const std::string& meshName ); + //static void setMeshName( const std::string& meshName ); + //static int getDataID( const std::string& dataName, const int meshID ); + + //static void writeInitialBlockScalarData( const int dataID, + // const int size, + // int* const valueIndices, + // double* const values ); + //static void announceAllInitialDataWritten(); + + bool hasToReadIterationCheckpoint(); + void announceIterationCheckpointRead(); + bool hasToWriteIterationCheckpoint(); + void announceIterationCheckpointWritten(); + + bool hasToWriteInitialData(); + void announceInitialDataWritten(); + + bool isInitialDataAvailable(); + + void setMesh( const std::string& meshName, + const size_t numPoints, + std::vector<double>& coordinates, + const std::vector<int>& dumuxFaceIDs ) ; + + double initialize(); + void initializeData(); + void finalize(); + //static void initializeData(); + + double advance( const double computedTimeStepLength ); + bool isCouplingOngoing(); + + size_t getNumberOfVertices(); + + + double getHeatFluxOnFace( const int faceID ) const; + void writeHeatFluxOnFace( const int faceID, const double value ); + + double getTemperatureOnFace( const int faceID ) const; + void writeTemperatureOnFace( const int faceID, const double value ); + + void writeHeatFluxToOtherSolver(); + void readHeatFluxFromOtherSolver(); + + void writeTemperatureToOtherSolver(); + void readTemperatureFromOtherSolver(); + + + bool isCoupledEntity( const int faceID ) const; + + + +// std::vector<double>& getHeatFluxToWrite(); + +// static void readScalarQuantitiy( const int dataID, std::vector<double>& data ); +// static void writeScalarQuantitiy( const int dataID, std::vector<double>& data ); + + + void print( std::ostream& os ); + + /* + static void writeSolidTemperature( std::vector<double>& temperature ); + static void readSolidTemperature( std::vector<double>& temperature ); + static void writeFluidTemperature( const std::vector<double>& temperature ); + static void readFluidTemperature( std::vector<double>& temperature ); + */ + + /* + static void readBlockScalarData( const int dataID, + const int size, + int* const valueIndices, + double* const values ); + + static void writeBlockScalarData( const int dataID, + const int size, + int* const valueIndices, + double* const values ); + */ + +}; + +} +#endif diff --git a/appl/conjugateheattransfer/monolithic/problem_freeflow.hh b/appl/conjugateheattransfer/monolithic/problem_freeflow.hh index 4c1004e..7db39a0 100644 --- a/appl/conjugateheattransfer/monolithic/problem_freeflow.hh +++ b/appl/conjugateheattransfer/monolithic/problem_freeflow.hh @@ -38,7 +38,7 @@ #include <dumux/discretization/staggered/freeflow/properties.hh> #include <dumux/freeflow/navierstokes/model.hh> -#include "../iterative/precicewrapper.hh" +#include "../iterative/preciceadapter.hh" namespace Dumux { template <class TypeTag> @@ -112,7 +112,7 @@ public: : ParentType(fvGridGeometry, "FreeFlow"), eps_(1e-6), couplingManager_(couplingManager) #else FreeFlowSubProblem(std::shared_ptr<const FVGridGeometry> fvGridGeometry) - : ParentType(fvGridGeometry, "FreeFlow"), eps_(1e-6), couplingInterface_(precice_wrapper::PreciceWrapper::getInstance()) + : ParentType(fvGridGeometry, "FreeFlow"), eps_(1e-6), couplingInterface_(precice_adapter::PreciceAdapter::getInstance()) #endif { problemName_ = getParam<std::string>("Vtk.OutputName") + "_" + getParamFromGroup<std::string>(this->paramGroup(), "Problem.Name"); @@ -297,7 +297,7 @@ private: #if ENABLEMONOLITHIC std::shared_ptr<CouplingManager> couplingManager_; #else - precice_wrapper::PreciceWrapper& couplingInterface_; + precice_adapter::PreciceAdapter& couplingInterface_; #endif }; diff --git a/appl/conjugateheattransfer/monolithic/problem_heat.hh b/appl/conjugateheattransfer/monolithic/problem_heat.hh index 832a32f..569cd8d 100644 --- a/appl/conjugateheattransfer/monolithic/problem_heat.hh +++ b/appl/conjugateheattransfer/monolithic/problem_heat.hh @@ -37,7 +37,7 @@ #include <dumux/porousmediumflow/problem.hh> #include "spatialparams.hh" -#include "../iterative/precicewrapper.hh" +#include "../iterative/preciceadapter.hh" namespace Dumux { template <class TypeTag> @@ -96,7 +96,7 @@ public: : ParentType(fvGridGeometry, "SolidEnergy"), eps_(1e-7), couplingManager_(couplingManager) #else HeatSubProblem(std::shared_ptr<const FVGridGeometry> fvGridGeometry) - : ParentType(fvGridGeometry, "SolidEnergy"), eps_(1e-7), couplingInterface_( precice_wrapper::PreciceWrapper::getInstance() ) + : ParentType(fvGridGeometry, "SolidEnergy"), eps_(1e-7), couplingInterface_( precice_adapter::PreciceAdapter::getInstance() ) #endif { problemName_ = getParam<std::string>("Vtk.OutputName") + "_" + getParamFromGroup<std::string>(this->paramGroup(), "Problem.Name"); @@ -229,7 +229,7 @@ private: #if ENABLEMONOLITHIC std::shared_ptr<CouplingManager> couplingManager_; #else - precice_wrapper::PreciceWrapper& couplingInterface_; + precice_adapter::PreciceAdapter& couplingInterface_; #endif }; -- GitLab