queso-0.53.0
Public Member Functions | List of all members
QUESO::InterpolationSurrogateIOASCII< V, M > Class Template Reference

#include <InterpolationSurrogateIOASCII.h>

Inheritance diagram for QUESO::InterpolationSurrogateIOASCII< V, M >:
Inheritance graph
[legend]
Collaboration diagram for QUESO::InterpolationSurrogateIOASCII< V, M >:
Collaboration graph
[legend]

Public Member Functions

 InterpolationSurrogateIOASCII ()
 
virtual ~InterpolationSurrogateIOASCII ()
 
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. More...
 
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. More...
 
- Public Member Functions inherited from QUESO::InterpolationSurrogateIOBase< V, M >
 InterpolationSurrogateIOBase ()
 
virtual ~InterpolationSurrogateIOBase ()
 
const
InterpolationSurrogateData< V,
M > & 
data () const
 Reference to data object. More...
 

Additional Inherited Members

- Protected Attributes inherited from QUESO::InterpolationSurrogateIOBase< V, M >
ScopedPtr< VectorSpace< V, M >
>::Type 
m_vector_space
 
ScopedPtr< BoxSubset< V, M >
>::Type 
m_domain
 
std::vector< unsigned int > m_n_points
 
ScopedPtr
< InterpolationSurrogateData
< V, M > >::Type 
m_data
 

Detailed Description

template<class V, class M>
class QUESO::InterpolationSurrogateIOASCII< V, M >

Definition at line 33 of file InterpolationSurrogateIOASCII.h.

Constructor & Destructor Documentation

template<class V , class M >
QUESO::InterpolationSurrogateIOASCII< V, M >::InterpolationSurrogateIOASCII ( )

Definition at line 40 of file InterpolationSurrogateIOASCII.C.

41  : InterpolationSurrogateIOBase<V,M>()
42  {}
template<class V , class M >
virtual QUESO::InterpolationSurrogateIOASCII< V, M >::~InterpolationSurrogateIOASCII ( )
inlinevirtual

Definition at line 39 of file InterpolationSurrogateIOASCII.h.

39 {};

Member Function Documentation

template<class V , class M >
void QUESO::InterpolationSurrogateIOASCII< V, M >::read ( const std::string &  filename,
const FullEnvironment env,
const std::string &  vector_space_prefix,
int  reading_rank = 0 
)
virtual

Read Interpolation surrogate data from filename using processor reading_rank.

This will read the data the file given by filename and setup all the infrastructure for InterpolationSurrogateData so that the user can then call the data() method. This can then be used to construct an Interpolation object. env.fullRank() must contain reading_rank. By default, processor 0 reads the data.

Implements QUESO::InterpolationSurrogateIOBase< V, M >.

Definition at line 45 of file InterpolationSurrogateIOASCII.C.

References QUESO::MpiComm::Bcast(), dim, QUESO::BaseEnvironment::fullComm(), QUESO::BaseEnvironment::fullRank(), queso_error_msg, and QUESO::StreamUtilities::skip_comment_lines().

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  }
ScopedPtr< InterpolationSurrogateData< V, M > >::Type m_data
ScopedPtr< VectorSpace< V, M > >::Type m_vector_space
#define queso_error_msg(msg)
Definition: asserts.h:47
ScopedPtr< BoxSubset< V, M > >::Type m_domain
Class for vector operations using GSL library.
Definition: GslVector.h:48
static void skip_comment_lines(std::istream &in, const char comment_start)
Skips comment lines until a line without a comment character is encountered.
int dim
Definition: ann2fig.cpp:81
template<class V , class M >
void QUESO::InterpolationSurrogateIOASCII< V, M >::write ( const std::string &  filename,
const InterpolationSurrogateData< V, M > &  data,
int  writing_rank = 0 
) const
virtual

Write interpolation surrogate data to filename using processor writing_rank.

env.fullRank() must contain writing_rank. By default processor 0 writes the data.

Implements QUESO::InterpolationSurrogateIOBase< V, M >.

Definition at line 184 of file InterpolationSurrogateIOASCII.C.

References dim, QUESO::InterpolationSurrogateData< V, M >::get_n_points(), QUESO::InterpolationSurrogateData< V, M >::get_paramDomain(), QUESO::InterpolationSurrogateData< V, M >::get_value(), QUESO::InterpolationSurrogateData< V, M >::n_values(), queso_error_msg, QUESO::InterpolationSurrogateData< V, M >::x_max(), and QUESO::InterpolationSurrogateData< V, M >::x_min().

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  }
const InterpolationSurrogateData< V, M > & data() const
Reference to data object.
#define queso_error_msg(msg)
Definition: asserts.h:47
int dim
Definition: ann2fig.cpp:81

The documentation for this class was generated from the following files:

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