Planetary Orbital Evolution due to Tides
Orbital evolution of two objects experiencing tides
YRECIO.cpp
Go to the documentation of this file.
1 
9 #define BUILDING_LIBRARY
10 #include "YRECIO.h"
11 #include <string>
12 #include <sstream>
13 #include <algorithm>
14 
15 const std::streamsize max_line_length=1000;
16 
18  mass_iter(orig.mass_iter), age_iter(orig.age_iter),
19  radius_iter(orig.radius_iter), luminosity_iter(orig.luminosity_iter),
20  rad_mass_iter(orig.rad_mass_iter),
21  core_boundary_iter(orig.core_boundary_iter),
22  conv_inertia_iter(orig.conv_inertia_iter),
23  rad_inertia_iter(orig.rad_inertia_iter) {}
24 
26 {
27  mass_iter=rhs.mass_iter;
28  age_iter=rhs.age_iter;
35  return *this;
36 }
37 
39 {
43  return *this;
44 }
45 
47 {
48  EvolutionIterator result(*this); ++(*this); return result;
49 }
50 
52 {
53  return mass_iter==rhs.mass_iter;
54 }
55 
56 YRECHeader::YRECHeader(std::ifstream &track, const std::string &filename)
57 {
58  const std::string mass_word="Mtot/Msun", age_word="Age(Gyr)",
59  radius_word="log(R/Rsun)", log_luminosity_word="log(L/Lsun)",
60  env_mass_word="m_envp/M", env_radius_word1="r_envp/M",
61  env_radius_word2="r_envp/R", Irad_word="I_rad", Iconv_word="I_env";
62  char word[max_line_length+1];
63  char line_characters[max_line_length+1];
64  while(track.get()=='#') {
65  track.getline(line_characters, max_line_length);
66  if(!track.good())
67  throw Error::IO(filename, "while reading header.");
68  std::istringstream line(line_characters);
69  assert(line.good());
70  for(int word_index=0; !line.eof(); word_index++) {
71  assert(line.good());
72  line >> word;
73  if(word==mass_word) {
74  std::string error_msg="failed to read track mass: ";
75  if(!line.good())
76  throw Error::IO(filename, error_msg +
77  (line.eof() ? "end of line before =" :
78  "read error before ="));
79  line >> word;
80  if(!line.good()) throw Error::IO(filename, error_msg +
81  (line.eof() ? "end of line after =" :
82  "read error at ="));
83  line >> track_mass;
84  if(!line.good()) throw Error::IO(filename, error_msg +
85  (line.eof() ? "end of line before value" :
86  "error parsing value"));
87  }
88  if(word==age_word) age_col=word_index;
89  else if(word==radius_word) radius_col=word_index;
90  else if(word==log_luminosity_word) log_luminosity_col=word_index;
91  else if(word==env_mass_word)
92  envelope_mass_col=word_index;
93  else if(word==env_radius_word1 || word==env_radius_word2)
94  rad_conv_boundary_col=word_index;
95  else if(word==Irad_word) rad_inertia_col=word_index;
96  else if(word==Iconv_word) conv_inertia_col=word_index;
97  }
98  }
99  track.unget();
100 }
101 
102 void YRECEvolution::read_model_file(const std::string &filename)
103 {
104  std::cerr << "Reading: " << filename << std::endl;
105  std::ifstream file(filename.c_str());
106  YRECHeader header(file, filename);
107  mass_list.push_back(header.get_mass());
108  int required_columns=std::max(header.get_age_col(),
109  header.get_radius_col());
110  required_columns=std::max(required_columns,
111  header.get_log_luminosity_col());
112  required_columns=std::max(required_columns,
113  header.get_envelope_mass_col());
114  required_columns=std::max(required_columns,
115  header.get_core_boundary_col());
116  required_columns=std::max(required_columns,
117  header.get_rad_inertia_col());
118  required_columns=std::max(required_columns,
119  header.get_conv_inertia_col());
120  char line_characters[max_line_length+1];
121  std::list<double> track_ages, track_radii, track_luminosities,
122  track_rad_masses, track_core_boundaries, track_conv_inertias,
123  track_rad_inertias;
124  double inertia_norm=AstroConst::solar_mass*std::pow(
125  AstroConst::solar_radius, 2)*1e7;
126  for(int line_number=0; !file.eof(); line_number++) {
127  std::cerr << "Reading line " << line_number << "\r";
128  std::cerr.flush();
129  std::ostringstream error_message;
130  error_message << "while reading data line " << line_number;
131  if(!file.good()) throw Error::IO(filename, error_message.str());
132  file.getline(line_characters, max_line_length);
133  if(file.gcount()==0) continue;
134  std::istringstream line(line_characters);
135  int column=0;
136  for(; !line.eof(); column++) {
137  double value;
138  line >> value;
139  if(column==header.get_age_col()) track_ages.push_back(value);
140  else if(column==header.get_radius_col())
141  track_radii.push_back(std::pow(10.0, value));
142  else if(column==header.get_log_luminosity_col())
143  track_luminosities.push_back(std::pow(10.0, value));
144  else if(column==header.get_envelope_mass_col())
145  track_rad_masses.push_back(value*header.get_mass());
146  else if(column==header.get_core_boundary_col())
147  track_core_boundaries.push_back(value*track_radii.back());
148  else if(column==header.get_rad_inertia_col())
149  track_rad_inertias.push_back(value/inertia_norm);
150  else if(column==header.get_conv_inertia_col())
151  track_conv_inertias.push_back(value/inertia_norm);
152  }
153  if(column<=required_columns)
154  throw Error::IO(filename, error_message.str());
155  }
156  ages.push_back(list_to_valarray(track_ages));
157  radii.push_back(list_to_valarray(track_radii));
158  luminosities.push_back(list_to_valarray(track_luminosities));
159  rad_masses.push_back(list_to_valarray(track_rad_masses));
160  core_boundaries.push_back(list_to_valarray(track_core_boundaries));
161  rad_inertias.push_back(list_to_valarray(track_rad_inertias));
162  conv_inertias.push_back(list_to_valarray(track_conv_inertias));
163 }
164 
166 {
167  EvolutionIterator result;
168  result.mass_iter=mass_list.begin();
169  result.age_iter=ages.begin();
170  result.radius_iter=radii.begin();
171  result.luminosity_iter=luminosities.begin();
172  result.rad_mass_iter=rad_masses.begin();
173  result.core_boundary_iter=core_boundaries.begin();
174  result.conv_inertia_iter=conv_inertias.begin();
175  result.rad_inertia_iter=rad_inertias.begin();
176  return result;
177 }
178 
180 {
181  EvolutionIterator result;
182  result.mass_iter=mass_list.end();
183  result.age_iter=ages.end();
184  result.radius_iter=radii.end();
185  result.luminosity_iter=luminosities.end();
186  result.rad_mass_iter=rad_masses.end();
187  result.core_boundary_iter=core_boundaries.end();
188  result.conv_inertia_iter=conv_inertias.end();
189  result.rad_inertia_iter=rad_inertias.end();
190  return result;
191 }
192 
194 {
195  mass_list.splice(dest.mass_iter, mass_list, source.mass_iter);
196  ages.splice(dest.age_iter, ages, source.age_iter);
197  radii.splice(dest.radius_iter, radii, source.radius_iter);
198  luminosities.splice(dest.luminosity_iter, luminosities,
199  source.luminosity_iter);
200  rad_masses.splice(dest.rad_mass_iter, rad_masses, source.rad_mass_iter);
201  core_boundaries.splice(dest.core_boundary_iter, core_boundaries,
202  source.core_boundary_iter);
203  conv_inertias.splice(dest.conv_inertia_iter, conv_inertias,
204  source.conv_inertia_iter);
205  rad_inertias.splice(dest.rad_inertia_iter, rad_inertias,
206  source.rad_inertia_iter);
207 }
208 
210 {
211  EvolutionIterator iter=begin(), stop_iter=end();
212  double largest_sorted=*iter.mass_iter;
213  while(iter!=stop_iter) {
214  while(*iter.mass_iter>=largest_sorted && iter!=stop_iter) {
215  largest_sorted=*iter.mass_iter;
216  ++iter;
217  }
218  if(iter==stop_iter) break;
219  EvolutionIterator dest=begin();
220  while(*(dest.mass_iter)<=*iter.mass_iter) {++dest;}
221  EvolutionIterator source=iter++;
222  move(dest, source);
223  }
224 }
225 
226 YRECEvolution::YRECEvolution(const std::string &model_directory,
227  double smooth_radius,
228  double smooth_conv_inertia,
229  double smooth_rad_inertia,
230  double smooth_rad_mass,
231  double smooth_core_env_boundary,
232  int radius_nodes,
233  int conv_inertia_nodes,
234  int rad_inertia_nodes,
235  int rad_mass_nodes,
236  int core_env_boundary_nodes)
237 {
238  DIR *dirstream=opendir(model_directory.c_str());
239  std::string join;
240  if(model_directory[model_directory.size()-1]=='/') join="";
241  else join="/";
242  if(dirstream==NULL) throw Error::PathNotFound(
243  "in YRECEvolution constructor.", model_directory);
244  struct dirent *entry;
245  while((entry=readdir(dirstream))) {
246  std::string fname(entry->d_name);
247  if(fname[0]!='.' && fname.substr(fname.size()-6)==".track")
248  read_model_file(model_directory+join+entry->d_name);
249  }
250  if(closedir(dirstream)) throw Error::Runtime(
251  "Failed to close directory stream tied to "+model_directory+
252  " in YRECEvolution constructor.");
253  sort_masses();
254  std::valarray<double> masses = list_to_valarray(mass_list);
255  interpolate_from(masses,
256  ages,
257  radii,
258  conv_inertias,
259  rad_inertias,
260  rad_masses,
261  core_boundaries,
262  smooth_radius,
263  smooth_conv_inertia,
264  smooth_rad_inertia,
265  smooth_rad_mass,
266  smooth_core_env_boundary,
267  radius_nodes,
268  conv_inertia_nodes,
269  rad_inertia_nodes,
270  rad_mass_nodes,
271  core_env_boundary_nodes,
272  luminosities,
273  NaN,
274  0);
275 }
std::list< std::valarray< double > >::iterator luminosity_iter
Iterator over the arrays of stellar lg(luminosity) of the tracks.
Definition: YRECIO.h:95
std::list< std::valarray< double > >::iterator core_boundary_iter
Iterator over the arrays of core-envelope boundaries of the tracks.
Definition: YRECIO.h:95
void read_model_file(const std::string &filename)
Reads a single evolution track file.
Definition: YRECIO.cpp:102
Defines the classes for generating stellar evolution interpolators from the YREC tracks.
EvolutionIterator & operator++()
Advance all iterators to the next track.
Definition: YRECIO.cpp:38
void move(EvolutionIterator &dest, EvolutionIterator &source)
Moves source to right before destination.
Definition: YRECIO.cpp:193
std::list< double >::iterator mass_iter
Iterator over the masses of the tracks.
Definition: YRECIO.h:92
std::list< std::valarray< double > >::iterator conv_inertia_iter
Iterator over the arrays of convective envelope moments of inertia of the tracks. ...
Definition: YRECIO.h:95
An iterator over the list of extracted tracks.
Definition: YRECIO.h:85
YRECEvolution()
Default constructor, use load_state to get a working interpolator.
Definition: YRECIO.h:186
EvolutionIterator & operator=(const EvolutionIterator &rhs)
Copy rhs to *this.
Definition: YRECIO.cpp:25
void sort_masses()
Sorts the data by mass.
Definition: YRECIO.cpp:209
const double solar_radius
Radius of the sun [m].
EvolutionIterator end()
Returns an EvolutionIterator pointing to the end of all quantities.
Definition: YRECIO.cpp:179
A class which parses the header of a YREC evolution track.
Definition: YRECIO.h:30
EvolutionIterator()
Create an iterator, which must have all its *_iter members set before it can be used.
Definition: YRECIO.h:89
std::list< std::valarray< double > >::iterator rad_inertia_iter
Iterator over the arrays of radiative core moments of inertia of the tracks.
Definition: YRECIO.h:95
const double solar_mass
Mass of the sun [kg].
std::list< std::valarray< double > >::iterator age_iter
Iterator over the arrays of ages of the tracks.
Definition: YRECIO.h:95
bool operator==(const EvolutionIterator &rhs)
Is RHS at the same position as this?
Definition: YRECIO.cpp:51
YRECHeader(std::ifstream &track, const std::string &filename)
Parse the header information from the given track stream.
Definition: YRECIO.cpp:56
EvolutionIterator begin()
Returns an EvolutionIterator pointing to the beginning of all quantities.
Definition: YRECIO.cpp:165
std::list< std::valarray< double > >::iterator radius_iter
Iterator over the arrays of stellar radii of the tracks.
Definition: YRECIO.h:95
std::list< std::valarray< double > >::iterator rad_mass_iter
Iterator over the arrays of core masses of the tracks.
Definition: YRECIO.h:95