GEOS
SourceFluxStatistics.hpp
Go to the documentation of this file.
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 TotalEnergies
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
20 #ifndef SRC_CORECOMPONENTS_PHYSICSSOLVERS_FLUIDFLOW_SOURCEFLUXSTATISTICS_HPP_
21 #define SRC_CORECOMPONENTS_PHYSICSSOLVERS_FLUIDFLOW_SOURCEFLUXSTATISTICS_HPP_
22 
25 #include "mesh/DomainPartition.hpp"
29 #include <map>
30 
31 namespace geos
32 {
33 
39 class SourceFluxStatsAggregator final : public FieldStatisticsBase< FlowSolverBase >
40 {
41 public:
42 
46  struct StatData
47  {
56 
61  void allocate( integer phaseCount );
65  void reset();
70  { return m_producedMass.size(); }
75  void combine( StatData const & other );
80  void mpiReduce();
81  };
87  {
88 public:
94  void setTarget( string_view aggregatorName, string_view fluxName );
95 
104  void gatherTimeStepStats( real64 currentTime, real64 dt,
105  arrayView1d< real64 const > const & producedMass,
106  integer elementCount );
107 
114 
121  void combine( WrappedStats const & other );
122 
127  { return m_stats; }
128 
132  StatData const & stats() const
133  { return m_stats; }
134 
139  { return m_statsPeriodStart; }
140 
145  { return m_statsPeriodDT; }
146 
151  { return m_aggregatorName; }
152 
157  { return m_fluxName; }
158 private:
160  StatData m_stats;
162  real64 m_statsPeriodStart{-LvArray::NumericLimits< real64 >::max};
164  real64 m_statsPeriodDT;
165 
169  struct PeriodStats
170  {
172  array1d< real64 > m_timeStepMass;
174  array1d< real64 > m_periodPendingMass;
176  real64 m_timeStepStart = 0.0;
178  real64 m_timeStepDeltaTime = 0.0;
180  real64 m_periodStart = -LvArray::NumericLimits< real64 >::max;
182  real64 m_periodPendingDeltaTime = 0.0;
184  integer m_elementCount = 0;
186  bool m_isGathering = false;
187 
192  void allocate( integer phaseCount );
196  void reset();
200  integer getPhaseCount() const
201  { return m_timeStepMass.size(); }
202  } m_periodStats;
203 
205  string m_aggregatorName;
207  string m_fluxName;
208  };
209 
210 
216  SourceFluxStatsAggregator( const string & name,
217  Group * const parent );
218 
222  static string catalogName() { return "SourceFluxStatistics"; }
223 
227  virtual bool execute( real64 const time_n,
228  real64 const dt,
229  integer const cycleNumber,
230  integer const eventCounter,
231  real64 const eventProgress,
232  DomainPartition & domain ) override;
233 
244  template< typename LAMBDA >
245  void forMeshLevelStatsWrapper( DomainPartition & domain, LAMBDA && lambda );
255  template< typename LAMBDA >
256  void forAllFluxStatsWrappers( MeshLevel & meshLevel, LAMBDA && lambda );
268  template< typename LAMBDA >
269  void forAllRegionStatsWrappers( MeshLevel & meshLevel, string_view fluxName, LAMBDA && lambda );
280  template< typename LAMBDA >
281  void forAllSubRegionStatsWrappers( ElementRegionBase & region, string_view fluxName, LAMBDA && lambda );
282 
295  template< typename LAMBDA >
296  static void forAllFluxStatWrappers( Group & container, string_view fluxName, LAMBDA && lambda );
297 
303  inline string getStatWrapperName( string_view fluxName ) const;
304 
305 
310  {
312  constexpr inline static string_view fluxNamesString() { return "fluxNames"; }
314  constexpr inline static string_view allRegionWrapperString() { return "all_regions"; }
316  constexpr inline static string_view fluxSetWrapperString() { return "flux_set"; }
317  };
318 
319 
320 private:
322 
324  string_array m_fluxNames;
326  TableLayout m_logLayout;
327  TableLayout m_csvLayout;
328 
329  string m_csvFilename;
330 
331 
335  void registerDataOnMesh( Group & meshBodies ) override;
336 
340  void postInputInitialization() override;
341 
342  dataRepository::Wrapper< WrappedStats > & registerWrappedStats( Group & group,
343  string_view fluxName );
344 
353  void gatherStatsForLog( bool logLevelActive,
354  string_view fluxName,
355  string_view elementSetName,
356  TableData & tableData,
357  WrappedStats const & wrappedStats );
363  void gatherStatsForCSV( string_view fluxName,
364  string_view elementSetName,
365  TableData & tableData,
366  WrappedStats const & stats );
367 
374  void outputStatsToLog( bool logLevelActive, string_view fluxesStr, TableData const & tableMeshData );
375 
380  void outputStatsToCSV( TableData & csvData );
381 
382 };
383 
384 
385 template< typename LAMBDA >
387  string_view fluxName,
388  LAMBDA && lambda )
389 {
390  container.forWrappers< WrappedStats >( [&]( dataRepository::Wrapper< WrappedStats > & statsWrapper )
391  {
392  if( statsWrapper.referenceAsView().getFluxName() == fluxName )
393  {
394  lambda( statsWrapper.reference() );
395  }
396  } );
397 }
398 
399 template< typename LAMBDA >
401  LAMBDA && lambda )
402 {
404  [&] ( string const &,
405  MeshLevel & meshLevel,
406  string_array const & )
407  {
408  string const wrapperName = getStatWrapperName( viewKeyStruct::fluxSetWrapperString() );
409  WrappedStats & stats = meshLevel.getWrapper< WrappedStats >( wrapperName ).reference();
410 
411  lambda( meshLevel, stats );
412  } );
413 }
414 template< typename LAMBDA >
416  LAMBDA && lambda )
417 {
418  for( string const & fluxName : m_fluxNames )
419  {
420  string const wrapperName = getStatWrapperName( fluxName );
421  WrappedStats & stats = meshLevel.getWrapper< WrappedStats >( wrapperName ).reference();
422 
423  lambda( meshLevel, stats );
424  }
425 }
426 template< typename LAMBDA >
428  string_view fluxName,
429  LAMBDA && lambda )
430 {
431  string const wrapperName = getStatWrapperName( fluxName );
432  meshLevel.getElemManager().forElementRegions( [&]( ElementRegionBase & region )
433  {
434  WrappedStats & stats = region.getWrapper< WrappedStats >( wrapperName ).reference();
435 
436  lambda( region, stats );
437  } );
438 }
439 template< typename LAMBDA >
441  string_view fluxName,
442  LAMBDA && lambda )
443 {
444  string const wrapperName = getStatWrapperName( fluxName );
445  region.forElementSubRegions( [&]( ElementSubRegionBase & subRegion )
446  {
447  WrappedStats & stats = subRegion.getWrapper< WrappedStats >( wrapperName ).reference();
448 
449  lambda( subRegion, stats );
450  } );
451 }
452 
454 { return GEOS_FMT( "{}_region_stats_for_{}", fluxName, getName() ); }
455 
456 
457 } /* namespace geos */
458 
459 #endif /* SRC_CORECOMPONENTS_PHYSICSSOLVERS_FLUIDFLOW_SOURCEFLUXSTATISTICS_HPP_ */
Partition of the decomposed physical domain. It also manages the connexion information to its neighbo...
Group const & getMeshBodies() const
Get the mesh bodies, const version.
The ElementRegionBase is the base class to manage the data stored at the element level.
void forElementSubRegions(LAMBDA &&lambda) const
Apply a lambda to all subregions.
void forElementRegions(LAMBDA &&lambda)
This function is used to launch kernel function over all the element regions with region type = Eleme...
FlowSolverBase * m_solver
Pointer to the physics solver.
Class facilitating the representation of a multi-level discretization of a MeshBody.
Definition: MeshLevel.hpp:42
ElementRegionManager const & getElemManager() const
Get the element region manager.
Definition: MeshLevel.hpp:207
void forDiscretizationOnMeshTargets(Group const &meshBodies, LAMBDA &&lambda) const
Loop over the target discretization on all mesh targets and apply callback.
Class that aggregate statistics of a flux over multiple time-steps for a given SourceFluxStatsAggrega...
void gatherTimeStepStats(real64 currentTime, real64 dt, arrayView1d< real64 const > const &producedMass, integer elementCount)
Set the current time step stats. Accumulate the statistics only if the time is strictly after the pre...
void combine(WrappedStats const &other)
Aggregate the statistics of the instance with those of another one.
void finalizePeriod()
Finalize the period statistics of each timestep gathering and render data over all mpi ranks....
void setTarget(string_view aggregatorName, string_view fluxName)
Set the subjects targeted by the stats.
string getStatWrapperName(string_view fluxName) const
virtual bool execute(real64 const time_n, real64 const dt, integer const cycleNumber, integer const eventCounter, real64 const eventProgress, DomainPartition &domain) override
Main extension point of executable targets.
void forAllSubRegionStatsWrappers(ElementRegionBase &region, string_view fluxName, LAMBDA &&lambda)
Apply a functor to all subregion WrappedStats (of the given region) that target a given flux.
void forAllFluxStatsWrappers(MeshLevel &meshLevel, LAMBDA &&lambda)
Apply a functor to each WrappedStats that combines the stats over all region for a flux.
static void forAllFluxStatWrappers(Group &container, string_view fluxName, LAMBDA &&lambda)
Apply a functor to all WrappedStats of the given group that target a given flux (and potentially mult...
void forAllRegionStatsWrappers(MeshLevel &meshLevel, string_view fluxName, LAMBDA &&lambda)
Apply a functor to all simulated region WrappedStats (of the given MeshLevel) that target a given flu...
SourceFluxStatsAggregator(const string &name, Group *const parent)
Constructor for the statistics class.
void forMeshLevelStatsWrapper(DomainPartition &domain, LAMBDA &&lambda)
Apply a functor to WrappedStats that combines all stats for each target solver discretization mesh le...
Class for managing table data.
Definition: TableData.hpp:35
Class for setup the table layout.
Definition: TableLayout.hpp:34
string const & getName() const
Get group name.
Definition: Group.hpp:1329
Wrapper< T > const & getWrapper(LOOKUP_TYPE const &index) const
Retrieve a Wrapper stored in this group.
Definition: Group.hpp:1214
void forWrappers(LAMBDA &&lambda)
Apply the given functor to wrappers.
Definition: Group.hpp:637
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
Definition: DataTypes.hpp:179
stdVector< string > string_array
A 1-dimensional array of geos::string types.
Definition: DataTypes.hpp:361
double real64
64-bit floating point type.
Definition: DataTypes.hpp:98
int integer
Signed integer type.
Definition: DataTypes.hpp:81
Array< T, 1 > array1d
Alias for 1D array.
Definition: DataTypes.hpp:175
std::string_view string_view
String type.
Definition: DataTypes.hpp:93
void reset()
reset the stats to 0.0
void allocate(integer phaseCount)
resize the phase data arrays if needed
void mpiReduce()
Aggregate the statistics of the instance with those from all instances from other MPI ranks....
void combine(StatData const &other)
Aggregate the statistics of the instance with those of another one.
integer m_elementCount
Owned cells producing/injecting. Local until mpiReduce()/finalizePeriod(), then global.