Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ElementSet.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file core/chemical/ElementSet.cc
11 /// @brief
12 /// @author P. Douglas Renfrew (renfrew@unc.edu)
13 
14 // Unit headers
16 #include <core/chemical/Element.hh>
17 
18 // Project headers
19 #include <basic/Tracer.hh>
20 #include <utility/string_util.hh>
21 #include <utility/io/izstream.hh>
22 
23 // C++ headers
24 #include <fstream>
25 #include <iostream>
26 
27 #include <utility/vector1.hh>
28 
29 
30 namespace core {
31 namespace chemical {
32 
33 static basic::Tracer tr("core.chemical");
34 
36  element_index_(),
37  elements_()
38 {
39 }
40 
42 
43 /// @details Initialize an ElementSet from an external file "filename",
44 /// and set parameters and properties for each Element.
45 /// Refer to minirosetta_database_stock/chemical/element_properties.txt
46 /// for file format
47 ///
48 void
50 {
51  utility::io::izstream data( filename.c_str() );
52 
53  if ( !data.good() ) utility_exit_with_message( "Unable to open element file: "+filename );
54 
55  // now parse the rest of the file
56  {
57  using namespace basic;
58 
59  std::string line;
60  // parse the header line
61  getline( data, line ); // throw out the header line (currently it is just for file readability)
62  while ( getline( data,line ) ) {
63  utility::trim(line, " \t\n"); // remove leading and trailing spaces
64  if ( line.empty() > 0 ) continue; //skip blank lines
65  if ( line.find("#",0) == 0 ) continue; // skip comment lines
66 
67  std::istringstream l( line );
68  std::string symbol, name;
69  Real weight;
70  Size z, mass;
71 
72  l >> z;
73 
74  if ( l.fail() ) {
75  utility_exit_with_message("bad line: "+line);
76  }
77 
78  l >> symbol >> name >> weight >> mass;
79 
80  if ( l.fail() ) {
81  utility_exit_with_message("bad line: "+line);
82  }
83 
84  Element* e( new Element( z, symbol, name, weight, mass) );
85 
86  // add this to the list
87  elements_.push_back( e );
88  if ( element_index_.count( symbol ) ) {
89  utility_exit_with_message("ElementSet:: duplicate element symbol "+symbol);
90  }
91  element_index_[ symbol ] = elements_.size();
92  tr.Debug << "New element: " << symbol << std::endl;
93  }
94  }
95 }
96 
97 /// @details This function iterates over each element in the element_index_ map and
98 /// prints both keys. It is only used for debugging.
99 void
101 {
102  for( std::map< std::string, int >::const_iterator i = element_index_.begin(), e = element_index_.end(); i != e; ++i )
103  {
104  std::cout << (*i).first << " " << (*i).second << std::endl;
105  }
106 }
107 
108 } // chemical
109 } // core