source: ThirdParty/mpqc_open/src/lib/util/misc/units.cc@ 860145

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 860145 was 860145, checked in by Frederik Heber <heber@…>, 8 years ago

Merge commit '0b990dfaa8c6007a996d030163a25f7f5fc8a7e7' as 'ThirdParty/mpqc_open'

  • Property mode set to 100644
File size: 5.9 KB
Line 
1//
2// units.cc
3//
4// Copyright (C) 1997 Limit Point Systems, Inc.
5//
6// Author: Curtis Janssen <cljanss@limitpt.com>
7// Maintainer: LPS
8//
9// This file is part of the SC Toolkit.
10//
11// The SC Toolkit is free software; you can redistribute it and/or modify
12// it under the terms of the GNU Library General Public License as published by
13// the Free Software Foundation; either version 2, or (at your option)
14// any later version.
15//
16// The SC Toolkit is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Library General Public License for more details.
20//
21// You should have received a copy of the GNU Library General Public License
22// along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24//
25// The U.S. Government is granted a limited license as per AL 91-7.
26//
27
28#ifdef __GNUG__
29#pragma implementation
30#endif
31
32#include <util/misc/math.h>
33#include <string.h>
34#include <util/misc/units.h>
35#include <util/state/stateio.h>
36
37#include <util/state/linkage.h>
38
39using namespace std;
40using namespace sc;
41
42namespace sc {
43
44//////////////////////////////////////////////////////////////////////
45// Utility functions
46
47static inline int eq(const char* a, const char* b)
48{
49 return !strcmp(a,b);
50}
51
52//////////////////////////////////////////////////////////////////////
53// Units class definition
54
55static ClassDesc Units_cd(typeid(Units),"Units",1,"public SavableState",
56 0, 0, create<Units>);
57
58Units::Units(const char *strrep)
59{
60 if (strrep) strrep_ = strcpy(new char[strlen(strrep)+1], strrep);
61 else strrep_ = 0;
62 parse_unit();
63}
64
65Units::Units(char *strrep, Units::Storage action)
66{
67 if (action == Copy) {
68 if (strrep) strrep_ = strcpy(new char[strlen(strrep)+1], strrep);
69 else strrep_ = 0;
70 }
71 else {
72 strrep_ = strrep;
73 }
74 parse_unit();
75}
76
77Units::Units(StateIn&s):
78 SavableState(s)
79{
80 s.getstring(strrep_);
81 parse_unit();
82}
83
84Units::~Units()
85{
86 delete[] strrep_;
87}
88
89void
90Units::save_data_state(StateOut&s)
91{
92 s.putstring(strrep_);
93}
94
95double
96Units::to(const Ref<Units> &units) const
97{
98 return to_atomic_units_/units->to_atomic_units_;
99}
100
101double
102Units::from(const Ref<Units> &units) const
103{
104 return 1.0/to(units);
105}
106
107double
108Units::to_atomic_units() const
109{
110 return to_atomic_units_;
111}
112
113double
114Units::from_atomic_units() const
115{
116 return 1.0/to_atomic_units_;
117}
118
119const char *
120Units::string_rep() const
121{
122 return strrep_;
123}
124
125void
126Units::parse_unit()
127{
128 to_atomic_units_ = 1.0;
129
130 int invert = 0;
131 const char *rest = strrep_;
132
133 while (rest) {
134 const char *end = ::strpbrk(rest, " */");
135 int nchar;
136 if (end) {
137 nchar = end - rest;
138 }
139 else {
140 nchar = strlen(rest);
141 }
142 char *unitstring = new char[nchar+1];
143 memcpy(unitstring,rest,nchar);
144 unitstring[nchar] = '\0';
145
146 // physical constants used for atomic unit conversion factors
147 // from CRC Handbook 77th Ed. Tables 1&2 (CODATA 1986)
148 const double a0 = 5.29177249e-11; // m
149 //const double hbar = 1.05457266e-34; // J s
150 const double e = 1.60217733e-19; // C
151 const double me = 9.1093897e-31; // kg
152 const double e0 = 8.854187817e-12; // F/m
153 const double NA = 6.0221367e23; // mol-1 (from CRC Handbook)
154
155 // derived au conversion factors
156 const double Ea = e*e/((4.0*M_PI*e0)*a0); // J
157 //const double time = hbar/Ea; // s
158
159 // other conversions
160 const double amu = 1.6605655e-27; // kg
161 const double cal = 4.184; // J
162
163 double factor = 1.0;
164 if (eq(unitstring, "bohr")
165 ||eq(unitstring, "bohrs")) {
166 }
167 else if (eq(unitstring, "hartree")
168 ||eq(unitstring, "hartrees")
169 ||eq(unitstring, "Hartree")
170 ||eq(unitstring, "Hartrees")) {
171 }
172 else if (eq(unitstring, "ev")
173 ||eq(unitstring, "eV")) {
174 factor = 1.0/27.2113834; // physics.nist.gov/constants
175 }
176 else if (eq(unitstring, "debye")) {
177 factor = 1.0/2.541765; // several WWW sources
178 }
179 else if (eq(unitstring, "radian")
180 ||eq(unitstring, "radians")) {
181 }
182 else if (eq(unitstring, "mol")
183 ||eq(unitstring, "mole")) {
184 factor = NA;
185 }
186 else if (eq(unitstring, "kcal")) {
187 factor = 1000.0*cal/Ea;
188 }
189 else if (eq(unitstring, "kcal_per_mol")) {
190 factor = 1000.0*cal/(Ea*NA);
191 }
192 else if (eq(unitstring, "N")
193 ||eq(unitstring, "newton")) {
194 factor = a0/Ea;
195 }
196 else if (eq(unitstring, "dyne")) {
197 factor = 1.0e-5*a0/Ea;
198 }
199 else if (eq(unitstring, "m")
200 ||eq(unitstring, "meter")) {
201 factor = 1.0/a0;
202 }
203 else if (eq(unitstring, "cm")
204 ||eq(unitstring, "centimeter")) {
205 factor = 1.0e-2/a0;
206 }
207 else if (eq(unitstring, "angstrom")
208 ||eq(unitstring, "angstroms")
209 ||eq(unitstring, "aangstrom")
210 ||eq(unitstring, "aangstroms")) {
211 factor = 1.0e-10/a0;
212 }
213 else if (eq(unitstring, "amu")) {
214 factor = amu/me;
215 }
216 else if (eq(unitstring, "degree")
217 ||eq(unitstring, "degrees")) {
218 factor = M_PI/180.0;
219 }
220 else {
221 ExEnv::errn() << "Units: Cannot handle \"" << unitstring << "\"" << endl;
222 abort();
223 }
224 delete[] unitstring;
225 if (invert) factor = 1.0/factor;
226 to_atomic_units_ *= factor;
227 rest = ::strpbrk(rest, " */");
228 while (rest && (*rest == ' ' || *rest == '*' || *rest == '/')) {
229 if (*rest == '/') invert = !invert;
230 rest++;
231 }
232 }
233}
234
235/////////////////////////////////////////////////////////////////////////////
236
237}
238
239// Local Variables:
240// mode: c++
241// c-file-style: "CLJ"
242// End:
Note: See TracBrowser for help on using the repository browser.