ROL
ROL_UserInputGenerator.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #ifndef ROL_USERINPUTGENERATOR_HPP
45 #define ROL_USERINPUTGENERATOR_HPP
46 
47 #include "ROL_SampleGenerator.hpp"
48 #include "ROL_BatchManager.hpp"
49 #include <fstream>
50 #include <iostream>
51 #include <string>
52 
53 namespace ROL {
54 
55 template<class Real>
56 class UserInputGenerator : public SampleGenerator<Real> {
57 private:
58  void sample(const std::string &file_pt,
59  const std::string &file_wt,
60  const int n,
61  const int dim,
62  const Teuchos::RCP<BatchManager<Real> > &bman) {
63  // Read in full point data and weight data
64  std::fstream input_pt;
65  input_pt.open(file_pt.c_str(),std::ios::in);
66  std::fstream input_wt;
67  input_wt.open(file_wt.c_str(),std::ios::in);
68  if ( !input_pt.is_open() || !input_wt.is_open() ) {
69  if ( !input_pt.is_open() ) {
70  if ( bman->batchID() == 0 ) {
71  std::cout << "CANNOT OPEN " << file_pt.c_str() << "\n";
72  }
73  }
74  if ( !input_wt.is_open() ) {
75  if ( bman->batchID() == 0 ) {
76  std::cout << "CANNOT OPEN " << file_wt.c_str() << "\n";
77  }
78  }
79  }
80  else {
81  std::vector<std::vector<Real> > pt(n);
82  std::vector<Real> wt(n,0.0);
83  std::vector<Real> point(dim,0.0);;
84  for (int i = 0; i < n; i++) {
85  for (int j = 0; j < dim; j++) {
86  input_pt >> point[j];
87  }
88  pt[i] = point;
89  input_wt >> wt[i];
90  }
91  // Get process rankd and number of processes
92  int rank = bman->batchID();
93  int nProc = bman->numBatches();
94  // Separate samples across processes
95  int frac = n/nProc;
96  int rem = n%nProc;
97  int N = frac;
98  if ( rank < rem ) {
99  N++;
100  }
101  std::vector<std::vector<Real> > my_pt(N);
102  std::vector<Real> my_wt(N,0.0);
103  int index = 0;
104  for (int i = 0; i < N; i++) {
105  index = i*nProc + rank;
106  my_pt[i] = pt[index];
107  my_wt[i] = wt[index];
108  }
111  }
112  input_pt.close();
113  input_wt.close();
114  }
115 
116 public:
117  UserInputGenerator(Teuchos::ParameterList &parlist,
118  const Teuchos::RCP<BatchManager<Real> > &bman)
119  : SampleGenerator<Real>(bman) {
120  Teuchos::ParameterList &list
121  = parlist.sublist("SOL").sublist("Sample Generator").sublist("User Input");
122  if ( list.isParameter("Points File") &&
123  list.isParameter("Weights File") &&
124  list.isParameter("Number of Samples") &&
125  list.isParameter("Dimension") ) {
126  std::string file_pt = list.get("Points File Name","points.txt");
127  std::string file_wt = list.get("Weights File Name","weights.txt");
128  int n = list.get("Number of Samples",100);
129  int dim = list.get("Dimension",4);
130  sample(file_pt,file_wt,n,dim,bman);
131  }
132  else {
133  TEUCHOS_TEST_FOR_EXCEPTION(true,std::invalid_argument,
134  ">>> (ROL::UserInputGenerator): ParameterList does not contain sufficient information.");
135  }
136  }
137 
138  UserInputGenerator(const std::string file_pt,
139  const std::string file_wt,
140  const int n,
141  const int dim,
142  const Teuchos::RCP<BatchManager<Real> > &bman)
143  : SampleGenerator<Real>(bman) {
144  sample(file_pt,file_wt,n,dim,bman);
145  }
146 
147  void refine(void) {}
148 };
149 
150 }
151 
152 #endif
void sample(const std::string &file_pt, const std::string &file_wt, const int n, const int dim, const Teuchos::RCP< BatchManager< Real > > &bman)
UserInputGenerator(const std::string file_pt, const std::string file_wt, const int n, const int dim, const Teuchos::RCP< BatchManager< Real > > &bman)
void setPoints(std::vector< std::vector< Real > > &p)
UserInputGenerator(Teuchos::ParameterList &parlist, const Teuchos::RCP< BatchManager< Real > > &bman)
void setWeights(std::vector< Real > &w)