Skip to content

Commit

Permalink
Add 'byfac' keyword to change mass/charge command (#1079)
Browse files Browse the repository at this point in the history
* Add byfac to change charge/mass

* Add test for change byfac

* Add byfac to manual

* 6.25.2. Revision bump for change byfac
  • Loading branch information
drroe authored May 7, 2024
1 parent 59c077d commit e043c39
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 10 deletions.
15 changes: 13 additions & 2 deletions doc/cpptraj.lyx
Original file line number Diff line number Diff line change
Expand Up @@ -14842,8 +14842,11 @@ change [parm <name> | parmindex <#> | <#> |
\end_layout

\begin_layout LyX-Code
{mass|charge} [of <mask>] {to <value>|by <offset>|fromset <data set>}
|
{mass|charge} [of <mask>] {to <value> |by <offset> |
\end_layout

\begin_layout LyX-Code
byfac <factor> |fromset <data set>} |
\end_layout

\begin_layout LyX-Code
Expand Down Expand Up @@ -15171,6 +15174,14 @@ by
<offset> Value to offset masses/charges by.
\end_layout

\begin_layout Description
byfac
\begin_inset space ~
\end_inset

<factor> Value to multiply masses/charges by.
\end_layout

\begin_layout Description
fromset
\begin_inset space ~
Expand Down
34 changes: 29 additions & 5 deletions src/Exec_Change.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ void Exec_Change::Help() const
"\t addbond <mask1> <mask2> [req <length> <rk> <force constant>] |\n"
"\t removebonds <mask1> [<mask2>] [out <file>] |\n"
"\t bondparm <mask1> [<mask2>] {setrk|scalerk|setreq|scalereq} <value> |\n"
"\t {mass|charge} [of <mask>] {to <value>|by <offset>|fromset <data set>} |\n"
"\t {mass|charge} [of <mask>] {to <value> |by <offset> |\n"
"\t byfac <factor> |fromset <data set>} |\n"
"\t mergeres firstres <start res#> lastres <stop res#>\n"
"\t}\n"
" Change specified parts of topology or topology of a COORDS data set.\n",
Expand Down Expand Up @@ -633,6 +634,16 @@ void Exec_Change::changeTopVal(Topology& topIn, int atnum, ChangeType typeIn, do
mprintf("\tChanging charge of atom '%s' by %g to %g\n", topIn.AtomMaskName(atnum).c_str(), newVal, oldVal+newVal);
currentAtom.SetCharge( oldVal + newVal );
break;
case MASS_BYFAC :
oldVal = currentAtom.Mass();
mprintf("\tChanging mass of atom '%s' by factor of %g to %g\n", topIn.AtomMaskName(atnum).c_str(), newVal, oldVal*newVal);
currentAtom.SetMass( oldVal * newVal );
break;
case CHARGE_BYFAC :
oldVal = currentAtom.Charge();
mprintf("\tChanging charge of atom '%s' by factor of %g to %g\n", topIn.AtomMaskName(atnum).c_str(), newVal, oldVal*newVal);
currentAtom.SetCharge( oldVal * newVal );
break;
}
}

Expand Down Expand Up @@ -698,11 +709,15 @@ const
ChangeType ctype;
bool change_to = argIn.Contains("to");
bool change_by = argIn.Contains("by");
if (!change_to && !change_by) {
mprinterr("Error: Expected either 'fromset', 'to', or 'by' for 'change %s'.\n", desc[typeIn]);
bool change_byfac = argIn.Contains("byfac");
int nchange = 0;
if (change_to) nchange++;
if (change_by) nchange++;
if (change_byfac) nchange++;
if (nchange < 1) {
mprinterr("Error: Expected either 'fromset', 'to', 'by', or 'byfac' for 'change %s'.\n", desc[typeIn]);
return 1;
}
if (change_to && change_by) {
} else if (nchange > 1) {
mprinterr("Error: Specify either 'to' or 'by', not both.\n");
return 1;
}
Expand All @@ -724,6 +739,15 @@ const
for (AtomMask::const_iterator at = atomsToChange.begin(); at != atomsToChange.end(); ++at) {
changeTopVal(topIn, *at, ctype, offset);
}
} else if (change_byfac) {
if (typeIn == 0)
ctype = MASS_BYFAC;
else
ctype = CHARGE_BYFAC;
double fac = argIn.getKeyDouble("byfac", 0.0);
for (AtomMask::const_iterator at = atomsToChange.begin(); at != atomsToChange.end(); ++at) {
changeTopVal(topIn, *at, ctype, fac);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Exec_Change.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Exec_Change : public Exec {
DispatchObject* Alloc() const { return (DispatchObject*)new Exec_Change(); }
RetType Execute(CpptrajState&, ArgList&);
private:
enum ChangeType { MASS_TO = 0, CHARGE_TO, MASS_BY, CHARGE_BY };
enum ChangeType { MASS_TO = 0, CHARGE_TO, MASS_BY, CHARGE_BY, MASS_BYFAC, CHARGE_BYFAC };
/// Change mass or charge
static inline void changeTopVal(Topology&, int, ChangeType, double);

Expand Down
2 changes: 1 addition & 1 deletion src/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Whenever a number that precedes <revision> is incremented, all subsequent
* numbers should be reset to 0.
*/
#define CPPTRAJ_INTERNAL_VERSION "V6.25.1"
#define CPPTRAJ_INTERNAL_VERSION "V6.25.2"
/// PYTRAJ relies on this
#define CPPTRAJ_VERSION_STRING CPPTRAJ_INTERNAL_VERSION
#endif
13 changes: 13 additions & 0 deletions test/Test_Change/AFV.1.byfac.dat.save
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#Atom Name #Res Name #Mol Type Charge Mass GBradius El rVDW eVDW
1 N 1 ALA 1 N3 -0.4293 30.0200 1.5500 N 1.8240 0.1700
2 H1 1 ALA 1 H -0.4002 4.0160 1.3000 H 0.6000 0.0157
3 H2 1 ALA 1 H -0.4002 4.0160 1.3000 H 0.6000 0.0157
4 H3 1 ALA 1 H -0.4002 4.0160 1.3000 H 0.6000 0.0157
5 CA 1 ALA 1 CX -0.4519 26.0200 1.7000 C 1.9080 0.1094
6 HA 1 ALA 1 HP -0.4556 4.0160 1.3000 H 1.1000 0.0157
7 CB 1 ALA 1 CT -0.5299 26.0200 1.7000 C 1.9080 0.1094
8 HB1 1 ALA 1 HC -0.4850 4.0160 1.3000 H 1.4870 0.0157
9 HB2 1 ALA 1 HC -0.4850 4.0160 1.3000 H 1.4870 0.0157
10 HB3 1 ALA 1 HC -0.4850 4.0160 1.3000 H 1.4870 0.0157
11 C 1 ALA 1 C -0.1918 26.0200 1.7000 C 1.9080 0.0860
12 O 1 ALA 1 O -0.7861 34.0000 1.5000 O 1.6612 0.2100
6 changes: 5 additions & 1 deletion test/Test_Change/RunTest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

CleanFiles change.in ala3.mod.pdb ala3.chain.pdb crdala3.chain.pdb \
AFV.zeroHmass.dat AFV.fluctMass.dat merged.?-?.mol2 \
AFV.zeroHcharge.dat AFV.1.offset.dat
AFV.zeroHcharge.dat AFV.1.offset.dat AFV.1.byfac.dat

TESTNAME='Change command test'

Expand Down Expand Up @@ -102,9 +102,13 @@ change charge of :1 by -1.0
change mass of :1 by 1.0
parmstrip !:1
atoms * out AFV.1.offset.dat
change charge of :1 byfac 0.5
change mass of :1 byfac 2.0
atoms * out AFV.1.byfac.dat
EOF
RunCpptraj "$UNITNAME"
DoTest AFV.1.offset.dat.save AFV.1.offset.dat
DoTest AFV.1.byfac.dat.save AFV.1.byfac.dat
fi

UNITNAME='Merge residues test'
Expand Down

0 comments on commit e043c39

Please sign in to comment.