Skip to content

Commit

Permalink
Prefer vari over rms in variable names for Averaging (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
trevilo committed Feb 26, 2024
1 parent a24d466 commit 2c04caa
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 63 deletions.
80 changes: 40 additions & 40 deletions src/averaging_and_rms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ Averaging::~Averaging() {
}
}

void Averaging::registerField(std::string name, const ParGridFunction *field_to_average, bool compute_rms,
int rms_start_index, int rms_components) {
void Averaging::registerField(std::string name, const ParGridFunction *field_to_average, bool compute_vari,
int vari_start_index, int vari_components) {
// quick return if not computing stats...
if (!compute_mean_) return;

Expand All @@ -98,13 +98,13 @@ void Averaging::registerField(std::string name, const ParGridFunction *field_to_
ParGridFunction *mean = new ParGridFunction(field_to_average->ParFESpace());
*mean = 0.0;

// and maybe the rms
// and maybe the vari
ParGridFunction *vari = nullptr;
if (compute_rms) {
// make sure incoming field has enough components to satisfy rms request
assert((rms_start_index + rms_components) <= field_to_average->ParFESpace()->GetVDim());
if (compute_vari) {
// make sure incoming field has enough components to satisfy vari request
assert((vari_start_index + vari_components) <= field_to_average->ParFESpace()->GetVDim());

const int num_variance = rms_components * (rms_components + 1) / 2;
const int num_variance = vari_components * (vari_components + 1) / 2;

const FiniteElementCollection *fec = field_to_average->ParFESpace()->FEColl();
const int order = fec->GetOrder();
Expand All @@ -118,7 +118,7 @@ void Averaging::registerField(std::string name, const ParGridFunction *field_to_
}

// and store those fields in an AveragingFamily object that gets appended to the avg_families_ vector
avg_families_.emplace_back(AveragingFamily(name, field_to_average, mean, vari, rms_start_index, rms_components));
avg_families_.emplace_back(AveragingFamily(name, field_to_average, mean, vari, vari_start_index, vari_components));
}

void Averaging::initializeViz() {
Expand All @@ -130,7 +130,7 @@ void Averaging::initializeViz() {
// Loop through the families and add them to the paraview output
for (size_t i = 0; i < avg_families_.size(); i++) {
ParGridFunction *mean = avg_families_[i].mean_fcn_;
ParGridFunction *vari = avg_families_[i].rms_fcn_;
ParGridFunction *vari = avg_families_[i].vari_fcn_;

const FiniteElementCollection *fec = mean->ParFESpace()->FEColl();
const int order = fec->GetOrder();
Expand Down Expand Up @@ -170,7 +170,7 @@ void Averaging::initializeVizForM2ulPhyS(ParFiniteElementSpace *fes, ParFiniteEl
assert(avg_families_.size() == 1);

ParGridFunction *meanUp = avg_families_[0].mean_fcn_;
ParGridFunction *rms = avg_families_[0].rms_fcn_;
ParGridFunction *vari = avg_families_[0].vari_fcn_;

const FiniteElementCollection *fec = meanUp->ParFESpace()->FEColl();
const int order = fec->GetOrder();
Expand All @@ -191,7 +191,7 @@ void Averaging::initializeVizForM2ulPhyS(ParFiniteElementSpace *fes, ParFiniteEl
pvdc_->RegisterField("dens", meanRho);
pvdc_->RegisterField("vel", meanV);
pvdc_->RegisterField("press", meanP);
pvdc_->RegisterField("rms", rms);
pvdc_->RegisterField("rms", vari);
}

void Averaging::addSample(const int &iter, GasMixture *mixture) {
Expand All @@ -208,7 +208,7 @@ void Averaging::addSample(const int &iter, GasMixture *mixture) {
// or the variances have been restarted. Either way, valid to
// set variances to zero.
for (size_t ifam = 0; ifam < avg_families_.size(); ifam++) {
*avg_families_[ifam].rms_fcn_ = 0.0;
*avg_families_[ifam].vari_fcn_ = 0.0;
}
}

Expand Down Expand Up @@ -249,7 +249,7 @@ void Averaging::addSampleInternal() {

const ParGridFunction *inst = fam.instantaneous_fcn_;
ParGridFunction *mean = fam.mean_fcn_;
ParGridFunction *vari = fam.rms_fcn_;
ParGridFunction *vari = fam.vari_fcn_;

const double *d_inst = inst->Read();
double *d_mean = mean->ReadWrite();
Expand All @@ -259,12 +259,12 @@ void Averaging::addSampleInternal() {
const int dof = mean->ParFESpace()->GetNDofs(); // dofs per scalar field
const int neq = mean->ParFESpace()->GetVDim(); // number of scalar variables in mean field

const int d_rms_start = fam.rms_start_index_;
const int d_rms_components = fam.rms_components_;
const int d_vari_start = fam.vari_start_index_;
const int d_vari_components = fam.vari_components_;

// Extract sample size information for use on device
double dSamplesMean = (double)ns_mean_;
double dSamplesRMS = (double)ns_vari_;
double dSamplesVari = (double)ns_vari_;

// "Loop" over all dofs and update statistics
MFEM_FORALL(n, dof, {
Expand All @@ -281,31 +281,31 @@ void Averaging::addSampleInternal() {
double val = 0.;
double delta_i = 0.;
double delta_j = 0.;
int rms_index = 0;
int vari_index = 0;

// Variances first (i.e., diagonal of the covariance matrix)
for (int i = d_rms_start; i < d_rms_start + d_rms_components; i++) {
for (int i = d_vari_start; i < d_vari_start + d_vari_components; i++) {
const double uinst = d_inst[n + i * dof];
const double umean = d_mean[n + i * dof];
delta_i = (uinst - umean);
val = d_vari[n + rms_index * dof];
d_vari[n + rms_index * dof] = (val * dSamplesRMS + delta_i * delta_i) / (dSamplesRMS + 1);
rms_index++;
val = d_vari[n + vari_index * dof];
d_vari[n + vari_index * dof] = (val * dSamplesVari + delta_i * delta_i) / (dSamplesVari + 1);
vari_index++;
}

// Covariances second (i.e., off-diagonal components, if any)
for (int i = d_rms_start; i < d_rms_start + d_rms_components - 1; i++) {
for (int i = d_vari_start; i < d_vari_start + d_vari_components - 1; i++) {
const double uinst_i = d_inst[n + i * dof];
const double umean_i = d_mean[n + i * dof];
delta_i = uinst_i - umean_i;
for (int j = d_rms_start + 1; j < d_rms_start + d_rms_components; j++) {
for (int j = d_vari_start + 1; j < d_vari_start + d_vari_components; j++) {
const double uinst_j = d_inst[n + j * dof];
const double umean_j = d_mean[n + j * dof];
delta_j = uinst_j - umean_j;

val = d_vari[n + rms_index * dof];
d_vari[n + rms_index * dof] = (val * dSamplesRMS + delta_i * delta_j) / (dSamplesRMS + 1);
rms_index++;
val = d_vari[n + vari_index * dof];
d_vari[n + vari_index * dof] = (val * dSamplesVari + delta_i * delta_j) / (dSamplesVari + 1);
vari_index++;
}
}
} // end variance
Expand All @@ -328,7 +328,7 @@ void Averaging::addSampleInternal(GasMixture *mixture) {

const ParGridFunction *inst = fam.instantaneous_fcn_;
ParGridFunction *mean = fam.mean_fcn_;
ParGridFunction *vari = fam.rms_fcn_;
ParGridFunction *vari = fam.vari_fcn_;

const double *d_inst = inst->Read();
double *d_mean = mean->ReadWrite();
Expand All @@ -348,12 +348,12 @@ void Averaging::addSampleInternal(GasMixture *mixture) {
const int neq = mean->ParFESpace()->GetVDim(); // number of scalar variables in mean field
const int dim = mean->ParFESpace()->GetParMesh()->Dimension(); // spatial dimension

const int d_rms_start = fam.rms_start_index_;
const int d_rms_components = fam.rms_components_;
const int d_vari_start = fam.vari_start_index_;
const int d_vari_components = fam.vari_components_;

// Extract sample size information for use on device
double dSamplesMean = (double)ns_mean_;
double dSamplesRMS = (double)ns_vari_;
double dSamplesVari = (double)ns_vari_;

// "Loop" over all dofs and update statistics
MFEM_FORALL(n, dof, {
Expand Down Expand Up @@ -391,24 +391,24 @@ void Averaging::addSampleInternal(GasMixture *mixture) {
double val = 0.;
double delta_i = 0.;
double delta_j = 0.;
int rms_index = 0;
int vari_index = 0;

// Variances first (i.e., diagonal of the covariance matrix)
for (int i = d_rms_start; i < d_rms_start + d_rms_components; i++) {
val = d_vari[n + rms_index * dof];
for (int i = d_vari_start; i < d_vari_start + d_vari_components; i++) {
val = d_vari[n + vari_index * dof];
delta_i = (nUp[i] - mean_state[i]);
d_vari[n + rms_index * dof] = (val * dSamplesRMS + delta_i * delta_i) / (dSamplesRMS + 1);
rms_index++;
d_vari[n + vari_index * dof] = (val * dSamplesVari + delta_i * delta_i) / (dSamplesVari + 1);
vari_index++;
}

// Covariances second (i.e., off-diagonal components, if any)
for (int i = d_rms_start; i < d_rms_start + d_rms_components - 1; i++) {
for (int j = d_rms_start + 1; j < d_rms_start + d_rms_components; j++) {
val = d_vari[n + rms_index * dof];
for (int i = d_vari_start; i < d_vari_start + d_vari_components - 1; i++) {
for (int j = d_vari_start + 1; j < d_vari_start + d_vari_components; j++) {
val = d_vari[n + vari_index * dof];
delta_i = (nUp[i] - mean_state[i]);
delta_j = (nUp[j] - mean_state[j]);
d_vari[n + rms_index * dof] = (val * dSamplesRMS + delta_i * delta_j) / (dSamplesRMS + 1);
rms_index++;
d_vari[n + vari_index * dof] = (val * dSamplesVari + delta_i * delta_j) / (dSamplesVari + 1);
vari_index++;
}
}
} // end variance
Expand Down
46 changes: 23 additions & 23 deletions src/averaging_and_rms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,21 @@ class AveragingFamily {
/** Variable index to "start" variances
*
* For example, if state vector is [rho, u, v, w, T] and you only
* want the velocity covariance matrix, rms_start_index_ should be 1
* want the velocity covariance matrix, vari_start_index_ should be 1
*/
int rms_start_index_;
int vari_start_index_;

/** Number of variables for covariance calculation
*
* For example, if state vector is [rho, u, v, w, T] and you only
* want the velocity covariance matrix, rms_components_ should be
* want the velocity covariance matrix, vari_components_ should be
* 3. This will lead to 6 covariance being computed (uu, vv, ww, uv,
* uw, and vw).
*
* It is assumed that the variance variables are contiguous---i.e.,
* there is no support for getting uT only.
*/
int rms_components_;
int vari_components_;

/** Pointer to function containing the instantaneous field being averaged (not owned) */
const ParGridFunction *instantaneous_fcn_;
Expand All @@ -103,47 +103,47 @@ class AveragingFamily {
ParGridFunction *mean_fcn_;

/** Pointer to variance field (owned) */
ParGridFunction *rms_fcn_;
ParGridFunction *vari_fcn_;

/**
* @brief Constructor
*
* Notes: the mean and rms grid functions should be allocated before
* Notes: the mean and vari grid functions should be allocated before
* constructing the AveragingFamily, but AveragingFamily then takes
* ownership.
*/
AveragingFamily(std::string name, const ParGridFunction *instant, ParGridFunction *mean, ParGridFunction *rms,
int rms_start_index = 0, int rms_components = 1) {
AveragingFamily(std::string name, const ParGridFunction *instant, ParGridFunction *mean, ParGridFunction *vari,
int vari_start_index = 0, int vari_components = 1) {
name_ = name;
rms_start_index_ = rms_start_index;
rms_components_ = rms_components;
vari_start_index_ = vari_start_index;
vari_components_ = vari_components;
instantaneous_fcn_ = instant;
mean_fcn_ = mean;
rms_fcn_ = rms;
vari_fcn_ = vari;
}

/// Move constructor (required for emplace_back)
AveragingFamily(AveragingFamily &&fam) {
this->name_ = fam.name_;
this->rms_start_index_ = fam.rms_start_index_;
this->rms_components_ = fam.rms_components_;
this->vari_start_index_ = fam.vari_start_index_;
this->vari_components_ = fam.vari_components_;

// Copy the pointers
this->instantaneous_fcn_ = fam.instantaneous_fcn_;
this->mean_fcn_ = fam.mean_fcn_;
this->rms_fcn_ = fam.rms_fcn_;
this->vari_fcn_ = fam.vari_fcn_;

// Set incoming mean_fcn_ to null, which ensures memory that
// this->mean_fcn_ now points to is not deleted when the
// destructor of fam is called
fam.mean_fcn_ = nullptr;
fam.rms_fcn_ = nullptr;
fam.vari_fcn_ = nullptr;
}

/// Destructor (deletes the mean and rms fields)
/// Destructor (deletes the mean and vari fields)
~AveragingFamily() {
delete mean_fcn_;
delete rms_fcn_;
delete vari_fcn_;
}
};

Expand Down Expand Up @@ -197,12 +197,12 @@ class Averaging {
*
* @param name Name for the data being averaged (e.g., "temperature", or "primitive-state")
* @param field_to_average Pointer to the instantaneous data that will be used to update the statistics
* @param compute_rms Set to true to compute variances. Otherwise only mean is computed.
* @param rms_start_index Variable index at which to start variances (see AveragingFamily)
* @param rms_components Number of variables in variances (see AveragingFamily)
* @param compute_vari Set to true to compute variances. Otherwise only mean is computed.
* @param vari_start_index Variable index at which to start variances (see AveragingFamily)
* @param vari_components Number of variables in variances (see AveragingFamily)
*/
void registerField(std::string name, const ParGridFunction *field_to_average, bool compute_rms = true,
int rms_start_index = 0, int rms_components = 1);
void registerField(std::string name, const ParGridFunction *field_to_average, bool compute_vari = true,
int vari_start_index = 0, int vari_components = 1);

/**
* @brief Initialize visualiztion for statistics
Expand Down Expand Up @@ -276,7 +276,7 @@ class Averaging {
ParGridFunction *GetVariField(std::string name) {
const int i = getFamilyIndex(name);
assert(i >= 0);
return avg_families_[i].rms_fcn_;
return avg_families_[i].vari_fcn_;
}

int GetSamplesMean() { return ns_mean_; }
Expand Down

0 comments on commit 2c04caa

Please sign in to comment.