Added routines to physics.c for converting a string to a unit entry and vice
authorDavid van der Spoel <spoel@xray.bmc.uu.se>
Mon, 16 Aug 2010 10:14:21 +0000 (12:14 +0200)
committerDavid van der Spoel <spoel@xray.bmc.uu.se>
Mon, 16 Aug 2010 10:14:21 +0000 (12:14 +0200)
versa.

include/physics.h
src/gmxlib/physics.c

index e7a0fc00cebfb34c23ff241a5477425002f1e04e..796bd30ca508b3bc448404c45b5d5ac6d0ddb9ed 100644 (file)
@@ -151,9 +151,15 @@ extern "C" {
      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
 }
index 4d7a298681c92c6b95e07709bec5e6a42763afea..6123adffb2c79aae92e0421de02820178884249b 100644 (file)
        
 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;
+}