Thermal Fracture: add python script for exporting template as surface.

This commit is contained in:
Kristian Bendiksen 2022-09-07 13:39:27 +02:00
parent 0e49d983a2
commit 619807b81f
11 changed files with 442 additions and 38 deletions

View File

@ -59,6 +59,7 @@ set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RifStimPlanModelDeviationFrkExporter.h
${CMAKE_CURRENT_LIST_DIR}/RifStimPlanModelPerfsFrkExporter.h
${CMAKE_CURRENT_LIST_DIR}/RifStimPlanModelAsymmetricFrkExporter.h
${CMAKE_CURRENT_LIST_DIR}/RifThermalFractureTemplateSurfaceExporter.h
${CMAKE_CURRENT_LIST_DIR}/RifSurfaceExporter.h
${CMAKE_CURRENT_LIST_DIR}/RifFaultRAXmlWriter.h
${CMAKE_CURRENT_LIST_DIR}/RifFaultRAJsonWriter.h
@ -135,6 +136,7 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RifStimPlanModelDeviationFrkExporter.cpp
${CMAKE_CURRENT_LIST_DIR}/RifStimPlanModelPerfsFrkExporter.cpp
${CMAKE_CURRENT_LIST_DIR}/RifStimPlanModelAsymmetricFrkExporter.cpp
${CMAKE_CURRENT_LIST_DIR}/RifThermalFractureTemplateSurfaceExporter.cpp
${CMAKE_CURRENT_LIST_DIR}/RifSurfaceExporter.cpp
${CMAKE_CURRENT_LIST_DIR}/RifFaultRAXmlWriter.cpp
${CMAKE_CURRENT_LIST_DIR}/RifFaultRAJsonWriter.cpp

View File

@ -0,0 +1,73 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2022- Equinor 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 "RifThermalFractureTemplateSurfaceExporter.h"
#include "RigThermalFractureDefinition.h"
#include "RimThermalFractureTemplate.h"
#include <QFile>
#include <QTextStream>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RifThermalFractureTemplateSurfaceExporter::writeToFile( RimThermalFractureTemplate* fractureTemplate,
int timeStepIndex,
const QString& filePath )
{
auto fractureData = fractureTemplate->fractureDefinition();
auto numNodes = fractureData->numNodes();
auto numTimeSteps = fractureData->numTimeSteps();
auto properties = fractureData->getPropertyNamesUnits();
if ( timeStepIndex < 0 || timeStepIndex >= static_cast<int>( numTimeSteps ) ) return false;
QFile file( filePath );
if ( file.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
QTextStream out( &file );
out.setRealNumberPrecision( 16 );
auto nameAndUnits = fractureData->getPropertyNamesUnits();
int numProperties = nameAndUnits.size();
for ( auto [name, unit] : nameAndUnits )
{
out << name << " ";
}
out << "\n";
for ( int nodeIndex = 0; nodeIndex < static_cast<int>( numNodes ); nodeIndex++ )
{
for ( int propertyIndex = 0; propertyIndex < numProperties; propertyIndex++ )
{
double value = fractureData->getPropertyValue( propertyIndex, nodeIndex, timeStepIndex );
out << value;
if ( propertyIndex < numProperties - 1 ) out << " ";
}
out << "\n";
}
}
file.close();
return true;
}

View File

@ -0,0 +1,31 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2022- Equinor 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.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
class RimThermalFractureTemplate;
class QString;
//==================================================================================================
//
//==================================================================================================
class RifThermalFractureTemplateSurfaceExporter
{
public:
static bool writeToFile( RimThermalFractureTemplate* stimPlanModel, int timeStep, const QString& filePath );
};

View File

@ -115,7 +115,7 @@ RimFractureTemplate::RimFractureTemplate()
CAF_PDM_InitField( &m_id, "Id", -1, "ID" );
m_id.uiCapability()->setUiReadOnly( true );
CAF_PDM_InitField( &m_name, "UserDescription", QString( "Fracture Template" ), "Name" );
CAF_PDM_InitScriptableField( &m_name, "UserDescription", QString( "Fracture Template" ), "Name" );
CAF_PDM_InitFieldNoDefault( &m_nameAndUnit, "NameAndUnit", "NameAndUnit" );
m_nameAndUnit.registerGetMethod( this, &RimFractureTemplate::nameAndUnit );

View File

@ -712,3 +712,11 @@ bool RimThermalFractureTemplate::isValidResult( double value ) const
{
return !std::isinf( value ) && !std::isnan( value );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
const RigThermalFractureDefinition* RimThermalFractureTemplate::fractureDefinition() const
{
return m_fractureDefinitionData.get();
}

View File

@ -103,6 +103,8 @@ public:
std::pair<cvf::Vec3d, cvf::Vec3d> computePositionAndRotation() const;
const RigThermalFractureDefinition* fractureDefinition() const;
protected:
QString getFileSelectionFilter() const override;
QStringList conductivityResultNames() const override;

View File

@ -20,6 +20,7 @@ set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RimcModeledWellPath.h
${CMAKE_CURRENT_LIST_DIR}/RimcWellPath.h
${CMAKE_CURRENT_LIST_DIR}/RimcFractureTemplateCollection.h
${CMAKE_CURRENT_LIST_DIR}/RimcThermalFractureTemplate.h
${CMAKE_CURRENT_LIST_DIR}/RimcIntersection.h
)
@ -45,6 +46,7 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RimcModeledWellPath.cpp
${CMAKE_CURRENT_LIST_DIR}/RimcWellPath.cpp
${CMAKE_CURRENT_LIST_DIR}/RimcFractureTemplateCollection.cpp
${CMAKE_CURRENT_LIST_DIR}/RimcThermalFractureTemplate.cpp
${CMAKE_CURRENT_LIST_DIR}/RimcIntersection.cpp
)

View File

@ -0,0 +1,123 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2022- Equinor 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 "RimcThermalFractureTemplate.h"
#include "RifThermalFractureTemplateSurfaceExporter.h"
#include "RimThermalFractureTemplate.h"
#include "RimcDataContainerString.h"
#include "cafPdmAbstractFieldScriptingCapability.h"
#include "cafPdmFieldScriptingCapability.h"
CAF_PDM_OBJECT_METHOD_SOURCE_INIT( RimThermalFractureTemplate, RimcThermalFractureTemplate_exportToFile, "ExportToFile" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimcThermalFractureTemplate_exportToFile::RimcThermalFractureTemplate_exportToFile( caf::PdmObjectHandle* self )
: caf::PdmObjectMethod( self )
{
CAF_PDM_InitObject( "Export Thermal Fracture Template", "", "", "Export Thermal Fracture Template to File" );
CAF_PDM_InitScriptableFieldNoDefault( &m_filePath, "FilePath", "", "", "", "File Path" );
CAF_PDM_InitScriptableField( &m_timeStep, "TimeStep", 0, "Time Step" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::PdmObjectHandle* RimcThermalFractureTemplate_exportToFile::execute()
{
RimThermalFractureTemplate* thermalFracture = self<RimThermalFractureTemplate>();
RifThermalFractureTemplateSurfaceExporter::writeToFile( thermalFracture, m_timeStep(), m_filePath() );
return nullptr;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimcThermalFractureTemplate_exportToFile::resultIsPersistent() const
{
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::unique_ptr<caf::PdmObjectHandle> RimcThermalFractureTemplate_exportToFile::defaultResult() const
{
return std::unique_ptr<caf::PdmObjectHandle>( new RimThermalFractureTemplate );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimcThermalFractureTemplate_exportToFile::isNullptrValidResult() const
{
return true;
}
CAF_PDM_OBJECT_METHOD_SOURCE_INIT( RimThermalFractureTemplate, RimcThermalFractureTemplate_timeSteps, "TimeSteps" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimcThermalFractureTemplate_timeSteps::RimcThermalFractureTemplate_timeSteps( caf::PdmObjectHandle* self )
: caf::PdmObjectMethod( self )
{
CAF_PDM_InitObject( "Get Thermal Fracture Template Time Steps", "", "", "Get Thermal Fracture Template Time Steps" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::PdmObjectHandle* RimcThermalFractureTemplate_timeSteps::execute()
{
RimThermalFractureTemplate* thermalFracture = self<RimThermalFractureTemplate>();
auto timeSteps = thermalFracture->timeStepsStrings();
auto dataObject = new RimcDataContainerString;
dataObject->m_stringValues = timeSteps;
return dataObject;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimcThermalFractureTemplate_timeSteps::resultIsPersistent() const
{
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::unique_ptr<caf::PdmObjectHandle> RimcThermalFractureTemplate_timeSteps::defaultResult() const
{
return std::unique_ptr<caf::PdmObjectHandle>( new RimcDataContainerString );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimcThermalFractureTemplate_timeSteps::isNullptrValidResult() const
{
return true;
}

View File

@ -0,0 +1,64 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2020- Equinor 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.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "cafPdmField.h"
#include "cafPdmObjectHandle.h"
#include "cafPdmObjectMethod.h"
#include "cafPdmPtrField.h"
#include <QString>
class RimThermalFractureTemplate;
//==================================================================================================
///
//==================================================================================================
class RimcThermalFractureTemplate_exportToFile : public caf::PdmObjectMethod
{
CAF_PDM_HEADER_INIT;
public:
RimcThermalFractureTemplate_exportToFile( caf::PdmObjectHandle* self );
caf::PdmObjectHandle* execute() override;
bool resultIsPersistent() const override;
std::unique_ptr<PdmObjectHandle> defaultResult() const override;
bool isNullptrValidResult() const override;
private:
caf::PdmField<QString> m_filePath;
caf::PdmField<int> m_timeStep;
};
//==================================================================================================
///
//==================================================================================================
class RimcThermalFractureTemplate_timeSteps : public caf::PdmObjectMethod
{
CAF_PDM_HEADER_INIT;
public:
RimcThermalFractureTemplate_timeSteps( caf::PdmObjectHandle* self );
caf::PdmObjectHandle* execute() override;
bool resultIsPersistent() const override;
std::unique_ptr<PdmObjectHandle> defaultResult() const override;
bool isNullptrValidResult() const override;
};

View File

@ -117,41 +117,5 @@ TEST( DISABLED_RifThermalFractureReaderTest, CreateXyzPointCloud )
QString fileName = CASE_REAL_TEST_DATA_DIRECTORY_03 + "fracture_OS_metric_units_final.csv";
auto [fractureData, errorMessage] = RifThermalFractureReader::readFractureCsvFile( fileName );
auto numNodes = fractureData->numNodes();
auto numTimeSteps = fractureData->numTimeSteps();
auto properties = fractureData->getPropertyNamesUnits();
for ( size_t timeStepIndex = 0; timeStepIndex < numTimeSteps; timeStepIndex++ )
{
QString exportfileName = QString( CASE_REAL_TEST_DATA_DIRECTORY_03 + "msjtest-%1.xyz" ).arg( timeStepIndex );
std::vector<double> xs;
std::vector<double> ys;
std::vector<double> zs;
for ( int nodeIndex = 0; nodeIndex < static_cast<int>( numNodes ); nodeIndex++ )
{
xs.push_back(
fractureData->getPropertyValue( 0, static_cast<int>( nodeIndex ), static_cast<int>( timeStepIndex ) ) );
ys.push_back(
fractureData->getPropertyValue( 1, static_cast<int>( nodeIndex ), static_cast<int>( timeStepIndex ) ) );
zs.push_back(
fractureData->getPropertyValue( 2, static_cast<int>( nodeIndex ), static_cast<int>( timeStepIndex ) ) );
}
QFile file( exportfileName );
if ( file.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
QTextStream out( &file );
out.setRealNumberPrecision( 16 );
for ( size_t i = 0; i < xs.size(); i++ )
{
out << xs[i] << " " << ys[i] << " " << zs[i] << "\n";
}
}
file.close();
}
// EXPECT_TRUE( );
}

View File

@ -0,0 +1,135 @@
#!/usr/bin/env python
# coding: utf-8
import rips
import tempfile
from os.path import expanduser
from pathlib import Path
import numpy as np
import pyvista as pv
def generate_surface_from_file(path):
point_cloud_data = np.loadtxt(path, delimiter=" ", skiprows=1)
# Get [x, y, z] components in separate matrix
num_rows = point_cloud_data.shape[0]
xyz = point_cloud_data[0:num_rows, 0:3]
# Generate surface
cloud = pv.PolyData(xyz)
surf = cloud.delaunay_2d()
# Read properties names from header data
f = open(path)
header = f.readline()
properties = header.strip().split(" ")
return (surf, point_cloud_data, properties)
def export_surface_as_ts_file(surf, point_cloud, properties, path):
# open text file
text_file = open(path, "w")
# write GOCAD header
top_header = """GOCAD TSurf 1
HEADER {
name:MF_027_SU
}
"""
properties_str = "PROPERTIES " + " ".join(properties)
bottom_header = """
GOCAD_ORIGINAL_COORDINATE_SYSTEM
NAME Default
AXIS_NAME "X" "Y" "Z"
AXIS_UNIT "m" "m" "m"
ZPOSITIVE Depth
END_ORIGINAL_COORDINATE_SYSTEM
TFACE
"""
text_file.write(top_header)
text_file.write(properties_str)
text_file.write(bottom_header)
i = 1
(num_rows, num_props) = point_cloud.shape
for row in range(0, num_rows):
x = point_cloud[row, 0]
y = point_cloud[row, 1]
z = point_cloud[row, 2]
txt = "PVRTX {} {:.3f} {:.3f} {:.3f} ".format(i, x, y, z)
for property_index in range(0, num_props):
txt += "{:.3f} ".format(point_cloud[row, property_index])
txt += "\n"
text_file.write(txt)
i += 1
mysurface = surf.faces.reshape(-1, 4)
for p in mysurface:
txt = "TRGL {} {} {}\n".format(p[1] + 1, p[2] + 1, p[3] + 1)
text_file.write(txt)
text_file.write("END")
text_file.close()
# Connect to ResInsight instance
resinsight = rips.Instance.find()
project = resinsight.project
fractures = project.descendants(rips.ThermalFractureTemplate)
print("Number of thermal fractures: ", len(fractures))
temp_folder = tempfile.gettempdir()
# Write results to a suitable directory
home_dir = expanduser("~")
for fracture in fractures:
fracture_name = fracture.user_description
# Create the ouput directory
output_directory = (
Path(home_dir) / "thermal_fracture_surfaces" / "{}".format(fracture_name)
)
output_directory.mkdir(parents=True, exist_ok=True)
print("Creating result directory: ", output_directory.as_posix())
time_steps = fracture.time_steps().values
for time_step_index, time_step in enumerate(time_steps):
print(
"Generating surface for time step #{}: {}".format(
time_step_index, time_step
)
)
temp_file_path = Path(temp_folder) / "output.xyz"
fracture.export_to_file(
file_path=temp_file_path.as_posix(), time_step=time_step_index
)
# Reconstruct a surface from the exported values file
(surface, point_cloud, properties) = generate_surface_from_file(
temp_file_path.as_posix()
)
# Export surface ts file from the surface data
output_file_path = output_directory / "time_step_{:03d}.ts".format(
time_step_index
)
export_surface_as_ts_file(
surface, point_cloud, properties, output_file_path.as_posix()
)
print(
"Wrote surface for time step #{} to {}".format(
time_step, output_file_path.as_posix()
)
)