#10861 Add import/export for grid calculations

This commit is contained in:
Kristian Bendiksen
2023-11-24 11:42:31 +01:00
parent 52259a69f3
commit fa7a56f1e2
18 changed files with 440 additions and 42 deletions

View File

@@ -89,6 +89,9 @@ set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RifFaultReactivationModelExporter.h
${CMAKE_CURRENT_LIST_DIR}/RifThermalToStimPlanFractureXmlOutput.h
${CMAKE_CURRENT_LIST_DIR}/RifEclipseSummaryAddressDefines.h
${CMAKE_CURRENT_LIST_DIR}/RifGridCalculation.h
${CMAKE_CURRENT_LIST_DIR}/RifGridCalculationImporter.h
${CMAKE_CURRENT_LIST_DIR}/RifGridCalculationExporter.h
)
set(SOURCE_GROUP_SOURCE_FILES
@@ -179,6 +182,8 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RifFaultReactivationModelExporter.cpp
${CMAKE_CURRENT_LIST_DIR}/RifThermalToStimPlanFractureXmlOutput.cpp
${CMAKE_CURRENT_LIST_DIR}/RifEclipseSummaryAddressDefines.cpp
${CMAKE_CURRENT_LIST_DIR}/RifGridCalculationImporter.cpp
${CMAKE_CURRENT_LIST_DIR}/RifGridCalculationExporter.cpp
)
list(APPEND CODE_HEADER_FILES ${SOURCE_GROUP_HEADER_FILES})

View File

@@ -0,0 +1,40 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023- 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 <string>
#include <vector>
//==================================================================================================
//
//==================================================================================================
struct RifGridCalculationVariable
{
std::string name;
std::string resultType;
std::string resultVariable;
};
struct RifGridCalculation
{
std::string description;
std::string expression;
std::string unit;
std::vector<RifGridCalculationVariable> variables;
};

View File

@@ -0,0 +1,72 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023- 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 "RifGridCalculationExporter.h"
#include <fstream>
#include <tomlplusplus/toml.hpp>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::pair<bool, std::string> RifGridCalculationExporter::writeToFile( const std::vector<RifGridCalculation>& calculations,
const std::string& filePath )
{
std::ofstream stream( filePath );
if ( !stream.good() ) return { false, "Unable to open file: " + filePath };
return writeToStream( calculations, stream );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::pair<bool, std::string> RifGridCalculationExporter::writeToStream( const std::vector<RifGridCalculation>& calculations,
std::ostream& stream )
{
auto calculationsVector = toml::array();
for ( auto calculation : calculations )
{
auto variablesVector = toml::array{};
for ( auto variable : calculation.variables )
{
variablesVector.push_back( toml::table{
{ "name", variable.name },
{ "variable", variable.resultVariable },
{ "type", variable.resultType },
} );
}
calculationsVector.push_back( toml::table{
{ "description", calculation.description },
{ "expression", calculation.expression },
{ "unit", calculation.unit },
{ "variables", variablesVector },
} );
}
auto tbl = toml::table{
{ "grid-calculation", calculationsVector },
};
stream << tbl;
return { stream.good(), "" };
}

View File

@@ -0,0 +1,34 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023- 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 "RifGridCalculation.h"
#include <string>
#include <vector>
//==================================================================================================
//
//==================================================================================================
class RifGridCalculationExporter
{
public:
static std::pair<bool, std::string> writeToFile( const std::vector<RifGridCalculation>& calculations, const std::string& filePath );
static std::pair<bool, std::string> writeToStream( const std::vector<RifGridCalculation>& calculations, std::ostream& stream );
};

View File

@@ -0,0 +1,81 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023- 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 "RifGridCalculationImporter.h"
#include <fstream>
#include <tomlplusplus/toml.hpp>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::pair<std::vector<RifGridCalculation>, std::string> RifGridCalculationImporter::readFromFile( const std::string& filePath )
{
std::ifstream stream( filePath );
if ( !stream.good() ) return { {}, "Unable to open file: " + filePath };
return readFromStream( stream );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::pair<std::vector<RifGridCalculation>, std::string> RifGridCalculationImporter::readFromStream( std::istream& stream )
{
toml::table tbl = toml::parse( stream );
auto calculationsVector = tbl["grid-calculation"];
std::vector<RifGridCalculation> calculations;
if ( toml::array* arr = calculationsVector.as_array() )
{
for ( auto&& a : *arr )
{
RifGridCalculation calculation;
if ( toml::table* calc = a.as_table() )
{
calculation.description = calc->at_path( "description" ).as_string()->value_or( "" );
calculation.expression = calc->at_path( "expression" ).as_string()->value_or( "" );
calculation.unit = calc->at_path( "unit" ).as_string()->value_or( "" );
if ( toml::array* vars = calc->at_path( "variables" ).as_array() )
{
std::vector<RifGridCalculationVariable> variables;
for ( auto&& v : *vars )
{
if ( toml::table* var = v.as_table() )
{
RifGridCalculationVariable variable;
variable.name = var->at_path( "name" ).as_string()->value_or( "" );
variable.resultType = var->at_path( "type" ).as_string()->value_or( "" );
variable.resultVariable = var->at_path( "variable" ).as_string()->value_or( "" );
variables.push_back( variable );
}
}
calculation.variables = variables;
}
calculations.push_back( calculation );
}
}
}
return { calculations, "" };
}

View File

@@ -0,0 +1,34 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2023- 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 "RifGridCalculation.h"
#include <string>
#include <vector>
//==================================================================================================
//
//==================================================================================================
class RifGridCalculationImporter
{
public:
static std::pair<std::vector<RifGridCalculation>, std::string> readFromFile( const std::string& filePath );
static std::pair<std::vector<RifGridCalculation>, std::string> readFromStream( std::istream& stream );
};