1 /*  _______________________________________________________________________
2 
3     DAKOTA: Design Analysis Kit for Optimization and Terascale Applications
4     Copyright 2014-2020 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
5     This software is distributed under the GNU Lesser General Public License.
6     For more information, see the README file in the top Dakota directory.
7     _______________________________________________________________________ */
8 
9 //- Class:       NonDMultilevelFunctionTrain
10 //- Description: Iterator to compute/employ Polynomial Chaos expansions
11 //- Owner:       Mike Eldred, Sandia National Laboratories
12 
13 #ifndef NOND_MULTILEVEL_FUNCTION_TRAIN_H
14 #define NOND_MULTILEVEL_FUNCTION_TRAIN_H
15 
16 #include "NonDC3FunctionTrain.hpp"
17 #include "SharedC3ApproxData.hpp"
18 
19 
20 namespace Dakota {
21 
22 /// Nonintrusive polynomial chaos expansion approaches to uncertainty
23 /// quantification
24 
25 /** The NonDMultilevelFunctionTrain class uses a set of function train
26     (FT) expansions, one per model fidelity or resolution, to
27     approximate the effect of parameter uncertainties on response
28     functions of interest. */
29 
30 class NonDMultilevelFunctionTrain: public NonDC3FunctionTrain
31 {
32 public:
33 
34   //
35   //- Heading: Constructors and destructor
36   //
37 
38   /// standard constructor
39   NonDMultilevelFunctionTrain(ProblemDescDB& problem_db, Model& model);
40   /*
41   /// alternate constructor for helper iterator
42   NonDMultilevelFunctionTrain(unsigned short method_name, Model& model,
43 			      const SizetArray& colloc_pts_seq,
44 			      const RealVector& dim_pref,
45 			      Real colloc_ratio, const SizetArray& pilot,
46 			      int seed, short u_space_type, short refine_type,
47 			      short refine_control, short covar_control,
48 			      short ml_alloc_cntl, short ml_discrep,
49 			      //short rule_nest, short rule_growth,
50 			      bool piecewise_basis, bool use_derivs,
51 			      bool cv_flag,
52 			      const String& import_build_pts_file,
53 			      unsigned short import_build_format,
54 			      bool import_build_active_only);
55   */
56   /// destructor
57   ~NonDMultilevelFunctionTrain();
58 
59   //
60   //- Heading: Virtual function redefinitions
61   //
62 
63   //bool resize();
64 
65 protected:
66 
67   //
68   //- Heading: Virtual function redefinitions
69   //
70 
71   void initialize_u_space_model();
72   void core_run();
73 
74   void assign_specification_sequence();
75   void increment_specification_sequence();
76 
77   size_t collocation_points() const;
78   int random_seed() const;
79   int first_seed() const;
80 
81   void initialize_ml_regression(size_t num_lev, bool& import_pilot);
82   void infer_pilot_sample(/*Real ratio, */SizetArray& delta_N_l);
83   void increment_sample_sequence(size_t new_samp, size_t total_samp,
84 				 size_t step);
85   void compute_sample_increment(const RealVector& regress_metrics,
86 				const SizetArray& N_l, SizetArray& delta_N_l);
87 
88   void print_results(std::ostream& s, short results_state = FINAL_RESULTS);
89 
90   //
91   //- Heading: Member functions
92   //
93 
94   /// assign defaults related to allocation control (currently for ML
95   /// regression approaches)
96   void assign_allocation_control();
97 
98 private:
99 
100   //
101   //- Heading: Utility functions
102   //
103 
104   // sequence handlers:
105 
106   size_t start_rank(size_t index) const;
107   size_t start_rank() const;
108 
109   unsigned short start_order(size_t index) const;
110   unsigned short start_order() const;
111 
112   void push_c3_active(const UShortArray& orders);
113   void push_c3_active();
114 
115   /// return the regression size used for different refinement options;
116   /// the index identifies the point in the specification sequence
117   size_t regression_size(size_t index);
118 
119   // scale sample profile to retain shape while enforcing an upper bound
120   //void scale_profile(..., RealVector& new_N_l);
121 
122   //
123   //- Heading: Data
124   //
125 
126   /// user specification for start_rank_sequence
127   SizetArray startRankSeqSpec;
128   /// user specification for start_order_sequence
129   UShortArray startOrderSeqSpec;
130 
131   /// sequence index for start{Rank,Order}SeqSpec
132   size_t sequenceIndex;
133 };
134 
135 
collocation_points() const136 inline size_t NonDMultilevelFunctionTrain::collocation_points() const
137 { return NonDExpansion::collocation_points(sequenceIndex); }
138 
139 
random_seed() const140 inline int NonDMultilevelFunctionTrain::random_seed() const
141 { return NonDExpansion::random_seed(sequenceIndex); }
142 
143 
first_seed() const144 inline int NonDMultilevelFunctionTrain::first_seed() const
145 { return NonDExpansion::random_seed(0); }
146 
147 
start_rank(size_t index) const148 inline size_t NonDMultilevelFunctionTrain::start_rank(size_t index) const
149 {
150   if (startRankSeqSpec.empty())
151     return startRankSpec; // use single-level default provided by DataMethod
152   else
153     return (index < startRankSeqSpec.size()) ?
154       startRankSeqSpec[index] : startRankSeqSpec.back();
155 }
156 
157 
start_rank() const158 inline size_t NonDMultilevelFunctionTrain::start_rank() const
159 { return start_rank(sequenceIndex); }
160 
161 
162 inline unsigned short NonDMultilevelFunctionTrain::
start_order(size_t index) const163 start_order(size_t index) const
164 {
165   if (startOrderSeqSpec.empty())
166     return startOrderSpec; // use single-level default provided by DataMethod
167   else
168     return (index < startOrderSeqSpec.size()) ?
169       startOrderSeqSpec[index] : startOrderSeqSpec.back();
170 }
171 
172 
start_order() const173 inline unsigned short NonDMultilevelFunctionTrain::start_order() const
174 { return start_order(sequenceIndex); }
175 
176 
177 inline void NonDMultilevelFunctionTrain::
push_c3_active(const UShortArray & orders)178 push_c3_active(const UShortArray& orders)
179 {
180   push_c3_start_rank(start_rank());
181   push_c3_max_rank(maxRankSpec);    // restore if adapted (no sequence)
182 
183   push_c3_seed(random_seed());
184 
185   push_c3_start_orders(orders);
186   push_c3_max_order(maxOrderSpec); // restore if adapted (no sequence)
187   std::shared_ptr<SharedC3ApproxData> shared_data_rep =
188     std::static_pointer_cast<SharedC3ApproxData>(
189     uSpaceModel.shared_approximation().data_rep());
190   shared_data_rep->update_basis(); // propagate order updates to oneApproxOpts
191 }
192 
193 
push_c3_active()194 inline void NonDMultilevelFunctionTrain::push_c3_active()
195 {
196   UShortArray orders;
197   configure_expansion_orders(start_order(), dimPrefSpec, orders);
198   push_c3_active(orders);
199 }
200 
201 } // namespace Dakota
202 
203 #endif
204