TransFlow  0.1.0
A transient pipeline flow simulation library
Sampler Class Reference

The Sampler class is used to sample selected Pipeline properties during simulations. More...

#include <sampler.hpp>

Public Member Functions

 Sampler (const std::filesystem::path &path, const arma::uword interval=60, const bool append=false, const arma::uvec indicesToSample={})
 Construct given output directory and (optional) print interval. More...
 
void addPropertyToPrint (PropertyGetter samplingFunction)
 Add property to save to file. This automatically determines the label and filename of the property. More...
 
void addPropertyToPrint (PropertyGetter samplingFunction, const std::string &label)
 Add property to print with explicit label. More...
 
bool sample (const Pipeline &pipeline, const bool forceSample=false)
 Sample current state. This is usually only called by Simulator, but can also be called to force a sample. More...
 
const std::filesystem::path & outputDir () const
 Get (const ref) output directory.
 
SamplersetIndicesToSample (const arma::uvec &indices)
 

Static Public Member Functions

static std::string getSampleLabel (PropertyGetter samplingFunction)
 
static std::filesystem::path makeOutputDir (const std::filesystem::path &path)
 

Private Attributes

std::vector< std::ofstream > m_outputFiles
 
std::vector< details::PropertyToSamplem_samplers
 
const std::filesystem::path m_outputDir
 Output directory.
 
arma::uword m_printInterval = 60
 How often to print (max) [s].
 
arma::uword m_timeOfLastPrint = 0
 Time of last print [s].
 
arma::uvec m_indicesToSample {}
 
bool m_append
 If we append to (true) or overwrite (false) existing files.
 

Detailed Description

The Sampler class is used to sample selected Pipeline properties during simulations.

Adding new properties to sample is done via Sampler::addPropertyToPrint.

Control of which properties to sample is implemented via pointers to member functions/getters of Pipeline, which are stored in m_samplers, together with a label.

Constructor & Destructor Documentation

◆ Sampler()

Sampler::Sampler ( const std::filesystem::path &  path,
const arma::uword  interval = 60,
const bool  append = false,
const arma::uvec  indicesToSample = {} 
)

Construct given output directory and (optional) print interval.

Parameters
pathWhere to store results
intervalHow often to sample (max) [s]
appendIf we should append to (true) or overwrite existing output files (false)

Member Function Documentation

◆ addPropertyToPrint() [1/2]

void Sampler::addPropertyToPrint ( PropertyGetter  samplingFunction)

Add property to save to file. This automatically determines the label and filename of the property.

Parameters
samplingFunctionPointer to Pipeline property getter

◆ addPropertyToPrint() [2/2]

void Sampler::addPropertyToPrint ( PropertyGetter  samplingFunction,
const std::string &  label 
)

Add property to print with explicit label.

Parameters
samplingFunctionPointer to Pipeline property getter
labelProperty label (filename will be <label>.csv)

◆ getSampleLabel()

static std::string Sampler::getSampleLabel ( PropertyGetter  samplingFunction)
static

Automatically determines property label from Pipeline getter function pointer.

◆ makeOutputDir()

static std::filesystem::path Sampler::makeOutputDir ( const std::filesystem::path &  path)
static

Make output directory from string. Does some checks to see if directory exists, and creates it if it doesn't exists. Throws error if directory is a file, or if directory can't be created.

◆ sample()

bool Sampler::sample ( const Pipeline pipeline,
const bool  forceSample = false 
)

Sample current state. This is usually only called by Simulator, but can also be called to force a sample.

Parameters
pipelinePipeline instance to sample
forceSample[bool]
Returns
true if a sample was recorded, else false

◆ setIndicesToSample()

Sampler& Sampler::setIndicesToSample ( const arma::uvec &  indices)

Set which indices to sample. Empty vector means that we sample all points.

Member Data Documentation

◆ m_indicesToSample

arma::uvec Sampler::m_indicesToSample {}
private

Which grid points to sample. Empty vector means that we sample all points.

◆ m_outputFiles

std::vector<std::ofstream> Sampler::m_outputFiles
private

Output file streams, one for each property. Should be in the same order as m_samplers.

◆ m_samplers

std::vector<details::PropertyToSample> Sampler::m_samplers
private

details::PropertyToSample instances with the label and sampling function for each property


The documentation for this class was generated from the following file: