source: src/Potentials/SerializablePotential.cpp@ acc9b1

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 Candidate_v1.7.0 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing 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_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since acc9b1 was acc9b1, checked in by Frederik Heber <heber@…>, 12 years ago

FIX: Added new copyright lines to files in src/Potentials, too.

  • Property mode set to 100644
File size: 6.2 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2012 University of Bonn. All rights reserved.
5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
6 * Please see the COPYING file or "Copyright notice" in builder.cpp for details.
7 *
8 *
9 * This file is part of MoleCuilder.
10 *
11 * MoleCuilder is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * MoleCuilder 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 General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25/*
26 * SerializablePotential.cpp
27 *
28 * Created on: 23.11.2012
29 * Author: heber
30 */
31
32// include config.h
33#ifdef HAVE_CONFIG_H
34#include <config.h>
35#endif
36
37#include "CodePatterns/MemDebug.hpp"
38
39#include "SerializablePotential.hpp"
40
41#include <algorithm>
42#include <boost/foreach.hpp>
43#include <boost/tokenizer.hpp>
44#include <iostream>
45#include <string>
46
47#include "CodePatterns/Assert.hpp"
48#include "CodePatterns/toString.hpp"
49
50#include "Potentials/Exceptions.hpp"
51
52std::ostream& operator<<(std::ostream &ost, const SerializablePotential &potential)
53{
54 potential.stream_to(ost);
55 return ost;
56}
57
58void SerializablePotential::stream_to(std::ostream &ost) const
59{
60 // check stream
61 if (ost.bad())
62 throw SerializablePotentialException();
63
64 /// print parameter key
65 ost << getToken() << ":";
66 /// print associated particles
67 const SerializablePotential::ParticleTypes_t &types = getParticleTypes();
68 for (size_t index=0; index < types.size(); ++index) {
69 ost << "\tparticle_type" << index+1 << "=" << types[index];
70 ost << (index != (types.size()-1) ? std::string(",") : std::string(""));
71 }
72 /// print coefficients
73 const SerializablePotential::ParameterNames_t &paramNames = getParameterNames();
74 const SerializablePotential::parameters_t &params = getParameters();
75 SerializablePotential::ParameterNames_t::const_iterator nameiter = paramNames.begin();
76 SerializablePotential::parameters_t::const_iterator valueiter = params.begin();
77 for (; valueiter != params.end(); ++valueiter, ++nameiter) {
78 ASSERT( nameiter != paramNames.end(),
79 "SerializablePotential::operator<<() - there are less names than parameters.");
80 if (*nameiter != std::string(""))
81 ost << ",\t" << *nameiter << "=" << *valueiter;
82 }
83 /// print terminating semi-colon
84 ost << ";";
85}
86
87std::istream& operator>>(std::istream &ist, SerializablePotential &potential)
88{
89 potential.stream_from(ist);
90 return ist;
91}
92
93void SerializablePotential::stream_from(std::istream &ist)
94{
95 // check stream
96 if (ist.bad())
97 throw SerializablePotentialException();
98
99 // create copy of current parameters, hence line may contain not all required
100 SerializablePotential::parameters_t params(getParameters());
101
102 // read in full line
103 std::string linestring;
104 getline(ist, linestring);
105 const std::string whitespace(" \t");
106 const size_t strBegin = linestring.find_first_not_of(whitespace);
107 const size_t colonpos = linestring.find(":");
108 if ((strBegin == std::string::npos) || (colonpos == std::string::npos) ||
109 (linestring.substr(strBegin, colonpos-1) != getToken()))
110 throw SerializablePotentialMissingValueException()
111 << SerializablePotentialKey(getName());
112
113 // tokenize by ","
114 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
115 boost::char_separator<char> pairsep(",\t ;");
116 std::string remainderstring(linestring.substr(colonpos+1));
117 tokenizer tokens(remainderstring, pairsep); //skip colon
118
119 // step through each token
120 ConvertTo<size_t> ConvertToIndex;
121 ConvertTo<SerializablePotential::parameter_t> ConvertToValue;
122 ConvertTo<SerializablePotential::ParticleType_t> ConvertToParticleType;
123 for (tokenizer::iterator tok_iter = tokens.begin();
124 tok_iter != tokens.end(); ++tok_iter) {
125 const std::string &keyvalue = *tok_iter;
126 const size_t equalitypos = keyvalue.find("=");
127 const std::string key = keyvalue.substr(0,equalitypos);
128 const std::string value = keyvalue.substr(equalitypos+1);
129
130 /// parse the particle_types
131 const std::string typetoken("particle_type");
132 const size_t pos = key.find(typetoken);
133 if (pos != std::string::npos) {
134 // split of type and convert rest to index
135 const size_t indexpos = pos+typetoken.length();
136 const std::string &indexstring = key.substr(indexpos);
137 const size_t index = ConvertToIndex(indexstring);
138 if(index == 0)
139 throw SerializablePotentialMissingValueException() << SerializablePotentialKey(key);
140 // and set the type
141 if (equalitypos == std::string::npos)
142 throw SerializablePotentialMissingValueException() << SerializablePotentialKey(key);
143 setParticleType(index-1, ConvertToParticleType(value));
144 } else {
145 const size_t index = getParameterIndex(key);
146 // parse the coefficients
147 if (index != (size_t)-1) {
148 if (equalitypos == std::string::npos)
149 throw SerializablePotentialMissingValueException() << SerializablePotentialKey(key);
150 params[index] = ConvertToValue(value);
151 } else {
152 throw SerializablePotentialIllegalKeyException() << SerializablePotentialKey(key);
153 }
154 }
155 }
156
157 /// set the new paremeters
158 setParameters(params);
159}
160
161const size_t SerializablePotential::getParameterIndex(const std::string &_name) const
162{
163 const ParameterNames_t& ParameterNames = getParameterNames();
164 ParameterNames_t::const_iterator iter =
165 std::find(ParameterNames.begin(), ParameterNames.end(), _name);
166 if (iter == ParameterNames.end())
167 return (size_t)-1;
168 else
169 return std::distance(ParameterNames.begin(), iter);
170}
171
172const std::string SerializablePotential::getName() const
173{
174 std::string returnstring = getToken() + std::string("_");
175 BOOST_FOREACH(const ParticleType_t &type, getParticleTypes()) {
176 returnstring += toString(type);
177 }
178 return returnstring;
179}
Note: See TracBrowser for help on using the repository browser.