Code beautification with uncrustify
[alexxy/gromacs.git] / src / gromacs / gmxlib / physics.c
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  * $Id: molprop_util.c,v 1.51 2009/06/01 06:13:18 spoel Exp $
3  *
4  *                This source code is part of
5  *
6  *                 G   R   O   M   A   C   S
7  *
8  *          GROningen MAchine for Chemical Simulations
9  *
10  *                        VERSION 4.0.99
11  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13  * Copyright (c) 2001-2008, The GROMACS development team,
14  * check out http://www.gromacs.org for more information.
15
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  *
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  *
31  * For more info, check our website at http://www.gromacs.org
32  *
33  * And Hey:
34  * Groningen Machine for Chemical Simulation
35  */
36 #include <stdio.h>
37 #include "string2.h"
38 #include "physics.h"
39
40 double convert2gmx(double x, int unit)
41 {
42     switch (unit)
43     {
44         case eg2cAngstrom:
45             return x*A2NM;
46         case eg2cNm:
47             return x;
48         case eg2cBohr:
49             return x*BOHR2NM;
50         case eg2cKcal_Mole:
51             return x/CAL2JOULE;
52         case eg2cHartree:
53             return x*ONE_4PI_EPS0/BOHR2NM;
54         case eg2cHartree_e:
55             return x*ONE_4PI_EPS0/BOHR2NM;
56         case eg2cAngstrom3:
57             return x*A2NM*A2NM*A2NM;
58         case eg2cCoulomb:
59             return x/E_CHARGE;
60         case eg2cDebye:
61             return x*DEBYE2ENM;
62         case eg2cElectron:
63             return x;
64         case eg2cBuckingham:
65             return x*A2NM*DEBYE2ENM;
66         default:
67             fprintf(stderr, "Unknown unit %d, not converting.\n", unit);
68     }
69     return x;
70 }
71
72 double gmx2convert(double x, int unit)
73 {
74     switch (unit)
75     {
76         case eg2cAngstrom:
77             return x/A2NM;
78         case eg2cNm:
79             return x;
80         case eg2cBohr:
81             return x/BOHR2NM;
82         case eg2cKcal_Mole:
83             return x*CAL2JOULE;
84         case eg2cHartree:
85             return x/(ONE_4PI_EPS0/BOHR2NM);
86         case eg2cHartree_e:
87             return x/(ONE_4PI_EPS0/BOHR2NM);
88         case eg2cAngstrom3:
89             return x/(A2NM*A2NM*A2NM);
90         case eg2cCoulomb:
91             return x*E_CHARGE;
92         case eg2cDebye:
93             return x/DEBYE2ENM;
94         case eg2cElectron:
95             return x;
96         case eg2cBuckingham:
97             return x/(A2NM*DEBYE2ENM);
98         default:
99             fprintf(stderr, "Unknown unit %d, not converting.\n", unit);
100     }
101     return x;
102 }
103
104 /* This has to have the same order as the enums. */
105 static const char *eg2c_names[eg2cNR] = {
106     "Angstrom", "Nm", "Bohr", "Kcal_Mole",
107     "Hartree", "Hartree_e", "Angstrom3", "Coulomb",
108     "Debye", "Electron", "Buckingham"
109 };
110
111 int string2unit(char *string)
112 {
113     int i;
114
115     for (i = 0; (i < eg2cNR); i++)
116     {
117         if (gmx_strcasecmp(string, eg2c_names[i]) == 0)
118         {
119             return i;
120         }
121     }
122     return -1;
123 }
124
125 const char *unit2string(int unit)
126 {
127     if ((unit >= 0) && (unit < eg2cNR))
128     {
129         return eg2c_names[unit];
130     }
131
132     return NULL;
133 }