22 #include "vesselbase/VesselRegister.h"
24 #include "vesselbase/ActionWithVessel.h"
27 namespace multicolvar{
44 bool applyForce( std::vector<double>& forces );
54 keys.
reserve(
"optional",
"MOMENTS",
"calculate the moments of the distribution of collective variables. "
55 "The \\f$m\\f$th moment of a distribution is calculated using \\f$\\frac{1}{N} \\sum_{i=1}^N ( s_i - \\overline{s} )^m \\f$, where \\f$\\overline{s}\\f$ is "
56 "the average for the distribution. The moments keyword takes a lists of integers as input or a range. Each integer is a value of \\f$m\\f$. The final "
57 "calculated values can be referenced using moment-\\f$m\\f$.");
58 keys.
addOutputComponent(
"moment",
"MOMENTS",
"the central moments of the distribution of values. The second moment "
59 "would be referenced elsewhere in the input file using "
60 "<em>label</em>.moment-2, the third as <em>label</em>.moment-3, etc.");
67 plumed_massert(a,
"cannot create passable values as base action does not inherit from ActionWithValue");
68 plumed_massert(!
diffweight,
"cannot do weighted moments properly");
72 for(
unsigned i=0;i<moments.size();++i){
77 if( nn<2 )
error(
"moments are only possible for m>=2" );
88 std::string descri, num;
90 descri =
"value " +
getAction()->
getLabel() +
"." +
"moment-" + num +
" contains the " + num +
"th moment of the distribution";
91 for(
unsigned i=1;i<
powers.size();++i){
93 descri = descri +
"\n value " +
getAction()->
getLabel() +
"." +
"moment-" + num +
" contains the " + num +
"th moment of the distribution";
99 const double pi=3.141592653589793238462643383279502884197169399375105820974944592307;
102 double mean=0;
Value myvalue;
106 pfactor = 2*pi / ( max-min ); myvalue.
setDomain( str_min, str_max );
107 double sinsum=0, cossum=0, val;
108 for(
unsigned i=0;i<nvals;++i){ val=pfactor*(
getValue(i) - min ); sinsum+=sin(val); cossum+=cos(val); }
109 mean = 0.5 + atan2( sinsum / static_cast<double>( nvals ) , cossum / static_cast<double>( nvals ) ) / (2*
pi);
110 mean = min + (max-min)*mean;
112 for(
unsigned i=0;i<nvals;++i) mean+=
getValue(i);
116 for(
unsigned npow=0;npow<
powers.size();++npow){
119 dev1/=
static_cast<double>( nvals );
121 double pref, tmp, moment=0;
122 for(
unsigned i=0;i<nvals;++i){
124 pref=pow( tmp,
powers[npow] - 1 ) - dev1;
125 moment+=pow( tmp,
powers[npow] );
128 value_out[npow]->chainRule(
powers[npow] / static_cast<double>( nvals ) );
129 value_out[npow]->set( moment / static_cast<double>( nvals ) );
134 std::vector<double> tmpforce( forces.size() );
135 forces.assign(forces.size(),0.0);
bool wasforced=
false;
136 for(
unsigned i=0;i<
value_out.size();++i){
139 for(
unsigned j=0;j<forces.size();++j) forces[j]+=tmpforce[j];
double difference(double) const
Calculate the difference between the instantaneous value of the function and some other point: other_...
void performCalculationUsingAllValues()
Do the calculation.
void componentIsNotPeriodic(const std::string &name)
Set your value component to have no periodicity.
static void reserveKeyword(Keywords &keys)
A class for holding the value of a function together with its derivatives.
virtual unsigned getNumberOfDerivatives()=0
Get the number of derivatives for final calculated quantity.
std::string description()
Return a description of the vessel contents.
unsigned getNumberOfStoredColvars() const
Get the number of stored colvar values.
const std::string & getLabel() const
Returns the label.
void addDerivatives(const unsigned &ival, double &pref, Value *value_out)
Add the derivatives from the value.
double getValue(const unsigned &) const
Get the ith value in the vessel.
void addOutputComponent(const std::string &name, const std::string &key, const std::string &descr)
Add a potential component which can be output by this particular action.
Used to create a PLMD::Action that has some scalar or vectorial output that may or may not have some ...
This class holds the keywords and their documentation.
void setNotPeriodic()
Set the function not periodic.
virtual void retrieveDomain(std::string &min, std::string &max)
What are the domains of the base quantities.
static void registerKeywords(Keywords &keys)
#define PLUMED_REGISTER_VESSEL(classname, keyword)
std::vector< unsigned > powers
Value * copyOutput(const std::string &name) const
Return a pointer to the value with name (this is used to retrieve values in other PLMD::Actions) You ...
void reserve(const std::string &t, const std::string &k, const std::string &d, const bool isvessel=false)
Reserve a keyword.
void error(const std::string &errmsg)
Report an error.
This class is used to pass the input to Vessels.
const double pi(3.141592653589793238462643383279502884197169399375105820974944592307)
PI.
void local_resizing()
This makes sure things further down the chain are resized.
void addComponentWithDerivatives(const std::string &name)
Add a value with a name like label.name that has derivatives.
static void registerKeywords(Keywords &keys)
bool applyForce(std::vector< double > &forces)
Retrieve the forces on the quantities in the vessel.
int getNumberOfComponents() const
Returns the number of values defined.
Moments(const vesselbase::VesselOptions &da)
bool diffweight
Are the weights differentiable.
std::vector< Value * > value_out
void setDomain(const std::string &, const std::string &)
Set the domain of the function.
std::string getAllInput()
This returns the whole input line (it is used for less_than/more_than/between)
ActionWithVessel * getAction()
Return a pointer to the action we are working in.