-
Notifications
You must be signed in to change notification settings - Fork 105
refactor: replace hand-written CSVs with the Table infrastructure #4039
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
Open
kdrienCG
wants to merge
13
commits into
develop
Choose a base branch
from
refactor/kdrienCG/useTablesWithCSVs
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+165
−110
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4973ac1
overload TableLayout constructor
kdrienCG 0a35970
replace manual CSVs with Tables
kdrienCG 4929636
relocate csv parts creation in the same place
kdrienCG f5ce7de
use strings instead of columns for readability
kdrienCG bf623e3
fix incomplete unit in a column title
kdrienCG c3d2b6d
remove unused lambda capture
kdrienCG 006f34d
add condition to write CSV header if the file is empty
kdrienCG d0b6e43
Merge branch 'develop' into refactor/kdrienCG/useTablesWithCSVs
kdrienCG 150dec4
remove comment
kdrienCG 20ec0ea
commit to trigger CI
kdrienCG b07a56b
Merge branch 'develop' into refactor/kdrienCG/useTablesWithCSVs
kdrienCG 80f88b4
modify fileName type to path
kdrienCG 0cbc4ab
Merge branch 'develop' into refactor/kdrienCG/useTablesWithCSVs
kdrienCG File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,14 @@ | |
|
|
||
| #include "codingUtilities/Utilities.hpp" | ||
| #include "common/DataTypes.hpp" | ||
| #include "common/Path.hpp" | ||
| #include "common/StdContainerWrappers.hpp" | ||
| #include "common/TimingMacros.hpp" | ||
| #include "common/format/Format.hpp" | ||
| #include "common/format/table/TableData.hpp" | ||
| #include "common/format/table/TableFormatter.hpp" | ||
| #include "common/format/table/TableLayout.hpp" | ||
| #include "common/logger/Logger.hpp" | ||
| #include "constitutive/ConstitutiveManager.hpp" | ||
| #include "constitutive/fluid/multifluid/MultiFluidBase.hpp" | ||
| #include "constitutive/fluid/multifluid/MultiFluidFields.hpp" | ||
|
|
@@ -50,6 +57,7 @@ | |
| #include "physicsSolvers/fluidFlow/kernels/compositional/ThermalPhaseVolumeFractionKernel.hpp" | ||
| #include "physicsSolvers/fluidFlow/kernels/compositional/FluidUpdateKernel.hpp" | ||
| #include "physicsSolvers/fluidFlow/CompositionalMultiphaseStatistics.hpp" | ||
| #include <filesystem> | ||
|
|
||
| #if defined( __INTEL_COMPILER ) | ||
| #pragma GCC optimize "O0" | ||
|
|
@@ -260,34 +268,6 @@ void CompositionalMultiphaseWell::registerDataOnMesh( Group & meshBodies ) | |
| wellControls.registerWrapper< real64 >( viewKeyStruct::massDensityString() ); | ||
|
|
||
| wellControls.registerWrapper< real64 >( viewKeyStruct::currentMassRateString() ); | ||
|
|
||
| // write rates output header | ||
| // the rank that owns the reference well element is responsible | ||
| if( m_writeCSV > 0 && subRegion.isLocallyOwned() ) | ||
| { | ||
| string const fileName = GEOS_FMT( "{}/{}.csv", m_ratesOutputDir, wellControls.getName() ); | ||
| string const massUnit = m_useMass ? "kg" : "mol"; | ||
| integer const useSurfaceConditions = wellControls.useSurfaceConditions(); | ||
| string const conditionKey = useSurfaceConditions ? "surface" : "reservoir"; | ||
| string const unitKey = useSurfaceConditions ? "s" : "r"; | ||
| integer const numPhase = m_numPhases; | ||
| integer const numComp = m_numComponents; | ||
| // format: time,bhp,total_rate,total_vol_rate,phase0_vol_rate,phase1_vol_rate,... | ||
| makeDirsForPath( m_ratesOutputDir ); | ||
| GEOS_LOG( GEOS_FMT( "{}: Rates CSV generated at {}", getName(), fileName ) ); | ||
| std::ofstream outputFile( fileName ); | ||
| outputFile << "Time [s],dt[s],BHP [Pa],Total rate [" << massUnit << "/s],Total " << conditionKey << " volumetric rate [" << unitKey << "m3/s]"; | ||
| for( integer ip = 0; ip < numPhase; ++ip ) | ||
| { | ||
| outputFile << ",Phase" << ip << " " << conditionKey << " volumetric rate [" << unitKey << "m3/s]"; | ||
| } | ||
| for( integer ic = 0; ic < numComp; ++ic ) | ||
| { | ||
| outputFile << ",Component" << ic << " rate [" << massUnit << "/s]"; | ||
| } | ||
| outputFile << std::endl; | ||
| outputFile.close(); | ||
| } | ||
| } ); | ||
| } ); | ||
|
|
||
|
|
@@ -2223,57 +2203,108 @@ void CompositionalMultiphaseWell::printRates( real64 const & time_n, | |
| } | ||
|
|
||
| string const wellControlsName = wellControls.getName(); | ||
|
|
||
| // format: time,total_rate,total_vol_rate,phase0_vol_rate,phase1_vol_rate,... | ||
| std::ofstream outputFile; | ||
| if( m_writeCSV > 0 ) | ||
| string const massUnit = m_useMass ? "kg" : "mol"; | ||
| integer const useSurfaceConditions = wellControls.useSurfaceConditions(); | ||
| string const conditionKey = useSurfaceConditions ? "surface" : "reservoir"; | ||
| string const unitKey = useSurfaceConditions ? "s" : "r"; | ||
|
|
||
| stdVector< string > columnNames; | ||
| columnNames.reserve( 5 + numPhase + numComp ); | ||
| columnNames.emplace_back( GEOS_FMT( "Time [{}]", units::getSymbol( units::Unit::Time ) ) ); | ||
| columnNames.emplace_back( GEOS_FMT( "dt [{}]", units::getSymbol( units::Unit::Time ) ) ); | ||
| columnNames.emplace_back( GEOS_FMT( "BHP [{}]", units::getSymbol( units::Unit::Pressure ) ) ); | ||
| columnNames.emplace_back( GEOS_FMT( "Total rate [{}/s]", massUnit ) ); | ||
| columnNames.emplace_back( GEOS_FMT( "Total {} volumetric rate [{}m3/s]", conditionKey, unitKey ) ); | ||
| for( integer ip = 0; ip < numPhase; ++ip ) | ||
| { | ||
| outputFile.open( m_ratesOutputDir + "/" + wellControlsName + ".csv", std::ios_base::app ); | ||
| outputFile << time_n << "," << dt; | ||
| columnNames.emplace_back( GEOS_FMT( "Phase {} {} volumetric rate [{}m3/s]", ip, conditionKey, unitKey ) ); | ||
| } | ||
| for( integer ic = 0; ic < numComp; ++ic ) | ||
| { | ||
| columnNames.emplace_back( GEOS_FMT( "Component {} rate [{}/s]", ic, massUnit ) ); | ||
| } | ||
|
Comment on lines
+2211
to
+2225
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If possible, relocate this work at initialization or only for the first time.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same request for |
||
| size_t const numColumns = columnNames.size(); | ||
|
|
||
| auto const buildRow = [&]( real64 const bhp, | ||
| real64 const totalRate, | ||
| real64 const totalVolRate, | ||
| auto const & phaseVolRates, | ||
| auto const & compRates ) | ||
| { | ||
| stdVector< TableData::CellData > row; | ||
| row.reserve( numColumns ); | ||
| row.push_back( { CellType::Value, GEOS_FMT( "{}", time_n ) } ); | ||
| row.push_back( { CellType::Value, GEOS_FMT( "{}", dt ) } ); | ||
| row.push_back( { CellType::Value, GEOS_FMT( "{}", bhp ) } ); | ||
| row.push_back( { CellType::Value, GEOS_FMT( "{}", totalRate ) } ); | ||
| row.push_back( { CellType::Value, GEOS_FMT( "{}", totalVolRate ) } ); | ||
| for( integer ip = 0; ip < numPhase; ++ip ) | ||
| { | ||
| row.push_back( { CellType::Value, GEOS_FMT( "{}", phaseVolRates[ ip ] ) } ); | ||
| } | ||
| for( integer ic = 0; ic < numComp; ++ic ) | ||
| { | ||
| row.push_back( { CellType::Value, GEOS_FMT( "{}", compRates[ ic ] ) } ); | ||
| } | ||
| GEOS_ERROR_IF_NE_MSG( row.size(), numColumns, | ||
| "CSV rates row size does not match header size for " | ||
| << wellControlsName ); | ||
| return row; | ||
| }; | ||
|
|
||
| std::filesystem::path const fileName = | ||
| std::filesystem::path( m_ratesOutputDir ) / ( wellControlsName + ".csv" ); | ||
|
|
||
| auto const writeCSVRow = [&]( TableData const & data ) | ||
| { | ||
| if( !std::filesystem::exists( fileName ) || std::filesystem::is_empty( fileName ) ) | ||
| { | ||
| makeDirsForPath( m_ratesOutputDir ); | ||
| TableLayout const tableLayout( columnNames ); | ||
| TableCSVFormatter const csvFormatter( tableLayout ); | ||
| std::ofstream outputFile( fileName ); | ||
| csvFormatter.headerToStream( outputFile ); | ||
| outputFile.close(); | ||
| GEOS_LOG( GEOS_FMT( "{}: Rates CSV generated at {}", getName(), fileName.string() ) ); | ||
| } | ||
| std::ofstream outputFile( fileName, std::ios_base::app ); | ||
| TableCSVFormatter const csvFormatter; | ||
| csvFormatter.dataToStream( outputFile, data ); | ||
| outputFile.close(); | ||
| }; | ||
|
|
||
| if( wellControls.getWellStatus() == WellControls::Status::CLOSED ) | ||
| { | ||
| GEOS_LOG( GEOS_FMT( "{}: well is shut", wellControlsName ) ); | ||
| if( outputFile.is_open()) | ||
| if( m_writeCSV > 0 ) | ||
| { | ||
| // print all zeros in the rates file | ||
| outputFile << ",0.0,0.0,0.0"; | ||
| for( integer ip = 0; ip < numPhase; ++ip ) | ||
| { | ||
| outputFile << ",0.0"; | ||
| } | ||
| for( integer ic = 0; ic < numComp; ++ic ) | ||
| { | ||
| outputFile << ",0.0"; | ||
| } | ||
| outputFile << std::endl; | ||
| outputFile.close(); | ||
| stdVector< real64 > const zeroesNumPhase( numPhase, 0.0 ); | ||
| stdVector< real64 > const zeroesNumComp( numComp, 0.0 ); | ||
| TableData ratesTableData; | ||
| ratesTableData.addRow( buildRow( 0.0, 0.0, 0.0, zeroesNumPhase, zeroesNumComp ) ); | ||
| writeCSVRow( ratesTableData ); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| localIndex const iwelemRef = subRegion.getTopWellElementIndex(); | ||
| string const massUnit = m_useMass ? "kg" : "mol"; | ||
|
|
||
| // subRegion data | ||
|
|
||
| arrayView1d< real64 const > const & connRate = | ||
| subRegion.getField< well::mixtureConnectionRate >(); | ||
|
|
||
| integer const useSurfaceConditions = wellControls.useSurfaceConditions(); | ||
|
|
||
| real64 const & currentBHP = | ||
| wellControls.getReference< real64 >( CompositionalMultiphaseWell::viewKeyStruct::currentBHPString() ); | ||
| arrayView1d< real64 const > const & currentPhaseVolRate = | ||
| wellControls.getReference< array1d< real64 > >( CompositionalMultiphaseWell::viewKeyStruct::currentPhaseVolRateString() ); | ||
| real64 const & currentTotalVolRate = | ||
| wellControls.getReference< real64 >( CompositionalMultiphaseWell::viewKeyStruct::currentTotalVolRateString() ); | ||
|
|
||
| TableData ratesTableData; | ||
|
|
||
| // bring everything back to host, capture the scalars by reference | ||
| forAll< serialPolicy >( 1, [&numPhase, | ||
| &numComp, | ||
| &useSurfaceConditions, | ||
| ¤tBHP, | ||
| connRate, | ||
| ¤tTotalVolRate, | ||
|
|
@@ -2282,11 +2313,11 @@ void CompositionalMultiphaseWell::printRates( real64 const & time_n, | |
| &iwelemRef, | ||
| &wellControlsName, | ||
| &massUnit, | ||
| &outputFile] ( localIndex const ) | ||
| &conditionKey, | ||
| &unitKey, | ||
| &ratesTableData, | ||
| &buildRow] ( localIndex const ) | ||
| { | ||
| string const conditionKey = useSurfaceConditions ? "surface" : "reservoir"; | ||
| string const unitKey = useSurfaceConditions ? "s" : "r"; | ||
|
|
||
| real64 const currentTotalRate = connRate[iwelemRef]; | ||
| GEOS_LOG( GEOS_FMT( "{}: BHP (at the specified reference elevation): {} Pa", | ||
| wellControlsName, currentBHP ) ); | ||
|
|
@@ -2295,22 +2326,13 @@ void CompositionalMultiphaseWell::printRates( real64 const & time_n, | |
| for( integer ip = 0; ip < numPhase; ++ip ) | ||
| GEOS_LOG( GEOS_FMT( "{}: Phase {} {} volumetric rate: {} {}m3/s", | ||
| wellControlsName, ip, conditionKey, currentPhaseVolRate[ip], unitKey ) ); | ||
| if( outputFile.is_open()) | ||
| { | ||
| outputFile << "," << currentBHP; | ||
| outputFile << "," << currentTotalRate << "," << currentTotalVolRate; | ||
| for( integer ip = 0; ip < numPhase; ++ip ) | ||
| { | ||
| outputFile << "," << currentPhaseVolRate[ip]; | ||
| } | ||
| for( integer ic = 0; ic < numComp; ++ic ) | ||
| { | ||
| outputFile << "," << compRate[ic]; | ||
| } | ||
| outputFile << std::endl; | ||
| outputFile.close(); | ||
| } | ||
| ratesTableData.addRow( buildRow( currentBHP, currentTotalRate, currentTotalVolRate, currentPhaseVolRate, compRate ) ); | ||
| } ); | ||
|
|
||
| if( m_writeCSV > 0 ) | ||
| { | ||
| writeCSVRow( ratesTableData ); | ||
| } | ||
| } ); | ||
| } ); | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not add already added includes (maybe deactivate clangd auto-include?)
(also for the
<filesystem>below)