Skip to content

Commit

Permalink
improve launching simulation, and result handling when finished,
Browse files Browse the repository at this point in the history
seems to fix #25
  • Loading branch information
vcloarec committed Aug 31, 2022
1 parent 4aaa6f2 commit 6dd1b18
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 78 deletions.
21 changes: 13 additions & 8 deletions src/core/hydraulicNetwork/reoshydraulicnetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

ReosHydraulicNetworkElement::ReosHydraulicNetworkElement( ReosHydraulicNetwork *parent ):
ReosDataObject( parent )
, mNetWork( parent )
, mNetwork( parent )
, mNameParameter( new ReosParameterString( tr( "Name" ), false, this ) )
{
mConstantTimeStepInTable = new ReosParameterDuration( tr( "Constant time step" ) );
Expand All @@ -36,7 +36,7 @@ ReosHydraulicNetworkElement::ReosHydraulicNetworkElement( ReosHydraulicNetwork *

ReosHydraulicNetworkElement::ReosHydraulicNetworkElement( const ReosEncodedElement &encodedElement, ReosHydraulicNetwork *parent )
: ReosDataObject( parent )
, mNetWork( parent )
, mNetwork( parent )
, mNameParameter( ReosParameterString::decode( encodedElement.getEncodedData( QStringLiteral( "name-parameter" ) ), false, tr( "Name" ), this ) )
{
ReosDataObject::decode( encodedElement );
Expand Down Expand Up @@ -73,15 +73,15 @@ ReosHydraulicNetworkElement::~ReosHydraulicNetworkElement()

void ReosHydraulicNetworkElement::destroy()
{
if ( !mNetWork.isNull() )
mNetWork->mElements.remove( id() );
if ( !mNetwork.isNull() )
mNetwork->mElements.remove( id() );
deleteLater();
}

void ReosHydraulicNetworkElement::positionChanged()
{
if ( mNetWork )
mNetWork->elemPositionChangedPrivate( this );
if ( mNetwork )
mNetwork->elemPositionChangedPrivate( this );
}

ReosParameterString *ReosHydraulicNetworkElement::elementName() const
Expand Down Expand Up @@ -120,8 +120,8 @@ void ReosHydraulicNetworkElement::notify( const ReosModule::Message &messageObje
{
ReosModule::Message sendedMessage = messageObject;
sendedMessage.prefixMessage( tr( "Routing %1: " ).arg( elementName()->value() ) );
if ( mNetWork )
mNetWork->message( sendedMessage );
if ( mNetwork )
mNetwork->message( sendedMessage );
}
}

Expand All @@ -139,6 +139,11 @@ ReosDuration ReosHydraulicNetworkElement::currentElementTimeStep() const
return ReosDuration( qint64( 0 ) );
}

ReosHydraulicNetwork *ReosHydraulicNetworkElement::network() const
{
return mNetwork;
}

ReosHydraulicNetwork::ReosHydraulicNetwork( ReosModule *parent, ReosGisEngine *gisEngine, ReosWatershedModule *watershedModule )
: ReosModule( parent )
, mGisEngine( gisEngine )
Expand Down
4 changes: 3 additions & 1 deletion src/core/hydraulicNetwork/reoshydraulicnetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,13 @@ class REOSCORE_EXPORT ReosHydraulicNetworkElement : public ReosDataObject

virtual ReosDuration currentElementTimeStep() const;

ReosHydraulicNetwork *network() const;

public slots:
virtual void updateCalculationContext( const ReosCalculationContext &context ) = 0;

protected:
QPointer<ReosHydraulicNetwork> mNetWork = nullptr;
QPointer<ReosHydraulicNetwork> mNetwork = nullptr;

void calculationUpdated()
{
Expand Down
1 change: 1 addition & 0 deletions src/core/hydraulicNetwork/reoshydraulicscheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ ReosHydraulicScheme::ReosHydraulicScheme( ReosHydraulicSchemeCollection *collect
, mStartTime( new ReosParameterDateTime( tr( "Start time" ), this ) )
, mEndTime( new ReosParameterDateTime( tr( "End time" ), this ) )
{
mEndTime->setValue( mEndTime->value().addSecs( 3600 ) );
init();
}

Expand Down
88 changes: 56 additions & 32 deletions src/core/hydraulicNetwork/reoshydraulicstructure2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ ReosHydraulicStructure2D::ReosHydraulicStructure2D( const QPolygonF &domain, con
, mTopographyCollection( ReosTopographyCollection::createTopographyCollection( context.network()->gisEngine(), this ) )
, mMesh( ReosMesh::createMeshFrame( crs ) )
, mRoughnessStructure( new ReosRoughnessStructure( crs ) )
, mHydraulicNetworkContext( context )
{
init();
}
Expand All @@ -50,7 +49,6 @@ ReosHydraulicStructure2D::ReosHydraulicStructure2D(
, mTopographyCollection( ReosTopographyCollection::createTopographyCollection( encodedElement.getEncodedData( QStringLiteral( "topography-collection" ) ), context.network()->gisEngine(), this ) )
, mRoughnessStructure( new ReosRoughnessStructure( encodedElement.getEncodedData( QStringLiteral( "roughness-structure" ) ) ) )
, m3dMapSettings( encodedElement.getEncodedData( "3d-map-setings" ) )
, mHydraulicNetworkContext( context )
{
if ( encodedElement.hasEncodedData( QStringLiteral( "mesh-resolution-controller" ) ) )
mMeshResolutionController = new ReosMeshResolutionController( encodedElement.getEncodedData( QStringLiteral( "mesh-resolution-controller" ) ), this );
Expand All @@ -75,7 +73,7 @@ ReosHydraulicStructure2D::ReosHydraulicStructure2D(
if ( bc )
bc->attachStructure( this );
else
mNetWork->addElement( new ReosHydraulicStructureBoundaryCondition( this, bcId, mHydraulicNetworkContext ) );
mNetwork->addElement( new ReosHydraulicStructureBoundaryCondition( this, bcId, network()->context() ) );
}

const QList<ReosEncodedElement> encodedSimulations = encodedElement.getListEncodedData( QStringLiteral( "simulations" ) );
Expand Down Expand Up @@ -106,27 +104,25 @@ ReosHydraulicStructure2D::ReosHydraulicStructure2D(
QString currentActivatedVectorMeshDataset;
encodedElement.getData( QStringLiteral( "current-mesh-vector-dataset-id" ), currentActivatedVectorMeshDataset );
mMesh->activateVectorDataset( currentActivatedVectorMeshDataset );
}

ReosHydraulicNetworkContext ReosHydraulicStructure2D::hydraulicNetworkContext() const
{
return mHydraulicNetworkContext;
if ( !encodedElement.getData( QStringLiteral( "mesh-need-to-be-generated" ), mMeshNeedToBeGenerated ) )
mMeshNeedToBeGenerated = true;
}

void ReosHydraulicStructure2D::exportResultAsMesh( const QString &fileName ) const
{
mMesh->exportAsMesh( fileName );
mMesh->exportSimulationResults( mSimulationResults.value( mHydraulicNetworkContext.network()->currentScheme()->id() ), fileName );
mMesh->exportSimulationResults( mSimulationResults.value( network()->currentScheme()->id() ), fileName );
}

void ReosHydraulicStructure2D::exportResultAsMeshInGisProject( const QString &fileName, bool keepLayers )
{
const QFileInfo fileInfo( fileName );
const QString meshName = elementName()->value() + '-' + mHydraulicNetworkContext.network()->currentScheme()->schemeName()->value();
const QString meshName = elementName()->value() + '-' + network()->currentScheme()->schemeName()->value();
QString meshFileName = fileInfo.dir().filePath( meshName );

meshFileName = mMesh->exportAsMesh( meshFileName );
mMesh->exportSimulationResults( mSimulationResults.value( mHydraulicNetworkContext.network()->currentScheme()->id() ), meshFileName );
mMesh->exportSimulationResults( mSimulationResults.value( network()->currentScheme()->id() ), meshFileName );

QMap<QString, ReosEncodedElement> scalarSymbologies;
QMap<QString, ReosEncodedElement> vectorSymbologies;
Expand All @@ -147,11 +143,11 @@ void ReosHydraulicStructure2D::exportResultAsMeshInGisProject( const QString &fi
vectorSymbologies.insert( groupName, symbology );
}

ReosDuration timeStep = simulation( mHydraulicNetworkContext.network()->currentScheme() )->representative2DTimeStep();
ReosDuration timeStep = simulation( mNetwork->currentScheme() )->representative2DTimeStep();
timeStep.setAdaptedUnit();

mHydraulicNetworkContext.network()->gisEngine()->createProjectFile( fileName, keepLayers );
mHydraulicNetworkContext.network()->gisEngine()->addMeshLayerToExistingProject(
mNetwork->gisEngine()->createProjectFile( fileName, keepLayers );
mNetwork->gisEngine()->addMeshLayerToExistingProject(
fileName,
meshName,
meshFileName,
Expand Down Expand Up @@ -237,7 +233,9 @@ void ReosHydraulicStructure2D::encodeData( ReosEncodedElement &element, const Re
element.addData( QStringLiteral( "wire-frame-color" ), wireframeSettings.color );
element.addData( QStringLiteral( "wire-frame-width" ), wireframeSettings.width );

element.addEncodedData( "3d-map-setings", m3dMapSettings.encode() );
element.addEncodedData( QStringLiteral( "3d-map-setings" ), m3dMapSettings.encode() );

element.addData( QStringLiteral( "mesh-need-to-be-generated" ), mMeshNeedToBeGenerated );
}

ReosRoughnessStructure *ReosHydraulicStructure2D::roughnessStructure() const
Expand All @@ -247,7 +245,7 @@ ReosRoughnessStructure *ReosHydraulicStructure2D::roughnessStructure() const

QDir ReosHydraulicStructure2D::structureDirectory() const
{
return QDir( mHydraulicNetworkContext.projectPath() + '/' + mHydraulicNetworkContext.projectName() + QStringLiteral( "-hydr-struct" ) + '/' + directory() );
return QDir( mNetwork->context().projectPath() + '/' + mNetwork->context().projectName() + QStringLiteral( "-hydr-struct" ) + '/' + directory() );
}

QList<ReosHydraulicStructureBoundaryCondition *> ReosHydraulicStructure2D::boundaryConditions() const
Expand Down Expand Up @@ -375,13 +373,13 @@ void ReosHydraulicStructure2D::setTerrain3DSettings( const Reos3DTerrainSettings

void ReosHydraulicStructure2D::onBoundaryConditionAdded( const QString &bid )
{
mNetWork->addElement( new ReosHydraulicStructureBoundaryCondition( this, bid, mNetWork->context() ) );
mNetwork->addElement( new ReosHydraulicStructureBoundaryCondition( this, bid, mNetwork->context() ) );
emit boundaryChanged();
}

void ReosHydraulicStructure2D::onBoundaryConditionRemoved( const QString &bid )
{
mNetWork->removeElement( boundaryConditionNetWorkElement( bid ) );
mNetwork->removeElement( boundaryConditionNetWorkElement( bid ) );
emit boundaryChanged();
}

Expand Down Expand Up @@ -442,33 +440,57 @@ QString ReosHydraulicStructure2D::meshDatasetName( const QString &id ) const
return mMesh->datasetName( id );
}

ReosProcess *ReosHydraulicStructure2D::getPreparationProcessSimulation( const ReosCalculationContext &context )
ReosSimulationPreparationProcess *ReosHydraulicStructure2D::getPreparationProcessSimulation( const ReosCalculationContext &context, QString &error )
{
if ( mMeshNeedToBeGenerated )
{
error = tr( "The mesh need to be regenerated" );
return nullptr;
}

if ( !currentSimulation() )
{
error = tr( "Current simulation not defined" );
return nullptr;
}

if ( mNetwork->projectFileName().isEmpty() )
{
error = tr( "Project must be saved at least one time." );
return nullptr;
}

return new ReosSimulationPreparationProcess( this, currentSimulation(), context );
}

ReosProcess *ReosHydraulicStructure2D::getPreparationProcessSimulation( const ReosCalculationContext &context, const QDir &directory )
ReosSimulationPreparationProcess *ReosHydraulicStructure2D::getPreparationProcessSimulation( const ReosCalculationContext &context, QString &error, const QDir &directory )
{
if ( !currentSimulation() )
return nullptr;

std::unique_ptr<ReosSimulationPreparationProcess> ret( new ReosSimulationPreparationProcess( this, currentSimulation(), context ) );
std::unique_ptr<ReosSimulationPreparationProcess> ret( getPreparationProcessSimulation( context, error ) );
ret->setDestination( directory );
return ret.release();
}

ReosSimulationProcess *ReosHydraulicStructure2D::startSimulation( const ReosCalculationContext &context )
ReosSimulationProcess *ReosHydraulicStructure2D::startSimulation( const ReosCalculationContext &context, QString &error )
{
if ( mMeshNeedToBeGenerated )
{
error = tr( "The mesh need to be regenerated" );
return nullptr;
}

if ( !currentSimulation() )
{
error = tr( "Current simulation not defined" );
return nullptr;
}

QString schemeId = context.schemeId();

if ( processFromScheme( context.schemeId() ) )
{
error = tr( "A simulation with the current hydraulic scheme is currently running." );
return nullptr;
}

QPointer<ReosHydraulicSimulation> sim = currentSimulation();

Expand Down Expand Up @@ -519,7 +541,7 @@ bool ReosHydraulicStructure2D::hasSimulationRunning() const

bool ReosHydraulicStructure2D::hasResults() const
{
ReosHydraulicSchemeCollection *schemesCollection = mHydraulicNetworkContext.network()->hydraulicSchemeCollection();
ReosHydraulicSchemeCollection *schemesCollection = mNetwork->hydraulicSchemeCollection();
int schemeCount = schemesCollection->schemeCount();
for ( int i = 0; i < schemeCount; ++i )
{
Expand Down Expand Up @@ -605,7 +627,7 @@ void ReosHydraulicStructure2D::removeAllResults()
qDeleteAll( mSimulationResults );
mSimulationResults.clear();

ReosHydraulicSchemeCollection *schemesColection = mHydraulicNetworkContext.network()->hydraulicSchemeCollection();
ReosHydraulicSchemeCollection *schemesColection = network()->hydraulicSchemeCollection();
int schemeCount = schemesColection->schemeCount();
for ( int i = 0; i < schemeCount; ++i )
{
Expand Down Expand Up @@ -634,10 +656,10 @@ void ReosHydraulicStructure2D::removeResults( const ReosCalculationContext &cont

if ( mSimulationResults.contains( schemeId ) )
{
delete mSimulationResults.value( schemeId );
delete mSimulationResults.value( schemeId ); // replace be deleteLater() ?
mSimulationResults.remove( schemeId );

ReosHydraulicScheme *scheme = mNetWork->hydraulicSchemeCollection()->scheme( schemeId );
ReosHydraulicScheme *scheme = mNetwork->hydraulicSchemeCollection()->scheme( schemeId );
if ( scheme )
{
ReosHydraulicSimulation *sim = simulation( scheme );
Expand All @@ -653,6 +675,7 @@ void ReosHydraulicStructure2D::init()

connect( mPolylinesStructures.get(), &ReosDataObject::dataChanged, this, [this]
{
mMeshNeedToBeGenerated = true;
if ( mMeshGenerator->autoUpdateParameter()->value() )
generateMeshInPlace();
} );
Expand All @@ -662,6 +685,7 @@ void ReosHydraulicStructure2D::init()

connect( mMeshResolutionController, &ReosDataObject::dataChanged, this, [this]
{
mMeshNeedToBeGenerated = true;
if ( mMeshGenerator->autoUpdateParameter()->value() )
generateMeshInPlace();
} );
Expand Down Expand Up @@ -692,7 +716,7 @@ QString ReosHydraulicStructure2D::directory() const

ReosHydraulicStructureBoundaryCondition *ReosHydraulicStructure2D::boundaryConditionNetWorkElement( const QString boundaryId ) const
{
const QList<ReosHydraulicNetworkElement *> hydrElems = mNetWork->getElements( ReosHydraulicStructureBoundaryCondition::staticType() );
const QList<ReosHydraulicNetworkElement *> hydrElems = mNetwork->getElements( ReosHydraulicStructureBoundaryCondition::staticType() );
for ( ReosHydraulicNetworkElement *hydrElem : hydrElems )
{
ReosHydraulicStructureBoundaryCondition *bcElem = qobject_cast<ReosHydraulicStructureBoundaryCondition *>( hydrElem );
Expand Down Expand Up @@ -774,6 +798,7 @@ void ReosHydraulicStructure2D::onMeshGenerated( const ReosMeshFrameData &meshDat

mBoundaryVertices = meshData.boundaryVertices;
mHolesVertices = meshData.holesVertices;
mMeshNeedToBeGenerated = false;

emit meshGenerated();
emit dataChanged();
Expand All @@ -782,9 +807,8 @@ void ReosHydraulicStructure2D::onMeshGenerated( const ReosMeshFrameData &meshDat
void ReosHydraulicStructure2D::onSimulationFinished( ReosHydraulicSimulation *simulation, const QString &schemeId, ReosSimulationProcess *process, bool success )
{
if ( !simulation )
{
return;
}

simulation->saveSimulationResult( this, schemeId, process, success );

//! Now we can remove the old one if still presents
Expand Down Expand Up @@ -878,7 +902,7 @@ void ReosHydraulicStructure2D::setResultsOnStructure( ReosHydraulicSimulationRes

void ReosHydraulicStructure2D::updateResults( const QString &schemeId )
{
if ( mNetWork->calculationContext().schemeId() != schemeId )
if ( mNetwork->calculationContext().schemeId() != schemeId )
return;

if ( currentSimulation() && currentSimulation()->hasResult( this, schemeId ) )
Expand Down
13 changes: 5 additions & 8 deletions src/core/hydraulicNetwork/reoshydraulicstructure2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ class REOSCORE_EXPORT ReosHydraulicStructure2D : public ReosHydraulicNetworkElem
QStringList simulationNames() const;

//! Returns a process that prepare the current simulation, caller take ownership
ReosProcess *getPreparationProcessSimulation( const ReosCalculationContext &context );
ReosSimulationPreparationProcess *getPreparationProcessSimulation( const ReosCalculationContext &context, QString &error );

//! Returns a process that prepare the current simulation files in a specific \a diectory, caller take ownership
ReosProcess *getPreparationProcessSimulation( const ReosCalculationContext &context, const QDir &directory );
ReosSimulationPreparationProcess *getPreparationProcessSimulation( const ReosCalculationContext &context, QString &error, const QDir &directory );

//! Starts the current simulation, return true if the calculation is effectivly started and returns a pointer to the process
ReosSimulationProcess *startSimulation( const ReosCalculationContext &context );
//! Starts the current simulation, returns a pointer to the process
ReosSimulationProcess *startSimulation( const ReosCalculationContext &context, QString &error );

//! Returns a pointer to the current simulation process
ReosSimulationProcess *simulationProcess( const ReosCalculationContext &context ) const;
Expand Down Expand Up @@ -214,8 +214,6 @@ class REOSCORE_EXPORT ReosHydraulicStructure2D : public ReosHydraulicNetworkElem
//! Return the name of the current dataset
QString currentDatasetName() const;

ReosHydraulicNetworkContext hydraulicNetworkContext() const;

void exportResultAsMesh( const QString &fileName ) const;

void exportResultAsMeshInGisProject( const QString &fileName, bool keepLayers );
Expand Down Expand Up @@ -251,6 +249,7 @@ class REOSCORE_EXPORT ReosHydraulicStructure2D : public ReosHydraulicNetworkElem
std::unique_ptr<ReosRoughnessStructure > mRoughnessStructure;
QVector<QVector<int>> mBoundaryVertices;
QVector<QVector<QVector<int>>> mHolesVertices;
bool mMeshNeedToBeGenerated = true;

QList<ReosHydraulicSimulation *> mSimulations;

Expand All @@ -264,8 +263,6 @@ class REOSCORE_EXPORT ReosHydraulicStructure2D : public ReosHydraulicNetworkElem
Reos3DMapSettings m3dMapSettings;
Reos3DTerrainSettings m3dTerrainSettings;

ReosHydraulicNetworkContext mHydraulicNetworkContext;

void init();
void generateMeshInPlace();
QString directory() const;
Expand Down
Loading

0 comments on commit 6dd1b18

Please sign in to comment.