Skip to content

Commit 9b057cf

Browse files
authored
Merge pull request #657 from drroe/scalevel
Add ability to scale velocities to 'setvelocity'
2 parents a3e08a2 + b530508 commit 9b057cf

File tree

5 files changed

+58
-9
lines changed

5 files changed

+58
-9
lines changed

src/Action_SetVelocity.cpp

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
Action_SetVelocity::Action_SetVelocity() : tempi_(0.0), zeroMomentum_(false) {}
77

88
void Action_SetVelocity::Help() const {
9-
mprintf("\t[<mask>] [{tempi <temperature> | modify}] [ig <random seed>]\n"
9+
mprintf("\t[<mask>] [{ tempi <temperature> |\n"
10+
"\t scale [factor <fac>] [sx <xfac>] [sy <yfac>] [sz <zfac>] |\n"
11+
"\t none | \n"
12+
"\t modify}] [ig <random seed>]\n"
1013
"\t[%s] [%s]\n", Constraints::constraintArgs, Constraints::rattleArgs);
11-
mprintf("\t[zeromomentum]\n");
14+
mprintf("\t[zeromomentum] [ig <random seed>]\n");
1215
mprintf(" Set velocities in frame for atoms in <mask> using Maxwellian distribution\n"
1316
" based on given temperature; default 300.0 K. If tempi is 0.0 set\n"
1417
" velocities of atoms in mask to 0.0.\n"
18+
" If 'scale' is specified scale the velocities by the given factor(s).\n"
19+
" If 'none' or a temperature of 0.0 is specified, velocities will be zeroed.\n"
1520
" If 'modify' is specified do not set; only modify existing velocities.\n"
1621
" This is useful e.g. with 'ntc' or 'zeromomentum'.\n"
1722
" If 'ntc' is specified attempt to correct velocities for constraints.\n"
@@ -24,11 +29,17 @@ Action::RetType Action_SetVelocity::Init(ArgList& actionArgs, ActionInit& init,
2429
{
2530
// Keywords
2631
tempi_ = actionArgs.getKeyDouble("tempi", 300.0);
27-
if (tempi_ < Constants::SMALL)
32+
if (actionArgs.hasKey("none") || tempi_ < Constants::SMALL)
2833
mode_ = ZERO;
2934
else if (actionArgs.hasKey("modify"))
3035
mode_ = MODIFY;
31-
else
36+
else if (actionArgs.hasKey("scale")) {
37+
double sf = actionArgs.getKeyDouble("factor", 1.0);
38+
scaleFac_[0] = actionArgs.getKeyDouble("sx", sf);
39+
scaleFac_[1] = actionArgs.getKeyDouble("sy", sf);
40+
scaleFac_[2] = actionArgs.getKeyDouble("sz", sf);
41+
mode_ = SCALE;
42+
} else
3243
mode_ = SET;
3344
int ig_ = actionArgs.getKeyInt("ig", -1);
3445
RN_.rn_set( ig_ );
@@ -56,6 +67,9 @@ Action::RetType Action_SetVelocity::Init(ArgList& actionArgs, ActionInit& init,
5667
mprintf("\tRandom seed is %i\n", ig_);
5768
} else if (mode_ == MODIFY)
5869
mprintf(" Modifying any existing velocities for atoms in mask '%s'\n", Mask_.MaskString());
70+
else if (mode_ == SCALE)
71+
mprintf(" Scaling velocities by X= %g, Y= %g, Z= %g\n",
72+
scaleFac_[0], scaleFac_[1], scaleFac_[2]);
5973
else if (mode_ == ZERO)
6074
mprintf(" Zeroing velocities for atoms in mask '%s'\n", Mask_.MaskString());
6175
if (cons_.Type() != Constraints::OFF) {
@@ -101,6 +115,11 @@ Action::RetType Action_SetVelocity::Setup(ActionSetup& setup) {
101115
mprintf("Warning: 'modify' specified but no velocity info, skipping.\n");
102116
return Action::SKIP;
103117
}
118+
// If scale need to have existing velocity info
119+
if (mode_ == SCALE && !setup.CoordInfo().HasVel()) {
120+
mprintf("Warning: 'scale' specified but no velocity info, skipping.\n");
121+
return Action::SKIP;
122+
}
104123
// Always add velocity info even if not strictly necessary
105124
cInfo_ = setup.CoordInfo();
106125
cInfo_.SetVelocity( true );
@@ -132,6 +151,14 @@ Action::RetType Action_SetVelocity::DoAction(int frameNum, ActionFrame& frm) {
132151
V[1] = RN_.rn_gauss(0.0, *sd);
133152
V[2] = RN_.rn_gauss(0.0, *sd);
134153
}
154+
} else if (mode_ == SCALE) {
155+
for (AtomMask::const_iterator atom = Mask_.begin(); atom != Mask_.end(); ++atom)
156+
{
157+
double* V = newFrame_.vAddress() + (*atom * 3);
158+
V[0] *= scaleFac_[0];
159+
V[1] *= scaleFac_[1];
160+
V[2] *= scaleFac_[2];
161+
}
135162
}
136163
// Correct velocities for constraints
137164
if (cons_.Type() != Constraints::OFF)

src/Action_SetVelocity.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ class Action_SetVelocity : public Action {
1515
Action::RetType DoAction(int, ActionFrame&);
1616
void Print() {}
1717

18-
enum ModeType { SET = 0, ZERO, MODIFY };
18+
enum ModeType { SET = 0, ZERO, MODIFY, SCALE };
1919

2020
typedef std::vector<double> Darray;
2121

2222
AtomMask Mask_; ///< Atoms to set/modify velocities of
2323
Darray SD_; ///< Hold sqrt(kB*(1/mass)) for each atom
2424
double tempi_; ///< Temperature to generate velocity distribution at.
25+
Vec3 scaleFac_; ///< Velocity scaling factors.
2526
ModeType mode_; ///< Set velocity, zero velocity, modify existing velocity.
2627
Constraints cons_; ///< Hold constraint info
2728
Random_Number RN_;

src/Version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
* Whenever a number that precedes <revision> is incremented, all subsequent
2121
* numbers should be reset to 0.
2222
*/
23-
#define CPPTRAJ_INTERNAL_VERSION "V4.10.7"
23+
#define CPPTRAJ_INTERNAL_VERSION "V4.10.8"
2424
#endif

test/Test_SetVelocity/RunTest.sh

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
. ../MasterTest.sh
44

5-
CleanFiles setvel.in tz2.vel.rst7 V1.dat
5+
CleanFiles setvel.in tz2.vel.rst7 V1.dat tz2.scale.rst7
66

77
INPUT='-i setvel.in'
88

9-
TESTNAME='Set Velocity test'
9+
TESTNAME='Set Velocity tests'
1010
Requires maxthreads 1
11+
12+
UNITNAME='Set Velocity test'
1113
cat > setvel.in <<EOF
1214
parm ../tz2.parm7
1315
trajin ../tz2.rst7
@@ -25,9 +27,20 @@ setvelocity modify zeromomentum
2527
vector V1 momentum out V1.dat
2628
go
2729
EOF
28-
RunCpptraj "$TESTNAME"
30+
RunCpptraj "$UNITNAME"
2931
DoTest tz2.vel.rst7.save tz2.vel.rst7
3032
DoTest V1.dat.save V1.dat
3133

34+
UNITNAME='Scale velocity test'
35+
cat > setvel.in <<EOF
36+
parm ../tz2.parm7
37+
trajin tz2.vel.rst7.save
38+
strip !:13
39+
setvelocity scale factor 2.0
40+
trajout tz2.scale.rst7
41+
EOF
42+
RunCpptraj "$UNITNAME"
43+
DoTest tz2.scale.rst7.save tz2.scale.rst7
44+
3245
EndTest
3346
exit 0
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Cpptraj Generated Restart
2+
6
3+
-4.8320000 -10.2020000 -0.3440000 -4.6470000 -9.2390000 -0.5870000
4+
-5.7200000 -10.8660000 -1.2770000 -5.5910000 -11.9460000 -1.3400000
5+
-6.7570000 -10.8860000 -0.9420000 -5.7770000 -10.3580000 -2.2400000
6+
0.3643370 0.0717144 0.1748470 1.7579932 1.0938414 2.7569718
7+
-0.7964882 0.7930536 -0.1924086 1.0052930 -0.5603402 0.6513296
8+
-0.3944468 2.7520096 3.7998472 -0.7440500 -1.4602778 -0.5855208

0 commit comments

Comments
 (0)