source: src/Parser/FormatParser_Parameters.cpp@ 50e4e5

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 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 50e4e5 was 41d023, checked in by Frederik Heber <heber@…>, 13 years ago

Renamed SetMpqcParametersAction to SetParserParametersAction and generalized.

  • now we may set the parameters of basically any parser, so far it's probably just mpqc and pis3.
  • changed operator<<(FormatParser_Parameters): no output when value is empty string.
  • changed operator>>(FormatParser_Parameters): Asserts were not really making sure that value_iter and param_iter were readible.
  • Added regression test on setting psi3 parameters (wfn).
  • Property mode set to 100644
File size: 6.4 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * FormatParser_Parameters.cpp
10 *
11 * Created on: Sep 30, 2011
12 * Author: heber
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "CodePatterns/MemDebug.hpp"
21
22#include <iostream>
23#include <boost/algorithm/string.hpp>
24#include <boost/tokenizer.hpp>
25#include <sstream>
26#include <string>
27
28#include "CodePatterns/Assert.hpp"
29#include "CodePatterns/Log.hpp"
30
31#include "Parameters/Parameter.hpp"
32#include "FormatParser_Parameters.hpp"
33
34/** Constructor of class FormatParser_Parameters.
35 *
36 * \note we make sure that storage is always present
37 *
38 */
39FormatParser_Parameters::FormatParser_Parameters() :
40 storage(new ParameterStorage)
41{}
42
43/** Copy constructor of class FormatParser_Parameters.
44 *
45 * @param _parameters instance to copy from
46 */
47FormatParser_Parameters::FormatParser_Parameters(const FormatParser_Parameters &_parameters) :
48 storage(new ParameterStorage(*_parameters.storage))
49{}
50
51
52/** Destructor of class FormatParser_Parameters.
53 *
54 */
55FormatParser_Parameters::~FormatParser_Parameters()
56{
57 delete storage;
58}
59
60/** Implements the Parameter::clone() function.
61 *
62 * @return another instance with an identical copy of each parameter in \a storage
63 */
64FormatParser_Parameters* FormatParser_Parameters::clone() const
65{
66 return (new FormatParser_Parameters(*this));
67}
68
69/** This makes this instance a clone of \a _instance.
70 *
71 * \note This is basically the other way round to clone().
72 *
73 * @param _instance instance to clone from
74 */
75void FormatParser_Parameters::makeClone(const FormatParser_Parameters &_instance)
76{
77 // we simply remove storage
78 delete storage;
79 // and clone the one from _instance
80 storage = new ParameterStorage(*_instance.storage);
81}
82
83/** Adds a parameter to \a storage.
84 *
85 * This just eases use, saves some typing and is more clear as to what is done.
86 *
87 * @param instance parameter to add
88 */
89void FormatParser_Parameters::appendParameter(Parameter *instance)
90{
91 storage->registerInstance(instance);
92}
93
94/** Checks for presence of a parameter in \a storage.
95 *
96 * This just eases use, saves some typing and is more clear as to what is done.
97 *
98 * @return true - parameter by this \a _name is present in storage, false - else
99 */
100bool FormatParser_Parameters::haveParameter(const std::string &_name) const
101{
102 return storage->isPresentByName(_name);
103}
104
105/** Gets a parameter from \a storage.
106 *
107 * This just eases use, saves some typing and is more clear as to what is done.
108 *
109 * @return pointer to instance with this \a _name
110 */
111Parameter *FormatParser_Parameters::getParameter(const std::string &_name) const
112{
113 return storage->getByName(_name);
114}
115
116/** Output operator for the contents of FormatParser_Parameters::params.
117 *
118 * @param ost output stream
119 * @param params reference to FormatParser_Parameters containing params.
120 * @return reference to output stream for concatenation
121 */
122std::ostream & operator << (std::ostream& ost, const FormatParser_Parameters &params)
123{
124 // this is ugly, but with boost::any to safeguard const-ness is plain impossible
125 std::ostringstream output;
126 ASSERT(params.storage != NULL,
127 "operator<<(FormatParser_Parameters) - storage is NULL.");
128 for (ParameterStorage::const_iterator iter = params.storage->getBeginIter();
129 iter != params.storage->getEndIter();
130 ++iter)
131 if (!iter->second->get().empty())
132 output << iter->first << "=" << iter->second->get() << ";";
133 ost << output.str();
134 return ost;
135}
136
137/** Input operator for a list of parameters to place into \a params.
138 *
139 * @param ist input stream
140 * @param params parameters to parse into
141 * @return input stream for concatenation
142 */
143std::istream & operator >> (std::istream& ist, FormatParser_Parameters &params)
144{
145 typedef boost::tokenizer<boost::char_separator<char> >
146 tokenizer;
147 boost::char_separator<char> semicolonsep(";");
148 boost::char_separator<char> equalitysep("=");
149 boost::char_separator<char> ticksep("\"");
150 std::string line;
151 std::getline( ist, line );
152 //DoLog(0) && (Log() << Verbose(0) << "INFO: full line of parameters is '" << line << "'" << std::endl);
153 tokenizer tokens(line, semicolonsep);
154 ASSERT(tokens.begin() != tokens.end(),
155 "operator<< on FormatParser_Parameters - empty string, need at least ';' in line "+line+"!");
156 for (tokenizer::iterator tok_iter = tokens.begin();
157 tok_iter != tokens.end(); ++tok_iter) {
158 //LOG(2, "INFO: (key,value) pair is: " << *tok_iter << ".");
159 tokenizer paramtokens(*tok_iter, equalitysep);
160 if (paramtokens.begin() != paramtokens.end()) {
161 tokenizer::iterator tok_paramiter = paramtokens.begin();
162 ASSERT(tok_paramiter != paramtokens.end(),
163 "operator<< on FormatParser_Parameters - missing value before ' =' in token "+*tok_iter+"!");
164 tokenizer::iterator tok_valueiter = tok_paramiter;
165 tokenizer::iterator tok_checkiter = ++tok_valueiter;
166 ASSERT(tok_valueiter != paramtokens.end(),
167 "operator<< on FormatParser_Parameters - missing value after ' =' in token "+*tok_iter+"!");
168 ++tok_checkiter;
169 ASSERT(tok_checkiter == paramtokens.end(),
170 "operator<< on FormatParser_Parameters - still more tokens after ' =' in token "+*tok_iter+":"
171 +*tok_checkiter+"!");
172 LOG(3, "INFO: key is '" << *tok_paramiter << "', value is '" << *tok_valueiter << "'.");
173 // TODO: throw exception instead of ASSERT
174 std::string key(*tok_paramiter);
175 std::string value(*tok_valueiter);
176 boost::trim(key);
177 boost::trim(value);
178 tokenizer ticklesstokens(value, ticksep);
179 ASSERT(ticklesstokens.begin() != ticklesstokens.end(),
180 "operator<< on FormatParser_Parameters - no tokens present after removing ticks in token "+*tok_valueiter+"!");
181 std::stringstream valuestream(*(ticklesstokens.begin()));
182 //LOG(2, "INFO: Token pair is " << key << "," << valuestream.str());
183
184 // TODO: throw exception instead of DoeLog()
185 ASSERT(params.haveParameter(key),
186 "operator >> on FormatParser_Parameters - unknown parameter name '"
187 +key+"' with value "+valuestream.str()+"!");
188 if (params.haveParameter(key)) {
189 Parameter *instance = params.getParameter(key);
190 instance->set(valuestream.str());
191 }
192 } else {
193 ist.setstate(std::ios::eofbit);
194 }
195 }
196 return ist;
197}
Note: See TracBrowser for help on using the repository browser.