Gromacs  2024.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
List of all members | Public Member Functions
gmx::BiasState Class Reference

#include <gromacs/applied_forces/awh/biasstate.h>

Description

The state of a bias.

The bias state has the current coordinate state: its value and the grid point it maps to (the grid point of the umbrella potential if needed). It contains a vector with the state for each point on the grid. It also counts the number of updates issued and tracks which points have been sampled since last update. Finally, the convergence state is a global property set ultimately by the histogram size histogramSize in the sub-class HistogramSize, since the update sizes are ~ 1/histogramSize.

Public Member Functions

 BiasState (const AwhBiasParams &awhBiasParams, double histogramSizeInitial, ArrayRef< const DimParams > dimParams, const BiasGrid &grid, const BiasSharing *biasSharing)
 Constructor. More...
 
void restoreFromHistory (const AwhBiasHistory &biasHistory, const BiasGrid &grid)
 Restore the bias state from history. More...
 
void broadcast (const t_commrec *commRecord)
 Broadcast the bias state over the MPI ranks in this simulation. More...
 
void initHistoryFromState (AwhBiasHistory *biasHistory) const
 Allocate and initialize a bias history with the given bias state. More...
 
void updateHistory (AwhBiasHistory *biasHistory, const BiasGrid &grid) const
 Update the bias state history with the current state. More...
 
void initGridPointState (const AwhBiasParams &awhBiasParams, ArrayRef< const DimParams > dimParams, const BiasGrid &grid, const BiasParams &params, const CorrelationGrid &forceCorrelation, const std::string &filename, int numBias)
 Initialize the state of grid coordinate points. More...
 
int warnForHistogramAnomalies (const BiasGrid &grid, int biasIndex, double t, FILE *fplog, int maxNumWarnings) const
 Performs statistical checks on the collected histograms and warns if issues are detected. More...
 
double calcUmbrellaForceAndPotential (ArrayRef< const DimParams > dimParams, const BiasGrid &grid, int point, ArrayRef< const double > neighborLambdaDhdl, ArrayRef< double > force) const
 Calculates and sets the force the coordinate experiences from an umbrella centered at the given point. More...
 
void calcConvolvedForce (ArrayRef< const DimParams > dimParams, const BiasGrid &grid, ArrayRef< const double > probWeightNeighbor, ArrayRef< const double > neighborLambdaDhdl, ArrayRef< double > forceWorkBuffer, ArrayRef< double > force) const
 Calculates and sets the convolved force acting on the coordinate. More...
 
double moveUmbrella (ArrayRef< const DimParams > dimParams, const BiasGrid &grid, ArrayRef< const double > probWeightNeighbor, ArrayRef< const double > neighborLambdaDhdl, ArrayRef< double > biasForce, int64_t step, int64_t seed, int indexSeed, bool onlySampleUmbrellaGridpoint)
 Move the center point of the umbrella potential. More...
 
void doSkippedUpdatesForAllPoints (const BiasParams &params)
 Do all previously skipped updates. Public for use by tests. More...
 
void doSkippedUpdatesInNeighborhood (const BiasParams &params, const BiasGrid &grid)
 Do previously skipped updates in this neighborhood. More...
 
void setCoordValue (const BiasGrid &grid, const awh_dvec coordValue)
 Update the reaction coordinate value. More...
 
void updateFreeEnergyAndAddSamplesToHistogram (ArrayRef< const DimParams > dimParams, const BiasGrid &grid, const BiasParams &params, const CorrelationGrid &forceCorrelation, double t, int64_t step, FILE *fplog, std::vector< int > *updateList)
 Performs an update of the bias. More...
 
double updateProbabilityWeightsAndConvolvedBias (ArrayRef< const DimParams > dimParams, const BiasGrid &grid, ArrayRef< const double > neighborLambdaEnergies, std::vector< double, AlignedAllocator< double >> *weight) const
 Update the probability weights and the convolved bias. More...
 
void sampleProbabilityWeights (const BiasGrid &grid, ArrayRef< const double > probWeightNeighbor)
 Take samples of the current probability weights for future updates and analysis. More...
 
void sampleCoordAndPmf (const std::vector< DimParams > &dimParams, const BiasGrid &grid, ArrayRef< const double > probWeightNeighbor, double convolvedBias)
 Sample the reaction coordinate and PMF for future updates or analysis. More...
 
double calcConvolvedBias (ArrayRef< const DimParams > dimParams, const BiasGrid &grid, const awh_dvec &coordValue) const
 Calculates the convolved bias for a given coordinate value. More...
 
void getPmf (ArrayRef< float >) const
 Fills the given array with PMF values. More...
 
const CoordStatecoordState () const
 Returns the current coordinate state.
 
const std::vector< PointState > & points () const
 Returns a const reference to the point state.
 
bool inInitialStage () const
 Returns true if we are in the initial stage.
 
HistogramSize histogramSize () const
 Returns the current histogram size.
 
void setUmbrellaGridpointToGridpoint ()
 Sets the umbrella grid point to the current grid point.
 
void updateSharedCorrelationTensorTimeIntegral (const BiasParams &biasParams, const CorrelationGrid &forceCorrelation, bool shareAcrossAllRanks)
 Updates sharedCorrelationTensorTimeIntegral_ for all points. More...
 
double getSharedCorrelationTensorTimeIntegral (int gridPointIndex, int correlationTensorIndex) const
 Gets the time integral, shared across all ranks, of a tensor of a correlation grid point. More...
 
const std::vector< double > & getSharedPointCorrelationIntegral (int gridPointIndex) const
 Gets the time integral (all tensors), shared across all ranks, of a correlation grid point. More...
 

Constructor & Destructor Documentation

gmx::BiasState::BiasState ( const AwhBiasParams &  awhBiasParams,
double  histogramSizeInitial,
ArrayRef< const DimParams dimParams,
const BiasGrid grid,
const BiasSharing *  biasSharing 
)

Constructor.

Constructs the global state and the point states on a provided geometric grid passed in grid.

Parameters
[in]awhBiasParamsThe Bias parameters from inputrec.
[in]histogramSizeInitialThe estimated initial histogram size. This is floating-point, since histograms use weighted entries and grow by a floating-point scaling factor.
[in]dimParamsThe dimension parameters.
[in]gridThe bias grid.
[in]biasSharingMultisim bias sharing object, can be nullptrx

Member Function Documentation

void gmx::BiasState::broadcast ( const t_commrec *  commRecord)

Broadcast the bias state over the MPI ranks in this simulation.

Parameters
[in]commRecordStruct for communication.
double gmx::BiasState::calcConvolvedBias ( ArrayRef< const DimParams dimParams,
const BiasGrid grid,
const awh_dvec coordValue 
) const

Calculates the convolved bias for a given coordinate value.

The convolved bias is the effective bias acting on the coordinate. Since the bias here has arbitrary normalization, this only makes sense as a relative, to other coordinate values, measure of the bias.

Note
If it turns out to be costly to calculate this pointwise the convolved bias for the whole grid could be returned instead.
Parameters
[in]dimParamsThe bias dimensions parameters
[in]gridThe grid.
[in]coordValueCoordinate value.
Returns
the convolved bias >= -GMX_FLOAT_MAX.
void gmx::BiasState::calcConvolvedForce ( ArrayRef< const DimParams dimParams,
const BiasGrid grid,
ArrayRef< const double >  probWeightNeighbor,
ArrayRef< const double >  neighborLambdaDhdl,
ArrayRef< double >  forceWorkBuffer,
ArrayRef< double >  force 
) const

Calculates and sets the convolved force acting on the coordinate.

The convolved force is the weighted sum of forces from umbrellas located at each point in the grid.

Parameters
[in]dimParamsThe bias dimensions parameters.
[in]gridThe grid.
[in]probWeightNeighborProbability weights of the neighbors.
[in]neighborLambdaDhdlAn array containing the dHdL at the neighboring lambda points. The array is of length numLambdas+1, where numLambdas is the number of free energy lambda states. Element 0 in the array is the dHdL of the current state and elements 1..numLambdas contain the dHdL of the system in the neighboring lambda states (also including the current state). When there are no free energy lambda state dimensions this can be empty.
[in]forceWorkBufferForce work buffer, values only used internally.
[in,out]forceBias force vector to set.
double gmx::BiasState::calcUmbrellaForceAndPotential ( ArrayRef< const DimParams dimParams,
const BiasGrid grid,
int  point,
ArrayRef< const double >  neighborLambdaDhdl,
ArrayRef< double >  force 
) const

Calculates and sets the force the coordinate experiences from an umbrella centered at the given point.

The umbrella potential is an harmonic potential given by 0.5k(coord value - point value)^2. This value is also returned.

Parameters
[in]dimParamsThe bias dimensions parameters.
[in]gridThe grid.
[in]pointPoint for umbrella center.
[in]neighborLambdaDhdlAn array containing the dHdL at the neighboring lambda points. The array is of length numLambdas+1, where numLambdas is the number of free energy lambda states. Element 0 in the array is the dHdL of the current state and elements 1..numLambdas contain the dHdL of the system in the neighboring lambda states (also including the current state). When there are no free energy lambda state dimensions this can be empty.
[in,out]forceForce vector to set. Returns the umbrella potential.
void gmx::BiasState::doSkippedUpdatesForAllPoints ( const BiasParams params)

Do all previously skipped updates. Public for use by tests.

Parameters
[in]paramsThe bias parameters.
void gmx::BiasState::doSkippedUpdatesInNeighborhood ( const BiasParams params,
const BiasGrid grid 
)

Do previously skipped updates in this neighborhood.

Parameters
[in]paramsThe bias parameters.
[in]gridThe grid.
void gmx::BiasState::getPmf ( gmx::ArrayRef< float >  pmf) const

Fills the given array with PMF values.

Points outside of the biasing target region will get PMF = GMX_FLOAT_MAX.

Note
: The PMF is in single precision, because it is a statistical quantity and therefore never reaches full float precision.
Parameters
[out]pmfArray(ref) to be filled with the PMF values, should have the same size as the bias grid.
double gmx::BiasState::getSharedCorrelationTensorTimeIntegral ( int  gridPointIndex,
int  correlationTensorIndex 
) const

Gets the time integral, shared across all ranks, of a tensor of a correlation grid point.

Parameters
[in]gridPointIndexThe index of the grid point from which to retrieve the tensor volume.
[in]correlationTensorIndexThe index of the tensor.
const std::vector< double > & gmx::BiasState::getSharedPointCorrelationIntegral ( int  gridPointIndex) const

Gets the time integral (all tensors), shared across all ranks, of a correlation grid point.

Parameters
[in]gridPointIndexThe index of the grid point from which to retrieve the tensor volume.
void gmx::BiasState::initGridPointState ( const AwhBiasParams &  awhBiasParams,
ArrayRef< const DimParams dimParams,
const BiasGrid grid,
const BiasParams params,
const CorrelationGrid forceCorrelation,
const std::string &  filename,
int  numBias 
)

Initialize the state of grid coordinate points.

Parameters
[in]awhBiasParamsBias parameters from inputrec.
[in]dimParamsThe dimension parameters.
[in]gridThe grid.
[in]paramsThe bias parameters.
[in]forceCorrelationThe force correlation statistics for every grid point.
[in]filenameName of file to read PMF and target from.
[in]numBiasThe number of biases.
void gmx::BiasState::initHistoryFromState ( AwhBiasHistory *  biasHistory) const

Allocate and initialize a bias history with the given bias state.

This function will be called at the start of a new simulation. Note that this only sets the correct size and does produce a valid history object, but with all data set to zero. Actual history data is set by updateHistory.

Parameters
[in,out]biasHistoryAWH history to initialize.
double gmx::BiasState::moveUmbrella ( ArrayRef< const DimParams dimParams,
const BiasGrid grid,
ArrayRef< const double >  probWeightNeighbor,
ArrayRef< const double >  neighborLambdaDhdl,
ArrayRef< double >  biasForce,
int64_t  step,
int64_t  seed,
int  indexSeed,
bool  onlySampleUmbrellaGridpoint 
)

Move the center point of the umbrella potential.

A new umbrella center is sampled from the biased distibution. Also, the bias force is updated and the new potential is return.

This function should only be called when the bias force is not being convolved. It is assumed that the probability distribution has been updated.

Parameters
[in]dimParamsBias dimension parameters.
[in]gridThe grid.
[in]probWeightNeighborProbability weights of the neighbors.
[in]neighborLambdaDhdlAn array containing the dHdL at the neighboring lambda points. The array is of length numLambdas+1, where numLambdas is the number of free energy lambda states. Element 0 in the array is the dHdL of the current state and elements 1..numLambdas contain the dHdL of the system in the neighboring lambda states (also including the current state). When there are no free energy lambda state dimensions this can be empty.
[in,out]biasForceThe AWH bias force.
[in]stepStep number, needed for the random number generator.
[in]seedRandom seed.
[in]indexSeedSecond random seed, should be the bias Index.
[in]onlySampleUmbrellaGridpointOnly sample the umbrella gridpoint without calculating force and potential.
Returns
the new potential value.
void gmx::BiasState::restoreFromHistory ( const AwhBiasHistory &  biasHistory,
const BiasGrid grid 
)

Restore the bias state from history.

Parameters
[in]biasHistoryBias history struct.
[in]gridThe bias grid.
void gmx::BiasState::sampleCoordAndPmf ( const std::vector< DimParams > &  dimParams,
const BiasGrid grid,
gmx::ArrayRef< const double >  probWeightNeighbor,
double  convolvedBias 
)

Sample the reaction coordinate and PMF for future updates or analysis.

These samples do not affect the (future) sampling and are thus pure observables. Statisics of these are stored in the energy file.

Parameters
[in]dimParamsThe bias dimensions parameters
[in]gridThe grid.
[in]probWeightNeighborProbability weights of the neighbors.
[in]convolvedBiasThe convolved bias.
void gmx::BiasState::sampleProbabilityWeights ( const BiasGrid grid,
gmx::ArrayRef< const double >  probWeightNeighbor 
)

Take samples of the current probability weights for future updates and analysis.

Points in the current neighborhood will now have data meaning they need to be included in the local update list of the next update. Therefore, the local update range is also update here.

Parameters
[in]gridThe grid.
[in]probWeightNeighborProbability weights of the neighbors.
void gmx::BiasState::setCoordValue ( const BiasGrid grid,
const awh_dvec  coordValue 
)
inline

Update the reaction coordinate value.

Parameters
[in]gridThe bias grid.
[in]coordValueThe current reaction coordinate value (there are no limits on allowed values).
void gmx::BiasState::updateFreeEnergyAndAddSamplesToHistogram ( ArrayRef< const DimParams dimParams,
const BiasGrid grid,
const BiasParams params,
const CorrelationGrid forceCorrelation,
double  t,
int64_t  step,
FILE *  fplog,
std::vector< int > *  updateList 
)

Performs an update of the bias.

The objective of the update is to use collected samples (probability weights) to improve the free energy estimate. For sake of efficiency, the update is local whenever possible, meaning that only points that have actually been sampled are accessed and updated here. For certain AWH settings or at certain steps however, global need to be performed. Besides the actual free energy update, this function takes care of ensuring future convergence of the free energy. Convergence is obtained by increasing the size of the reference weight histogram in a controlled (sometimes dynamic) manner. Also, there are AWH variables that are direct functions of the free energy or sampling history that need to be updated here, namely the target distribution and the bias function.

Parameters
[in]dimParamsThe dimension parameters.
[in]gridThe grid.
[in]paramsThe bias parameters.
[in]forceCorrelationThe force correlation statistics for every grid point.
[in]tTime.
[in]stepTime step.
[in,out]fplogLog file.
[in,out]updateListWork space to store a temporary list.
void gmx::BiasState::updateHistory ( AwhBiasHistory *  biasHistory,
const BiasGrid grid 
) const

Update the bias state history with the current state.

Parameters
[out]biasHistoryBias history struct.
[in]gridThe bias grid.
double gmx::BiasState::updateProbabilityWeightsAndConvolvedBias ( ArrayRef< const DimParams dimParams,
const BiasGrid grid,
ArrayRef< const double >  neighborLambdaEnergies,
std::vector< double, AlignedAllocator< double >> *  weight 
) const

Update the probability weights and the convolved bias.

Given a coordinate value, each grid point is assigned a probability weight, w(point|value), that depends on the current bias function. The sum of these weights is needed for normalizing the probability sum to 1 but also equals the effective, or convolved, biasing weight for this coordinate value. The convolved bias is needed e.g. for extracting the PMF, so we save it here since this saves us from doing extra exponential function evaluations later on.

Parameters
[in]dimParamsThe bias dimensions parameters
[in]gridThe grid.
[in]neighborLambdaEnergiesAn array containing the energy of the system in neighboring lambdas. The array is of length numLambdas+1, where numLambdas is the number of free energy lambda states. Element 0 in the array is the energy of the current state and elements 1..numLambdas contain the energy of the system in the neighboring lambda states (also including the current state). When there are no free energy lambda state dimensions this can be empty.
[out]weightProbability weights of the neighbors, SIMD aligned.
Returns
the convolved bias.
void gmx::BiasState::updateSharedCorrelationTensorTimeIntegral ( const BiasParams biasParams,
const CorrelationGrid forceCorrelation,
bool  shareAcrossAllRanks 
)

Updates sharedCorrelationTensorTimeIntegral_ for all points.

Parameters
[in]biasParamsThe bias parameters.
[in]forceCorrelationThe force correlation grid.
[in]shareAcrossAllRanksShare the data across all ranks? Otherwise only the main ranks.
int gmx::BiasState::warnForHistogramAnomalies ( const BiasGrid grid,
int  biasIndex,
double  t,
FILE *  fplog,
int  maxNumWarnings 
) const

Performs statistical checks on the collected histograms and warns if issues are detected.

Parameters
[in]gridThe grid.
[in]biasIndexThe index of the bias we are checking for.
[in]tTime.
[in,out]fplogOutput file for warnings.
[in]maxNumWarningsDon't issue more than this number of warnings.
Returns
the number of warnings issued.

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