Files
ResInsight/ApplicationLibCode/ProjectDataModel/Completions/RimFractureTemplateCollection.cpp
T
Magne Sjaastad 56e98ba1c4 #14476 ApplicationLibCode: Replace CVF_ASSERT with CAF_ASSERT
Migrate all assert macros in ApplicationLibCode to CAF_ASSERT and remove every
use of cvfAssert.h.

CVF_ASSERT is replaced one to one. CVF_TIGHT_ASSERT is also replaced by
CAF_ASSERT, which is semantically exact: CVF_ENABLE_TIGHT_ASSERTS is 1 only
under _DEBUG, and that is what CAF_ASSERT now does. The two CVF_FAIL_MSG sites
become CAF_ASSERT( false && "message" ), preserving the message with the idiom
already used elsewhere in the code base.

Counts before and after: CVF_ASSERT 1044 to 0, CVF_TIGHT_ASSERT 66 to 0,
CVF_FAIL_MSG 2 to 0, cvfAssert.h references 154 to 0.

Include handling: files that included cvfAssert.h directly now include
cafAssert.h instead, includes left dead by the migration are removed, and files
that were relying on cvfAssert.h transitively get an explicit cafAssert.h. Files
that reach cafAssert.h through another caf header are left unchanged; a missing
include here is a compile error, not a silently disabled assert.

ResultStatisticsCache links only LibCore and therefore had no path to
cafAssert.h. Add the cafPdmCore directory as a private include path rather than
linking the library, since cafAssert.h is header only.

Note that this stops these asserts from firing in Release and RelWithDebInfo,
where CVF_ASSERT was previously active.
2026-08-07 15:05:57 +02:00

322 lines
12 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2016- Statoil ASA
//
// 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 <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RimFractureTemplateCollection.h"
#include "RiaLogging.h"
#include "RigStatisticsMath.h"
#include "RimCase.h"
#include "RimEclipseCase.h"
#include "RimEclipseView.h"
#include "RimEllipseFractureTemplate.h"
#include "RimFracture.h"
#include "RimFractureTemplate.h"
#include "RimMeshFractureTemplate.h"
#include "RimProject.h"
#include "cafPdmObject.h"
#include "cafPdmObjectScriptingCapability.h"
#include <algorithm>
CAF_PDM_SOURCE_INIT( RimFractureTemplateCollection, "FractureTemplateCollection", "FractureDefinitionCollection" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimFractureTemplateCollection::RimFractureTemplateCollection()
{
CAF_PDM_InitScriptableObject( "Fracture Templates", ":/FractureTemplates16x16.png" );
CAF_PDM_InitField( &m_defaultUnitsForFracTemplates,
"DefaultUnitForTemplates",
caf::AppEnum<RiaDefines::EclipseUnitSystem>( RiaDefines::EclipseUnitSystem::UNITS_METRIC ),
"Default unit system for fracture templates" );
CAF_PDM_InitFieldNoDefault( &m_items, "FractureDefinitions", "" );
CAF_PDM_InitField( &m_nextValidFractureTemplateId_OBSOLETE, "NextValidFractureTemplateId", 0, "" );
m_nextValidFractureTemplateId_OBSOLETE.xmlCapability()->setIOWritable( false );
m_nextValidFractureTemplateId_OBSOLETE.uiCapability()->setUiHidden( true );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimFractureTemplate* RimFractureTemplateCollection::fractureTemplate( int id ) const
{
for ( const auto& templ : m_items )
{
if ( templ->id() == id ) return templ;
}
return nullptr;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<RimFractureTemplate*> RimFractureTemplateCollection::fractureTemplates() const
{
std::vector<RimFractureTemplate*> templates;
for ( auto& templ : m_items )
{
templates.push_back( templ );
}
return templates;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimEllipseFractureTemplate* RimFractureTemplateCollection::addDefaultEllipseTemplate()
{
RimEllipseFractureTemplate* ellipseFractureTemplate = new RimEllipseFractureTemplate();
addFractureTemplate( ellipseFractureTemplate );
ellipseFractureTemplate->setName( "Ellipse Fracture Template" );
ellipseFractureTemplate->setUnitSystem( defaultUnitSystemType() );
ellipseFractureTemplate->setDefaultValuesFromUnit();
return ellipseFractureTemplate;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimFractureTemplateCollection::addFractureTemplate( RimFractureTemplate* templ )
{
templ->setId( nextFractureTemplateId() );
addItem( templ );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::AppEnum<RiaDefines::EclipseUnitSystem> RimFractureTemplateCollection::defaultUnitSystemType() const
{
return m_defaultUnitsForFracTemplates;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimFractureTemplateCollection::setDefaultUnitSystemBasedOnLoadedCases()
{
RimProject* proj = RimProject::current();
auto commonUnitSystem = proj->commonUnitSystemForAllCases();
if ( commonUnitSystem != RiaDefines::EclipseUnitSystem::UNITS_UNKNOWN )
{
m_defaultUnitsForFracTemplates = commonUnitSystem;
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimFractureTemplate* RimFractureTemplateCollection::firstFractureOfUnit( RiaDefines::EclipseUnitSystem unitSet ) const
{
for ( RimFractureTemplate* f : m_items() )
{
if ( f->fractureTemplateUnit() == unitSet )
{
return f;
}
}
return nullptr;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<std::pair<QString, QString>> RimFractureTemplateCollection::resultNamesAndUnits() const
{
std::set<std::pair<QString, QString>> nameSet;
for ( const RimFractureTemplate* f : m_items() )
{
std::vector<std::pair<QString, QString>> namesAndUnits = f->uiResultNamesWithUnit();
for ( const auto& nameAndUnit : namesAndUnits )
{
nameSet.insert( nameAndUnit );
}
}
std::vector<std::pair<QString, QString>> names( nameSet.begin(), nameSet.end() );
return names;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimFractureTemplateCollection::computeMinMax( const QString& uiResultName,
const QString& unit,
double* minValue,
double* maxValue,
double* posClosestToZero,
double* negClosestToZero ) const
{
MinMaxAccumulator minMaxAccumulator;
PosNegAccumulator posNegAccumulator;
for ( const RimFractureTemplate* f : m_items() )
{
if ( f )
{
f->appendDataToResultStatistics( uiResultName, unit, minMaxAccumulator, posNegAccumulator );
}
}
if ( *minValue ) *minValue = minMaxAccumulator.min;
if ( *maxValue ) *maxValue = minMaxAccumulator.max;
if ( *posClosestToZero ) *posClosestToZero = posNegAccumulator.pos;
if ( *negClosestToZero ) *negClosestToZero = posNegAccumulator.neg;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimFractureTemplateCollection::createAndAssignTemplateCopyForNonMatchingUnit()
{
// If a fracture has different unit than the associated template, create a copy of template in correct unit
std::vector<RimFractureTemplate*> templatesToBeAdded;
for ( RimFractureTemplate* fractureTemplate : m_items() )
{
if ( fractureTemplate )
{
RimFractureTemplate* templateWithMatchingUnit = nullptr;
std::vector<RimFracture*> referringObjects = fractureTemplate->objectsWithReferringPtrFieldsOfType<RimFracture>();
for ( auto fracture : referringObjects )
{
if ( fracture && fracture->fractureUnit() != fractureTemplate->fractureTemplateUnit() )
{
if ( !templateWithMatchingUnit )
{
templateWithMatchingUnit = fractureTemplate->copyObject<RimFractureTemplate>();
auto currentUnit = fractureTemplate->fractureTemplateUnit();
auto neededUnit = RiaDefines::EclipseUnitSystem::UNITS_UNKNOWN;
if ( currentUnit == RiaDefines::EclipseUnitSystem::UNITS_METRIC )
{
neededUnit = RiaDefines::EclipseUnitSystem::UNITS_FIELD;
}
else if ( currentUnit == RiaDefines::EclipseUnitSystem::UNITS_FIELD )
{
neededUnit = RiaDefines::EclipseUnitSystem::UNITS_METRIC;
}
templateWithMatchingUnit->convertToUnitSystem( neededUnit );
QString name = templateWithMatchingUnit->name();
name += " (created to match fracture unit)";
templateWithMatchingUnit->setName( name );
templatesToBeAdded.push_back( templateWithMatchingUnit );
}
RiaLogging::warning( "Detected fracture with different unit than fracture template. Creating copy of template "
"with matching unit." );
CAF_ASSERT( templateWithMatchingUnit->fractureTemplateUnit() == fracture->fractureUnit() );
fracture->setFractureTemplateNoUpdate( templateWithMatchingUnit );
}
}
}
}
for ( auto templateWithMatchingUnit : templatesToBeAdded )
{
addItem( templateWithMatchingUnit );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimFractureTemplateCollection::loadAndUpdateData()
{
for ( RimFractureTemplate* f : m_items() )
{
RimMeshFractureTemplate* stimPlanFracture = dynamic_cast<RimMeshFractureTemplate*>( f );
if ( stimPlanFracture )
{
stimPlanFracture->loadDataAndUpdate();
}
RimEllipseFractureTemplate* ellipseFracture = dynamic_cast<RimEllipseFractureTemplate*>( f );
if ( ellipseFracture )
{
ellipseFracture->loadDataAndUpdate();
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimFractureTemplateCollection::initAfterRead()
{
// Assign template id if not already assigned
for ( auto& templ : m_items )
{
if ( templ->id() < 0 ) templ->setId( nextFractureTemplateId() );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RimFractureTemplateCollection::nextFractureTemplateId()
{
int nextValidId = 0;
for ( const auto& templ : m_items )
{
nextValidId = std::max( nextValidId, templ->id() + 1 );
}
return nextValidId;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimFractureTemplateCollection::onChildDeleted( caf::PdmChildArrayFieldHandle* childArray,
std::vector<caf::PdmObjectHandle*>& referringObjects )
{
RimProject* proj = RimProject::current();
if ( proj )
{
proj->scheduleCreateDisplayModelAndRedrawAllViews();
}
for ( Rim3dView* visibleView : proj->allVisibleViews() )
{
if ( dynamic_cast<RimEclipseView*>( visibleView ) )
{
visibleView->updateConnectedEditors();
}
}
}