which should be taken from the enum above. */
extern double convert2gmx(double x,int unit);
- /* Conert value x from GROMACS units to the desired one.
+ /* Convert value x from GROMACS units to the desired one.
The type of return value is deduced from unit, see above */
- double gmx2convert(double x,int unit);
+ extern double gmx2convert(double x,int unit);
+
+ /* Convert the string to one of the units supported. Returns -1 if not found. */
+ extern int string2unit(char *string);
+
+ /* Convert the unit to a strong. Return NULL when unit is out of range. */
+ extern const char *unit2string(int unit);
#ifdef __cplusplus
}
double convert2gmx(double x,int unit)
{
- switch (unit)
- {
- case eg2cAngstrom:
- return x*A2NM;
- case eg2cNm:
+ switch (unit)
+ {
+ case eg2cAngstrom:
+ return x*A2NM;
+ case eg2cNm:
+ return x;
+ case eg2cBohr:
+ return x*BOHR2NM;
+ case eg2cKcal_Mole:
+ return x/CAL2JOULE;
+ case eg2cHartree:
+ return x*ONE_4PI_EPS0/BOHR2NM;
+ case eg2cHartree_e:
+ return x*ONE_4PI_EPS0/BOHR2NM;
+ case eg2cAngstrom3:
+ return x*A2NM*A2NM*A2NM;
+ case eg2cCoulomb:
+ return x/E_CHARGE;
+ case eg2cDebye:
+ return x*DEBYE2ENM;
+ case eg2cElectron:
+ return x;
+ case eg2cBuckingham:
+ return x*A2NM*DEBYE2ENM;
+ default:
+ fprintf(stderr,"Unknown unit %d, not converting.\n",unit);
+ }
return x;
- case eg2cBohr:
- return x*BOHR2NM;
- case eg2cKcal_Mole:
- return x/CAL2JOULE;
- case eg2cHartree:
- return x*ONE_4PI_EPS0/BOHR2NM;
- case eg2cHartree_e:
- return x*ONE_4PI_EPS0/BOHR2NM;
- case eg2cAngstrom3:
- return x*A2NM*A2NM*A2NM;
- case eg2cCoulomb:
- return x/E_CHARGE;
- case eg2cDebye:
- return x*DEBYE2ENM;
- case eg2cElectron:
- return x;
- case eg2cBuckingham:
- return x*A2NM*DEBYE2ENM;
- default:
- fprintf(stderr,"Unknown unit %d, not converting.\n",unit);
- }
- return x;
}
double gmx2convert(double x,int unit)
{
- switch (unit)
- {
- case eg2cAngstrom:
- return x/A2NM;
- case eg2cNm:
- return x;
- case eg2cBohr:
- return x/BOHR2NM;
- case eg2cKcal_Mole:
- return x*CAL2JOULE;
- case eg2cHartree:
- return x/(ONE_4PI_EPS0/BOHR2NM);
- case eg2cHartree_e:
- return x/(ONE_4PI_EPS0/BOHR2NM);
- case eg2cAngstrom3:
- return x/(A2NM*A2NM*A2NM);
- case eg2cCoulomb:
- return x*E_CHARGE;
- case eg2cDebye:
- return x/DEBYE2ENM;
- case eg2cElectron:
+ switch (unit)
+ {
+ case eg2cAngstrom:
+ return x/A2NM;
+ case eg2cNm:
+ return x;
+ case eg2cBohr:
+ return x/BOHR2NM;
+ case eg2cKcal_Mole:
+ return x*CAL2JOULE;
+ case eg2cHartree:
+ return x/(ONE_4PI_EPS0/BOHR2NM);
+ case eg2cHartree_e:
+ return x/(ONE_4PI_EPS0/BOHR2NM);
+ case eg2cAngstrom3:
+ return x/(A2NM*A2NM*A2NM);
+ case eg2cCoulomb:
+ return x*E_CHARGE;
+ case eg2cDebye:
+ return x/DEBYE2ENM;
+ case eg2cElectron:
+ return x;
+ case eg2cBuckingham:
+ return x/(A2NM*DEBYE2ENM);
+ default:
+ fprintf(stderr,"Unknown unit %d, not converting.\n",unit);
+ }
return x;
- case eg2cBuckingham:
- return x/(A2NM*DEBYE2ENM);
- default:
- fprintf(stderr,"Unknown unit %d, not converting.\n",unit);
- }
- return x;
}
+/* This has to have the same order as the enums. */
+static const char *eg2c_names[eg2cNR] = {
+ "Angstrom", "Nm", "Bohr", "Kcal_Mole",
+ "Hartree", "Hartree_e", "Angstrom3", "Coulomb",
+ "Debye", "Electron", "Buckingham"
+};
+
+int string2unit(char *string)
+{
+ int i;
+
+ for(i=0; (i<eg2cNR); i++)
+ if (strcasecmp(string,eg2c_names[i]) == 0)
+ return i;
+ return -1;
+}
+
+const char *unit2string(int unit)
+{
+ if ((unit >= 0) && (unit < eg2cNR))
+ return eg2c_names[unit];
+
+ return NULL;
+}