Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding possibility to output all TableFunction in log/CSV + Solving a bug on PVT CSV export #3537

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ CO2BrineFluid( string const & name, Group * const parent ):
this->registerWrapper( viewKeyStruct::writeCSVFlagString(), &m_writeCSV ).
setInputFlag( InputFlags::OPTIONAL ).
setRestartFlags( RestartFlags::NO_WRITE ).
setDescription( "Write PVT tables into a CSV file" ).
setDescription( "When set to 1, write PVT tables into a CSV file" ).
setDefaultValue( 0 );

// if this is a thermal model, we need to make sure that the arrays will be properly displayed and saved to restart
Expand Down Expand Up @@ -333,7 +333,7 @@ void CO2BrineFluid< PHASE1, PHASE2, FLASH >::createPVTModels()
bool const isClone = this->isClone();
TableFunction::OutputOptions const pvtOutputOpts = {
!isClone && m_writeCSV,// writeCSV
!isClone && (getLogLevel() > 0 && logger::internal::rank==0), // writeInLog
!isClone && getLogLevel() > 0, // writeInLog
};

m_phase1 = std::make_unique< PHASE1 >( getName() + "_phaseModel1",
Expand Down Expand Up @@ -369,7 +369,7 @@ void CO2BrineFluid< PHASE1, PHASE2, FLASH >::createPVTModels()
{
TableFunction::OutputOptions const flashOutputOpts = {
!isClone && m_writeCSV,// writeCSV
!isClone && (getLogLevel() > 0 && logger::internal::rank==0), // writeInLog
!isClone && getLogLevel() > 0, // writeInLog
};
m_flash = std::make_unique< FLASH >( getName() + '_' + FLASH::catalogName(),
strs,
Expand Down Expand Up @@ -415,7 +415,7 @@ void CO2BrineFluid< PHASE1, PHASE2, FLASH >::createPVTModels()

TableFunction::OutputOptions const flashOutputOpts = {
!isClone && m_writeCSV,// writeCSV
!isClone && (getLogLevel() >= 0 && logger::internal::rank==0), // writeInLog
!isClone && getLogLevel() > 0, // writeInLog
};

m_flash = std::make_unique< FLASH >( getName() + '_' + FLASH::catalogName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ BrineEnthalpy::BrineEnthalpy( string const & name,
m_CO2EnthalpyTable = makeCO2EnthalpyTable( inputParams, m_functionName, FunctionManager::getInstance() );
m_brineEnthalpyTable = makeBrineEnthalpyTable( inputParams, m_functionName, FunctionManager::getInstance() );

m_CO2EnthalpyTable->outputPVTTableData( pvtOutputOpts );
m_brineEnthalpyTable->outputPVTTableData( pvtOutputOpts );
m_CO2EnthalpyTable->outputTableData( pvtOutputOpts );
m_brineEnthalpyTable->outputTableData( pvtOutputOpts );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ CO2Enthalpy::CO2Enthalpy( string const & name,

m_CO2EnthalpyTable = makeCO2EnthalpyTable( inputParams, m_functionName, FunctionManager::getInstance() );

m_CO2EnthalpyTable->outputPVTTableData( pvtOutputOpts );
m_CO2EnthalpyTable->outputPVTTableData( pvtOutputOpts );
m_CO2EnthalpyTable->outputTableData( pvtOutputOpts );
m_CO2EnthalpyTable->outputTableData( pvtOutputOpts );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ CO2Solubility::CO2Solubility( string const & name,

std::tie( m_CO2SolubilityTable, m_WaterVapourisationTable ) = makeSolubilityTables( m_modelName, inputParams, solubilityModel );

m_CO2SolubilityTable->outputPVTTableData( pvtOutputOpts );
m_WaterVapourisationTable->outputPVTTableData( pvtOutputOpts );
m_CO2SolubilityTable->outputTableData( pvtOutputOpts );
m_WaterVapourisationTable->outputTableData( pvtOutputOpts );

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ EzrokhiBrineDensity::EzrokhiBrineDensity( string const & name,
m_waterSatDensityTable = PureWaterProperties::makeSaturationDensityTable( m_functionName, FunctionManager::getInstance() );
m_waterSatPressureTable = PureWaterProperties::makeSaturationPressureTable( m_functionName, FunctionManager::getInstance() );

m_waterSatPressureTable->outputPVTTableData( pvtOutputOpts );
m_waterSatDensityTable->outputPVTTableData( pvtOutputOpts );
m_waterSatPressureTable->outputTableData( pvtOutputOpts );
m_waterSatDensityTable->outputTableData( pvtOutputOpts );
}

void EzrokhiBrineDensity::makeCoefficients( string_array const & inputPara )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ EzrokhiBrineViscosity::EzrokhiBrineViscosity( string const & name,
makeCoefficients( inputPara );
m_waterViscosityTable = PureWaterProperties::makeSaturationViscosityTable( m_functionName, FunctionManager::getInstance() );

m_waterViscosityTable->outputPVTTableData( pvtOutputOpts );
m_waterViscosityTable->outputTableData( pvtOutputOpts );
}

void EzrokhiBrineViscosity::makeCoefficients( string_array const & inputPara )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ FenghourCO2Viscosity::FenghourCO2Viscosity( string const & name,
{
m_CO2ViscosityTable = makeViscosityTable( inputParams, m_functionName, FunctionManager::getInstance() );

m_CO2ViscosityTable->outputPVTTableData( pvtOutputOpts );
m_CO2ViscosityTable->outputTableData( pvtOutputOpts );
}

void FenghourCO2Viscosity::checkTablesParameters( real64 const pressure,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ PhillipsBrineDensity::PhillipsBrineDensity( string const & name,

m_brineDensityTable = makeDensityTable( inputParams, m_functionName, FunctionManager::getInstance() );

m_brineDensityTable->outputPVTTableData( pvtOutputOpts );
m_brineDensityTable->outputTableData( pvtOutputOpts );
}

PhillipsBrineDensity::KernelWrapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ PhillipsBrineViscosity::PhillipsBrineViscosity( string const & name,
m_waterViscosityTable = PureWaterProperties::makeSaturationViscosityTable( m_functionName, FunctionManager::getInstance() );
makeCoefficients( inputPara );

m_waterViscosityTable->outputPVTTableData( pvtOutputOpts );
m_waterViscosityTable->outputTableData( pvtOutputOpts );
}

void PhillipsBrineViscosity::makeCoefficients( string_array const & inputPara )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ SpanWagnerCO2Density::SpanWagnerCO2Density( string const & name,

m_CO2DensityTable = makeDensityTable( inputParams, m_functionName, FunctionManager::getInstance() );

m_CO2DensityTable->outputPVTTableData( pvtOutputOpts );
m_CO2DensityTable->outputTableData( pvtOutputOpts );
}

void SpanWagnerCO2Density::checkTablesParameters( real64 const pressure,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ WaterDensity::WaterDensity( string const & name,
GEOS_UNUSED_VAR( inputParams );
m_waterDensityTable = PureWaterProperties::makeSaturationDensityTable( m_functionName, FunctionManager::getInstance() );

m_waterDensityTable->outputPVTTableData( pvtOutputOpts );
m_waterDensityTable->outputTableData( pvtOutputOpts );
}

void WaterDensity::checkTablesParameters( real64 const pressure,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ ReactiveBrineFluid( string const & name, Group * const parent ):
setApplyDefaultValue( 0 ).
setInputFlag( InputFlags::OPTIONAL ).
setRestartFlags( RestartFlags::NO_WRITE ).
setDescription( "Write PVT tables into a CSV file" );
setDescription( "When set to 1, write PVT tables into a CSV file" );

// if this is a thermal model, we need to make sure that the arrays will be properly displayed and saved to restart
if( isThermal() )
Expand Down Expand Up @@ -203,7 +203,7 @@ void ReactiveBrineFluid< PHASE > ::createPVTModels()
bool const isClone = this->isClone();
TableFunction::OutputOptions const pvtOutputOpts = {
!isClone && m_writeCSV,// writeCSV
!isClone && (getLogLevel() >= 0 && logger::internal::rank==0), // writeInLog
!isClone && getLogLevel() >= 0, // writeInLog
};

// then, we are ready to instantiate the phase models
Expand Down
7 changes: 6 additions & 1 deletion src/coreComponents/dataRepository/Group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1503,8 +1503,13 @@ class Group
*/
void setLogLevel( integer const logLevel ) { m_logLevel = logLevel; }

/// @return The verbosity level
/**
* @return The verbosity level of the Group instance.
* @warning For logging activation, *Please use `isLogLevelActive< logInfo::yourInfo >( getLogLevel() )`*
* to be sure to document to the user what the Group is able to output.
*/
integer getLogLevel() const { return m_logLevel; }

///@}

/**
Expand Down
1 change: 1 addition & 0 deletions src/coreComponents/functions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Contains classes for storing and computing arbitrary N-dimensional functions.
set( functions_headers
FunctionBase.hpp
FunctionManager.hpp
LogLevelsInfo.hpp
TableFunction.hpp
)

Expand Down
7 changes: 7 additions & 0 deletions src/coreComponents/functions/FunctionBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#include "FunctionBase.hpp"
#include "common/MpiWrapper.hpp"

namespace geos
{
Expand Down Expand Up @@ -81,6 +82,12 @@ void FunctionBase::setOutputDirectory( string const & dir )
{
string & outputDirectory = const_cast< string & >( getOutputDirectory() );
outputDirectory = dir;

if( MpiWrapper::commRank( MPI_COMM_GEOS ) == 0 )
{
makeDirsForPath( dir );
}
MpiWrapper::barrier( MPI_COMM_GEOS );
}


Expand Down
50 changes: 50 additions & 0 deletions src/coreComponents/functions/LogLevelsInfo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* ------------------------------------------------------------------------------------------------------------
* SPDX-License-Identifier: LGPL-2.1-only
*
* Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
* Copyright (c) 2018-2024 TotalEnergies
* Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
* Copyright (c) 2023-2024 Chevron
* Copyright (c) 2019- GEOS/GEOSX Contributors
* All rights reserved
*
* See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
* ------------------------------------------------------------------------------------------------------------
*/

/**
* @file LogLevelsInfo.hpp
* This file contains common log level informations for physics solvers
*/

#ifndef GEOS_PHYSICSSOLVERS_LOGLEVELSINFO_HPP
#define GEOS_PHYSICSSOLVERS_LOGLEVELSINFO_HPP

#include "common/DataTypes.hpp"

namespace geos
{

namespace logInfo
{

/// @cond DO_NOT_DOCUMENT

struct TableDataOutput
{
static constexpr int getMinLogLevel() { return 1; }
static constexpr std::string_view getDescription()
{
return "Output the loaded/computed table data in the log if succinct enough,"
" otherwise output it in a CSV file.";
}
};

/// @endcond

}

}

#endif // GEOS_PHYSICSSOLVERS_LOGLEVELSINFO_HPP
87 changes: 53 additions & 34 deletions src/coreComponents/functions/TableFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
*/

#include "TableFunction.hpp"
#include "LogLevelsInfo.hpp"
#include "codingUtilities/Parsing.hpp"
#include "common/DataTypes.hpp"
#include "common/MpiWrapper.hpp"

#include <algorithm>

Expand Down Expand Up @@ -57,6 +59,14 @@ TableFunction::TableFunction( const string & name,
setInputFlag( InputFlags::OPTIONAL ).
setDescription( "Interpolation method. Valid options:\n* " + EnumStrings< InterpolationType >::concat( "\n* " ) ).
setApplyDefaultValue( m_interpolationMethod );

registerWrapper( viewKeyStruct::writeCSVFlagString(), &m_writeCSV ).
setApplyDefaultValue( 0 ).
setInputFlag( InputFlags::OPTIONAL ).
setRestartFlags( RestartFlags::NO_WRITE ).
setDescription( "When set to 1, write the table into a CSV file" );

addLogLevel< logInfo::TableDataOutput >();
}

void TableFunction::readFile( string const & filename, array1d< real64 > & target )
Expand Down Expand Up @@ -264,25 +274,38 @@ void collectValues( std::ostringstream & formatterStream,
}
}

void TableFunction::outputPVTTableData( OutputOptions const pvtOutputOpts ) const
void TableFunction::outputTableData( OutputOptions const outputOpts ) const
{
if( pvtOutputOpts.writeInLog && this->numDimensions() <= 2 )
// we only output from rank 0
if( MpiWrapper::commRank() != 0 )
return;

if( outputOpts.writeInLog && this->numDimensions() <= 2 )
{
TableTextFormatter textFormatter;
GEOS_LOG_RANK_0( textFormatter.toString( *this ));
GEOS_LOG( textFormatter.toString( *this ));
}
if( pvtOutputOpts.writeCSV || ( pvtOutputOpts.writeInLog && this->numDimensions() >= 3 ) )
if( outputOpts.writeCSV || ( outputOpts.writeInLog && this->numDimensions() >= 3 ) )
{
string const filename = this->getName();
std::ofstream logStream( joinPath( FunctionBase::getOutputDirectory(), filename + ".csv" ) );
GEOS_LOG_RANK_0( GEOS_FMT( "CSV Generated to {}/{}.csv \n",
FunctionBase::getOutputDirectory(),
filename ));
GEOS_LOG( GEOS_FMT( "CSV Generated to {}/{}.csv \n",
FunctionBase::getOutputDirectory(),
filename ));
TableCSVFormatter csvFormatter;
logStream << csvFormatter.toString( *this );
}
}

void TableFunction::initializePostSubGroups()
{
// Output user defined tables (not generated PVT tables)
outputTableData( OutputOptions{
m_writeCSV != 0, // writeCSV
isLogLevelActive< logInfo::TableDataOutput >( getLogLevel() ) // writeInLog
} );
}

template<>
string TableCSVFormatter::toString< TableFunction >( TableFunction const & tableFunction ) const
{
Expand Down Expand Up @@ -318,16 +341,15 @@ string TableCSVFormatter::toString< TableFunction >( TableFunction const & table
template<>
string TableTextFormatter::toString< TableFunction >( TableFunction const & tableFunction ) const
{
static constexpr integer maxRows = 500;
ArrayOfArraysView< real64 const > coordinates = tableFunction.getCoordinates();
units::Unit const valueUnit = tableFunction.getValueUnit();
arrayView1d< real64 const > const values = tableFunction.getValues();
integer const numDimensions = LvArray::integerConversion< integer >( coordinates.size() );
std::string_view filename = tableFunction.getName();
string logOutput;

GEOS_LOG_RANK_0( GEOS_FMT( "Values in the table are represented by : {}", units::getDescription( valueUnit )));

if( numDimensions == 1 )
if( numDimensions == 1 && coordinates[0].size() < maxRows )
{
TableData tableData;
arraySlice1d< real64 const > const coords = coordinates[0];
Expand All @@ -342,31 +364,28 @@ string TableTextFormatter::toString< TableFunction >( TableFunction const & tabl
TableTextFormatter const logTable( tableLayout );
logOutput = logTable.toString( tableData );
}
else if( numDimensions == 2 )
else if( numDimensions == 2 && ( coordinates[0].size() * coordinates[1].size() ) < maxRows )
{
integer const nX = coordinates[0].size();
integer const nY = coordinates[1].size();
if( nX * nY <= 500 )
{
TableData2D tableData2D;
TableData2D::TableDataHolder tableConverted;
tableConverted = tableData2D.convertTable2D( values,
valueUnit,
coordinates,
units::getDescription( tableFunction.getDimUnit( 0 ) ),
units::getDescription( tableFunction.getDimUnit( 1 ) ));

TableLayout const tableLayout( filename, tableConverted.headerNames );
TableTextFormatter const table2DLog( tableLayout );
logOutput = table2DLog.toString( tableConverted.tableData );
}
else
{
string const log = GEOS_FMT( "The {} PVT table exceeding 500 rows.\nTo visualize the tables, go to the generated csv", filename );
TableLayout const tableLayoutInfos( filename, {log} );
TableTextFormatter const tableLog( tableLayoutInfos );
logOutput = tableLog.toString();
}
TableData2D tableData2D;
TableData2D::TableDataHolder tableConverted;
tableConverted = tableData2D.convertTable2D( values,
valueUnit,
coordinates,
units::getDescription( tableFunction.getDimUnit( 0 ) ),
units::getDescription( tableFunction.getDimUnit( 1 ) ));

TableLayout const tableLayout( filename, tableConverted.headerNames );
TableTextFormatter const table2DLog( tableLayout );
logOutput = table2DLog.toString( tableConverted.tableData );
}
else
{
string const tooLongOutputMsg = GEOS_FMT( "The {} table is too heavy for log output.\n"
"To visualize the table, please refer to the generated csv.",
filename, maxRows );
TableLayout const tableLayoutInfos( filename, {tooLongOutputMsg} );
TableTextFormatter const tableLog( tableLayoutInfos );
logOutput = tableLog.toString();
}
return logOutput;
}
Expand Down
Loading
Loading