Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
IdealBondLengthSet.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/IdealBondLengthSet.cc
11 /// @brief
12 /// @author Gordon Lemmon
13 
14 // Unit headers
18 
19 // Project headers
20 #include <basic/Tracer.hh>
21 #include <utility/string_util.hh>
22 #include <utility/io/izstream.hh>
23 
24 // C++ headers
25 #include <fstream>
26 #include <iostream>
27 
28 #include <utility/vector1.hh>
29 #include <utility/exit.hh>
30 
31 namespace core {
32 namespace chemical {
33 
34 static basic::Tracer tr("core.chemical");
35 
37  bond_lengths_()
38 {}
39 
41 
42 bool IdealBondLengthSet::contains_bond_length(std::string const & atom_type_name1, std::string const & atom_type_name2) const{
45 
46  AtomTypeIndex atom_type_index1 = atom_type_set->atom_type_index(atom_type_name1);
47  AtomTypeIndex atom_type_index2 = atom_type_set->atom_type_index(atom_type_name2);
48 
49  return contains_bond_length(atom_type_index1, atom_type_index2);
50 }
51 
52 bool IdealBondLengthSet::contains_bond_length(AtomTypeIndex atom_type_index1, AtomTypeIndex atom_type_index2) const{
53  if(atom_type_index1 > atom_type_index2) std::swap(atom_type_index1, atom_type_index2);
54  std::pair<AtomTypeIndex,AtomTypeIndex> index_pair(atom_type_index1,atom_type_index2);
55 
56  if(bond_lengths_.find(index_pair) == bond_lengths_.end()){
57  return false;
58  }
59 
60  return true;
61 }
62 
64 IdealBondLengthSet::get_bond_length( std::string const & atom_type_name1, std::string const & atom_type_name2) const{
67 
68  Size atom_type_index1 = atom_type_set->atom_type_index(atom_type_name1);
69  Size atom_type_index2 = atom_type_set->atom_type_index(atom_type_name2);
70 
71  if(atom_type_index1 > atom_type_index2) std::swap(atom_type_index1, atom_type_index2);
72 
73  if( ! contains_bond_length(atom_type_index1, atom_type_index2) ){
74  utility_exit_with_message( "ideal bond_length not defined between these atom types: "+ atom_type_name1+" "+atom_type_name2);
75  }
76  return get_bond_length(atom_type_index1, atom_type_index2);
77 }
78 
80 IdealBondLengthSet::get_bond_length(AtomTypeIndex const atom_type_index1, AtomTypeIndex const atom_type_index2) const{
81  assert(contains_bond_length(atom_type_index1, atom_type_index2));
82 
83  std::pair<AtomTypeIndex,AtomTypeIndex> index_pair(atom_type_index1,atom_type_index2);
84 
85  return bond_lengths_.find(index_pair)->second;
86 }
87 
88 void
90  std::string const & atom_type_name1,
91  std::string const & atom_type_name2,
92  BondLength const length
93 ){
94 
97 
98  Size atom_type_index1 = atom_type_set->atom_type_index(atom_type_name1);
99  Size atom_type_index2 = atom_type_set->atom_type_index(atom_type_name2);
100 
101  if(atom_type_index1 > atom_type_index2) std::swap(atom_type_index1, atom_type_index2);
102 
103  if( contains_bond_length(atom_type_index1, atom_type_index2) ){
104  utility_exit_with_message("this pair is already in the table... "+ atom_type_name1+" "+atom_type_name2);
105  }else{
106  add_bond_length(atom_type_index1, atom_type_index2, length);
107  }
108 }
109 
110 void
112  AtomTypeIndex const atom_type_index1,
113  AtomTypeIndex const atom_type_index2,
114  BondLength const length
115 ){
116  assert(atom_type_index1 <= atom_type_index2);
117  assert(!contains_bond_length(atom_type_index1, atom_type_index2) );
118 
119  std::pair<AtomTypeIndex,AtomTypeIndex> index_pair(atom_type_index1,atom_type_index2);
120  bond_lengths_[index_pair]= length;
121 }
122 
123 
124 void
126 {
127  utility::io::izstream data( filename.c_str() );
128 
129  if ( !data.good() ) utility_exit_with_message( "Unable to open element file: "+filename );
130 
131  // now parse the rest of the file
132 
133  using namespace basic;
134 
135  std::string line;
136  // parse the header line
137  getline( data, line ); // throw out the header line (currently it is just for file readability)
138  while ( getline( data,line ) ) {
139  utility::trim(line, " \t\n"); // remove leading and trailing spaces
140  if ( line.empty() > 0 ) continue; //skip blank lines
141  if ( line.find("#",0) == 0 ) continue; // skip comment lines
142 
143  std::istringstream l( line );
144  std::string name1, name2;
145  Real length;
146 
147  l >> name1 >> name2 >> length;
148 
149  if ( l.fail() ) {
150  utility_exit_with_message("bad line: "+line);
151  }
152 
153  add_bond_length( name1, name2, length);
154  }
155 }
156 
157 
158 
159 /// @details This function iterates over each bond_type pair in the bond_length_ map and
160 /// prints both keys. It is only used for debugging.
161 void
163 {
164  for(
165  std::map< std::pair<AtomTypeIndex, AtomTypeIndex>, BondLength >::const_iterator i = bond_lengths_.begin(),
166  e = bond_lengths_.end();
167  i != e;
168  ++i
169  ){
170  std::cout << i->first.first << " " << i->first.second << " " << i->second << std::endl;
171  }
172 }
173 
174 } // chemical
175 } // core