Skip to content
Snippets Groups Projects
Commit 0f64bf2a authored by Giovanni Bussi's avatar Giovanni Bussi
Browse files

Forbidding use of multiple values with unnamed values

I forbid Actions to declare both an unnamed value and named values.
In a far future, the action name alone will imply passing the full structure.
Example:
d1: DISTANCE ATOMS=1,2 COMPONENTS
now only declared d1.x d1.y and d1.z, whereas d1 cannot be accessed
directly as a value (since it is a complex structure that we cannot handle
yet)
parent 15423eae
No related branches found
No related tags found
No related merge requests found
......@@ -13,7 +13,9 @@ void ActionWithValue::enforceNumericalDerivatives(){
ActionWithValue::ActionWithValue(const ActionOptions&ao):
Action(ao),
numberOfParameters(0),
numericalDerivatives(false)
numericalDerivatives(false),
hasMultipleValues(false),
hasUnnamedValue(false)
{
parseFlag("NUMERICAL_DERIVATIVES",numericalDerivatives);
if(numericalDerivatives) log.printf(" using numerical derivatives\n");
......@@ -23,13 +25,24 @@ ActionWithValue::~ActionWithValue(){
for(unsigned i=0;i<values.size();++i)delete values[i];
}
void ActionWithValue::addValue(const std::string&name){
void ActionWithValue::check(const std::string&name){
assertUnique(name);
if(name==""){
hasUnnamedValue=true;
assert(!hasMultipleValues);
}else{
hasMultipleValues=true;
assert(!hasUnnamedValue);
}
}
void ActionWithValue::addValue(const std::string&name){
check(name);
values.push_back(new Value(*this,name));
}
void ActionWithValue::addValueWithDerivatives(const std::string&name){
assertUnique(name);
check(name);
Value* v=new Value(*this,name);
v->enableDerivatives();
values.push_back(v);
......
......@@ -18,8 +18,11 @@ class ActionWithValue:
int numberOfParameters;
std::vector<Value*> values;
void assertUnique(const std::string&name);
void check(const std::string&name);
int getValueIndex(const std::string&name)const;
bool numericalDerivatives;
bool hasMultipleValues;
bool hasUnnamedValue;
protected:
/// Enforce the use of numerical derivatives.
/// This may be useful during the implementation of new collective
......
......@@ -125,7 +125,6 @@ PLUMED_BIAS_INIT(ao)
log.printf("\n");
};
addValue("");
addValue("Energy");
addValue("Force2");
addValue("Work");
......@@ -164,7 +163,6 @@ void BiasMovingRestraint::calculate(){
totf2+=f*f;
};
Value* value;
setValue(ene);
value=getValue("Energy");
setValue(value,ene);
value=getValue("Force2");
......
......@@ -65,7 +65,6 @@ kappa(getNumberOfArguments(),0.0)
for(unsigned i=0;i<kappa.size();i++) log.printf(" %f",kappa[i]);
log.printf("\n");
addValue("");
addValue("Energy");
addValue("Force2");
}
......@@ -82,7 +81,6 @@ void BiasRestraint::calculate(){
setOutputForces(i,f);
totf2+=f*f;
};
setValue(ene);
Value* value;
value=getValue("Energy"); setValue(value,ene);
value=getValue("Force2"); setValue(value,totf2);
......
......@@ -66,10 +66,13 @@ pbc(true)
else log.printf(" without periodic boundary conditions\n");
addValueWithDerivatives("");
getValue("")->setPeriodicity(false);
if(!components){
addValueWithDerivatives("");
getValue("")->setPeriodicity(false);
}else{
if(components){
addValueWithDerivatives("x");
getValue("x")->setPeriodicity(false);
addValueWithDerivatives("y");
......@@ -94,12 +97,15 @@ void ColvarDistance::calculate(){
const double value=distance.modulo();
const double invvalue=1.0/value;
setAtomsDerivatives(0,-invvalue*distance);
setAtomsDerivatives(1,invvalue*distance);
setBoxDerivatives (-invvalue*Tensor(distance,distance));
setValue (value);
if(!components){
setAtomsDerivatives(0,-invvalue*distance);
setAtomsDerivatives(1,invvalue*distance);
setBoxDerivatives (-invvalue*Tensor(distance,distance));
setValue (value);
}else{
if(components){
Value* valuex=getValue("x");
Value* valuey=getValue("y");
Value* valuez=getValue("z");
......
......@@ -17,7 +17,6 @@ This is just a template variable
//+ENDPLUMEDOC
class ColvarTemplate : public Colvar {
bool components;
bool pbc;
public:
......@@ -30,13 +29,11 @@ PLUMED_REGISTER_ACTION(ColvarTemplate,"TEMPLATE")
ColvarTemplate::ColvarTemplate(const ActionOptions&ao):
PLUMED_COLVAR_INIT(ao),
components(false),
pbc(true)
{
vector<AtomNumber> atoms;
parseAtomList("ATOMS",atoms);
assert(atoms.size()==2);
parseFlag("COMPONENTS",components);
bool nopbc=!pbc;
parseFlag("NOPBC",nopbc);
pbc=!nopbc;
......@@ -49,12 +46,6 @@ pbc(true)
addValueWithDerivatives("");
if(components){
addValueWithDerivatives("x");
addValueWithDerivatives("y");
addValueWithDerivatives("z");
}
requestAtoms(atoms);
}
......@@ -75,27 +66,6 @@ void ColvarTemplate::calculate(){
setAtomsDerivatives(1,invvalue*distance);
setBoxDerivatives (-invvalue*Tensor(distance,distance));
setValue (value);
if(components){
Value* valuex=getValue("x");
Value* valuey=getValue("y");
Value* valuez=getValue("z");
setAtomsDerivatives (valuex,0,Vector(-1,0,0));
setAtomsDerivatives (valuex,1,Vector(+1,0,0));
setBoxDerivatives (valuex,Tensor(distance,Vector(-1,0,0)));
setValue (valuex,distance[0]);
setAtomsDerivatives (valuey,0,Vector(0,-1,0));
setAtomsDerivatives (valuey,1,Vector(0,+1,0));
setBoxDerivatives (valuey,Tensor(distance,Vector(0,-1,0)));
setValue (valuey,distance[1]);
setAtomsDerivatives (valuez,0,Vector(0,0,-1));
setAtomsDerivatives (valuez,1,Vector(0,0,+1));
setBoxDerivatives (valuez,Tensor(distance,Vector(0,0,-1)));
setValue (valuez,distance[2]);
};
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment