source: src/Potentials/Particles/Particle.cpp@ d24750

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 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_ParseParticles 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_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra 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 d24750 was 35d171, checked in by Frederik Heber <heber@…>, 9 years ago

FIX: Particle lower-cases all but first letter of element name in input stream.

  • this allows to read TREMOLO particle entries.
  • Property mode set to 100644
File size: 6.8 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2013 Frederik Heber. All rights reserved.
5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24 * Particle.cpp
25 *
26 * Created on: May 13, 2013
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include "CodePatterns/MemDebug.hpp"
36
37#include "Particle.hpp"
38
39#include <boost/assign.hpp>
40#include <boost/lexical_cast.hpp>
41#include <boost/tokenizer.hpp>
42#include <iostream>
43#include <iterator>
44#include <string>
45
46#include "CodePatterns/Assert.hpp"
47
48#include "Element/element.hpp"
49#include "Element/periodentafel.hpp"
50#include "Potentials/Exceptions.hpp"
51#include "Potentials/Particles/ParticleRegistry.hpp"
52
53using namespace boost::assign;
54
55std::vector<std::string> getParameterNames()
56{
57 std::vector<std::string> tempnames;
58 tempnames += "particle_type", "element_name", "sigma", "epsilon", "sigma14", "epsilon14", "mass", "free", "charge";
59 return tempnames;
60}
61
62const std::vector<std::string> Particle::ParameterNames = getParameterNames();
63
64Particle::Particle(
65 const periodentafel &_periode,
66 const std::string &_token,
67 const atomicNumber_t &_number) :
68 name(_token),
69 periode(_periode),
70 charge(0.),
71 mass(0.),
72 dof(3),
73 atomic_number(_number),
74 sigma(0.),
75 epsilon(0.),
76 sigma_14(0.),
77 epsilon_14(0.)
78{
79 ParticleRegistry::getInstance().registerInstance(this);
80}
81
82std::string Particle::findFreeName(
83 const periodentafel &_periode,
84 const atomicNumber_t &_number)
85{
86 const element *e = _periode.FindElement(_number);
87 ASSERT (e != NULL,
88 "Particle::findFreeName() - cannot find element with number "
89 +toString(_number)+".");
90 bool FoundFlag = false;
91 size_t index = 1;
92 std::string returnname;
93 while(!FoundFlag) {
94 returnname = e->getSymbol()+toString(index++);
95 FoundFlag = !ParticleRegistry::getInstance().isPresentByName(returnname);
96 }
97 return returnname;
98}
99
100Particle::Particle(
101 const periodentafel &_periode,
102 const atomicNumber_t &_number) :
103 name(findFreeName(_periode, _number)),
104 periode(_periode),
105 charge(0.),
106 mass(0.),
107 dof(3),
108 atomic_number(_number),
109 sigma(0.),
110 epsilon(0.),
111 sigma_14(0.),
112 epsilon_14(0.)
113{
114 ParticleRegistry::getInstance().registerInstance(this);
115}
116
117Particle::Particle(const periodentafel &_periode) :
118 periode(_periode),
119 charge(0.),
120 mass(0.),
121 dof(3),
122 atomic_number(-1),
123 sigma(0.),
124 epsilon(0.),
125 sigma_14(0.),
126 epsilon_14(0.)
127{
128 // not registered as it does not have a name yet
129}
130
131void Particle::stream_to(std::ostream &ost) const
132{
133 // check stream
134 if (ost.bad())
135 throw SerializerStreamException();
136
137 /// print parameter key
138 ost << "\tparticle:";
139 /// print name and values
140 ost << "\tparticle_type=" << getName();
141 ost << ",\telement_name=" << getElement();
142 ost << ",\tsigma=" << sigma;
143 ost << ",\tepsilon=" << epsilon;
144 ost << ",\tsigma14=" << sigma_14;
145 ost << ",\tepsilon14=" << epsilon_14;
146 ost << ",\tmass=" << mass;
147 ost << ",\tfree=" << dof;
148 ost << ",\tcharge=" << std::setprecision(10) << charge;
149 ost << ";";
150}
151
152size_t Particle::lookupParameterName(const std::string &name) const
153{
154 std::vector<std::string>::const_iterator iter =
155 std::find(ParameterNames.begin(), ParameterNames.end(), name);
156 if (iter == ParameterNames.end())
157 return -1;
158 else
159 return std::distance(ParameterNames.begin(), iter);
160}
161
162void Particle::stream_from(std::istream &ist)
163{
164 // check stream
165 if (ist.bad())
166 throw SerializerStreamException();
167
168 // read in full line
169 std::string linestring;
170 getline(ist, linestring);
171 const std::string whitespace(" \t");
172 const size_t strBegin = linestring.find_first_not_of(whitespace);
173 const size_t colonpos = linestring.find(":");
174 if ((strBegin == std::string::npos) || (colonpos == std::string::npos) ||
175 (linestring.substr(strBegin, colonpos-strBegin) != std::string("particle")))
176 throw SerializerMissingValueException()
177 << SerializerKey(std::string("particle"));
178
179 // tokenize by ","
180 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
181 boost::char_separator<char> pairsep(",\t ;");
182 std::string remainderstring(linestring.substr(colonpos+1));
183 tokenizer tokens(remainderstring, pairsep); //skip colon
184
185 // step through each token
186 for (tokenizer::iterator tok_iter = tokens.begin();
187 tok_iter != tokens.end(); ++tok_iter) {
188 const std::string &keyvalue = *tok_iter;
189 const size_t equalitypos = keyvalue.find("=");
190 const std::string key = keyvalue.substr(0,equalitypos);
191 std::string value = keyvalue.substr(equalitypos+1);
192
193 const size_t index = lookupParameterName(key);
194
195 if (equalitypos == std::string::npos)
196 throw SerializerMissingValueException() << SerializerKey(key);
197
198 switch((parameters_t)index) {
199 case e_particle_type:
200 const_cast<std::string &>(name) = value;
201 break;
202 case e_element_name:
203 std::transform(value.begin()+1, value.end(), value.begin()+1, ::tolower);
204 setElement(value);
205 break;
206 case e_sigma:
207 sigma = boost::lexical_cast<double>(value);
208 break;
209 case e_epsilon:
210 epsilon = boost::lexical_cast<double>(value);
211 break;
212 case e_sigma_14:
213 sigma_14 = boost::lexical_cast<double>(value);
214 break;
215 case e_epsilon_14:
216 epsilon_14 = boost::lexical_cast<double>(value);
217 break;
218 case e_mass:
219 mass = boost::lexical_cast<double>(value);
220 break;
221 case e_free:
222 dof = boost::lexical_cast<unsigned int>(value);
223 break;
224 case e_charge:
225 charge = boost::lexical_cast<double>(value);
226 break;
227 default:
228 throw SerializerMissingValueException() << SerializerKey(key);
229 break;
230 }
231 }
232}
233
234std::ostream& operator<<(std::ostream &ost, const Particle &p)
235{
236 p.stream_to(ost);
237 return ost;
238}
239
240std::string Particle::getElement() const
241{
242 std::string returnstring;
243 const element *e = periode.FindElement(atomic_number);
244 if (e != NULL)
245 returnstring = e->getSymbol();
246
247 return returnstring;
248}
249
250void Particle::setElement(const std::string &element_name)
251{
252 const element *e = periode.FindElement(element_name);
253 if (e != NULL)
254 atomic_number = e->getAtomicNumber();
255}
Note: See TracBrowser for help on using the repository browser.