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 Total, S.A
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 
23 #include "../FieldStatisticsBase.hpp"
24 #include "FlowSolverBase.hpp"
25 #include "mesh/DomainPartition.hpp"
26 
27 
28 namespace geos
29 {
30 
36 class SourceFluxStatsAggregator final : public FieldStatisticsBase< FlowSolverBase >
37 {
38 public:
39 
43  struct StatData
44  {
53 
58  void allocate( integer phaseCount );
62  void reset();
67  { return m_producedMass.size(); }
72  void combine( StatData const & other );
77  void mpiReduce();
78  };
84  {
85 public:
91  void setTarget( string_view aggregatorName, string_view fluxName );
92 
101  void gatherTimeStepStats( real64 currentTime, real64 dt,
102  arrayView1d< real64 const > const & producedMass,
103  integer elementCount );
104 
111 
116  { return m_stats; }
117 
121  StatData const & stats() const
122  { return m_stats; }
123 
128  { return m_statsPeriodStart; }
129 
134  { return m_statsPeriodDT; }
135 
140  { return m_aggregatorName; }
141 
146  { return m_fluxName; }
147 private:
149  StatData m_stats;
151  real64 m_statsPeriodStart;
153  real64 m_statsPeriodDT;
154 
158  struct PeriodStats
159  {
161  array1d< real64 > m_timeStepMass;
163  array1d< real64 > m_periodPendingMass;
165  real64 m_timeStepStart = 0.0;
167  real64 m_timeStepDeltaTime = 0.0;
169  real64 m_periodStart = 0.0;
171  real64 m_periodPendingDeltaTime = 0.0;
173  integer m_elementCount = 0;
175  bool m_isGathering = false;
176 
181  void allocate( integer phaseCount );
185  void reset();
189  integer getPhaseCount() const
190  { return m_timeStepMass.size(); }
191  } m_periodStats;
192 
194  string m_aggregatorName;
196  string m_fluxName;
197  };
198 
199 
205  SourceFluxStatsAggregator( const string & name,
206  Group * const parent );
207 
211  static string catalogName() { return "SourceFluxStatistics"; }
212 
216  virtual bool execute( real64 const time_n,
217  real64 const dt,
218  integer const cycleNumber,
219  integer const eventCounter,
220  real64 const eventProgress,
221  DomainPartition & domain ) override;
222 
233  template< typename LAMBDA >
234  void forMeshLevelStatsWrapper( DomainPartition & domain, LAMBDA && lambda );
244  template< typename LAMBDA >
245  void forAllFluxStatsWrappers( MeshLevel & meshLevel, LAMBDA && lambda );
257  template< typename LAMBDA >
258  void forAllRegionStatsWrappers( MeshLevel & meshLevel, string_view fluxName, LAMBDA && lambda );
269  template< typename LAMBDA >
270  void forAllSubRegionStatsWrappers( ElementRegionBase & region, string_view fluxName, LAMBDA && lambda );
271 
284  template< typename LAMBDA >
285  static void forAllFluxStatWrappers( Group & container, string_view fluxName, LAMBDA && lambda );
286 
292  inline string getStatWrapperName( string_view fluxName ) const;
293 
294 
299  {
301  constexpr inline static string_view fluxNamesString() { return "fluxNames"; }
303  constexpr inline static string_view allRegionWrapperString() { return "all_regions"; }
305  constexpr inline static string_view fluxSetWrapperString() { return "flux_set"; }
306  };
307 
308 
309 private:
311 
313  string_array m_fluxNames;
314 
318  void registerDataOnMesh( Group & meshBodies ) override;
319 
323  void postInputInitialization() override;
324 
325  dataRepository::Wrapper< WrappedStats > & registerWrappedStats( Group & group,
326  string_view fluxName,
327  string_view elementSetName );
328 
335  void writeStatsToLog( integer minLogLevel, string_view elementSetName, WrappedStats const & stats );
342  void writeStatsToCSV( string_view elementSetName, WrappedStats const & stats, bool writeHeader );
343 
344 };
345 
346 
347 template< typename LAMBDA >
349  string_view fluxName,
350  LAMBDA && lambda )
351 {
352  container.forWrappers< WrappedStats >( [&]( dataRepository::Wrapper< WrappedStats > & statsWrapper )
353  {
354  if( statsWrapper.referenceAsView().getFluxName() == fluxName )
355  {
356  lambda( statsWrapper.reference() );
357  }
358  } );
359 }
360 
361 template< typename LAMBDA >
363  LAMBDA && lambda )
364 {
366  [&] ( string const &,
367  MeshLevel & meshLevel,
369  {
370  string const wrapperName = getStatWrapperName( viewKeyStruct::fluxSetWrapperString() );
371  WrappedStats & stats = meshLevel.getWrapper< WrappedStats >( wrapperName ).reference();
372 
373  lambda( meshLevel, stats );
374  } );
375 }
376 template< typename LAMBDA >
378  LAMBDA && lambda )
379 {
380  for( string const & fluxName : m_fluxNames )
381  {
382  string const wrapperName = getStatWrapperName( fluxName );
383  WrappedStats & stats = meshLevel.getWrapper< WrappedStats >( wrapperName ).reference();
384 
385  lambda( meshLevel, stats );
386  }
387 }
388 template< typename LAMBDA >
390  string_view fluxName,
391  LAMBDA && lambda )
392 {
393  string const wrapperName = getStatWrapperName( fluxName );
394  meshLevel.getElemManager().forElementRegions( [&]( ElementRegionBase & region )
395  {
396  WrappedStats & stats = region.getWrapper< WrappedStats >( wrapperName ).reference();
397 
398  lambda( region, stats );
399  } );
400 }
401 template< typename LAMBDA >
403  string_view fluxName,
404  LAMBDA && lambda )
405 {
406  string const wrapperName = getStatWrapperName( fluxName );
407  region.forElementSubRegions( [&]( ElementSubRegionBase & subRegion )
408  {
409  WrappedStats & stats = subRegion.getWrapper< WrappedStats >( wrapperName ).reference();
410 
411  lambda( subRegion, stats );
412  } );
413 }
414 
416 { return GEOS_FMT( "{}_region_stats_for_{}", fluxName, getName() ); }
417 
418 
419 } /* namespace geos */
420 
421 #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 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...
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:655
ArrayView< T, 1 > arrayView1d
Alias for 1D array view.
Definition: DataTypes.hpp:180
array1d< string > string_array
A 1-dimensional array of geos::string types.
Definition: DataTypes.hpp:392
double real64
64-bit floating point type.
Definition: DataTypes.hpp:99
std::int32_t integer
Signed integer type.
Definition: DataTypes.hpp:82
Array< T, 1 > array1d
Alias for 1D array.
Definition: DataTypes.hpp:176
std::string_view string_view
String type.
Definition: DataTypes.hpp:94
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
Number of elements in which we are producing / injecting.