///////////////////////////////////////////////////////////////////////////////// // // Copyright (C) 2011- Statoil ASA // Copyright (C) 2013- Ceetron Solutions AS // Copyright (C) 2011-2012 Ceetron AS // // ResInsight is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. // // See the GNU General Public License at // for more details. // ///////////////////////////////////////////////////////////////////////////////// #include "RimWellPathCollection.h" #include "Cloud/RiaOsduConnector.h" #include "RiaColorTables.h" #include "RiaDateStringParser.h" #include "RiaGuiApplication.h" #include "RiaLogging.h" #include "RiaPreferences.h" #include "RiaQStringFormatter.h" #include "RiaTextStringTools.h" #include "RiaWellNameComparer.h" #include "RiuMessageDialog.h" #include "RifOsduWellLogReader.h" #include "RifOsduWellPathReader.h" #include "RifWellPathImporter.h" #include "RigEclipseCaseData.h" #include "RigMainGrid.h" #include "Well/RigOsduWellLogData.h" #include "Well/RigWellPath.h" #include "Formations/RimWellFormationsCollection.h" #include "Formations/RimWellFormationsFile.h" #include "RimEclipseCase.h" #include "RimEclipseCaseCollection.h" #include "RimEclipseCaseTools.h" #include "RimEclipseView.h" #include "RimFileWellPath.h" #include "RimModeledWellPath.h" #include "RimMswSegmentCollection.h" #include "RimOilField.h" #include "RimOsduWellLog.h" #include "RimOsduWellPath.h" #include "RimPerforationCollection.h" #include "RimProject.h" #include "RimStimPlanModel.h" #include "RimStimPlanModelCollection.h" #include "RimWellLogLasFile.h" #include "RimWellMeasurementCollection.h" #include "RimWellPath.h" #include "RimWellPathCompletionSettings.h" #include "RimWellPathCompletions.h" #include "RimWellPathTieIn.h" #include "WellEvents/RimWellEventTimeline.h" #include "RiuMainWindow.h" #include "cafTreeNode.h" // TODO: Move to caf #include "Riu3DMainWindowTools.h" #include "cafDataLoadController.h" #include "cafDataLoader.h" #include "cafPdmFieldScriptingCapability.h" #include "cafPdmObjectScriptingCapability.h" #include "cafPdmUiEditorHandle.h" #include "cafPdmUiTreeOrdering.h" #include "cafProgressInfo.h" #include "cvfBoundingBox.h" #include #include #include #include #include #include #include #include namespace caf { template <> void RimWellPathCollection::WellVisibilityEnum::setUp() { addItem( RimWellPathCollection::FORCE_ALL_OFF, "FORCE_ALL_OFF", "Off" ); addItem( RimWellPathCollection::ALL_ON, "ALL_ON", "Individual" ); addItem( RimWellPathCollection::FORCE_ALL_ON, "FORCE_ALL_ON", "On" ); } template <> void RimWellPathCollection::MswGroupingEnum::setUp() { addItem( RimWellPathCollection::MswGroupingMode::USE_PREFERENCES, "USE_PREFERENCES", "Use Preferences" ); addItem( RimWellPathCollection::MswGroupingMode::CUSTOM, "CUSTOM", "Custom" ); addItem( RimWellPathCollection::MswGroupingMode::DISABLED, "DISABLED", "Disabled" ); setDefault( RimWellPathCollection::MswGroupingMode::USE_PREFERENCES ); } } // namespace caf CAF_PDM_SOURCE_INIT( RimWellPathCollection, "WellPaths" ); //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- RimWellPathCollection::RimWellPathCollection() { CAF_PDM_InitScriptableObjectWithNameAndComment( "Wells", ":/WellCollection.png", "", "", "WellPathCollection", "Collection of Well Paths" ); CAF_PDM_InitField( &isActive, "Active", true, "Active" ); isActive.uiCapability()->setUiHidden( true ); CAF_PDM_InitField( &showWellPathLabel, "ShowWellPathLabel", true, "Show Well Path Labels" ); cvf::Color3f defWellLabelColor = RiaPreferences::current()->defaultWellLabelColor(); CAF_PDM_InitField( &wellPathLabelColor, "WellPathLabelColor", defWellLabelColor, "Well label color" ); CAF_PDM_InitField( &wellPathVisibility, "GlobalWellPathVisibility", WellVisibilityEnum( ALL_ON ), "Global Well Path Visibility" ); CAF_PDM_InitField( &wellPathRadiusScaleFactor, "WellPathRadiusScale", 0.1, "Well Path Radius Scale" ); CAF_PDM_InitField( &wellPathCrossSectionVertexCount, "WellPathVertexCount", 12, "Well Path Vertex Count" ); wellPathCrossSectionVertexCount.xmlCapability()->disableIO(); wellPathCrossSectionVertexCount.uiCapability()->setUiHidden( true ); CAF_PDM_InitField( &wellPathClip, "WellPathClip", true, "Clip Well Paths" ); CAF_PDM_InitField( &wellPathClipZDistance, "WellPathClipZDistance", 100, "Well Path Clipping Depth Distance" ); CAF_PDM_InitScriptableFieldNoDefault( &m_wellPaths, "WellPaths", "Well Paths" ); m_wellPaths.uiCapability()->setUiTreeChildrenHidden( true ); CAF_PDM_InitFieldNoDefault( &m_wellMeasurements, "WellMeasurements", "Measurements" ); m_wellMeasurements = new RimWellMeasurementCollection; CAF_PDM_InitScriptableFieldNoDefault( &m_eventTimeline, "EventTimeline", "Event Timeline" ); m_eventTimeline = new RimWellEventTimeline; CAF_PDM_InitFieldNoDefault( &m_wellPathNodes, "WellPathNodes", "Well Path Nodes" ); m_wellPathNodes.xmlCapability()->disableIO(); CAF_PDM_InitFieldNoDefault( &m_mswNameGrouping, "MswWellNameGrouping", "Grouping" ); CAF_PDM_InitFieldNoDefault( &m_mswNameGroupingPattern, "MswWellNameGroupingPattern", "Grouping Pattern" ); CAF_PDM_InitFieldNoDefault( &m_mswEclipseCase, "MswEclipseCase", "Eclipse Case" ); CAF_PDM_InitField( &m_mswShowBands, "MswShowBands", true, "Show Bands" ); CAF_PDM_InitField( &m_mswAutoUpdateSegments, "MswAutoUpdateSegments", true, "Auto Update Segments" ); m_wellPathImporter = std::make_unique(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- RimWellPathCollection::~RimWellPathCollection() { } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- RimWellPathCollection* RimWellPathCollection::instance() { RimProject* proj = RimProject::current(); if ( proj && proj->activeOilField() ) { return proj->activeOilField()->wellPathCollection(); } return nullptr; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::fieldChangedByUi( const caf::PdmFieldHandle* changedField, const QVariant& oldValue, const QVariant& newValue ) { scheduleRedrawAffectedViews(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- QList RimWellPathCollection::calculateValueOptions( const caf::PdmFieldHandle* fieldNeedingOptions ) { QList options; if ( fieldNeedingOptions == &m_mswEclipseCase ) { options.push_back( caf::PdmOptionItemInfo( "None", nullptr ) ); auto cases = RimEclipseCaseTools::eclipseCases(); for ( auto* c : cases ) { options.push_back( caf::PdmOptionItemInfo( c->caseUserDescription(), c, false, c->uiIconProvider() ) ); } } return options; } //-------------------------------------------------------------------------------------------------- /// Read files containing well path data, or create geometry based on the targets //-------------------------------------------------------------------------------------------------- bool RimWellPathCollection::loadDataAndUpdate() { auto hasOsduData = []( const std::vector& wellPaths ) -> bool { for ( RimWellPath* wellPath : wellPaths ) { if ( dynamic_cast( wellPath ) ) { return true; } } return false; }; auto countWellLogs = []( const std::vector& wellPaths ) -> size_t { size_t count = 0; for ( RimWellPath* wellPath : wellPaths ) { count += wellPath->wellLogs().size(); } return count; }; caf::ProgressInfoEventProcessingBlocker blocker; caf::ProgressInfo progress( allWellPaths().size() + countWellLogs( allWellPaths() ) + 2, "Reading well paths from file", false, true ); readWellPathFormationFiles(); progress.incrementProgress(); if ( hasOsduData( allWellPaths() ) ) { if ( auto osduConnector = RiaApplication::instance()->makeOsduConnector() ) { osduConnector->requestTokenWithCancelButton(); } } caf::DataLoadController* dataLoadController = caf::DataLoadController::instance(); const QString wellPathGeometryKeyword = "WELL_PATH_GEOMETRY"; const QString wellLogKeyword = "WELL_LOG"; progress.setProgressDescription( QString( "Reading well path geometry." ) ); for ( RimWellPath* wellPath : allWellPaths() ) { dataLoadController->loadData( *wellPath, wellPathGeometryKeyword, progress ); } dataLoadController->blockUntilDone( wellPathGeometryKeyword ); if ( progress.isCancelled() ) { return false; } progress.setProgressDescription( QString( "Reading well logs." ) ); for ( RimWellPath* wellPath : allWellPaths() ) { for ( RimWellLog* wellLog : wellPath->wellLogs() ) { dataLoadController->loadData( *wellLog, wellLogKeyword, progress ); } } dataLoadController->blockUntilDone( wellLogKeyword ); progress.setProgressDescription( QString( "Reading additional data." ) ); for ( RimWellPath* wellPath : allWellPaths() ) { RimStimPlanModelCollection* stimPlanModelCollection = wellPath->stimPlanModelCollection(); if ( stimPlanModelCollection ) { for ( RimStimPlanModel* stimPlanModel : stimPlanModelCollection->allStimPlanModels() ) { stimPlanModel->loadDataAndUpdate(); } } } progress.incrementProgress(); rebuildWellPathNodes(); sortWellsByName(); return true; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::vector RimWellPathCollection::addWellPaths( QStringList filePaths, QStringList* errorMessages ) { CAF_ASSERT( errorMessages ); std::vector wellPathArray; for ( const QString& filePath : filePaths ) { std::expected, QString> result = addWellPathsForFile( filePath ); if ( result ) { wellPathArray.insert( wellPathArray.end(), result.value().begin(), result.value().end() ); } else if ( errorMessages ) { errorMessages->append( result.error() ); } } std::vector addedWellPaths = readAndAddWellPaths( wellPathArray ); CAF_ASSERT( wellPathArray.empty() ); scheduleRedrawAffectedViews(); updateAllRequiredEditors(); return addedWellPaths; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::expected, QString> RimWellPathCollection::addWellPathsForFile( const QString& filePath ) { auto isAlreadyLoaded = []( const QString& filePath, const std::vector& wellPaths ) -> bool { for ( const auto& wellPath : wellPaths ) { if ( auto* fWPath = dynamic_cast( wellPath ) ) { QFile f1; f1.setFileName( filePath ); QFile f2; f2.setFileName( fWPath->filePath() ); if ( f1.fileName() == f2.fileName() ) return true; } } return false; }; // Check if this file is already open bool isJsonFile = filePath.endsWith( "json", Qt::CaseInsensitive ); if ( isJsonFile && isAlreadyLoaded( filePath, m_wellPaths.childrenByType() ) ) { return std::unexpected( QString( "%1 is already loaded" ).arg( filePath ) ); } std::vector wellPaths; if ( isJsonFile ) { auto* wellPath = new RimFileWellPath(); wellPath->setFilepath( filePath ); wellPaths.push_back( wellPath ); } else { // Create Well path objects for all the paths in the assumed ascii file size_t wellPathCount = m_wellPathImporter->wellDataCount( filePath ); for ( size_t i = 0; i < wellPathCount; ++i ) { auto* wellPath = new RimFileWellPath(); wellPath->setFilepath( filePath ); wellPath->setWellPathIndexInFile( static_cast( i ) ); wellPaths.push_back( wellPath ); } } return wellPaths; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::addWellPath( RimWellPath* wellPath, bool assignUnit ) { if ( assignUnit ) assignUnitSystem( wellPath ); m_wellPaths.push_back( wellPath ); rebuildWellPathNodes(); } //-------------------------------------------------------------------------------------------------- /// Single place where the unit system of a new well path is decided //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::assignUnitSystem( RimWellPath* wellPath ) { if ( !wellPath ) return; using Unit = RiaDefines::EclipseUnitSystem; if ( dynamic_cast( wellPath ) ) { // Files have no unit information, so use the case the well path overlaps. This is unknown if there is no overlap. wellPath->setUnitSystem( findUnitSystemForWellPath( wellPath ) ); return; } Unit unitSystem = Unit::UNITS_UNKNOWN; if ( wellPath->wellPathTieIn() && wellPath->wellPathTieIn()->parentWell() ) { // Laterals use the same unit system as their parent unitSystem = wellPath->wellPathTieIn()->parentWell()->unitSystem(); } else if ( auto* project = firstAncestorOrThisOfType() ) { unitSystem = project->commonUnitSystemForAllCases(); } // Keep the current unit system if it can not be determined if ( unitSystem != Unit::UNITS_UNKNOWN ) wellPath->setUnitSystem( unitSystem ); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::vector RimWellPathCollection::allWellPaths() const { return m_wellPaths.childrenByType(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- cvf::BoundingBox RimWellPathCollection::wellPathsBoundingBox() const { cvf::BoundingBox bb; for ( const auto wellPath : allWellPaths() ) { if ( !wellPath || !wellPath->wellPathGeometry() ) continue; for ( const auto& point : wellPath->wellPathGeometry()->wellPathPoints() ) bb.add( point ); } return bb; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::vector RimWellPathCollection::readAndAddWellPaths( std::vector& wellPathArray ) { caf::ProgressInfo progress( wellPathArray.size(), "Reading well paths from file" ); // Keep track of the well paths that were actually added. std::vector addedWellPaths; for ( RimFileWellPath* wellPath : wellPathArray ) { QString errorMessage; if ( !wellPath->readWellPathFile( &errorMessage, m_wellPathImporter.get(), true ) ) { RiaLogging::warning( errorMessage.toStdString() ); } progress.setProgressDescription( QString( "Reading file %1" ).arg( wellPath->name() ) ); wellPath->setWellPathColor( RiaColorTables::wellPathsPaletteColors().cycledColor3f( m_wellPaths.size() ) ); addWellPath( wellPath ); addedWellPaths.push_back( wellPath ); progress.incrementProgress(); } wellPathArray.clear(); // This should not be used again. We may have deleted items groupWellPaths( allWellPaths() ); sortWellsByName(); rebuildWellPathNodes(); return addedWellPaths; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::addWellPaths( const std::vector incomingWellPaths ) { for ( const auto& wellPath : incomingWellPaths ) { addWellPath( wellPath ); } groupWellPaths( allWellPaths() ); sortWellsByName(); rebuildWellPathNodes(); updateAllRequiredEditors(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::vector RimWellPathCollection::addWellLogs( const QStringList& filePaths, QStringList* errorMessages ) { CAF_ASSERT( errorMessages ); std::vector logFileInfos; for ( const QString& filePath : filePaths ) { QString errorMessage; RimWellLogLasFile* logFileInfo = RimWellLogLasFile::readWellLogFile( filePath, &errorMessage ); if ( !errorMessage.isEmpty() ) { errorMessages->push_back( errorMessage ); } if ( logFileInfo ) { RimWellPath* wellPath = tryFindMatchingWellPath( logFileInfo->wellName() ); if ( !wellPath ) { wellPath = new RimWellPath(); addWellPath( wellPath ); } wellPath->addWellLog( logFileInfo ); logFileInfos.push_back( logFileInfo ); } } sortWellsByName(); updateAllRequiredEditors(); return logFileInfos; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::addWellLog( RimWellLog* wellLog, RimWellPath* wellPath ) { wellPath->addWellLog( wellLog ); sortWellsByName(); updateAllRequiredEditors(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::addWellPathFormations( const QStringList& filePaths ) { auto oilField = RimProject::current()->activeOilField(); if ( !oilField ) return; QString outputMessage = "Well Picks Import\n"; outputMessage += "-----------------------------------------------\n"; outputMessage += "Well Name \tDetected Well Path \tCount\n"; // The collection is deletable from the project tree if ( !oilField->wellFormationsCollection() ) oilField->wellFormationsCollection = new RimWellFormationsCollection(); bool fileReadSuccess = false; QString errorMessage; for ( const QString& filePath : filePaths ) { auto file = oilField->wellFormationsCollection->findOrCreate( filePath ); if ( !file ) { errorMessage += "\nError in: " + filePath + "\n\t" + file.error(); continue; } RimWellFormationsFile* wellFormationsFile = *file; for ( const QString& wellName : wellFormationsFile->wellNames() ) { auto formations = wellFormationsFile->formationsForWell( wellName ); if ( !formations ) continue; fileReadSuccess = true; RimWellPath* wellPath = tryFindMatchingWellPath( wellName ); if ( !wellPath ) { wellPath = new RimWellPath(); wellPath->setName( wellName ); addWellPath( wellPath ); RiaLogging::info( std::format( "Created new well: {}", wellPath->name() ) ); } wellPath->setWellFormationsFile( wellFormationsFile, wellName ); QString wellFormationsCount = QString( "%1" ).arg( formations->formationNamesCount() ); outputMessage += wellName + "\t\t"; outputMessage += wellPath->name() + " \t\t\t"; outputMessage += wellFormationsCount + "\n"; } } outputMessage += "-----------------------------------------------"; if ( fileReadSuccess ) { RiuMessageDialog::showError( Riu3DMainWindowTools::mainWindowWidget(), "Well Picks Import", outputMessage ); } if ( !errorMessage.isEmpty() ) { RiuMessageDialog::showError( Riu3DMainWindowTools::mainWindowWidget(), "Well Picks Import", errorMessage ); } sortWellsByName(); updateAllRequiredEditors(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering& uiOrdering ) { caf::PdmUiGroup* wellHeadGroup = uiOrdering.addNewGroup( "Well labels" ); wellHeadGroup->add( &showWellPathLabel ); wellHeadGroup->add( &wellPathLabelColor ); caf::PdmUiGroup* wellPipe = uiOrdering.addNewGroup( "Well pipe" ); wellPipe->add( &wellPathVisibility ); wellPipe->add( &wellPathRadiusScaleFactor ); caf::PdmUiGroup* advancedGroup = uiOrdering.addNewGroup( "Clipping" ); advancedGroup->add( &wellPathClip ); advancedGroup->add( &wellPathClipZDistance ); caf::PdmUiGroup* wellGrouping = uiOrdering.addNewGroup( "MSW Grouping" ); wellGrouping->add( &m_mswNameGrouping ); if ( m_mswNameGrouping() != MswGroupingMode::DISABLED ) { if ( m_mswNameGrouping() == MswGroupingMode::USE_PREFERENCES ) { m_mswNameGroupingPattern = RiaPreferences::current()->multiLateralWellNamePattern(); } wellGrouping->add( &m_mswNameGroupingPattern ); m_mswNameGroupingPattern.uiCapability()->setUiReadOnly( m_mswNameGrouping() == MswGroupingMode::USE_PREFERENCES ); } caf::PdmUiGroup* mswSegmentGroup = uiOrdering.addNewGroup( "MSW Segment Visualization" ); mswSegmentGroup->add( &m_mswEclipseCase ); mswSegmentGroup->add( &m_mswAutoUpdateSegments ); if ( !m_mswAutoUpdateSegments() ) { mswSegmentGroup->addNewButton( "Update Segments", [this]() { updateMswSegments(); } ); } mswSegmentGroup->add( &m_mswShowBands ); uiOrdering.skipRemainingFields(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::defineUiTreeOrdering( caf::PdmUiTreeOrdering& uiTreeOrdering, QString uiConfigName /*= ""*/ ) { if ( !m_wellMeasurements->isEmpty() ) { uiTreeOrdering.add( &m_wellMeasurements ); } if ( m_eventTimeline() && m_eventTimeline()->eventCount() > 0 ) { uiTreeOrdering.add( &m_eventTimeline ); } for ( const auto& wellPathNode : m_wellPathNodes() ) { RimWellPathCollection::buildUiTreeOrdering( wellPathNode, &uiTreeOrdering, uiConfigName ); } uiTreeOrdering.skipRemainingChildren( true ); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- caf::PdmFieldHandle* RimWellPathCollection::objectToggleField() { return &isActive; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::updateMswSegmentsForObject( caf::PdmObject* changedObject ) { if ( !m_mswAutoUpdateSegments() ) return; if ( !changedObject ) return; auto wellPath = changedObject->firstAncestorOfType(); if ( !wellPath ) return; auto topLevelWell = wellPath->topLevelWellPath(); if ( !topLevelWell ) return; for ( const auto& wp : m_wellPaths ) { if ( !wp || wp->topLevelWellPath() != topLevelWell ) continue; auto* completions = wp->completions(); if ( !completions ) continue; auto* segmentCollection = completions->mswSegmentCollection(); if ( !segmentCollection ) continue; segmentCollection->clearSegments(); } RimMswSegmentCollection::updateSegments( topLevelWell, m_mswEclipseCase() ); if ( RimProject* project = RimProject::current() ) { project->scheduleCreateDisplayModelAndRedrawAllViews(); } } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::updateMswSegments() { std::set topLevelWells; for ( const auto& wellPath : m_wellPaths ) { if ( !wellPath ) continue; auto* completions = wellPath->completions(); if ( !completions ) continue; auto* segmentCollection = completions->mswSegmentCollection(); if ( !segmentCollection ) continue; segmentCollection->clearSegments(); topLevelWells.insert( wellPath->topLevelWellPath() ); } // Update segments for all top level wells, which will update the segments for all connected well paths for ( RimWellPath* topLevelWell : topLevelWells ) { RimMswSegmentCollection::updateSegments( topLevelWell, m_mswEclipseCase() ); } if ( RimProject* project = RimProject::current() ) { project->scheduleCreateDisplayModelAndRedrawAllViews(); } } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- bool RimWellPathCollection::showMswSegmentBands() const { return m_mswShowBands(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::scheduleRedrawAffectedViews() { RimProject* proj = RimProject::current(); if ( proj ) proj->reloadCompletionTypeResultsInAllViews(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- bool RimWellPathCollection::anyWellsContainingPerforationIntervals() const { for ( const auto& wellPath : m_wellPaths ) { if ( !wellPath->perforationIntervalCollection()->perforations().empty() ) return true; } return false; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- size_t RimWellPathCollection::modelledWellPathCount() const { size_t count = 0; for ( const auto& wellPath : m_wellPaths ) { if ( dynamic_cast( wellPath.p() ) ) { count++; } } return count; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- RimWellPath* RimWellPathCollection::wellPathByName( const QString& wellPathName ) const { for ( const auto& wellPath : m_wellPaths ) { if ( wellPath->name() == wellPathName ) { return wellPath; } } return nullptr; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- RimWellPath* RimWellPathCollection::tryFindMatchingWellPath( const QString& wellName ) const { QString matchedWellPath = RiaWellNameComparer::tryFindMatchingWellPath( wellName ); return !matchedWellPath.isEmpty() ? wellPathByName( matchedWellPath ) : nullptr; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::deleteAllWellPaths() { m_wellPaths.deleteChildren(); m_wellPathNodes.deleteChildren(); m_wellPathImporter->clear(); updateAllRequiredEditors(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::deleteWell( RimWellPath* wellPath ) { removeWellPath( wellPath ); m_wellPaths.removeChild( wellPath ); delete wellPath; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::setMswWellPattern( const QString& pattern ) { if ( pattern.isEmpty() ) { m_mswNameGrouping = MswGroupingMode::DISABLED; m_mswNameGroupingPattern = ""; } else { m_mswNameGrouping = MswGroupingMode::CUSTOM; m_mswNameGroupingPattern = pattern; } } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::groupWellPaths( const std::vector& wellPaths ) { const auto& rootWells = wellPathsForWellNameStem( wellPaths ); for ( const auto& [groupName, wellPathsInGroup] : rootWells ) { if ( groupName == unGroupedText() ) continue; for ( const auto& wellPathToConnect : wellPathsInGroup ) { // Assign the group names as well name for export wellPathToConnect->completionSettings()->setWellNameForExport( groupName ); const auto wellPathGeometry = wellPathToConnect->wellPathGeometry(); if ( wellPathGeometry ) { std::map sharedWellPathLengths; for ( const auto& otherWellPath : wellPathsInGroup ) { if ( otherWellPath == wellPathToConnect ) continue; if ( otherWellPath && otherWellPath->wellPathGeometry() ) { const double sharedWellPathLength = otherWellPath->wellPathGeometry()->identicalTubeLength( *wellPathGeometry ); const double eps = 1.0e-2; if ( sharedWellPathLength > eps ) { sharedWellPathLengths[otherWellPath] = sharedWellPathLength; } } } RimWellPath* longestSharedWellPath = nullptr; double longestSharedWellPathLength = 0.0; for ( const auto& [wellPathCandidate, sharedWellPathLength] : sharedWellPathLengths ) { if ( wellPathCandidate ) { const double distanceDifference = fabs( sharedWellPathLength - longestSharedWellPathLength ); const double differenceThreshold = 1.0; if ( longestSharedWellPath && ( distanceDifference < differenceThreshold ) ) { // If the main well is named WELL_A, each side steps of a MSW can be given the following names // WELL_A_Y1 // WELL_A_Y2 // WELL_A_Y3 // // If Y3 has equal shared geometry with both Y2 and Y1, make sure that Y3 is connected to Y1. if ( wellPathCandidate->name() < longestSharedWellPath->name() ) { longestSharedWellPath = wellPathCandidate; longestSharedWellPathLength = sharedWellPathLength; } } else if ( sharedWellPathLength > longestSharedWellPathLength ) { longestSharedWellPath = wellPathCandidate; longestSharedWellPathLength = sharedWellPathLength; } } } if ( longestSharedWellPath ) { if ( wellPathToConnect->name() > longestSharedWellPath->name() ) { wellPathToConnect->connectWellPaths( longestSharedWellPath, longestSharedWellPathLength ); } else { longestSharedWellPath->connectWellPaths( wellPathToConnect, longestSharedWellPathLength ); } } } } } } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------- /// Resolves the cached formations for each well path from its linked RimWellFormationsFile. Files /// referenced by a project-level formations collection entry are read lazily by that entry itself. //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::readWellPathFormationFiles() { for ( const auto& wellPath : m_wellPaths ) { wellPath->refreshFormationsFromFile(); } } //-------------------------------------------------------------------------------------------------- /// Reloads every well formations file referenced by a well path from disk, then re-resolves each /// well path's cached formations. //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::reloadAllWellPathFormations() { std::set filesToReload; for ( const auto& wellPath : m_wellPaths ) { if ( auto file = wellPath->wellFormationsFile() ) { filesToReload.insert( file ); } } caf::ProgressInfo progress( filesToReload.size(), "Reloading well picks from file" ); for ( auto file : filesToReload ) { progress.setProgressDescription( QString( "Reloading %1" ).arg( file->shortName() ) ); if ( auto result = file->reload(); !result ) { RiuMessageDialog::showError( Riu3DMainWindowTools::mainWindowWidget(), "File open error", result.error() ); } progress.incrementProgress(); } readWellPathFormationFiles(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::removeWellPath( RimWellPath* wellToRemove ) { auto* fileWellToRemove = dynamic_cast( wellToRemove ); if ( fileWellToRemove ) { bool isFilePathUsed = false; for ( const auto& well : m_wellPaths ) { auto fileWell = dynamic_cast( well.p() ); if ( fileWell == fileWellToRemove ) continue; if ( fileWell && fileWell->filePath() == fileWellToRemove->filePath() ) { isFilePathUsed = true; break; } } if ( !isFilePathUsed ) { // One file can have multiple well paths // If no other well paths are referencing the filepath, remove cached data from the file reader m_wellPathImporter->removeFilePath( fileWellToRemove->filePath() ); } } updateAllRequiredEditors(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- bool lessWellPath( const caf::PdmPointer& w1, const caf::PdmPointer& w2 ) { if ( w1.notNull() && w2.notNull() ) { // Use QCollator to sort integer values in addition to text QCollator collator; collator.setNumericMode( true ); return collator.compare( w1->name(), w2->name() ) < 0; } if ( w1.notNull() ) return true; return false; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::sortWellsByName() { std::sort( m_wellPaths.begin(), m_wellPaths.end(), lessWellPath ); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- caf::AppEnum RimWellPathCollection::findUnitSystemForWellPath( const RimWellPath* wellPath ) { if ( !wellPath || !wellPath->wellPathGeometry() ) return RiaDefines::EclipseUnitSystem::UNITS_UNKNOWN; RimProject* project = RimProject::current(); if ( project->activeOilField()->analysisModels->cases.empty() ) { return RiaDefines::EclipseUnitSystem::UNITS_UNKNOWN; } const RigEclipseCaseData* eclipseCaseData = project->activeOilField()->analysisModels->cases()[0]->eclipseCaseData(); if ( !eclipseCaseData || !eclipseCaseData->mainGrid() ) { return RiaDefines::EclipseUnitSystem::UNITS_UNKNOWN; } cvf::BoundingBox caseBoundingBox = eclipseCaseData->mainGrid()->boundingBox(); cvf::BoundingBox wellPathBoundingBox; for ( const auto& wellPathPoint : wellPath->wellPathGeometry()->wellPathPoints() ) { wellPathBoundingBox.add( wellPathPoint ); } if ( wellPathBoundingBox.isValid() && caseBoundingBox.isValid() && caseBoundingBox.intersects( wellPathBoundingBox ) ) { return eclipseCaseData->unitsType(); } return RiaDefines::EclipseUnitSystem::UNITS_UNKNOWN; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::rebuildWellPathNodes() { m_wellPathNodes.deleteChildren(); std::map> rootWells = wellPathsForWellNameStem( m_wellPaths.childrenByType() ); for ( auto [groupName, wellPathGroup] : rootWells ) { if ( groupName == unGroupedText() ) { // For single wells, create well paths directly in the well collection folder for ( auto wellPath : wellPathGroup ) { if ( !wellPath ) continue; if ( !wellPath->isTopLevelWellPath() ) continue; auto node = addWellToWellNode( nullptr, wellPath ); m_wellPathNodes.push_back( node ); } } else { // Create a group node with group name and put related wells into this group auto rootNode = new cafNamedTreeNode; rootNode->setName( groupName ); rootNode->setIcon( ":/WellPathGroup.svg" ); m_wellPathNodes.push_back( rootNode ); for ( auto wellPath : wellPathsWithNoParent( wellPathGroup ) ) { if ( !wellPath ) continue; addWellToWellNode( rootNode, wellPath ); } } } } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- cafTreeNode* RimWellPathCollection::addWellToWellNode( cafTreeNode* parent, RimWellPath* wellPath ) { if ( wellPath == nullptr ) return nullptr; std::vector wellPaths = connectedWellPathLaterals( wellPath ); auto node = new cafObjectReferenceTreeNode; node->setReferencedObject( wellPath ); if ( parent ) parent->addChild( node ); for ( auto w : wellPaths ) { addWellToWellNode( node, w ); } return node; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::vector RimWellPathCollection::wellPathsWithNoParent( const std::vector& sourceWellPaths ) const { std::vector wellPaths; for ( const auto& w : sourceWellPaths ) { if ( !w ) continue; if ( w->isTopLevelWellPath() ) { wellPaths.push_back( w ); } } return wellPaths; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::vector RimWellPathCollection::connectedWellPathLaterals( const RimWellPath* parentWellPath ) const { if ( !parentWellPath ) return {}; std::vector wellPaths; for ( const auto& w : m_wellPaths ) { if ( !w ) continue; if ( w->wellPathTieIn() && ( w->wellPathTieIn()->parentWell() == parentWellPath ) ) { wellPaths.push_back( w ); } } return wellPaths; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::map> RimWellPathCollection::wellPathsForWellNameStem( const std::vector& sourceWellPaths ) const { auto getMultiLateralWellPathPattern = [this]() -> QString { if ( m_mswNameGrouping() == MswGroupingMode::DISABLED ) { return ""; } else if ( m_mswNameGrouping() == MswGroupingMode::USE_PREFERENCES ) { return RiaPreferences::current()->multiLateralWellNamePattern(); } else { return m_mswNameGroupingPattern(); } }; QString multiLateralWellPathPattern = getMultiLateralWellPathPattern(); QString regexPattern = QRegularExpression::wildcardToRegularExpression( multiLateralWellPathPattern ); QRegularExpression re( regexPattern, QRegularExpression::CaseInsensitiveOption ); std::map> rootWells; for ( auto wellPath : sourceWellPaths ) { QString name = wellPath->name(); if ( re.match( name ).hasMatch() ) { int indexOfLateralStart = name.indexOf( 'Y' ); if ( indexOfLateralStart > 0 ) { QString rootWellName = wellPath->name().left( indexOfLateralStart ); rootWells[rootWellName].push_back( wellPath ); continue; } } rootWells[RimWellPathCollection::unGroupedText()].push_back( wellPath ); } for ( auto [groupName, wellPaths] : rootWells ) { std::sort( wellPaths.begin(), wellPaths.end(), lessWellPath ); } return rootWells; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::buildUiTreeOrdering( cafTreeNode* treeNode, caf::PdmUiTreeOrdering* parentUiTreeNode, const QString& uiConfigName ) { CAF_ASSERT( treeNode ); CAF_ASSERT( parentUiTreeNode ); if ( auto obj = treeNode->referencedObject() ) { // Create the standard uiTreeOrdering with no relationship to parent using the helper function in // PdmUiObjectHandle. This will ensure that any child objects will be created correctly including any // recursive children. If a PdmUiTreeOrdering is created directly based on the PdmObject, andy child objects // must be added manually. auto uiTreeNode = obj->uiCapability()->uiTreeOrdering( uiConfigName ); parentUiTreeNode->appendChild( uiTreeNode ); // Build additional child nodes recursively for ( auto childNode : treeNode->childNodes() ) { buildUiTreeOrdering( childNode, uiTreeNode, uiConfigName ); } } else { // If no referenced object is attached, fallback to the node. Not sure if this is the best solution auto uiTreeNode = parentUiTreeNode->add( treeNode ); // Build additional child nodes recursively for ( auto childNode : treeNode->childNodes() ) { buildUiTreeOrdering( childNode, uiTreeNode, uiConfigName ); } } } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- QString RimWellPathCollection::unGroupedText() { return "UnGrouped"; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- RimWellMeasurementCollection* RimWellPathCollection::measurementCollection() { return m_wellMeasurements; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- const RimWellMeasurementCollection* RimWellPathCollection::measurementCollection() const { return m_wellMeasurements; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- RimWellEventTimeline* RimWellPathCollection::eventTimeline() { return m_eventTimeline; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- const RimWellEventTimeline* RimWellPathCollection::eventTimeline() const { return m_eventTimeline; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::onChildDeleted( caf::PdmChildArrayFieldHandle* childArray, std::vector& referringObjects ) { rebuildWellPathNodes(); scheduleRedrawAffectedViews(); uiCapability()->updateConnectedEditors(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::onChildAdded( caf::PdmFieldHandle* containerForNewObject ) { // Well paths created from Python are added directly to the child field if ( containerForNewObject == &m_wellPaths && !m_wellPaths.empty() ) { assignUnitSystem( m_wellPaths[m_wellPaths.size() - 1] ); } rebuildWellPathNodes(); scheduleRedrawAffectedViews(); uiCapability()->updateConnectedEditors(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::pair, QString> RimWellPathCollection::loadWellPathGeometryFromOsdu( RiaOsduConnector* osduConnector, const QString& wellboreTrajectoryId, double datumElevation ) { auto [fileContents, errorMessage] = osduConnector->requestWellboreTrajectoryParquetDataByIdBlocking( wellboreTrajectoryId ); if ( !errorMessage.isEmpty() ) { return { nullptr, errorMessage }; } return RifOsduWellPathReader::readWellPathData( fileContents, datumElevation ); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RimWellPathCollection::initAfterRead() { if ( m_mswNameGrouping() == MswGroupingMode::USE_PREFERENCES ) { m_mswNameGroupingPattern = RiaPreferences::current()->multiLateralWellNamePattern(); } } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- std::pair, QString> RimWellPathCollection::loadWellLogFromOsdu( RiaOsduConnector* osduConnector, const QString& wellLogId ) { auto [fileContents, errorMessage] = osduConnector->requestWellLogParquetDataByIdBlocking( wellLogId ); if ( !errorMessage.isEmpty() ) { return { nullptr, errorMessage }; } return RifOsduWellLogReader::readWellLogData( fileContents ); }