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:	 NonDSampling
10 //- Description: Wrapper class for Fortran 90 LHS library
11 //- Owner:       Mike Eldred
12 //- Checked by:
13 //- Version:
14 
15 #ifndef NOND_SAMPLING_H
16 #define NOND_SAMPLING_H
17 
18 #include "dakota_data_types.hpp"
19 #include "DakotaNonD.hpp"
20 #include "LHSDriver.hpp"
21 #include "SensAnalysisGlobal.hpp"
22 
23 namespace Dakota {
24 
25 
26 /// Base class for common code between NonDLHSSampling,
27 /// NonDAdaptImpSampling, and other specializations
28 
29 /** This base class provides common code for sampling methods which
30     employ the Latin Hypercube Sampling (LHS) package from Sandia
31     Albuquerque's Risk and Reliability organization. NonDSampling now
32     exclusively utilizes the 1998 Fortran 90 LHS version as documented
33     in SAND98-0210, which was converted to a UNIX link library in
34     2001.  The 1970's vintage LHS (that had been f2c'd and converted
35     to incomplete classes) has been removed. */
36 class NonDSampling: public NonD
37 {
38 public:
39 
40   //
41   //- Heading: Constructors and destructor
42   //
43 
44   /// alternate constructor for evaluating and computing statistics
45   /// for the provided set of samples
46   NonDSampling(Model& model, const RealMatrix& sample_matrix);
47 
48   /// destructor
49   ~NonDSampling();
50 
51   //
52   //- Heading: Public member functions
53   //
54 
55   /// For the input sample set, computes mean, standard deviation, and
56   /// probability/reliability/response levels (aleatory uncertainties)
57   /// or intervals (epsitemic or mixed uncertainties)
58   void compute_statistics(const RealMatrix&     vars_samples,
59 			  const IntResponseMap& resp_samples);
60 
61   /// called by compute_statistics() to calculate min/max intervals
62   /// using allResponses
63   void compute_intervals(RealRealPairArray& extreme_fns);
64   /// called by compute_statistics() to calculate extremeValues from samples
65   void compute_intervals(const IntResponseMap& samples);
66   /// called by compute_statistics() to calculate min/max intervals
67   /// using samples
68   void compute_intervals(RealRealPairArray& extreme_fns,
69 			 const IntResponseMap& samples);
70 
71   /// calculates sample moments from a matrix of observations for a set of QoI
72   void compute_moments(const RealVectorArray& fn_samples);
73   /// calculate sample moments and confidence intervals from a map of
74   /// response observations
75   void compute_moments(const IntResponseMap& samples);
76   /// convert IntResponseMap to RealVectorArray and invoke helpers
77   void compute_moments(const IntResponseMap& samples, RealMatrix& moment_stats,
78 		       RealMatrix& moment_grads, RealMatrix& moment_conf_ints,
79 		       short moments_type, const StringArray& labels);
80   /// core compute_moments() implementation with all data as inputs
81   static void compute_moments(const RealVectorArray& fn_samples,
82 			      SizetArray& sample_counts,
83 			      RealMatrix& moment_stats, short moments_type,
84 			      const StringArray& labels);
85   /// core compute_moments() implementation with all data as inputs
86   static void compute_moments(const RealVectorArray& fn_samples,
87 			      RealMatrix& moment_stats, short moments_type);
88   /// alternate RealMatrix samples API for use by external clients
89   static void compute_moments(const RealMatrix& fn_samples,
90 			      RealMatrix& moment_stats, short moments_type);
91 
92   /// compute moment_grads from function and gradient samples
93   void compute_moment_gradients(const RealVectorArray& fn_samples,
94 				const RealMatrixArray& grad_samples,
95 				const RealMatrix& moment_stats,
96 				RealMatrix& moment_grads, short moments_type);
97 
98   /// compute moment confidence intervals from moment values
99   void compute_moment_confidence_intervals(const RealMatrix& moment_stats,
100 					   RealMatrix& moment_conf_ints,
101 					   const SizetArray& sample_counts,
102 					   short moments_type);
103 
104   /// archive moment statistics in results DB
105   void archive_moments(size_t inc_id = 0);
106   /// archive moment confidence intervals in results DB
107   void archive_moment_confidence_intervals(size_t inc_id = 0);
108 
109   /// archive extreme values (epistemic result) in results DB
110   void archive_extreme_responses(size_t inc_id = 0);
111 
112   /// called by compute_statistics() to calculate CDF/CCDF mappings of
113   /// z to p/beta and of p/beta to z as well as PDFs
114   void compute_level_mappings(const IntResponseMap& samples);
115 
116   /// prints the statistics computed in compute_statistics()
117   void print_statistics(std::ostream& s) const;
118 
119   /// prints the intervals computed in compute_intervals() with default
120   /// qoi_type and moment_labels
121   void print_intervals(std::ostream& s) const;
122   /// prints the intervals computed in compute_intervals()
123   void print_intervals(std::ostream& s, String qoi_type,
124 		       const StringArray& interval_labels) const;
125 
126   /// prints the moments computed in compute_moments() with default
127   /// qoi_type and moment_labels
128   void print_moments(std::ostream& s) const;
129   /// prints the moments computed in compute_moments()
130   void print_moments(std::ostream& s, String qoi_type,
131 		     const StringArray& moment_labels) const;
132   /// core print moments that can be called without object
133   static void print_moments(std::ostream& s, const RealMatrix& moment_stats,
134 			    const RealMatrix moment_cis, String qoi_type,
135 			    short moments_type,
136 			    const StringArray& moment_labels, bool print_cis);
137 
138   /// prints the Wilks stastics
139   void print_wilks_stastics(std::ostream& s) const;
140 
141   /// update finalStatistics from minValues/maxValues, momentStats,
142   /// and computedProbLevels/computedRelLevels/computedRespLevels
143   void update_final_statistics();
144 
145   /// calculates the number of samples using the Wilks formula
146   /// Static so I can test without instantiating a NonDSampling object - RWH
147   static int compute_wilks_sample_size(unsigned short order, Real alpha, Real beta, bool twosided = false);
148 
149   /// Helper function - calculates the Wilks residual
150   static Real compute_wilks_residual(unsigned short order, int nsamples, Real alpha, Real beta, bool twosided);
151 
152   /// calculates the alpha paramter given number of samples using the Wilks formula
153   /// Static so I can test without instantiating a NonDSampling object - RWH
154   static Real compute_wilks_alpha(unsigned short order, int nsamples, Real beta, bool twosided = false);
155 
156   /// calculates the beta paramter given number of samples using the Wilks formula
157   /// Static so I can test without instantiating a NonDSampling object - RWH
158   static Real compute_wilks_beta(unsigned short order, int nsamples, Real alpha, bool twosided = false);
159 
160   /// Get the lower and upper bounds supported by Wilks bisection solves
get_wilks_alpha_min()161   static Real get_wilks_alpha_min() { return 1.e-6; }
get_wilks_alpha_max()162   static Real get_wilks_alpha_max() { return 0.999999; }
get_wilks_beta_min()163   static Real get_wilks_beta_min() { return 1.e-6; }
get_wilks_beta_max()164   static Real get_wilks_beta_max() { return 0.999999; }
165 
166   /// transform allSamples imported by alternate constructor.  This is needed
167   /// since random variable distribution parameters are not updated until
168   /// run time and an imported sample_matrix is typically in x-space.
169   void transform_samples(Pecos::ProbabilityTransformation& nataf,
170   			 bool x_to_u = true);
171 
172   /// transform the specified samples matrix from x to u or u to x
173   void transform_samples(Pecos::ProbabilityTransformation& nataf,
174 			 RealMatrix& sample_matrix, int num_samples = 0,
175 			 bool x_to_u = true);
176 
177   /// return sampleType
178   unsigned short sampling_scheme() const;
179   /// return rngName
180   const String& random_number_generator() const;
181 
182 protected:
183 
184   //
185   //- Heading: Constructors and destructor
186   //
187 
188   /// constructor
189   NonDSampling(ProblemDescDB& problem_db, Model& model);
190   /// alternate constructor for sample generation and evaluation "on the fly"
191   NonDSampling(unsigned short method_name, Model& model,
192 	       unsigned short sample_type, int samples, int seed,
193 	       const String& rng, bool vary_pattern, short sampling_vars_mode);
194   /// alternate constructor for sample generation "on the fly"
195   NonDSampling(unsigned short sample_type, int samples, int seed,
196 	       const String& rng, const RealVector& lower_bnds,
197 	       const RealVector& upper_bnds);
198   /// alternate constructor for sample generation of correlated normals
199   /// "on the fly"
200   NonDSampling(unsigned short sample_type, int samples, int seed,
201                const String& rng, const RealVector& means,
202                const RealVector& std_devs, const RealVector& lower_bnds,
203                const RealVector& upper_bnds, RealSymMatrix& correl);
204 
205   //
206   //- Heading: Virtual function redefinitions
207   //
208 
209   void pre_run();
210   void core_run();
211 
212   int num_samples() const;
213 
214   /// resets number of samples and sampling flags
215   void sampling_reset(int min_samples, bool all_data_flag, bool stats_flag);
216 
217   /// set reference number of samples, which is a lower bound during reset
218   void sampling_reference(int samples_ref);
219 
220   /// assign randomSeed
221   void random_seed(int seed);
222 
223   /// set varyPattern
224   void vary_pattern(bool pattern_flag);
225 
226   /// Uses lhsDriver to generate a set of samples from the
227   /// distributions/bounds defined in the incoming model.
228   void get_parameter_sets(Model& model);
229 
230   /// Uses lhsDriver to generate a set of samples from the
231   /// distributions/bounds defined in the incoming model and populates
232   /// the specified design matrix.
233   void get_parameter_sets(Model& model, const int num_samples,
234                           RealMatrix& design_matrix);
235 
236   /// core of get_parameter_sets that accepts message print control
237   void get_parameter_sets(Model& model, const int num_samples,
238                           RealMatrix& design_matrix, bool write_msg);
239 
240   /// Uses lhsDriver to generate a set of uniform samples over
241   /// lower_bnds/upper_bnds.
242   void get_parameter_sets(const RealVector& lower_bnds,
243                           const RealVector& upper_bnds);
244 
245   /// Uses lhsDriver to generate a set of normal samples
246   void get_parameter_sets(const RealVector& means,
247                           const RealVector& std_devs,
248                           const RealVector& lower_bnds,
249                           const RealVector& upper_bnds,
250                           RealSymMatrix& correl);
251 
252   /// Override default update of continuous vars only
253   void update_model_from_sample(Model& model, const Real* sample_vars);
254   /// override default mapping of continuous variables only
255   void sample_to_variables(const Real* sample_vars, Variables& vars);
256   /// override default mapping of continuous variables only
257   void variables_to_sample(const Variables& vars, Real* sample_vars);
258 
259   /// return error estimates associated with each of the finalStatistics
260   const RealVector& response_error_estimates() const;
261 
262   //
263   //- Heading: Convenience member functions for derived classes
264   //
265 
266   /// increments numLHSRuns, sets random seed, and initializes lhsDriver
267   void initialize_lhs(bool write_message, int num_samples);
268 
269   /// in the case of sub-iteration, map from finalStatistics.active_set()
270   /// requests to activeSet used in evaluate_parameter_sets()
271   void active_set_mapping();
272 
273   /// compute sampled subsets (all, active, uncertain) within all
274   /// variables (acv/adiv/adrv) from samplingVarsMode and model
275   void mode_counts(const Variables& vars, size_t& cv_start, size_t& num_cv,
276 		   size_t& div_start, size_t& num_div, size_t& dsv_start,
277 		   size_t& num_dsv, size_t& drv_start, size_t& num_drv) const;
278   /// define subset views for sampling modes
279   void mode_bits(const Variables& vars, BitArray& active_vars,
280 		 BitArray& active_corr) const;
281 
282   /// helper to accumulate sum of finite samples
283   static void accumulate_mean(const RealVectorArray& fn_samples, size_t q,
284 			      size_t& num_samp, Real& mean);
285   /// helper to accumulate higher order sums of finite samples
286   static void accumulate_moments(const RealVectorArray& fn_samples, size_t q,
287 				 short moments_type, Real* moments);
288   /// helper to accumulate gradient sums
289   static void accumulate_moment_gradients(const RealVectorArray& fn_samples,
290 					  const RealMatrixArray& grad_samples,
291 					  size_t q, short moments_type,
292 					  Real mean, Real mom2, Real* mean_grad,
293 					  Real* mom2_grad);
294 
295   //
296   //- Heading: Data members
297   //
298 
299   int       seedSpec;    ///< the user seed specification (default is 0)
300   int       randomSeed;  ///< the current seed
301   const int samplesSpec; ///< initial specification of number of samples
302   int       samplesRef;  ///< reference number of samples updated for refinement
303   int       numSamples;  ///< the current number of samples to evaluate
304   String    rngName;	 ///< name of the random number generator
305   unsigned short sampleType; ///< the sample type: default, random, lhs,
306                              ///< incremental random, or incremental lhs
307   bool      wilksFlag; ///< flags use of Wilks formula to calculate num samples
308   unsigned short wilksOrder;
309   Real      wilksAlpha;
310   Real      wilksBeta;
311   short     wilksSidedness;
312 
313   /// gradients of standardized or central moments of response functions, as
314   /// determined by finalMomentsType.  Calculated in compute_moments() and
315   /// indexed as (var,moment) when moment id runs from 1:2*numFunctions.
316   RealMatrix momentGrads;
317 
318   /// standard errors (estimator std deviation) for each of the finalStatistics
319   RealVector finalStatErrors;
320 
321   /// current increment in a sequence of samples
322   int samplesIncrement;
323 
324   Pecos::LHSDriver lhsDriver; ///< the C++ wrapper for the F90 LHS library
325 
326   bool statsFlag;   ///< flags computation/output of statistics
327   bool allDataFlag; ///< flags update of allResponses
328                     ///< (allVariables or allSamples already defined)
329 
330   /// the sampling mode: ALEATORY_UNCERTAIN{,_UNIFORM},
331   /// EPISTEMIC_UNCERTAIN{,_UNIFORM}, UNCERTAIN{,_UNIFORM},
332   /// ACTIVE{,_UNIFORM}, or ALL{,_UNIFORM}.  This is a secondary control
333   /// on top of the variables view that allows sampling over subsets of
334   /// variables that may differ from the view.
335   short samplingVarsMode;
336   /// mode for input/output of LHS sample ranks: IGNORE_RANKS, GET_RANKS,
337   /// SET_RANKS, or SET_GET_RANKS
338   short sampleRanksMode;
339 
340   /// flag for generating a sequence of seed values within multiple
341   /// get_parameter_sets() calls so that these executions (e.g., for
342   /// SBO/SBNLS) are not repeated, but are still repeatable
343   bool varyPattern;
344   /// data structure to hold the sample ranks
345   RealMatrix sampleRanks;
346 
347    /// initialize statistical post processing
348   SensAnalysisGlobal nonDSampCorr;
349 
350   /// flags whether to use backfill to enforce uniqueness of discrete
351   /// LHS samples
352   bool backfillFlag;
353 
354   /// Minimum and maximum values of response functions for epistemic
355   /// calculations (calculated in compute_intervals()),
356   RealRealPairArray extremeValues;
357 
358   /// Function moments have been computed; used to determine whether
359   /// to archive the moments
360   bool functionMomentsComputed;
361 
362 private:
363 
364   //
365   //- Heading: Convenience functions
366   //
367 
368   /// helper function to consolidate update code
369   void sample_to_variables(const Real* sample_vars, Variables& vars,
370 			   Model& model);
371   /// helper function to copy a range from sample_vars to a variables type
372   void sample_to_type(const Real* sample_vars, Variables& vars,
373 		      size_t& cv_index, size_t num_cv, size_t& div_index,
374 		      size_t num_div, size_t& dsv_index, size_t num_dsv,
375 		      size_t& drv_index, size_t num_drv, size_t& samp_index,
376 		      Model& model);
377   /// helper function to copy a range from sample_vars to a variables type
378   void sample_to_cv_type(const Real* sample_vars, Variables& vars,
379 			 size_t& cv_index, size_t num_cv, size_t& div_index,
380 			 size_t num_div, size_t& dsv_index, size_t num_dsv,
381 			 size_t& drv_index, size_t num_drv, size_t& samp_index);
382                          //, Model& model);
383   /// helper function to copy a range from sample_vars to continuous variables
384   void sample_to_cv(const Real* sample_vars, Variables& vars, size_t& acv_index,
385 		    size_t num_acv, size_t& samp_index);
386   /// helper function to copy a range from sample_vars to discrete int variables
387   void sample_to_div(const Real* sample_vars, Variables& vars,
388 		     size_t& adiv_index, size_t num_adiv, size_t& samp_index);
389   /// helper function to copy a range from sample_vars to discrete string vars
390   void sample_to_dsv(const Real* sample_vars, Variables& vars,
391 		     size_t& adsv_index, size_t num_adsv, size_t& samp_index,
392 		     const StringSetArray& dss_values);
393   /// helper function to copy a range from sample_vars to discrete real vars
394   void sample_to_drv(const Real* sample_vars, Variables& vars,
395 		     size_t& adrv_index, size_t num_adrv, size_t& samp_index);
396 
397   //
398   //- Heading: Data
399   //
400 
401   /// counter for number of executions of get_parameter_sets() for this object
402   size_t numLHSRuns;
403 
404   /// Matrix of confidence internals on moments, with rows for mean_lower,
405   /// mean_upper, sd_lower, sd_upper (calculated in compute_moments())
406   RealMatrix momentCIs;
407 };
408 
409 
random_number_generator() const410 inline const String& NonDSampling::random_number_generator() const
411 { return rngName; }
412 
413 
pre_run()414 inline void NonDSampling::pre_run()
415 {
416   Analyzer::pre_run();
417 
418   // synchronize the derivative components flowing down from a NestedModel's
419   // call to subIterator.response_results_active_set(), so that the correct
420   // derivs are computed in Analyzer::evaluate_parameter_sets()
421   if (subIteratorFlag)
422     active_set_mapping();
423 }
424 
425 
compute_moments(const RealVectorArray & fn_samples)426 inline void NonDSampling::compute_moments(const RealVectorArray& fn_samples)
427 {
428   SizetArray sample_counts;
429   compute_moments(fn_samples, sample_counts, momentStats,
430 		  finalMomentsType, iteratedModel.response_labels());
431 }
432 
433 
compute_moments(const IntResponseMap & samples)434 inline void NonDSampling::compute_moments(const IntResponseMap& samples)
435 {
436   compute_moments(samples, momentStats, momentGrads, momentCIs,
437 		  finalMomentsType, iteratedModel.response_labels());
438 }
439 
440 
compute_intervals(RealRealPairArray & extreme_fns)441 inline void NonDSampling::compute_intervals(RealRealPairArray& extreme_fns)
442 { compute_intervals(extreme_fns, allResponses); }
443 
444 
compute_intervals(const IntResponseMap & samples)445 inline void NonDSampling::compute_intervals(const IntResponseMap& samples)
446 { compute_intervals(extremeValues, samples); }
447 
448 
print_intervals(std::ostream & s) const449 inline void NonDSampling::print_intervals(std::ostream& s) const
450 { print_intervals(s, "response function", iteratedModel.response_labels()); }
451 
452 
453 inline void NonDSampling::
print_moments(std::ostream & s,String qoi_type,const StringArray & moment_labels) const454 print_moments(std::ostream& s, String qoi_type,
455 	      const StringArray& moment_labels) const
456 {
457   bool print_cis = (numSamples > 1);
458   print_moments(s, momentStats, momentCIs, qoi_type, finalMomentsType,
459 		moment_labels, print_cis);
460 }
461 
462 
print_moments(std::ostream & s) const463 inline void NonDSampling::print_moments(std::ostream& s) const
464 { print_moments(s, "response function", iteratedModel.response_labels()); }
465 
466 
sampling_reference(int samples_ref)467 inline void NonDSampling::sampling_reference(int samples_ref)
468 { samplesRef = samples_ref; }
469 
470 
num_samples() const471 inline int NonDSampling::num_samples() const
472 { return numSamples; }
473 
474 
475 /** used by DataFitSurrModel::build_global() to publish the minimum
476     number of samples needed from the sampling routine (to build a
477     particular global approximation) and to set allDataFlag and
478     statsFlag.  In this case, allDataFlag is set to true (vectors of
479     variable and response sets must be returned to build the global
480     approximation) and statsFlag is set to false (statistics
481     computations are not needed). */
482 inline void NonDSampling::
sampling_reset(int min_samples,bool all_data_flag,bool stats_flag)483 sampling_reset(int min_samples, bool all_data_flag, bool stats_flag)
484 {
485   // allow sample reduction relative to previous sampling_reset() calls
486   // (that is, numSamples may be increased or decreased to match min_samples),
487   // but not relative to the original user specification (samplesSpec is a hard
488   // lower bound).  With the introduction of uniform/adaptive refinements,
489   // samplesRef (which is incremented from samplesSpec) replaces samplesSpec as
490   // the lower bound.  maxEvalConcurrency must not be updated since parallel
491   // config management depends on having the same value at ctor/run/dtor times.
492   numSamples = (min_samples > samplesRef) ? min_samples : samplesRef;
493   // note that previous value of numSamples is irrelevant: may increase or
494   // decrease relative to previous value
495   samplesIncrement = 0;
496 
497   allDataFlag = all_data_flag;
498   statsFlag   = stats_flag;
499 }
500 
501 
random_seed(int seed)502 inline void NonDSampling::random_seed(int seed)
503 { /*seedSpec = */randomSeed = seed; } // lhsDriver assigned in initialize_lhs()
504 
505 
sampling_scheme() const506 inline unsigned short NonDSampling::sampling_scheme() const
507 { return sampleType; }
508 
509 
vary_pattern(bool pattern_flag)510 inline void NonDSampling::vary_pattern(bool pattern_flag)
511 { varyPattern = pattern_flag; }
512 
513 
514 /** transform x_samples to u_samples for use by expansionSampler */
515 inline void NonDSampling::
transform_samples(Pecos::ProbabilityTransformation & nataf,bool x_to_u)516 transform_samples(Pecos::ProbabilityTransformation& nataf, bool x_to_u)
517 { transform_samples(nataf, allSamples, numSamples, x_to_u); }
518 
519 
520 /** This version of get_parameter_sets() extracts data from the
521     user-defined model in any of the four sampling modes and populates
522     the specified design matrix. */
523 inline void NonDSampling::
get_parameter_sets(Model & model,const int num_samples,RealMatrix & design_matrix)524 get_parameter_sets(Model& model, const int num_samples,
525 		   RealMatrix& design_matrix)
526 { get_parameter_sets(model, num_samples, design_matrix, true); }
527 
528 
529 /** This version of get_parameter_sets() extracts data from the
530     user-defined model in any of the four sampling modes and populates
531     class member allSamples. */
get_parameter_sets(Model & model)532 inline void NonDSampling::get_parameter_sets(Model& model)
533 { get_parameter_sets(model, numSamples, allSamples); }
534 
535 
536 inline void NonDSampling::
sample_to_cv(const Real * sample_vars,Variables & vars,size_t & acv_index,size_t num_acv,size_t & samp_index)537 sample_to_cv(const Real* sample_vars, Variables& vars, size_t& acv_index,
538 	     size_t num_acv, size_t& samp_index)
539 {
540   // sampled continuous vars (by value)
541   for (size_t i=0; i<num_acv; ++i, ++samp_index, ++acv_index)
542     vars.all_continuous_variable(sample_vars[samp_index], acv_index);
543 }
544 
545 
546 inline void NonDSampling::
sample_to_div(const Real * sample_vars,Variables & vars,size_t & adiv_index,size_t num_adiv,size_t & samp_index)547 sample_to_div(const Real* sample_vars, Variables& vars, size_t& adiv_index,
548 	      size_t num_adiv, size_t& samp_index)
549 {
550   // sampled discrete int vars (by value cast from Real)
551   for (size_t i=0; i<num_adiv; ++i, ++samp_index, ++adiv_index)
552     vars.all_discrete_int_variable((int)sample_vars[samp_index], adiv_index);
553 }
554 
555 
556 inline void NonDSampling::
sample_to_dsv(const Real * sample_vars,Variables & vars,size_t & adsv_index,size_t num_adsv,size_t & samp_index,const StringSetArray & dss_values)557 sample_to_dsv(const Real* sample_vars, Variables& vars, size_t& adsv_index,
558 	      size_t num_adsv, size_t& samp_index,
559 	      const StringSetArray& dss_values)
560 {
561   // sampled discrete string vars (by index cast from Real)
562   size_t i, set_index;
563   for (i=0; i<num_adsv; ++i, ++samp_index, ++adsv_index) {
564     set_index = (size_t)sample_vars[samp_index];
565     const String& dss = set_index_to_value(set_index, dss_values[adsv_index]);
566     vars.all_discrete_string_variable(dss, adsv_index);
567   }
568 }
569 
570 
571 inline void NonDSampling::
sample_to_drv(const Real * sample_vars,Variables & vars,size_t & adrv_index,size_t num_adrv,size_t & samp_index)572 sample_to_drv(const Real* sample_vars, Variables& vars, size_t& adrv_index,
573 	      size_t num_adrv, size_t& samp_index)
574 {
575   // sampled discrete real vars (by value)
576   for (size_t i=0; i<num_adrv; ++i, ++samp_index, ++adrv_index)
577     vars.all_discrete_real_variable(sample_vars[samp_index], adrv_index);
578 }
579 
580 
581 inline void NonDSampling::
sample_to_type(const Real * sample_vars,Variables & vars,size_t & cv_index,size_t num_cv,size_t & div_index,size_t num_div,size_t & dsv_index,size_t num_dsv,size_t & drv_index,size_t num_drv,size_t & samp_index,Model & model)582 sample_to_type(const Real* sample_vars, Variables& vars, size_t& cv_index,
583 	       size_t num_cv, size_t& div_index, size_t num_div,
584 	       size_t& dsv_index, size_t num_dsv, size_t& drv_index,
585 	       size_t num_drv, size_t& samp_index, Model& model)
586 {
587   sample_to_cv(sample_vars,  vars,  cv_index, num_cv,  samp_index);
588   sample_to_div(sample_vars, vars, div_index, num_div, samp_index);
589   if (num_dsv) {
590     short active_view = vars.view().first, all_view =
591       ( active_view == RELAXED_ALL || ( active_view >= RELAXED_DESIGN &&
592         active_view <= RELAXED_STATE )) ? RELAXED_ALL : MIXED_ALL;
593     // Note: Model::activeDiscSetStringValues is cached, so no penalty for
594     //       repeated query with same view
595     sample_to_dsv(sample_vars, vars, dsv_index, num_dsv, samp_index,
596 		  model.discrete_set_string_values(all_view));
597   }
598   sample_to_drv(sample_vars, vars, drv_index, num_drv, samp_index);
599 }
600 
601 
602 inline void NonDSampling::
sample_to_cv_type(const Real * sample_vars,Variables & vars,size_t & cv_index,size_t num_cv,size_t & div_index,size_t num_div,size_t & dsv_index,size_t num_dsv,size_t & drv_index,size_t num_drv,size_t & samp_index)603 sample_to_cv_type(const Real* sample_vars, Variables& vars, size_t& cv_index,
604 		  size_t num_cv, size_t& div_index, size_t num_div,
605 		  size_t& dsv_index, size_t num_dsv, size_t& drv_index,
606 		  size_t num_drv, size_t& samp_index)//, Model& model)
607 {
608   // UNIFORM views do not currently support non-relaxed discrete
609 
610   sample_to_cv(sample_vars, vars, cv_index, num_cv, samp_index);
611   //sample_to_div(sample_vars, vars, div_index, num_div, samp_index);
612   //if (num_dsv) {
613     //short active_view = vars.view().first, all_view = () ? : ;
614     //sample_to_dsv(sample_vars, vars, dsv_index,num_dsv,samp_index,
615     //              model.discrete_set_string_values(all_view));
616   //}
617   //sample_to_drv(sample_vars, vars, drv_index, num_drv, samp_index);
618 }
619 
620 
621 inline void NonDSampling::
update_model_from_sample(Model & model,const Real * sample_vars)622 update_model_from_sample(Model& model, const Real* sample_vars)
623 { sample_to_variables(sample_vars, model.current_variables(), model); }
624 
625 
626 inline void NonDSampling::
sample_to_variables(const Real * sample_vars,Variables & vars)627 sample_to_variables(const Real* sample_vars, Variables& vars)
628 { sample_to_variables(sample_vars, vars, iteratedModel); }
629 // default to iteratedModel for dss values
630 
631 
response_error_estimates() const632 inline const RealVector& NonDSampling::response_error_estimates() const
633 { return finalStatErrors; }
634 
635 } // namespace Dakota
636 
637 #endif
638