Skip to content

Commit

Permalink
Python: Enforce checking on script name to make sure conversion betwe…
Browse files Browse the repository at this point in the history
…en camelCase and snake_case works correctly

* #12044 Python: Add Non-Darcy properties available in Python

* #12049 Python: Add checking for multiple consecutive upper case letters
The scripting keyword is transformed to snake_case for use in Python. When data in Python is sent back to ResInsight, the opposite operation happens. This concept works well for most variants of keywords, but there are some corner cases that is not working.

Add compile time checking to make sure that the scripting keywords are formatted correctly.

Fix keyword that has been formatted the wrong way and has never worked.

* Avoid running test if ResInsight executable path is not defined in env
* Make sure dash is allowed in enumeration text string
  • Loading branch information
magnesj authored Jan 10, 2025
1 parent 90a1642 commit da69c1f
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,16 @@ RicfExportWellPathCompletions::RicfExportWellPathCompletions()

CAF_PDM_InitScriptableField( &m_performTransScaling, "performTransScaling", false, "Perform Transmissibility Scaling" );
CAF_PDM_InitScriptableField( &m_transScalingTimeStep, "transScalingTimeStep", 0, "Transmissibility Scaling Pressure Time Step" );
CAF_PDM_InitScriptableField( &m_transScalingInitialWBHP,
"transScalingWBHPFromSummary",
RicExportCompletionDataSettingsUi::TransScalingWBHPSource(),
"Transmissibility Scaling WBHP from summary" );
CAF_PDM_InitScriptableField( &m_transScalingWBHP, "transScalingWBHP", 200.0, "Transmissibility Scaling Constant WBHP Value" );
CAF_PDM_InitScriptableFieldWithScriptKeyword( &m_transScalingInitialWBHP,
"transScalingWBHPFromSummary",
"transScalingWbhpFromSummary",
RicExportCompletionDataSettingsUi::TransScalingWBHPSource(),
"Transmissibility Scaling WBHP from summary" );
CAF_PDM_InitScriptableFieldWithScriptKeyword( &m_transScalingWBHP,
"transScalingWBHP",
"transScalingWbhp",
200.0,
"Transmissibility Scaling Constant WBHP Value" );

CAF_PDM_InitScriptableField( &m_exportDataSourceAsComments, "exportComments", true, "Export Data Source as Comments" );
CAF_PDM_InitScriptableField( &m_exportWelspec, "exportWelspec", true, "Export WELSPEC keyword" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,34 +156,37 @@ RimFractureTemplate::RimFractureTemplate()
m_fractureContainment.uiCapability()->setUiTreeChildrenHidden( true );

// Non-Darcy Flow options
CAF_PDM_InitFieldNoDefault( &m_nonDarcyFlowType, "NonDarcyFlowType", "Non-Darcy Flow" );

CAF_PDM_InitField( &m_userDefinedDFactor, "UserDefinedDFactor", 1.0, "D Factor" );

CAF_PDM_InitFieldNoDefault( &m_fractureWidthType, "FractureWidthType", "Type" );
CAF_PDM_InitField( &m_fractureWidth, "FractureWidth", 0.01, "Fracture Width (h)" );

CAF_PDM_InitFieldNoDefault( &m_betaFactorType, "BetaFactorType", "Type" );
CAF_PDM_InitField( &m_inertialCoefficient, "InertialCoefficient", 0.006083236, "<html>Inertial Coefficient (&beta;)</html> [Forch. unit]" );

CAF_PDM_InitFieldNoDefault( &m_permeabilityType, "PermeabilityType", "Type" );
CAF_PDM_InitField( &m_relativePermeability, "RelativePermeability", 1.0, "Relative Permeability" );
CAF_PDM_InitField( &m_userDefinedEffectivePermeability, "EffectivePermeability", 0.0, "Effective Permeability (Ke) [mD]" );

CAF_PDM_InitField( &m_relativeGasDensity,
"RelativeGasDensity",
0.8,
"<html>Relative Gas Density (&gamma;)</html>",
"",
"Relative density of gas at surface conditions with respect to air at STP",
"" );
CAF_PDM_InitField( &m_gasViscosity,
"GasViscosity",
0.02,
"<html>Gas Viscosity (&mu;)</html> [cP]",
"",
"Gas viscosity at bottom hole pressure",
"" );
CAF_PDM_InitScriptableFieldNoDefault( &m_nonDarcyFlowType, "NonDarcyFlowType", "Non-Darcy Flow" );

CAF_PDM_InitScriptableField( &m_userDefinedDFactor, "UserDefinedDFactor", 1.0, "D Factor" );

CAF_PDM_InitScriptableFieldNoDefault( &m_fractureWidthType, "FractureWidthType", "Type" );
CAF_PDM_InitScriptableField( &m_fractureWidth, "FractureWidth", 0.01, "Fracture Width (h)" );

CAF_PDM_InitScriptableFieldNoDefault( &m_betaFactorType, "BetaFactorType", "Type" );
CAF_PDM_InitScriptableField( &m_inertialCoefficient,
"InertialCoefficient",
0.006083236,
"<html>Inertial Coefficient (&beta;)</html> [Forch. unit]" );

CAF_PDM_InitScriptableFieldNoDefault( &m_permeabilityType, "PermeabilityType", "Type" );
CAF_PDM_InitScriptableField( &m_relativePermeability, "RelativePermeability", 1.0, "Relative Permeability" );
CAF_PDM_InitScriptableField( &m_userDefinedEffectivePermeability, "EffectivePermeability", 0.0, "Effective Permeability (Ke) [mD]" );

CAF_PDM_InitScriptableField( &m_relativeGasDensity,
"RelativeGasDensity",
0.8,
"<html>Relative Gas Density (&gamma;)</html>",
"",
"Relative density of gas at surface conditions with respect to air at STP",
"" );
CAF_PDM_InitScriptableField( &m_gasViscosity,
"GasViscosity",
0.02,
"<html>Gas Viscosity (&mu;)</html> [cP]",
"",
"Gas viscosity at bottom hole pressure",
"" );

CAF_PDM_InitFieldNoDefault( &m_dFactorDisplayField, "dFactorDisplayField", "D Factor" );
m_dFactorDisplayField.registerGetMethod( this, &RimFractureTemplate::dFactorForTemplate );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,19 @@ RimWellPathCompletionSettings::RimWellPathCompletionSettings()
CAF_PDM_InitScriptableFieldWithScriptKeyword( &m_groupName, "WellGroupNameForExport", "GroupNameForExport", QString(), "Group Name" );
CAF_PDM_InitScriptableField( &m_referenceDepth, "ReferenceDepthForExport", QString(), "Reference Depth for BHP" );
CAF_PDM_InitScriptableFieldNoDefault( &m_preferredFluidPhase, "WellTypeForExport", "Preferred Fluid Phase" );
CAF_PDM_InitScriptableField( &m_drainageRadiusForPI, "DrainageRadiusForPI", QString( "0.0" ), "Drainage Radius for PI" );
CAF_PDM_InitScriptableFieldWithScriptKeyword( &m_drainageRadiusForPI,
"DrainageRadiusForPI",
"DrainageRadiusForPi",
QString( "0.0" ),
"Drainage Radius for PI" );
CAF_PDM_InitScriptableFieldNoDefault( &m_gasInflowEquation, "GasInflowEq", "Gas Inflow Equation" );
CAF_PDM_InitScriptableFieldNoDefault( &m_automaticWellShutIn, "AutoWellShutIn", "Automatic well shut-in" );
CAF_PDM_InitScriptableField( &m_allowWellCrossFlow, "AllowWellCrossFlow", true, "Allow Well Cross-Flow" );
CAF_PDM_InitScriptableField( &m_wellBoreFluidPVTTable, "WellBoreFluidPVTTable", 0, "Wellbore Fluid PVT table" );
CAF_PDM_InitScriptableFieldWithScriptKeyword( &m_wellBoreFluidPVTTable,
"WellBoreFluidPVTTable",
"WellBoreFluidPvtTable",
0,
"Wellbore Fluid PVT table" );
CAF_PDM_InitScriptableFieldNoDefault( &m_hydrostaticDensity, "HydrostaticDensity", "Hydrostatic Density" );
CAF_PDM_InitScriptableField( &m_fluidInPlaceRegion, "FluidInPlaceRegion", 0, "Fluid In-Place Region" );

Expand Down
68 changes: 49 additions & 19 deletions ApplicationLibCode/ProjectDataModel/RimWbsParameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,49 @@ RimWbsParameters::RimWbsParameters()
"",
"Data source for Non-Reservoir Pore Pressure",
"" );
CAF_PDM_InitScriptableField( &m_userDefinedPPShale, "UserPPNonReservoir", 1.0, " Multiplier of hydrostatic PP" );
CAF_PDM_InitScriptableFieldWithScriptKeyword( &m_userDefinedPPShale,
"UserPPNonReservoir",
"UserPpNonReservoir",
1.0,
" Multiplier of hydrostatic PP" );

CAF_PDM_InitScriptableFieldNoDefault( &m_poissonRatioSource, "PoissionRatioSource", "Poisson Ratio", "", "Data source for Poisson Ratio", "" );

CAF_PDM_InitScriptableFieldNoDefault( &m_ucsSource, "UcsSource", "Uniaxial Compressive Strength", "", "Data source for UCS", "" );

CAF_PDM_InitScriptableFieldNoDefault( &m_OBG0Source, "OBG0Source", "Initial Overburden Gradient", "", "Data source for OBG0", "" );
CAF_PDM_InitScriptableFieldNoDefault( &m_DFSource, "DFSource", "Depletion Factor (DF)", "", "Data source for Depletion Factor", "" );

CAF_PDM_InitScriptableFieldNoDefault( &m_K0SHSource,
"K0SHSource",
"K0_SH",
"",
"SH from Matthews & Kelly = K0_SH * (OBG0-PP0) + PP0 + DF * "
"(PP-PP0)\nK0_SH = "
"(SH - PP)/(OBG-PP)",
"" );

CAF_PDM_InitScriptableFieldNoDefault( &m_FGShaleSource, "FGShaleSource", "FG in Shale Calculation" );
CAF_PDM_InitScriptableFieldNoDefault( &m_K0FGSource, "K0FGSource", "K0_FG", "", "FG in shale = K0_FG * (OBG0-PP0)\nK0_FG = (FG-PP)/(OBG-PP)", "" );
CAF_PDM_InitScriptableFieldWithScriptKeywordNoDefault( &m_OBG0Source,
"OBG0Source",
"ObgSource",
"Initial Overburden Gradient",
"",
"Data source for OBG0",
"" );
CAF_PDM_InitScriptableFieldWithScriptKeywordNoDefault( &m_DFSource,
"DFSource",
"DfSource",
"Depletion Factor (DF)",
"",
"Data source for Depletion Factor",
"" );

CAF_PDM_InitScriptableFieldWithScriptKeywordNoDefault( &m_K0SHSource,
"K0SHSource",
"KshSource",
"K0_SH",
"",
"SH from Matthews & Kelly = K0_SH * (OBG0-PP0) + PP0 + DF * "
"(PP-PP0)\nK0_SH = "
"(SH - PP)/(OBG-PP)",
"" );

CAF_PDM_InitScriptableFieldWithScriptKeywordNoDefault( &m_FGShaleSource, "FGShaleSource", "FgShaleSource", "FG in Shale Calculation" );
CAF_PDM_InitScriptableFieldWithScriptKeywordNoDefault( &m_K0FGSource,
"K0FGSource",
"KfgSource",
"K0_FG",
"",
"FG in shale = K0_FG * (OBG0-PP0)\nK0_FG = (FG-PP)/(OBG-PP)",
"" );

CAF_PDM_InitFieldNoDefault( &m_waterDensitySource, "WaterDensitySource", "Water Density" );
m_waterDensitySource.uiCapability()->setUiHidden( true );
Expand All @@ -86,10 +109,17 @@ RimWbsParameters::RimWbsParameters()
// Typical UCS for Shale is 5 - 100 MPa -> 50 - 1000 bar.
CAF_PDM_InitScriptableField( &m_userDefinedUcs, "UserUcs", 100.0, "User Defined UCS [bar]", "", "User Defined UCS [bar]", "" );

CAF_PDM_InitScriptableField( &m_userDefinedDF, "UserDF", 0.7, "User Defined DF", "", "User Defined Depletion Factor", "" );
CAF_PDM_InitScriptableField( &m_userDefinedK0FG, "UserK0FG", 0.75, "User Defined K0_FG" );
CAF_PDM_InitScriptableField( &m_userDefinedK0SH, "UserK0SH", 0.65, "User Defined K0_SH" );
CAF_PDM_InitScriptableField( &m_FGShaleMultiplier, "FGMultiplier", 1.05, "SH Multiplier for FG in Shale", "", "FG in Shale = Multiplier * SH", "" );
CAF_PDM_InitScriptableFieldWithScriptKeyword( &m_userDefinedDF, "UserDF", "UserDf", 0.7, "User Defined DF", "", "User Defined Depletion Factor", "" );
CAF_PDM_InitScriptableFieldWithScriptKeyword( &m_userDefinedK0FG, "UserK0FG", "UserKfg", 0.75, "User Defined K0_FG" );
CAF_PDM_InitScriptableFieldWithScriptKeyword( &m_userDefinedK0SH, "UserK0SH", "UserKsh", 0.65, "User Defined K0_SH" );
CAF_PDM_InitScriptableFieldWithScriptKeyword( &m_FGShaleMultiplier,
"FGMultiplier",
"FgMultiplier",
1.05,
"SH Multiplier for FG in Shale",
"",
"FG in Shale = Multiplier * SH",
"" );

CAF_PDM_InitScriptableField( &m_userDefinedDensity, "WaterDensity", 1.03, "Density of Sea Water [g/cm^3]", "", "Units: g/cm^3", "" );

Expand Down
75 changes: 74 additions & 1 deletion Fwk/AppFwk/cafPdmScripting/cafPdmFieldScriptingCapability.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,75 @@
#include <QIODevice>
#include <QTextStream>

namespace
{
constexpr bool isUpper( char c )
{
return c >= 'A' && c <= 'Z';
}

constexpr bool isLower( char c )
{
return c >= 'a' && c <= 'z';
}

constexpr bool isNumber( char c )
{
return c >= '0' && c <= '9';
}

constexpr bool isCamelCase( std::string_view str )
{
if ( str.empty() ) return false;

bool hasLower = false;
int prevUpperCount = 0;

if ( isUpper( str[0] ) || isNumber( str[0] ) ) prevUpperCount++;

for ( size_t i = 1; i < str.size(); ++i )
{
if ( isUpper( str[i] ) || isNumber( str[i] ) )
{
if ( prevUpperCount >= 2 )
{
// Three consecutive uppercase letters/numbers
return false;
}
prevUpperCount++;
}
else if ( isLower( str[i] ) )
{
prevUpperCount = 0;
hasLower = true;
}
else
{
// Invalid character
return false;
}
}

if ( prevUpperCount > 1 )
{
// Two or more consecutive uppercase letters/numbers at the end
return false;
}

return hasLower;
}

} //namespace

// The scripting system converts from camel case to snake case by inserting an underscore before each uppercase letter.
// When an object is updated in Python, the keyword is converted back to camel case. This operation does not work if
// there are three or more consecutive uppercase letters.
// See PdmPythonGenerator::camelToSnakeCase()
// See snake_to_camel() and camel_to_snake() in pdmObject.py
// Conversion of values from rips object to caf object is done in RiaGrpcServiceInterface::copyPdmObjectFromRipsToCaf
#define CAF_PDM_CheckScriptableKeyword( keyword ) \
static_assert( isCamelCase( keyword ), "Keyword used for scripting must be in compatible formatted camel casing" );

#define CAF_PDM_InitScriptableField( field, keyword, default, uiName, ... ) \
{ \
std::vector<QString> arguments = { __VA_ARGS__ }; \
Expand All @@ -64,6 +133,7 @@
iconResourceName, \
caf::PdmAbstractFieldScriptingCapability::helpString( toolTip, keyword ), \
whatsThis ); \
CAF_PDM_CheckScriptableKeyword( keyword ); \
caf::AddScriptingCapabilityToField( field, keyword ); \
}

Expand All @@ -83,6 +153,7 @@
iconResourceName, \
caf::PdmAbstractFieldScriptingCapability::helpString( toolTip, keyword ), \
whatsThis ); \
CAF_PDM_CheckScriptableKeyword( keyword ); \
caf::AddScriptingCapabilityToField( field, keyword ); \
}

Expand All @@ -103,6 +174,7 @@
iconResourceName, \
caf::PdmAbstractFieldScriptingCapability::helpString( toolTip, scriptKeyword ), \
whatsThis ); \
CAF_PDM_CheckScriptableKeyword( scriptKeyword ); \
caf::AddScriptingCapabilityToField( field, scriptKeyword ); \
}

Expand All @@ -122,6 +194,7 @@
iconResourceName, \
caf::PdmAbstractFieldScriptingCapability::helpString( toolTip, scriptKeyword ), \
whatsThis ); \
CAF_PDM_CheckScriptableKeyword( scriptKeyword ); \
caf::AddScriptingCapabilityToField( field, scriptKeyword ); \
}

Expand Down Expand Up @@ -196,7 +269,7 @@ struct PdmFieldScriptingCapabilityIOHandler<AppEnum<T>>
while ( !inputStream.atEnd() )
{
nextChar = errorMessageContainer->peekNextChar( inputStream );
if ( nextChar.isLetterOrNumber() || nextChar == QChar( '_' ) )
if ( nextChar.isLetterOrNumber() || nextChar == QChar( '_' ) || nextChar == QChar( '-' ) )
{
currentChar = errorMessageContainer->readCharWithLineNumberCount( inputStream );
accumulatedFieldValue += currentChar;
Expand Down
Loading

0 comments on commit da69c1f

Please sign in to comment.