From e043c3968d1b6834ddd0123fa31dba31e7c04193 Mon Sep 17 00:00:00 2001 From: "Daniel R. Roe" Date: Tue, 7 May 2024 14:46:11 -0400 Subject: [PATCH] Add 'byfac' keyword to change mass/charge command (#1079) * Add byfac to change charge/mass * Add test for change byfac * Add byfac to manual * 6.25.2. Revision bump for change byfac --- doc/cpptraj.lyx | 15 ++++++++++-- src/Exec_Change.cpp | 34 +++++++++++++++++++++++---- src/Exec_Change.h | 2 +- src/Version.h | 2 +- test/Test_Change/AFV.1.byfac.dat.save | 13 ++++++++++ test/Test_Change/RunTest.sh | 6 ++++- 6 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 test/Test_Change/AFV.1.byfac.dat.save diff --git a/doc/cpptraj.lyx b/doc/cpptraj.lyx index edb8994619..56dba3d66f 100644 --- a/doc/cpptraj.lyx +++ b/doc/cpptraj.lyx @@ -14842,8 +14842,11 @@ change [parm | parmindex <#> | <#> | \end_layout \begin_layout LyX-Code - {mass|charge} [of ] {to |by |fromset } - | + {mass|charge} [of ] {to |by | +\end_layout + +\begin_layout LyX-Code + byfac |fromset } | \end_layout \begin_layout LyX-Code @@ -15171,6 +15174,14 @@ by Value to offset masses/charges by. \end_layout +\begin_layout Description +byfac +\begin_inset space ~ +\end_inset + + Value to multiply masses/charges by. +\end_layout + \begin_layout Description fromset \begin_inset space ~ diff --git a/src/Exec_Change.cpp b/src/Exec_Change.cpp index eeadcab3f4..be877f9316 100644 --- a/src/Exec_Change.cpp +++ b/src/Exec_Change.cpp @@ -18,7 +18,8 @@ void Exec_Change::Help() const "\t addbond [req ] |\n" "\t removebonds [] [out ] |\n" "\t bondparm [] {setrk|scalerk|setreq|scalereq} |\n" - "\t {mass|charge} [of ] {to |by |fromset } |\n" + "\t {mass|charge} [of ] {to |by |\n" + "\t byfac |fromset } |\n" "\t mergeres firstres lastres \n" "\t}\n" " Change specified parts of topology or topology of a COORDS data set.\n", @@ -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; } } @@ -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; } @@ -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); + } } } diff --git a/src/Exec_Change.h b/src/Exec_Change.h index 606958d87b..6236e64748 100644 --- a/src/Exec_Change.h +++ b/src/Exec_Change.h @@ -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); diff --git a/src/Version.h b/src/Version.h index a3aa4bf490..99734a3ff0 100644 --- a/src/Version.h +++ b/src/Version.h @@ -12,7 +12,7 @@ * Whenever a number that precedes 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 diff --git a/test/Test_Change/AFV.1.byfac.dat.save b/test/Test_Change/AFV.1.byfac.dat.save new file mode 100644 index 0000000000..155c0925b1 --- /dev/null +++ b/test/Test_Change/AFV.1.byfac.dat.save @@ -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 diff --git a/test/Test_Change/RunTest.sh b/test/Test_Change/RunTest.sh index 06c1fbe328..3aab525807 100755 --- a/test/Test_Change/RunTest.sh +++ b/test/Test_Change/RunTest.sh @@ -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' @@ -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'