queso-0.53.0
InterpolationSurrogateIOASCII.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008-2015 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 // This class
26 #include <queso/InterpolationSurrogateIOASCII.h>
27 
28 // QUESO
29 #include <queso/StreamUtilities.h>
30 #include <queso/GslVector.h>
31 #include <queso/GslMatrix.h>
32 
33 // C++
34 #include <fstream>
35 
36 namespace QUESO
37 {
38 
39  template<class V, class M>
42  {}
43 
44  template<class V, class M>
45  void InterpolationSurrogateIOASCII<V,M>::read( const std::string& filename,
46  const FullEnvironment& env,
47  const std::string& vector_space_prefix,
48  int reading_rank )
49  {
50  // Root processor
51  int root = reading_rank;
52 
53  MpiComm full_comm = env.fullComm();
54 
55  std::ifstream input;
56 
57  unsigned int dim;
58 
59  // Only processor 0 does the reading.
60  // We'll broadcast the data as needed
61  if( env.fullRank() == root )
62  {
63  input.open( filename.c_str() );
64 
65  // skip the header
67 
68  // Read in dimension
69  input >> dim;
70  }
71 
72  // Broadcast the parsed dimension
73  full_comm.Bcast( &dim, 1, MPI_UNSIGNED, root,
74  "InterpolationSurrogateIOASCII::read()",
75  "MpiComm::Bcast() failed!" );
76 
77  // Construct vector space
78  this->m_vector_space.reset( new VectorSpace<V,M>(env,
79  vector_space_prefix.c_str(),
80  dim,
81  NULL) );
82 
83  // Read in n_points in each dimension
84  this->m_n_points.resize(dim);
85 
86  if( env.fullRank() == root )
87  {
88  for( unsigned int d = 0; d < dim; d++ )
89  {
90  // Skip any comments
92 
93  // Make sure file is still good
94  if( !input.good() )
95  queso_error_msg("ERROR: Found unexpected end-of-file");
96 
97  input >> this->m_n_points[d];
98  }
99  }
100 
101  // Broadcast m_n_points
102  full_comm.Bcast( &this->m_n_points[0], dim, MPI_UNSIGNED, root,
103  "InterpolationSurrogateIOASCII::read()",
104  "MpiComm::Bcast() failed!" );
105 
106  // Read parameter bounds
107  std::vector<double> param_mins(dim);
108  std::vector<double> param_maxs(dim);
109 
110  if( env.fullRank() == root )
111  {
112  for( unsigned int d = 0; d < dim; d++ )
113  {
114  // Skip any comments
116 
117  // Make sure file is still good
118  if( !input.good() )
119  queso_error_msg("ERROR: Found unexpected end-of-file");
120 
121  input >> param_mins[d] >> param_maxs[d];
122  }
123  }
124 
125  // Broadcast the bounds
126  full_comm.Bcast( &param_mins[0], dim, MPI_DOUBLE, root,
127  "InterpolationSurrogateIOASCII::read()",
128  "MpiComm::Bcast() failed!" );
129 
130  full_comm.Bcast( &param_maxs[0], dim, MPI_DOUBLE, root,
131  "InterpolationSurrogateIOASCII::read()",
132  "MpiComm::Bcast() failed!" );
133 
134  // Construct parameter domain
135  /* BoxSubset copies the incoming paramMins/paramMaxs so we don't
136  need to cache these copies, they can die. */
137  QUESO::GslVector paramMins(this->m_vector_space->zeroVector());
138  QUESO::GslVector paramMaxs(this->m_vector_space->zeroVector());
139 
140  for( unsigned int d = 0; d < dim; d++ )
141  {
142  paramMins[d] = param_mins[d];
143  paramMaxs[d] = param_maxs[d];
144  }
145 
146  this->m_domain.reset( new BoxSubset<V,M>(vector_space_prefix.c_str(),
147  *(this->m_vector_space.get()),
148  paramMins,
149  paramMaxs) );
150 
151  // Construct data object
152  this->m_data.reset( new InterpolationSurrogateData<V,M>(*(this->m_domain.get()),
153  this->m_n_points) );
154 
155  // Now read in the values
156  if( env.fullRank() == root )
157  {
158  for( unsigned int n = 0; n < this->m_data->n_values(); n++ )
159  {
160  // Skip any comments
162 
163  // Make sure file is still good
164  if( !input.good() )
165  queso_error_msg("ERROR: Found unexpected end-of-file");
166 
167  double value;
168  input >> value;
169 
170  this->m_data->set_value( n, value );
171  }
172 
173  // We are done parsing now, so close the file
174  input.close();
175  }
176 
177  // Broadcast the values
178  this->m_data->sync_values(root);
179 
180  // Fin
181  }
182 
183  template<class V, class M>
184  void InterpolationSurrogateIOASCII<V,M>::write( const std::string& filename,
186  int writing_rank ) const
187  {
188  // Make sure there are values in the data. If not the user didn't populate the data
189  if( !(data.n_values() > 0) )
190  {
191  std::string error = "ERROR: No values found in InterpolationSurrogateData.\n";
192  error += "Cannot write data without values.\n";
193  error += "Use InterpolationSurrogateBuilder or the read method to populate\n";
194  error += "data values.\n";
195 
196  queso_error_msg(error);
197  }
198 
199  std::ofstream output;
200 
201  // Only processor 0 does the writing
202  if( data.get_paramDomain().env().fullRank() == writing_rank )
203  {
204  output.open( filename.c_str() );
205 
206  // Write simpler header comments
207  std::string header = "# Data for interpolation surrogate\n";
208  header += "# Format is as follows:\n";
209  header += "# dimension (unsigned int)\n";
210  header += "# n_points in each dimension\n";
211  header += "# x_min, x_max pairs for each dimension\n";
212  header += "# values for each point in parameter space\n";
213  header += "# values must be ordered in structured format.\n";
214  output << header;
215 
216  // Write dimension
217  unsigned int dim = data.get_paramDomain().vectorSpace().dimGlobal();
218  output << dim << std::endl;
219 
220  // Write n_points
221  output << "# n_points" << std::endl;
222  for( unsigned int d = 0; d < dim; d++ )
223  {
224  output << data.get_n_points()[d] << std::endl;
225  }
226 
227  // Set precision for double output
228  output << std::scientific << std::setprecision(16);
229 
230  // Write domain bounds
231  output << "# domain bounds" << std::endl;
232  for( unsigned int d = 0; d < dim; d++ )
233  {
234  output << data.x_min(d) << " "
235  << data.x_max(d) << std::endl;
236  }
237 
238  // Write values
239  output << "# values" << std::endl;
240  for( unsigned int n = 0; n < data.n_values(); n++ )
241  {
242  output << data.get_value(n) << std::endl;
243  }
244 
245  // All done
246  output.close();
247 
248  } // data.get_paramDomain().env().fullRank() == writing_rank
249  }
250 
251 } // end namespace QUESO
252 
253 // Instantiate
const BoxSubset< V, M > & get_paramDomain() const
double x_min(unsigned int dim) const
Lower bound of domain along dimension dim.
int fullRank() const
Returns the process full rank.
Definition: Environment.C:222
#define queso_error_msg(msg)
Definition: asserts.h:47
const MpiComm & fullComm() const
Access function for MpiComm full communicator.
Definition: Environment.C:228
double get_value(unsigned int n) const
const std::vector< unsigned int > & get_n_points() const
The QUESO MPI Communicator Class.
Definition: MpiComm.h:75
virtual void write(const std::string &filename, const InterpolationSurrogateData< V, M > &data, int writing_rank=0) const
Write interpolation surrogate data to filename using processor writing_rank.
Class for vector operations using GSL library.
Definition: GslVector.h:48
This class sets up the full environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:416
void Bcast(void *buffer, int count, RawType_MPI_Datatype datatype, int root, const char *whereMsg, const char *whatMsg) const
Broadcast values from the root process to the slave processes.
Definition: MpiComm.C:133
static void skip_comment_lines(std::istream &in, const char comment_start)
Skips comment lines until a line without a comment character is encountered.
Class representing a subset of a vector space shaped like a hypercube.
Definition: BoxSubset.h:44
virtual void read(const std::string &filename, const FullEnvironment &env, const std::string &vector_space_prefix, int reading_rank=0)
Read Interpolation surrogate data from filename using processor reading_rank.
int dim
Definition: ann2fig.cpp:81
double x_max(unsigned int dim) const
Upper bound of domain along dimension dim.
A class representing a vector space.
Definition: VectorSet.h:49

Generated on Thu Jun 11 2015 13:52:33 for queso-0.53.0 by  doxygen 1.8.5