Ignore:
Timestamp:
Jun 13, 2012, 5:38:39 PM (13 years ago)
Author:
Frederik Heber <heber@…>
Branches:
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
Children:
bf72ec
Parents:
7b1a19
git-author:
Frederik Heber <heber@…> (05/31/12 13:24:04)
git-committer:
Frederik Heber <heber@…> (06/13/12 17:38:39)
Message:

Implemented all stream operator >> functions in StreamOperators.hpp.

  • also added operators for const atom* and vector<const atom *>.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Parameters/StreamOperators.hpp

    r7b1a19 r16876c  
    33 *
    44 *  Created on: Apr 16, 2012
    5  *      Author: ankele
     5 *      Author: ankele, heber
    66 */
    77
    88#ifndef STREAMOPERATORS_HPP_
    99#define STREAMOPERATORS_HPP_
    10 
    11 
    12 
    1310
    1411// include config.h
     
    1714#endif
    1815
     16#include <boost/tokenizer.hpp>
    1917#include <string>
     18#include <sstream>
    2019#include <iosfwd>
    2120
     
    2322#include "LinearAlgebra/RealSpaceMatrix.hpp"
    2423#include "Box.hpp"
    25 #include "Element/element.hpp"
    26 #include "molecule.hpp"
     24#include "Descriptors/AtomIdDescriptor.hpp"
     25#include "Descriptors/MoleculeIdDescriptor.hpp"
     26#include "Element/periodentafel.hpp"
     27#include "World.hpp"
    2728
    2829#include "CodePatterns/Assert.hpp"
    2930
    30 class Vector;
    31 class Box;
    32 class RealSpaceMatrix;
     31class atom;
     32class element;
    3333class molecule;
    34 class element;
    35 
    36 inline std::istream& operator>>(std::istream& ist, Vector& m)
    37 {
    38 
     34
     35/** Returns a single line from \a ist without leading and ending brackets.
     36 *
     37 * @param ist stream to get line from
     38 * @return line as string without ()
     39 */
     40inline std::string getLine(std::istream& ist)
     41{
    3942  std::string line;
    4043  getline(ist,line);
     
    4447  if (!line.empty() && line[line.size() - 1] == ')')
    4548    line.erase(line.size() - 1, 1);
    46   Vector temp_vector;
    47 
    48   // dissect by ","
    49   double coord = 0.;
    50   int counter = 0;
    51   std::string::iterator olditer = line.begin();
    52   for(std::string::iterator iter = line.begin(); (iter != line.end()) && (counter != 3); ++iter) {
    53     if ((*iter == ',') || (*iter == ' ')) {
    54       std::istringstream stream(std::string(olditer, iter));
    55 
    56       stream >> coord;
    57 
    58       temp_vector[counter++] = coord;
    59 
    60       olditer = iter + 1;
    61     }
    62   }
    63   if ((olditer != line.begin()) && (counter != 3)) { // insert last part also
    64 
    65     std::istringstream stream(std::string(olditer, line.end()));
    66     stream >> coord;
    67     temp_vector[counter++] = coord;
    68   }
    69   m = temp_vector;
     49
     50  return line;
     51}
     52
     53inline std::istream& operator>>(std::istream& ist, Vector& m)
     54{
     55  std::string line = getLine(ist);
     56
     57  // dissect by " " or ","
     58  ConvertTo<double> converter;
     59  std::vector<double> temp_vector;
     60  boost::char_separator<char> sep(", ");
     61  boost::tokenizer<boost::char_separator<char> > tok(line, sep);
     62  for(boost::tokenizer<boost::char_separator<char> >::const_iterator iter = tok.begin();
     63      iter != tok.end(); ++iter)
     64    temp_vector.push_back(converter(*iter));
     65  ASSERT (temp_vector.size() == (size_t)NDIM,
     66      "operator>>(std::istream& ist, Vector& m) - not "+toString((size_t)NDIM)+" but "
     67      +toString(temp_vector.size())+" components given.");
     68  for (size_t i=0;i<NDIM;++i)
     69    m[i] = temp_vector[i];
    7070  return ist;
    7171};
     
    7373inline std::istream& operator>>(std::istream& ist, RealSpaceMatrix& m)
    7474{
    75 
    76   std::string line;
    77   getline(ist,line);
    78 
    79   if (!line.empty() && line[0] == '(')
    80     line.erase(0, 1);
    81   if (!line.empty() && line[line.size() - 1] == ')')
    82     line.erase(line.size() - 1, 1);
    83   double temp[6];
    84 
    85   // dissect by ","
    86   double coord = 0.;
    87   int counter = 0;
    88   std::string::iterator olditer = line.begin();
    89   for(std::string::iterator iter = line.begin(); (iter != line.end()) && (counter != 6); ++iter) {
    90     if ((*iter == ',') || (*iter == ' ')) {
    91       std::istringstream stream(std::string(olditer, iter));
    92 
    93       stream >> coord;
    94 
    95       temp[counter++] = coord;
    96 
    97       olditer = iter + 1;
    98     }
    99   }
    100   if ((olditer != line.begin()) && (counter != 6)) { // insert last part also
    101 
    102     std::istringstream stream(std::string(olditer, line.end()));
    103     stream >> coord;
    104     temp[counter++] = coord;
    105   }
    106   m.set(0,0, temp[0]);
    107   m.set(0,1, temp[1]);
    108   m.set(0,2, temp[2]);
    109   m.set(1,0, temp[1]);
    110   m.set(1,1, temp[3]);
    111   m.set(1,2, temp[4]);
    112   m.set(2,0, temp[2]);
    113   m.set(2,1, temp[4]);
    114   m.set(2,2, temp[5]);
     75  std::string line = getLine(ist);
     76
     77  // dissect by " " or ","
     78  ConvertTo<double> converter;
     79  std::vector<double> temp_vector;
     80  boost::char_separator<char> sep(", ");
     81  boost::tokenizer<boost::char_separator<char> > tok(line, sep);
     82  for(boost::tokenizer<boost::char_separator<char> >::const_iterator iter = tok.begin();
     83      iter != tok.end(); ++iter)
     84    temp_vector.push_back(converter(*iter));
     85  ASSERT (temp_vector.size() == (size_t)NDIM*(NDIM+1)/2,
     86      "operator>>(std::istream& ist, RealSpaceMatrix& m) - not "+toString((size_t)NDIM*((size_t)NDIM+1)/2)+" but "
     87      +toString(temp_vector.size())+" components given.");
     88  m.set(0,0, temp_vector[0]);
     89  m.set(0,1, temp_vector[1]);
     90  m.set(0,2, temp_vector[2]);
     91  m.set(1,0, temp_vector[1]);
     92  m.set(1,1, temp_vector[3]);
     93  m.set(1,2, temp_vector[4]);
     94  m.set(2,0, temp_vector[2]);
     95  m.set(2,1, temp_vector[4]);
     96  m.set(2,2, temp_vector[5]);
    11597  return ist;
    11698};
     
    125107
    126108
     109inline std::istream& operator>>(std::istream& ist, const atom* & m)
     110{
     111  std::string line = getLine(ist);
     112
     113  ConvertTo<unsigned int> converter;
     114  const atomId_t temp_value = converter(line);
     115  const atom *a = World::getInstance().getAtom(AtomById(temp_value));
     116  ASSERT( a != NULL,
     117      "operator>>(std::istream& ist, const atom* & m) - atom molecule id "
     118      +toString(temp_value)+".");
     119  m = a;
     120  return ist;
     121}
     122
    127123inline std::istream& operator>>(std::istream& ist, const molecule* & m)
    128124{
    129   std::cout << "===============   operator >> (istream, const element *) not implemented!\n";
    130   ASSERT(0, "operator >> (istream, const element *) not implemented!");
     125  std::string line = getLine(ist);
     126
     127  ConvertTo<unsigned int> converter;
     128  const moleculeId_t temp_value = converter(line);
     129  const molecule *mol = World::getInstance().getMolecule(MoleculeById(temp_value));
     130  ASSERT( mol != NULL,
     131      "operator>>(std::istream& ist, const molecule* & m) - invalid molecule id "
     132      +toString(temp_value)+".");
     133  m = mol;
    131134  return ist;
    132135}
     
    135138inline std::istream& operator>>(std::istream& ist, const element* & m)
    136139{
    137   std::cout << "===============   operator >> (istream, const element *) not implemented!\n";
    138   ASSERT(0, "operator >> (istream, const element *) not implemented!");
     140  std::string line = getLine(ist);
     141
     142  ConvertTo<unsigned int> converter;
     143  const atomicNumber_t temp_value = converter(line);
     144  const element *elem = World::getInstance().getPeriode()->FindElement(temp_value);
     145  ASSERT( elem != NULL,
     146      "operator>>(std::istream& ist, const element* & m) - invalid atomic number "
     147      +toString(temp_value)+".");
     148  m = elem;
    139149  return ist;
    140150}
     
    143153inline std::istream& operator>>(std::istream& ist, std::vector<const molecule*> & m)
    144154{
    145   std::cout << "===============   operator >> (istream, std::vector<const element *>) not implemented!\n";
    146   ASSERT(0, "operator >> (istream, std::vector<const element *>) not implemented!");
    147   return ist;
    148 }
    149 
     155  std::string line = getLine(ist);
     156
     157  // dissect by " " or ","
     158  boost::char_separator<char> sep(", ");
     159  boost::tokenizer<boost::char_separator<char> > tok(line, sep);
     160  ConvertTo<unsigned int> converter;
     161  for(boost::tokenizer<boost::char_separator<char> >::const_iterator iter = tok.begin();
     162      iter != tok.end(); ++iter) {
     163    const moleculeId_t temp_value = converter(*iter);
     164    const molecule *mol = World::getInstance().getMolecule(MoleculeById(temp_value));
     165    ASSERT( mol != NULL,
     166        "operator>>(std::istream& ist, std::vector<const molecule*> & m) - invalid molecule id "
     167        +toString(temp_value)+".");
     168    m.push_back(mol);
     169  }
     170  return ist;
     171}
     172
     173
     174inline std::istream& operator>>(std::istream& ist, std::vector<const atom*> & m)
     175{
     176  std::string line = getLine(ist);
     177
     178  // dissect by " " or ","
     179  boost::char_separator<char> sep(", ");
     180  boost::tokenizer<boost::char_separator<char> > tok(line, sep);
     181  ConvertTo<unsigned int> converter;
     182  for(boost::tokenizer<boost::char_separator<char> >::const_iterator iter = tok.begin();
     183      iter != tok.end(); ++iter) {
     184    const atomId_t temp_value = converter(line);
     185    const atom *a = World::getInstance().getAtom(AtomById(temp_value));
     186    ASSERT( a != NULL,
     187        "operator>>(std::istream& ist, std::vector<const atom*> & m) - invalid atom id "
     188        +toString(temp_value)+".");
     189    m.push_back(a);
     190  }
     191  return ist;
     192}
    150193
    151194inline std::istream& operator>>(std::istream& ist, std::vector<const element*> & m)
    152195{
    153   std::cout << "===============   operator >> (istream, std::vector<const element *>) not implemented!\n";
    154   ASSERT(0, "operator >> (istream, std::vector<const element *>) not implemented!");
     196  std::string line = getLine(ist);
     197
     198  // dissect by " " or ","
     199  boost::char_separator<char> sep(", ");
     200  boost::tokenizer<boost::char_separator<char> > tok(line, sep);
     201  ConvertTo<unsigned int> converter;
     202  for(boost::tokenizer<boost::char_separator<char> >::const_iterator iter = tok.begin();
     203      iter != tok.end(); ++iter) {
     204    const atomicNumber_t temp_value = converter(*iter);
     205    const element *elem = World::getInstance().getPeriode()->FindElement(temp_value);
     206    ASSERT( elem != NULL,
     207        "operator>>(std::istream& ist, std::vector<const element*> & m) - invalid atomic number "
     208        +toString(temp_value)+".");
     209    m.push_back(elem);
     210  }
    155211  return ist;
    156212}
     
    159215inline std::istream& operator>>(std::istream& ist, std::vector<std::string> & m)
    160216{
    161   std::cout << "===============   operator >> (istream, std::vector<std::string>) not implemented!\n";
    162   ASSERT(0, "operator >> (istream, std::vector<std::string>) not implemented!");
     217  std::string line = getLine(ist);
     218
     219  // dissect by " " or ","
     220  boost::char_separator<char> sep(", ");
     221  boost::tokenizer<boost::char_separator<char> > tok(line, sep);
     222  for(boost::tokenizer<boost::char_separator<char> >::const_iterator iter = tok.begin();
     223      iter != tok.end(); ++iter)
     224    m.push_back(*iter);
     225  return ist;
     226}
     227
     228inline std::istream& operator>>(std::istream& ist, std::vector<unsigned int> & m)
     229{
     230  std::string line = getLine(ist);
     231
     232  // dissect by " " or ","
     233  boost::char_separator<char> sep(", ");
     234  boost::tokenizer<boost::char_separator<char> > tok(line, sep);
     235  ConvertTo<unsigned int> converter;
     236  for(boost::tokenizer<boost::char_separator<char> >::const_iterator iter = tok.begin();
     237      iter != tok.end(); ++iter)
     238    m.push_back(converter(*iter));
    163239  return ist;
    164240}
Note: See TracChangeset for help on using the changeset viewer.