queso-0.55.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/MpiComm.h>
30 #include <queso/StreamUtilities.h>
31 #include <queso/GslVector.h>
32 #include <queso/GslMatrix.h>
33 
34 // C++
35 #include <fstream>
36 
37 namespace QUESO
38 {
39 
40  template<class V, class M>
43  {}
44 
45  template<class V, class M>
46  void InterpolationSurrogateIOASCII<V,M>::read( const std::string& filename,
47  const FullEnvironment& env,
48  const std::string& vector_space_prefix,
49  int reading_rank )
50  {
51  // Root processor
52  unsigned int root = reading_rank;
53 
54  MpiComm full_comm = env.fullComm();
55 
56  std::ifstream input;
57 
58  unsigned int dim;
59 
60  // Only processor 0 does the reading.
61  // We'll broadcast the data as needed
62  if( env.fullRank() == root )
63  {
64  input.open( filename.c_str() );
65 
66  // skip the header
68 
69  // Read in dimension
70  input >> dim;
71  }
72 
73  // Broadcast the parsed dimension
74  full_comm.Bcast( &dim, 1, RawValue_MPI_UNSIGNED, root,
75  "InterpolationSurrogateIOASCII::read()",
76  "MpiComm::Bcast() failed!" );
77 
78  // Construct vector space
79  this->m_vector_space.reset( new VectorSpace<V,M>(env,
80  vector_space_prefix.c_str(),
81  dim,
82  NULL) );
83 
84  // Read in n_points in each dimension
85  this->m_n_points.resize(dim);
86 
87  if( env.fullRank() == root )
88  {
89  for( unsigned int d = 0; d < dim; d++ )
90  {
91  // Skip any comments
93 
94  // Make sure file is still good
95  if( !input.good() )
96  queso_error_msg("ERROR: Found unexpected end-of-file");
97 
98  input >> this->m_n_points[d];
99  }
100  }
101 
102  // Broadcast m_n_points
103  full_comm.Bcast( &this->m_n_points[0], dim, RawValue_MPI_UNSIGNED, root,
104  "InterpolationSurrogateIOASCII::read()",
105  "MpiComm::Bcast() failed!" );
106 
107  // Read parameter bounds
108  std::vector<double> param_mins(dim);
109  std::vector<double> param_maxs(dim);
110 
111  if( env.fullRank() == root )
112  {
113  for( unsigned int d = 0; d < dim; d++ )
114  {
115  // Skip any comments
117 
118  // Make sure file is still good
119  if( !input.good() )
120  queso_error_msg("ERROR: Found unexpected end-of-file");
121 
122  input >> param_mins[d] >> param_maxs[d];
123  }
124  }
125 
126  // Broadcast the bounds
127  full_comm.Bcast( &param_mins[0], dim, RawValue_MPI_DOUBLE, root,
128  "InterpolationSurrogateIOASCII::read()",
129  "MpiComm::Bcast() failed!" );
130 
131  full_comm.Bcast( &param_maxs[0], dim, RawValue_MPI_DOUBLE, root,
132  "InterpolationSurrogateIOASCII::read()",
133  "MpiComm::Bcast() failed!" );
134 
135  // Construct parameter domain
136  /* BoxSubset copies the incoming paramMins/paramMaxs so we don't
137  need to cache these copies, they can die. */
138  QUESO::GslVector paramMins(this->m_vector_space->zeroVector());
139  QUESO::GslVector paramMaxs(this->m_vector_space->zeroVector());
140 
141  for( unsigned int d = 0; d < dim; d++ )
142  {
143  paramMins[d] = param_mins[d];
144  paramMaxs[d] = param_maxs[d];
145  }
146 
147  this->m_domain.reset( new BoxSubset<V,M>(vector_space_prefix.c_str(),
148  *(this->m_vector_space.get()),
149  paramMins,
150  paramMaxs) );
151 
152  // Construct data object
153  this->m_data.reset( new InterpolationSurrogateData<V,M>(*(this->m_domain.get()),
154  this->m_n_points) );
155 
156  // Now read in the values
157  if( env.fullRank() == root )
158  {
159  for( unsigned int n = 0; n < this->m_data->n_values(); n++ )
160  {
161  // Skip any comments
163 
164  // Make sure file is still good
165  if( !input.good() )
166  queso_error_msg("ERROR: Found unexpected end-of-file");
167 
168  double value;
169  input >> value;
170 
171  this->m_data->set_value( n, value );
172  }
173 
174  // We are done parsing now, so close the file
175  input.close();
176  }
177 
178  // Broadcast the values
179  this->m_data->sync_values(root);
180 
181  // Fin
182  }
183 
184  template<class V, class M>
185  void InterpolationSurrogateIOASCII<V,M>::write( const std::string& filename,
187  int writing_rank ) const
188  {
189  // Make sure there are values in the data. If not the user didn't populate the data
190  if( !(data.n_values() > 0) )
191  {
192  std::string error = "ERROR: No values found in InterpolationSurrogateData.\n";
193  error += "Cannot write data without values.\n";
194  error += "Use InterpolationSurrogateBuilder or the read method to populate\n";
195  error += "data values.\n";
196 
197  queso_error_msg(error);
198  }
199 
200  std::ofstream output;
201 
202  // Only processor 0 does the writing
203  if( data.get_paramDomain().env().fullRank() == writing_rank )
204  {
205  output.open( filename.c_str() );
206 
207  // Write simpler header comments
208  std::string header = "# Data for interpolation surrogate\n";
209  header += "# Format is as follows:\n";
210  header += "# dimension (unsigned int)\n";
211  header += "# n_points in each dimension\n";
212  header += "# x_min, x_max pairs for each dimension\n";
213  header += "# values for each point in parameter space\n";
214  header += "# values must be ordered in structured format.\n";
215  output << header;
216 
217  // Write dimension
218  unsigned int dim = data.get_paramDomain().vectorSpace().dimGlobal();
219  output << dim << std::endl;
220 
221  // Write n_points
222  output << "# n_points" << std::endl;
223  for( unsigned int d = 0; d < dim; d++ )
224  {
225  output << data.get_n_points()[d] << std::endl;
226  }
227 
228  // Set precision for double output
229  output << std::scientific << std::setprecision(16);
230 
231  // Write domain bounds
232  output << "# domain bounds" << std::endl;
233  for( unsigned int d = 0; d < dim; d++ )
234  {
235  output << data.x_min(d) << " "
236  << data.x_max(d) << std::endl;
237  }
238 
239  // Write values
240  output << "# values" << std::endl;
241  for( unsigned int n = 0; n < data.n_values(); n++ )
242  {
243  output << data.get_value(n) << std::endl;
244  }
245 
246  // All done
247  output.close();
248 
249  } // data.get_paramDomain().env().fullRank() == writing_rank
250  }
251 
252 } // end namespace QUESO
253 
254 // Instantiate
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.
#define RawValue_MPI_UNSIGNED
Definition: MpiComm.h:68
#define queso_error_msg(msg)
Definition: asserts.h:47
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:181
int dim
Definition: ann2fig.cpp:81
double get_value(unsigned int n) const
const BoxSubset< V, M > & get_paramDomain() const
const std::vector< unsigned int > & get_n_points() const
int fullRank() const
Returns the process full rank.
Definition: Environment.C:226
double x_min(unsigned int dim) const
Lower bound of domain along dimension dim.
const MpiComm & fullComm() const
Access function for MpiComm full communicator.
Definition: Environment.C:232
static void skip_comment_lines(std::istream &in, const char comment_start)
Skips comment lines until a line without a comment character is encountered.
This class sets up the full environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:416
double x_max(unsigned int dim) const
Upper bound of domain along dimension dim.
Class representing a subset of a vector space shaped like a hypercube.
Definition: BoxSubset.h:44
A class representing a vector space.
Definition: VectorSet.h:49
Class for vector operations using GSL library.
Definition: GslVector.h:49
The QUESO MPI Communicator Class.
Definition: MpiComm.h:203
#define RawValue_MPI_DOUBLE
Definition: MpiComm.h:67
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.

Generated on Fri Jun 17 2016 14:17:42 for queso-0.55.0 by  doxygen 1.8.5