1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 Copyright (c) 2012-2021 The plumed team
3 (see the PEOPLE file at the root of the distribution for a list of names)
4
5 See http://www.plumed.org for more information.
6
7 This file is part of plumed, version 2.
8
9 plumed is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 plumed is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 #include "Bias.h"
23 #include "ActionRegister.h"
24
25
26 using namespace std;
27
28
29 namespace PLMD {
30 namespace bias {
31
32 //+PLUMEDOC BIAS BIASVALUE
33 /*
34 Takes the value of one variable and use it as a bias
35
36 This is the simplest possible bias: the bias potential is equal to a collective variable.
37 It is useful to create custom biasing potential, e.g. applying a function (see \ref Function)
38 to some collective variable then using the value of this function directly as a bias.
39
40 \par Examples
41
42 The following input tells plumed to use the value of the distance between atoms 3 and 5
43 and the value of the distance between atoms 2 and 4 as biases.
44 It then tells plumed to print the energy of the restraint
45 \plumedfile
46 DISTANCE ATOMS=3,5 LABEL=d1
47 DISTANCE ATOMS=3,6 LABEL=d2
48 BIASVALUE ARG=d1,d2 LABEL=b
49 PRINT ARG=d1,d2,b.d1_bias,b.d2_bias
50 \endplumedfile
51
52 Another thing one can do is asking one system to follow
53 a circle in sin/cos according a time dependence
54
55 \plumedfile
56 t: TIME
57 # this just print cos and sin of time
58 cos: MATHEVAL ARG=t VAR=t FUNC=cos(t) PERIODIC=NO
59 sin: MATHEVAL ARG=t VAR=t FUNC=sin(t) PERIODIC=NO
60 c1: COM ATOMS=1,2
61 c2: COM ATOMS=3,4
62 d: DISTANCE COMPONENTS ATOMS=c1,c2
63 PRINT ARG=t,cos,sin,d.x,d.y,d.z STRIDE=1 FILE=colvar FMT=%8.4f
64 # this calculates sine and cosine of a projected component of distance
65 mycos: MATHEVAL ARG=d.x,d.y VAR=x,y FUNC=x/sqrt(x*x+y*y) PERIODIC=NO
66 mysin: MATHEVAL ARG=d.x,d.y VAR=x,y FUNC=y/sqrt(x*x+y*y) PERIODIC=NO
67 # this creates a moving spring so that the system follows a circle-like dynamics
68 # but it is not a bias, it is a simple value now
69 vv1: MATHEVAL ARG=mycos,mysin,cos,sin VAR=mc,ms,c,s FUNC=100*((mc-c)^2+(ms-s)^2) PERIODIC=NO
70 # this takes the value calculated with matheval and uses as a bias
71 cc: BIASVALUE ARG=vv1
72 # some printout
73 PRINT ARG=t,cos,sin,d.x,d.y,d.z,mycos,mysin,cc.vv1_bias STRIDE=1 FILE=colvar FMT=%8.4f
74 \endplumedfile
75
76 */
77 //+ENDPLUMEDOC
78
79 class BiasValue : public Bias {
80 public:
81 explicit BiasValue(const ActionOptions&);
82 void calculate() override;
83 static void registerKeywords(Keywords& keys);
84 };
85
86 PLUMED_REGISTER_ACTION(BiasValue,"BIASVALUE")
87
registerKeywords(Keywords & keys)88 void BiasValue::registerKeywords(Keywords& keys) {
89 Bias::registerKeywords(keys);
90 keys.use("ARG");
91 // Should be _bias below
92 keys.addOutputComponent("_bias","default","one or multiple instances of this quantity can be referenced elsewhere in the input file. "
93 "these quantities will named with the arguments of the bias followed by "
94 "the character string _bias. These quantities tell the user how much the bias is "
95 "due to each of the colvars.");
96 }
97
BiasValue(const ActionOptions & ao)98 BiasValue::BiasValue(const ActionOptions&ao):
99 PLUMED_BIAS_INIT(ao)
100 {
101 checkRead();
102 // add one bias for each argument
103 for(unsigned i=0; i<getNumberOfArguments(); ++i) {
104 string ss=getPntrToArgument(i)->getName()+"_bias";
105 addComponent(ss); componentIsNotPeriodic(ss);
106 }
107 }
108
calculate()109 void BiasValue::calculate() {
110 double bias=0.0;
111 for(unsigned i=0; i<getNumberOfArguments(); ++i) {
112 double val; val=getArgument(i);
113 getPntrToComponent(i+1)->set(val);
114 setOutputForce(i,-1.);
115 bias+=val;
116 }
117 setBias(bias);
118 }
119
120 }
121 }
122