Skip to content

Commit

Permalink
merged row updated + multiphase case updated
Browse files Browse the repository at this point in the history
  • Loading branch information
arng40 committed Dec 16, 2024
1 parent 4030629 commit 9d6aab9
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 44 deletions.
7 changes: 4 additions & 3 deletions src/coreComponents/common/format/table/TableData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@
namespace geos
{

void TableData::addRow( std::vector< TableData::CellData > & cells )
void TableData::addRow( std::vector< TableData::CellData > const & rows )
{
if( m_rows.size() != 0 && cells.size() != m_rows[m_rows.size() - 1].size() )
if( m_rows.size() != 0 && rows.size() != m_rows[m_rows.size() - 1].size() )
{
string msg = "Remarks : some cells may be missing";
std::cout << '\n';
if( std::find( m_errorsMsg.begin(), m_errorsMsg.end(), msg ) == m_errorsMsg.end())
{
m_errorsMsg.push_back( msg );
}
}

m_rows.push_back( cells );
m_rows.push_back( rows );
}

void TableData::addSeparator()
Expand Down
8 changes: 4 additions & 4 deletions src/coreComponents/common/format/table/TableData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class TableData
struct CellData
{
CellType type;
string value;
string value = "";
};

/**
Expand All @@ -53,7 +53,7 @@ class TableData
* @brief Add a row to the table
* @param row A vector of string representing a row
*/
void addRow( std::vector< CellData > & row );
void addRow( std::vector< CellData > const & row );

/**
* @brief Add a line separator to the table
Expand Down Expand Up @@ -175,11 +175,11 @@ void TableData::addRow( Args const &... args )
if constexpr (std::is_same_v< Args, CellType >) {
if( args == CellType::Separator )
{
cells.push_back( {CellType::Separator, "-"} );
cells.push_back( {CellType::Separator} );
}
else
{
cells.push_back( {CellType::MergeNext, " "} );
cells.push_back( {CellType::MergeNext} );
}
}
else
Expand Down
60 changes: 53 additions & 7 deletions src/coreComponents/common/format/table/TableFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ string TableCSVFormatter::dataToString( TableData const & tableData ) const
result.append( stringutilities::join( rowConverted.cbegin(), rowConverted.cend(), "," ));
result.append( "\n" );
}

return result;
}

Expand Down Expand Up @@ -256,21 +256,19 @@ void TableTextFormatter::populateHeaderCellsLayout( TableLayout & tableLayout,
cellsHeaderLayout[idxRow + 1].push_back( emptyCell );
}
}

currentCell.m_maxDataLength = it->getMaxStringSize();

if( it->hasParent() )
{
it->m_parent->m_cellWidth += it->m_cellWidth == 0 ? 1 : it->m_cellWidth;
}

sublineHeaderCounts[currentLayer] = std::max( sublineHeaderCounts[currentLayer],
currentCell.m_lines.size() );

if( it->m_cellWidth > 1 )
{
it->m_cellWidth--;
}

sublineHeaderCounts[currentLayer] = std::max( sublineHeaderCounts[currentLayer],
currentCell.m_lines.size() );
for( size_t idxColumn = 0; idxColumn < it->m_cellWidth; idxColumn++ )
{
TableLayout::CellLayout mergingCell{ CellType::MergeNext, "", TableLayout::Alignment::center };
Expand Down Expand Up @@ -324,10 +322,13 @@ void TableTextFormatter::populateDataCellsLayout( TableLayout & tableLayout,
{
cell.type = it->m_columName.m_cellType;
}
if( it->m_columName.m_cellType == CellType::Separator )
{
cell.value = m_horizontalLine;
}
cellsDataLayout[idxRow][idxColumn] = TableLayout::CellLayout( cell.type, cell.value, alignement );
maxLinesPerRow = std::max( maxLinesPerRow, cellsDataLayout[idxRow][idxColumn].m_lines.size() );
idxColumn++;

}
}
subDataLineCounts.push_back( { maxLinesPerRow } );
Expand Down Expand Up @@ -378,26 +379,70 @@ void TableTextFormatter::updateColumnMaxLength( TableLayout & tableLayout,
};

size_t const numColumns = cellsHeaderLayout[0].size();
//each idx per row
std::vector< size_t > accMaxStringColumn( cellsDataLayout.size(), 0 );
for( size_t idxColumn = 0; idxColumn < numColumns; ++idxColumn )
{
size_t maxColumnSize = 1;

// init header column max length
for( size_t rowIdx = 0; rowIdx < cellsDataLayout.size(); ++rowIdx )
{
size_t const cellDataLength = getMaxLineLength( cellsDataLayout[rowIdx][idxColumn].m_lines );
maxColumnSize = std::max( {maxColumnSize, cellDataLength} );
}

for( size_t rowIdx = 0; rowIdx < cellsHeaderLayout.size(); ++rowIdx )
{
size_t const cellHeaderLength = getMaxLineLength( cellsHeaderLayout[rowIdx][idxColumn].m_lines );
maxColumnSize = std::max( {maxColumnSize, cellHeaderLength} );
cellsHeaderLayout[rowIdx][idxColumn].m_maxDataLength = maxColumnSize;
}

// update maxColumnSize for data cell
for( size_t rowIdx = 0; rowIdx < cellsDataLayout.size(); ++rowIdx )
{
TableLayout::CellLayout & dataCell = cellsDataLayout[rowIdx][idxColumn];
TableLayout::CellLayout * previousDataCell = (idxColumn > 0) ? &cellsDataLayout[rowIdx][idxColumn - 1] : nullptr;

if( dataCell.m_cellType == CellType::MergeNext )
{
accMaxStringColumn[rowIdx] += cellsHeaderLayout[0][idxColumn].m_maxDataLength + tableLayout.getColumnMargin();
}

if( idxColumn > 0 &&
previousDataCell->m_cellType == CellType::MergeNext && dataCell.m_cellType != CellType::MergeNext )
{
size_t sumOfMergingCell = accMaxStringColumn[rowIdx] + cellsHeaderLayout[0][idxColumn].m_maxDataLength;
if( sumOfMergingCell < dataCell.m_maxDataLength )
{
maxColumnSize += dataCell.m_maxDataLength - sumOfMergingCell;
for( size_t rowIdx2 = 0; rowIdx2 < rowIdx; rowIdx2++ )
{
TableLayout::CellLayout * previousDataCellTemp = (idxColumn > 0) ? &cellsDataLayout[rowIdx2][idxColumn - 1] : nullptr;
if( previousDataCellTemp )
{
previousDataCellTemp->m_maxDataLength = cellsHeaderLayout[0][idxColumn - 1].m_maxDataLength;
}
updateCellMaxLength( cellsDataLayout[rowIdx2][idxColumn], maxColumnSize, previousDataCellTemp );
if( cellsDataLayout[rowIdx2][idxColumn].m_cellType == CellType::Separator )
{
size_t const additionnalMargin = (idxColumn == numColumns - 1) ?
tableLayout.getBorderMargin() * 2 + 2 :
tableLayout.getColumnMargin();
cellsDataLayout[rowIdx2][idxColumn].m_lines[0] = std::string( maxColumnSize + additionnalMargin, '-' );
}
}
}

accMaxStringColumn[rowIdx] = 0;
}
else
{
size_t const cellDataLength = getMaxLineLength( cellsDataLayout[rowIdx][idxColumn].m_lines );
maxColumnSize = std::max( {maxColumnSize, cellDataLength} );
}

updateCellMaxLength( dataCell, maxColumnSize, previousDataCell );

if( dataCell.m_cellType == CellType::Separator )
Expand All @@ -411,6 +456,7 @@ void TableTextFormatter::updateColumnMaxLength( TableLayout & tableLayout,
}
}

// last pass for updating cells header
for( size_t rowIdx = 0; rowIdx < cellsHeaderLayout.size(); ++rowIdx )
{
TableLayout::CellLayout & headerCell = cellsHeaderLayout[rowIdx][idxColumn];
Expand Down
5 changes: 2 additions & 3 deletions src/coreComponents/common/format/table/TableLayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,11 @@ class TableLayout

/**
* @struct CellLayout
* @brief Structure representing a cell in a table.
* This structure contains information about the cell such as its type, alignment, maximum data length, and width.
* @brief Structure grouping the cell information to display it in a table (content, type, alignment, ...).
*/
struct CellLayout
{
/// vector containing the cell name separated by a '\n'.
/// vector containing each cell content, separated by lines.
std::vector< string > m_lines;
/// The type of the cell (Header,Value, Merge, ...).
CellType m_cellType;
Expand Down
44 changes: 25 additions & 19 deletions src/coreComponents/common/format/table/unitTests/testTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,39 +87,35 @@ TEST( testTable, tableClassic )
);
}

TEST( testTable, tableColumnParamClassic ) //TODO
TEST( testTable, tableColumnParamClassic )
{
TableLayout tableLayout( {
TableLayout const tableLayout( {
TableLayout::Column()
.setName( "Cras egestas" )
.setHeaderAlignment( TableLayout::Alignment::left ),
.setName( "Cras egestas" ),
TableLayout::Column()
.setName( "CoordX" )
.setHeaderAlignment( TableLayout::Alignment::left ),
.setName( "CoordX" ),
TableLayout::Column()
.setName( "C" )
.setHeaderAlignment( TableLayout::Alignment::left ),
.setName( "C" ),
TableLayout::Column()
.setName( "CoordZ" )
.setHeaderAlignment( TableLayout::Alignment::left ),
.setName( "CoordZ" ),
TableLayout::Column()
.setName( "Prev\nelement" )
.setHeaderAlignment( TableLayout::Alignment::right ),
.setName( "Prev\nelement" ),
TableLayout::Column()
.setName( "Next\nelement" )
.setHeaderAlignment( TableLayout::Alignment::right )} );
.setName( "Next\nelement" )} );

TableData tableData;
tableData.addRow( "value1", "", "3.0", 3.0129877, 2.0f, 1 );
tableData.addRow( "value1", "gaz\nwater", "3.0\n42.0", "3.0129877\n0.0123456", "2\n3", "1\n4" );
tableData.addRow( "val1", "v", "[3.045,42.02,89.25]", 3.0, 10.0f, 3 );

TableTextFormatter const tableText( tableLayout );
std::cout << tableText.toString( tableData )<< std::endl;
EXPECT_EQ( tableText.toString( tableData ),
"\n-------------------------------------------------------------------------------------------\n"
"| Cras egestas | CoordX | C | CoordZ | Prev | Next |\n"
"| | | | | element | element |\n"
"-------------------------------------------------------------------------------------------\n"
"| value1 | | 3.0 | 3.0129877 | 2 | 1 |\n"
"| value1 | gaz | 3.0 | 3.0129877 | 2 | 1 |\n"
"| | water | 42.0 | 0.0123456 | 3 | 4 |\n"
"| val1 | v | [3.045,42.02,89.25] | 3 | 10 | 3 |\n"
"-------------------------------------------------------------------------------------------\n\n"
);
Expand Down Expand Up @@ -357,7 +353,7 @@ TEST( testTable, variadicTest )
std::cout << std::endl;
TableData tableData;
tableData.addRow( "min(local/total)", 1, 2, 3, 4, 5, 6, 7 );
tableData.addRow( "min(local/total)", 1, 2, 3, 4, 5, 6, 7 ) ;
tableData.addRow( "min(local/total)", 1, 2, 3, 4, 5, 6, 7 );
TableTextFormatter log( layoutTest );
EXPECT_EQ( log.toString( tableData ),
"\n--------------------------------------------------------------------------------------------------------------------------------------\n"
Expand Down Expand Up @@ -415,27 +411,37 @@ TEST( testTable, testCellMerging )

TableData tableData;
tableData.addRow( "ProductA", 1234, 40, "ProductName", 5678, 60 );
tableData.addRow( "ProductA", 54, 4564575, "long size value", 5454554512, 60 );
tableData.addSeparator();
tableData.addRow( "P1\nP2\nP3", "2002\n2003\n2004", CellType::MergeNext, 3003, 4004, CellType::MergeNext );
tableData.addRow( "ProductA", 54, 4564575, "long size value", 5454554512, 60 );
tableData.addRow( 3.14f, 2.718f, CellType::MergeNext, 1.618f, 0.577f, CellType::MergeNext );
tableData.addRow( "P1\nP2\nP3", "2002\n2003\n2004", CellType::MergeNext, "1212121245452145454545", 4004, CellType::MergeNext );
tableData.addRow( "Long product size", 54, 4564575, "long size value", 5454554512, 60 );
tableData.addRow( "ProductAfdggfd", 5445, 4565, "PrName", 5454512, 64650 );
tableData.addRow( 3.14f, 2.718f, CellType::MergeNext, 1.618f, 0.577f, CellType::MergeNext );
tableData.addSeparator();
tableData.addRow( CellType::MergeNext, CellType::MergeNext, CellType::MergeNext, CellType::MergeNext, CellType::MergeNext, "Item2" );
tableData.addSeparator();
tableData.addRow( 1500, 2500, CellType::MergeNext, CellType::MergeNext, CellType::MergeNext, CellType::MergeNext );
tableData.addSeparator();
tableData.addRow( 1.23f, 4.56f, CellType::MergeNext, 7.89f, 0.12f, 40 );
tableData.addRow( "Long product size", 54, 4564575, "long size value", 5454554512, 60 );
tableData.addRow( "ProductA", 54, 4564575, "long size value", 5454554512, 60 );
tableData.addSeparator();
tableData.addRow( "P1", "2002", CellType::MergeNext, CellType::MergeNext, CellType::MergeNext, "1212121245452145454545" );
tableData.addSeparator();
tableData.addRow( "Alpha", 1001, 8, "Beta\nwater", "2002\n1.0", CellType::MergeNext );

TableTextFormatter const tableText( tableLayout );
std::cout <<tableText.toString( tableData ) << std::endl;
EXPECT_EQ( tableText.toString( tableData ),
"\n----------------------------------------------------------------------------\n"
"| Cras egestas | CoordX | C | CoordZ | Prev | Next |\n"
"| | | | | element | element |\n"
"----------------------------------------------------------------------------\n"
"| ProductA | 1234 | 40 | ProductName | 5678 | 60 |\n"
"----------------------------------------------------------------------------\n"
"| P1 | 2002 | 3003 | 4004 | |\n"
"| P1 | 2002 | 1212121245452145454545 | 4004 | |\n"
"| P2 | 2003 | | | |\n"
"| P3 | 2004 | | | |\n"
"| 3.14 | 2.718 | 1.618 | 0.577 | |\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,23 +410,76 @@ void CompositionalMultiphaseStatistics::computeRegionStatistics( real64 const ti

string_view massUnit = units::getSymbol( m_solver->getMassUnit() );

std::vector< string > phaseCompName;
phaseCompName.reserve( numPhases*numComps );
std::vector< string > massValues;
phaseCompName.reserve( numPhases*numComps );

ConstitutiveManager const & constitutiveManager = this->getGroupByPath< ConstitutiveManager >( "/Problem/domain/Constitutive" );
MultiFluidBase const & baseFluid = constitutiveManager.getGroup< MultiFluidBase >( m_solver->referenceFluidModelName() );
auto const phaseNames = baseFluid.phaseNames();
auto const componentNames = baseFluid.componentNames();
for( integer ip = 0; ip < numPhases; ++ip )
{
for( integer ic = 0; ic < numComps; ++ic )
{
std::stringstream ss;
ss << phaseNames[ip]<< ", " <<componentNames[ic];
phaseCompName.push_back( ss.str() );
massValues.push_back( GEOS_FMT( "{}", stats.componentMass[ip][ic] ) );
}
}

TableData compPhaseStatsData;
compPhaseStatsData.addRow( "Pressure[Pa]", stats.minPressure, stats.averagePressure, stats.maxPressure );
compPhaseStatsData.addRow( "Delta pressure [Pa]", stats.minDeltaPressure, "/", stats.maxDeltaPressure );
compPhaseStatsData.addRow( "Temperature [K]", stats.minTemperature, stats.averageTemperature, stats.maxTemperature );
compPhaseStatsData.addSeparator();

compPhaseStatsData.addRow( "Total dynamic pore volume [rm^3]", CellType::MergeNext, CellType::MergeNext, stats.totalPoreVolume );
compPhaseStatsData.addRow( "Phase dynamic pore volume: {} rm^3", CellType::MergeNext, CellType::MergeNext, stats.phasePoreVolume );
compPhaseStatsData.addRow( GEOS_FMT( "Phase mass {}", massUnit ), CellType::MergeNext, CellType::MergeNext, stats.phaseMass );
compPhaseStatsData.addSeparator();
compPhaseStatsData.addRow( "statistics", "phase", CellType::MergeNext, "value" );
compPhaseStatsData.addSeparator();

compPhaseStatsData.addRow( "Total dynamic pore volume [rm^3]", "all", CellType::MergeNext, stats.totalPoreVolume );
compPhaseStatsData.addSeparator();
compPhaseStatsData.addRow( "Phase dynamic pore volume: [rm^3]",
stringutilities::joinLamda( phaseNames, "\n", []( auto data ) { return data[0]; } ),
CellType::MergeNext,
stringutilities::joinLamda( stats.phasePoreVolume, "\n", []( auto data ) { return data[0]; } ) );
compPhaseStatsData.addSeparator();

compPhaseStatsData.addRow( GEOS_FMT( "Trapped phase mass (metric 1) {}", massUnit ), CellType::MergeNext, CellType::MergeNext, stats.trappedPhaseMass );
compPhaseStatsData.addRow( GEOS_FMT( "nonTrappedPhaseMass {}", massUnit ), CellType::MergeNext, CellType::MergeNext, nonTrappedPhaseMass );
compPhaseStatsData.addRow( GEOS_FMT( "Phase mass [{}]", massUnit ),
stringutilities::joinLamda( phaseNames, "\n", []( auto data ) { return data[0]; } ),
CellType::MergeNext,
stringutilities::joinLamda( stats.phaseMass, "\n", []( auto data ) { return data[0]; } ) );
compPhaseStatsData.addSeparator();

compPhaseStatsData.addRow( GEOS_FMT( "Immobile phase mass (metric 2) {}", massUnit ), CellType::MergeNext, CellType::MergeNext, stats.immobilePhaseMass );
compPhaseStatsData.addRow( GEOS_FMT( "Mobile phase mass (metric 2) {}", massUnit ), CellType::MergeNext, CellType::MergeNext, mobilePhaseMass );
compPhaseStatsData.addRow( GEOS_FMT( "Trapped phase mass (metric 1) [{}]", massUnit ),
stringutilities::joinLamda( phaseNames, "\n", []( auto value ) { return value[0]; } ),
CellType::MergeNext,
stringutilities::joinLamda( stats.trappedPhaseMass, "\n", []( auto value ) { return value[0]; } ) );
compPhaseStatsData.addSeparator();
compPhaseStatsData.addRow( GEOS_FMT( "nonTrappedPhaseMass [{}]", massUnit ),
stringutilities::joinLamda( phaseNames, "\n", []( auto value ) { return value[0]; } ),
CellType::MergeNext,
stringutilities::joinLamda( nonTrappedPhaseMass, "\n", []( auto value ) { return value[0]; } ) );
compPhaseStatsData.addSeparator();

compPhaseStatsData.addRow( GEOS_FMT( "Immobile phase mass (metric 2) [{}]", massUnit ),
stringutilities::joinLamda( phaseNames, "\n", []( auto value ) { return value[0]; } ),
CellType::MergeNext,
stringutilities::joinLamda( stats.immobilePhaseMass, "\n", []( auto value ) { return value[0]; } ) );
compPhaseStatsData.addSeparator();
compPhaseStatsData.addRow( GEOS_FMT( "Mobile phase mass (metric 2) [{}]", massUnit ),
stringutilities::joinLamda( phaseNames, "\n", []( auto value ) { return value[0]; } ),
CellType::MergeNext,
stringutilities::joinLamda( mobilePhaseMass, "\n", []( auto value ) { return value[0]; } ) );
compPhaseStatsData.addSeparator();

compPhaseStatsData.addRow( GEOS_FMT( "Component mass {}", massUnit ), CellType::MergeNext, CellType::MergeNext, stats.componentMass );
compPhaseStatsData.addRow( GEOS_FMT( "Component mass [{}]", massUnit ),
stringutilities::join( phaseCompName, '\n' ),
CellType::MergeNext,
stringutilities::join( massValues, '\n' ) );

string const title = GEOS_FMT( "{}, {} (time {} s):", getName(), regionNames[i], time );
TableLayout const compPhaseStatsLayout( title, { "statistics", "min", "average", "max" } );
Expand Down

0 comments on commit 9d6aab9

Please sign in to comment.