queso-0.57.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
QUESO::InterpolationSurrogateIOASCII< V, M > Class Template Reference

#include <InterpolationSurrogateIOASCII.h>

Inheritance diagram for QUESO::InterpolationSurrogateIOASCII< V, M >:
QUESO::InterpolationSurrogateIOBase< V, M >

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 41 of file InterpolationSurrogateIOASCII.C.

42  : InterpolationSurrogateIOBase<V,M>()
43  {}
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 46 of file InterpolationSurrogateIOASCII.C.

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

50  {
51  // Root processor
52  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  }
ScopedPtr< VectorSpace< V, M > >::Type m_vector_space
ScopedPtr< InterpolationSurrogateData< V, M > >::Type m_data
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 for vector operations using GSL library.
Definition: GslVector.h:49
int dim
Definition: ann_test.cpp:472
ScopedPtr< BoxSubset< V, M > >::Type m_domain
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 185 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::InterpolationSurrogateData< V, M >::x_max(), and QUESO::InterpolationSurrogateData< V, M >::x_min().

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  }
const InterpolationSurrogateData< V, M > & data() const
Reference to data object.
int dim
Definition: ann_test.cpp:472

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

Generated on Tue Jun 5 2018 19:49:41 for queso-0.57.1 by  doxygen 1.8.5