Skip to content

Commit

Permalink
Add command line flag to calculate charge (#1067)
Browse files Browse the repository at this point in the history
* Add --charge command line flag

* 6.23.2. Revision bump for --charge command line flag

* Add help text, add manual entry.
  • Loading branch information
drroe authored Feb 3, 2024
1 parent dc4357b commit f4fd563
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
28 changes: 24 additions & 4 deletions doc/cpptraj.lyx
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ cpptraj [-p <Top0>] [-i <Input0>] [-y <trajin>] [-x <trajout>]
\end_layout

\begin_layout LyX-Code
[--rng {marsaglia|stdlib|mt|pcg32|xo128}]
[--rng {marsaglia|stdlib|mt|pcg32|xo128}] [--charge <mask>]
\end_layout

\begin_deeper
Expand Down Expand Up @@ -580,7 +580,11 @@ cpptraj [-p <Top0>] [-i <Input0>] [-y <trajin>] [-x <trajout>]
\end_layout

\begin_layout Description
-mr <mask> : Print selected residue numbers to STDOUT.
-mr
\begin_inset space ~
\end_inset

<mask> : Print selected residue numbers to STDOUT.
Selected residues are written out as 'Selected= 1 2 3 ...'
\end_layout

Expand All @@ -593,11 +597,27 @@ cpptraj [-p <Top0>] [-i <Input0>] [-y <trajin>] [-x <trajout>]
\end_layout

\begin_layout Description
–resmask <mask> : Print detailed residue selection to STDOUT.
–resmask
\begin_inset space ~
\end_inset

<mask> Print detailed residue selection to STDOUT.
\end_layout

\begin_layout Description
--rng <type> : Change default random number generator.
--rng
\begin_inset space ~
\end_inset

<type> Change default random number generator.
\end_layout

\begin_layout Description
--charge
\begin_inset space ~
\end_inset

<mask> Print total charge (in e-) of atoms selected by <mask> to STDOUT.
\end_layout

\end_deeper
Expand Down
36 changes: 35 additions & 1 deletion src/Cpptraj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void Cpptraj::Usage() {
" [-h | --help] [-V | --version] [--defines] [-debug <#>]\n"
" [--interactive] [--log <logfile>] [-tl]\n"
" [-ms <mask>] [-mr <mask>] [--mask <mask>] [--resmask <mask>]\n"
" [--rng %s]\n"
" [--rng %s] [--charge <mask>]\n"
"\t-p <Top0> : * Load <Top0> as a topology file.\n"
"\t-i <Input0> : * Read input from file <Input0>.\n"
"\t-y <trajin> : * Read from trajectory file <trajin>; same as input 'trajin <trajin>'.\n"
Expand All @@ -97,6 +97,7 @@ void Cpptraj::Usage() {
"\t--mask <mask> : Print detailed atom selection to STDOUT.\n"
"\t--resmask <mask> : Print detailed residue selection to STDOUT.\n"
"\t--rng <type> : Change default random number generator.\n"
"\t--charge <mask> : Print total charge in e- of atoms selected by <mask>.\n"
" * Denotes flag may be specified multiple times.\n"
"\n", CpptrajState::RngKeywords());
}
Expand Down Expand Up @@ -273,6 +274,35 @@ std::string Cpptraj::Defines() {
return defined_str;
}

/** Calculate the total charge from the command line. */
int Cpptraj::CalcCharge(Sarray const& topFiles, std::string const& maskexpr)
const
{
SetWorldSilent(true);
if (maskexpr.empty()) {
mprinterr("Error: No mask given for '--charge'.\n");
return 1;
}
if (topFiles.empty()) {
mprinterr("Error: No topology file specified.\n");
return 1;
}
ParmFile pfile;
Topology parm;
if (pfile.ReadTopology(parm, topFiles[0], State_.Debug())) return 1;
AtomMask tempMask( maskexpr );
if (parm.SetupIntegerMask( tempMask )) return 1;
if (tempMask.None()) {
mprinterr("Error: Nothing selected by mask '%s'\n", maskexpr.c_str());
return 1;
}
double qtotal = 0;
for (AtomMask::const_iterator it = tempMask.begin(); it != tempMask.end(); ++it)
qtotal += parm[*it].Charge();
loudPrintf("%g\n", qtotal);
return 0;
}

/** Process a mask from the command line. */
int Cpptraj::ProcessMask( Sarray const& topFiles, Sarray const& refFiles,
std::string const& maskexpr,
Expand Down Expand Up @@ -521,6 +551,10 @@ Cpptraj::Mode Cpptraj::ProcessCmdLineArgs(int argc, char** argv) {
// --resmask: Parse mask string, print selected residue details
if (ProcessMask( topFiles, refFiles, cmdLineArgs[++iarg], true, true )) return ERROR;
return QUIT;
} else if ( NotFinalArg(cmdLineArgs, "--charge", iarg) ) {
// --charge: Print the total charge of atoms selected by the mask
if (CalcCharge( topFiles, cmdLineArgs[++iarg])) return ERROR;
return QUIT;
} else if ( NotFinalArg(cmdLineArgs, "--rng", iarg) ) {
// --rng: Change default RNG
if (State_.ChangeDefaultRng( cmdLineArgs[++iarg] )) return ERROR;
Expand Down
2 changes: 2 additions & 0 deletions src/Cpptraj.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Cpptraj {
static void Finalize();
static inline void AddArgs(Sarray&, ArgList const&, int&);
static inline void ResizeArgs(Sarray const&, Sarray&, const char*);
/// Calculate total charge from command line
int CalcCharge(Sarray const&, std::string const&) const;
int ProcessMask(Sarray const&, Sarray const&, std::string const&, bool,bool) const;
Mode ProcessCmdLineArgs(int, char**);
int Interactive();
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.23.1"
#define CPPTRAJ_INTERNAL_VERSION "V6.23.2"
/// PYTRAJ relies on this
#define CPPTRAJ_VERSION_STRING CPPTRAJ_INTERNAL_VERSION
#endif

0 comments on commit f4fd563

Please sign in to comment.