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:        NIDRProblemDescDB
10 //- Description: Implementation code for the New IDR ProblemDescDB class.
11 //-              It defines the keyword handlers that yacc calls to populate
12 //-              the database based on the parsed input.
13 //- Owner:       David M. Gay
14 //- Checked by:
15 //- Version: $Id$
16 
17 #include "NIDRProblemDescDB.hpp"
18 #include "ParallelLibrary.hpp"
19 #include "WorkdirHelper.hpp"     // for copy/link file op utilities
20 #include "dakota_data_util.hpp"
21 #include "pecos_stat_util.hpp"
22 #include "LognormalRandomVariable.hpp"
23 #include "LoguniformRandomVariable.hpp"
24 #include "TriangularRandomVariable.hpp"
25 #include "BetaRandomVariable.hpp"
26 #include "GammaRandomVariable.hpp"
27 #include "GumbelRandomVariable.hpp"
28 #include "FrechetRandomVariable.hpp"
29 #include "WeibullRandomVariable.hpp"
30 #include "HistogramBinRandomVariable.hpp"
31 #include "PoissonRandomVariable.hpp"
32 #include "BinomialRandomVariable.hpp"
33 #include "NegBinomialRandomVariable.hpp"
34 #include "GeometricRandomVariable.hpp"
35 #include "HypergeometricRandomVariable.hpp"
36 #include "DiscreteSetRandomVariable.hpp"
37 #include <functional>
38 #include <string>
39 #include <sstream>
40 #include <algorithm>
41 #include <stdarg.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <boost/algorithm/string.hpp>
45 #ifdef HAVE_OPTPP
46 #include "globals.h"
47 #endif
48 
49 #ifdef DAKOTA_DL_SOLVER
50 #ifdef _WIN32
51 #include "dakota_windows.h"
52 #define dlopen(x,y) LoadLibrary(x)
53 #else
54 #include <dlfcn.h>
55 #endif
56 #endif /*DAKOTA_DL_SOLVER*/
57 
58 /// Set input to NIDR via string argument instead of input file
59 extern "C" void nidr_set_input_string(const char *);
60 
61 extern "C" void nidr_lib_cleanup(void);
62 
63 namespace Dakota {
64 extern ProblemDescDB *Dak_pddb;
65 
66 extern "C" FILE *nidrin;
67 extern "C" int nidr_parse(const char*, FILE*);
68 
69 /// maximum error length is roughly 100 lines at 80 char; using fixed
70 /// error length instead of investing in converting to vsnprintf (C++11)
71 const size_t NIDR_MAX_ERROR_LEN = 8192;
72 
73 int NIDRProblemDescDB::nerr = 0;
74 NIDRProblemDescDB* NIDRProblemDescDB::pDDBInstance(NULL);
75 
76 
NIDRProblemDescDB(ParallelLibrary & parallel_lib)77 NIDRProblemDescDB::NIDRProblemDescDB(ParallelLibrary& parallel_lib):
78   ProblemDescDB(BaseConstructor(), parallel_lib)
79 {}
80 
81 
~NIDRProblemDescDB()82 NIDRProblemDescDB::~NIDRProblemDescDB()
83 {
84 #ifndef NO_NIDR_DYNLIB
85   nidr_lib_cleanup(); // close any explicitly opened shared libraries
86 #endif
87 }
88 
botch(const char * fmt,...)89 void NIDRProblemDescDB::botch(const char *fmt, ...)
90 {
91   va_list ap;
92   va_start(ap, fmt);
93   char msg[NIDR_MAX_ERROR_LEN];
94   std::vsprintf(msg, fmt, ap);
95   va_end(ap);
96   Cerr << "\nError: " << msg << ".\n";
97 
98   abort_handler(PARSE_ERROR);
99 }
100 
squawk(const char * fmt,...)101 void NIDRProblemDescDB::squawk(const char *fmt, ...)
102 {
103   va_list ap;
104   va_start(ap, fmt);
105   char msg[NIDR_MAX_ERROR_LEN];
106   std::vsprintf(msg, fmt, ap);
107   va_end(ap);
108   Cerr << "\nError: " << msg << ".\n";
109 
110   ++nerr;
111 }
112 
warn(const char * fmt,...)113 void NIDRProblemDescDB::warn(const char *fmt, ...)
114 {
115   va_list ap;
116   va_start(ap, fmt);
117   char msg[NIDR_MAX_ERROR_LEN];
118   std::vsprintf(msg, fmt, ap);
119   va_end(ap);
120   Cerr << "\nWarning: " << msg << ".\n";
121 }
122 
123 /** Parse the input file using the Input Deck Reader (IDR) parsing system.
124     IDR populates the IDRProblemDescDB object with the input file data. */
125 void NIDRProblemDescDB::
derived_parse_inputs(const std::string & dakota_input_file,const std::string & dakota_input_string,const std::string & parser_options)126 derived_parse_inputs(const std::string& dakota_input_file,
127 		     const std::string& dakota_input_string,
128 		     const std::string& parser_options)
129 {
130   // set the pDDBInstance
131   pDDBInstance = this;
132 
133   // Open the dakota input file passed in and "attach" it to stdin
134   // (required by nidr_parse)
135   if(!dakota_input_string.empty()) {
136     Cout << "Using provided Dakota input string" << std::endl;
137     // BMA TODO: output the string contents if verbose
138     nidr_set_input_string(dakota_input_string.c_str());
139   } else if (!dakota_input_file.empty()) {
140       Cout << "Using Dakota input file '" << dakota_input_file << "'" << std::endl;
141       if( !(nidrin = std::fopen(dakota_input_file.c_str(), "r")) )
142         botch("cannot open \"%s\"", dakota_input_file.c_str());
143   } else {
144     Cerr << "\nError: NIDR parser called with no input." << std::endl;
145     abort_handler(PARSE_ERROR);
146   }
147 
148   // // nidr_parse parses the input file and invokes the keyword handlers
149   // // NIDR expects a null pointer if unused, not an empty string
150   // const char* ptr_parse_opts = NULL;
151   // if (!parser_options.empty())
152   //   ptr_parse_opts = parser_options.c_str();
153   FILE *dump_file = NULL;
154   if (nidr_parse(parser_options.c_str(), dump_file)) {
155     //Cerr << "\nErrors parsing input file." << std::endl;
156     abort_handler(PARSE_ERROR); // allows proper abort in parallel case
157   }
158   if (nerr)
159     abort_handler(PARSE_ERROR);
160   if (parallel_library().command_line_run()) {
161     const char *s;
162     // manage dynamic solver plugins specified in the input
163 #ifdef DAKOTA_DL_SOLVER
164     std::list<DataMethod>::iterator
165       Dml  = dataMethodList.begin(),
166       Dmle = dataMethodList.end();
167     DataMethodRep *Dr;
168     const char *s0;
169     char *s1;
170     size_t L;
171 
172     for(; Dml != Dmle; ++Dml) {
173       Dr = Dml->dataMethodRep;
174       if ((s0 = Dr->dlDetails) && *(s = s0) > ' ') {
175 	while(*s > ' ')
176 	  ++s;
177 	s1 = 0;
178 	if (*s) {
179 	  L = s - s0;
180 	  s1 = new char[L+1];
181 	  memcpy(s1, s0, L);
182 	  s1[L] = 0;
183 	  s0 = s1;
184 	}
185 	if (!(Dr->dlLib = dlopen(s0, RTLD_NOW))) {
186 #ifndef _WIN32
187 	  const char *se;
188 	  if ((se = dlerror()))
189 	    squawk("Cannot open dl_solver \"%s\":\n\t%s", s0, se);
190 	  else
191 #endif
192 	    squawk("Cannot open dl_solver \"%s\"", s0);
193 	}
194 	if (s1)
195 	  delete[] s1;
196       }
197     }
198 #endif
199   }
200 }
201 
202 
derived_broadcast()203 void NIDRProblemDescDB::derived_broadcast()
204 {
205   check_variables(&dataVariablesList);
206   check_responses(&dataResponsesList);
207 }
208 // check_variables() invokes check_variables_node(), either directly or after
209 // some manip of interval/histogram/correlation data.  check_variables_node
210 // does label processing (?) followed by additional processing in
211 // make_variable_defaults() below, which calls BuildLabels().  Need to
212 // understand the reason for this step.
213 
214 // Basic flow should be:
215 // (1) db.parse_inputs(file), db.insert_node(), db.set(), or some combination
216 //     only on rank 0.
217 // (2) perform sanity checking (once) on sets of vector inputs across all kw's,
218 //     but for only those vector inputs that have been provided (see 4).
219 //     Note: if only sanity checking (no set up), then derived_broadcast() only
220 //           as a pre-processor on rank 0.
221 // (3) send (minimal) db buffer from rank 0 to all other ranks
222 // (4) define (large-scale) vector defaults across all keywords for all ranks,
223 //     retaining sanity from 2
224 //
225 // Q: is 2 really needed, or would it be OK to perform all checks in 4?
226 //    Evolution: checks/defaults as part of parsing (original)
227 //           --> checks after parsing/mixed input and defaults after DB
228 //               send/receive (current)
229 //           --> checks/defaults after DB send/receive (proposed new)
230 
231 
derived_post_process()232 void NIDRProblemDescDB::derived_post_process()
233 {
234   // finish processing dataVariableList
235   make_variable_defaults(&dataVariablesList);
236   // finish processing dataResponsesList
237   make_response_defaults(&dataResponsesList);
238 }
239 
240 
241 #define Squawk NIDRProblemDescDB::squawk
242 #define Warn NIDRProblemDescDB::warn
243 
244 typedef unsigned long UL;
245 
246 struct Iface_Info {
247   DataInterfaceRep *di;
248   DataInterface *di_handle;
249 };
250 
251 struct Iface_mp_Rlit {
252   String DataInterfaceRep::* sp;
253   RealVector DataInterfaceRep::* rv;
254   const char *lit;
255 };
256 
257 struct Iface_mp_ilit {
258   String DataInterfaceRep::* sp;
259   int DataInterfaceRep::* iv;
260   const char *lit;
261 };
262 
263 struct Iface_mp_lit {
264   String DataInterfaceRep::* sp;
265   const char *lit;
266 };
267 
268 struct Iface_mp_type {
269   short DataInterfaceRep::* sp;
270   short type;
271 };
272 
273 struct Iface_mp_utype {
274   unsigned short DataInterfaceRep::* sp;
275   unsigned short type;
276 };
277 
278 struct Meth_Info {
279   DataMethodRep *dme;
280   DataMethod *dme0;
281 };
282 
283 struct Method_mp_ilit2 {
284   String DataMethodRep::* sp;
285   int DataMethodRep::* ip;
286   const char *lit;
287 };
288 
289 struct Method_mp_ilit2z {
290   String DataMethodRep::* sp;
291   size_t DataMethodRep::* ip;
292   const char *lit;
293 };
294 
295 struct Method_mp_lit {
296   String DataMethodRep::* sp;
297   const char *lit;
298 };
299 
300 struct Method_mp_litc {
301   String DataMethodRep::* sp;
302   Real DataMethodRep::* rp;
303   const char *lit;
304 };
305 
306 struct Method_mp_litrv {
307   String DataMethodRep::* sp;
308   RealVector DataMethodRep::* rp;
309   const char *lit;
310 };
311 
312 struct Method_mp_slit2 {
313   String DataMethodRep::* sp;
314   String DataMethodRep::* sp2;
315   const char *lit;
316 };
317 
318 struct Method_mp_utype_lit {
319   unsigned short DataMethodRep::* ip;
320   String DataMethodRep::* sp;
321   unsigned short utype;
322 };
323 
324 struct Method_mp_ord {
325   short DataMethodRep::* sp;
326   int ord;
327 };
328 
329 struct Method_mp_type {
330   short DataMethodRep::* ip;
331   short type;
332 };
333 
334 struct Method_mp_utype {
335   unsigned short DataMethodRep::* ip;
336   unsigned short utype;
337 };
338 
339 struct Mod_Info {
340   DataModelRep *dmo;
341   DataModel *dmo0;
342 };
343 
344 struct Model_mp_lit {
345   String DataModelRep::* sp;
346   const char *lit;
347 };
348 
349 struct Model_mp_ord {
350   short DataModelRep::* sp;
351   int ord;
352 };
353 
354 struct Model_mp_type {
355   short DataModelRep::* sp;
356   short type;
357 };
358 
359 struct Model_mp_utype {
360   unsigned short DataModelRep::* sp;
361   unsigned short utype;
362 };
363 
364 struct Resp_Info {
365   DataResponsesRep *dr;
366   DataResponses *dr0;
367 };
368 
369 struct Resp_mp_lit {
370   String DataResponsesRep::* sp;
371   const char *lit;
372 };
373 
374 struct Resp_mp_utype {
375   unsigned short DataResponsesRep::* sp;
376   unsigned short utype;
377 };
378 
379 //struct Env_mp_lit {
380 //  String DataEnvironmentRep::* sp;
381 //  const char *lit;
382 //};
383 
384 struct Env_mp_utype {
385   unsigned short DataEnvironmentRep::* sp;
386   unsigned short utype;
387 };
388 
389 enum { // kinds of continuous aleatory uncertain variables
390   CAUVar_normal = 0,
391   CAUVar_lognormal = 1,
392   CAUVar_uniform = 2,
393   CAUVar_loguniform = 3,
394   CAUVar_triangular = 4,
395   CAUVar_exponential = 5,
396   CAUVar_beta = 6,
397   CAUVar_gamma = 7,
398   CAUVar_gumbel = 8,
399   CAUVar_frechet = 9,
400   CAUVar_weibull = 10,
401   CAUVar_histogram_bin = 11,
402   CAUVar_Nkinds = 12	// number of kinds of cauv variables
403 };
404 
405 enum { // kinds of discrete aleatory uncertain integer variables
406   DAUIVar_poisson = 0,
407   DAUIVar_binomial = 1,
408   DAUIVar_negative_binomial = 2,
409   DAUIVar_geometric = 3,
410   DAUIVar_hypergeometric = 4,
411   DAUIVar_histogram_point_int = 5,
412   DAUIVar_Nkinds = 6	// number of kinds of dauiv variables
413 };
414 
415 enum { // kinds of discrete aleatory uncertain string variables
416   DAUSVar_histogram_point_str = 0,
417   DAUSVar_Nkinds = 1	// number of kinds of dausv variables
418 };
419 
420 enum { // kinds of discrete aleatory uncertain real variables
421   DAURVar_histogram_point_real = 0,
422   DAURVar_Nkinds = 1	// number of kinds of daurv variables
423 };
424 
425 enum { // kinds of continuous epistemic uncertain variables
426   CEUVar_interval = 0,
427   CEUVar_Nkinds = 1	// number of kinds of cauv variables
428 };
429 
430 enum { // kinds of discrete epistemic uncertain integer variables
431   DEUIVar_interval = 0,
432   DEUIVar_set_int = 1,
433   DEUIVar_Nkinds = 2	// number of kinds of deuiv variables
434 };
435 
436 enum { // kinds of discrete epistemic uncertain string variables
437   DEUSVar_set_str = 0,
438   DEUSVar_Nkinds = 1	// number of kinds of deusv variables
439 };
440 
441 enum { // kinds of discrete epistemic uncertain real variables
442   DEURVar_set_real = 0,
443   DEURVar_Nkinds = 1	// number of kinds of deurv variables
444 };
445 
446 enum { // kinds of discrete set variables
447   DiscSetVar_design_set_int = 0,
448   DiscSetVar_design_set_str = 1,
449   DiscSetVar_design_set_real = 2,
450   DiscSetVar_state_set_int = 3,
451   DiscSetVar_state_set_str = 4,
452   DiscSetVar_state_set_real = 5,
453   DiscSetVar_Nkinds = 6	// number of kinds of discrete set variables
454 };
455 
456 struct VarLabel {
457   size_t n;
458   const char **s;
459 };
460 
461 struct Var_Info {
462   DataVariablesRep *dv;
463   DataVariables    *dv_handle;
464   VarLabel  CAUv[ CAUVar_Nkinds],  CEUv[ CEUVar_Nkinds];
465   VarLabel DAUIv[DAUIVar_Nkinds], DAUSv[DAUSVar_Nkinds], DAURv[DAURVar_Nkinds];
466   VarLabel DEUIv[DEUIVar_Nkinds], DEUSv[DEUSVar_Nkinds], DEURv[DEURVar_Nkinds];
467   IntArray   *nddsi, *nddss, *nddsr, *nCI, *nDI, *nhbp,
468              *nhpip, *nhpsp, *nhprp,
469              *ndusi, *nduss, *ndusr,
470              *ndssi, *ndsss, *ndssr;
471 
472   RealVector *ddsr, *CIlb, *CIub, *CIp, *DIp, *DSIp, *DSSp, *DSRp, *dusr,
473              *hba, *hbo, *hbc,
474              *hpic, *hpsc, *hpra, *hprc,
475              *ucm, *dssr;
476   IntVector  *ddsi, *DIlb, *DIub, *hpia, *dusi, *dssi, *ddsia, *ddssa, *ddsra;
477   StringArray *ddss, *hpsa, *duss, *dsss;
478 };
479 
480 struct Var_check
481 {
482   const char *name;
483   size_t DataVariablesRep::* n;
484   void (*vgen)(DataVariablesRep*, size_t);
485 };
486 
487 /// structure for verifying bounds and initial point for real-valued vars
488 struct Var_rcheck
489 {
490   const char *name;
491   size_t DataVariablesRep::* n;
492   void (*vgen)(DataVariablesRep*, size_t);
493   RealVector  DataVariablesRep::* L;   // when static, initialized to NULL
494   RealVector  DataVariablesRep::* U;   // when static, initialized to NULL
495   RealVector  DataVariablesRep::* V;   // when static, initialized to NULL
496   StringArray DataVariablesRep::* Lbl; // when static, initialized to NULL
497 };
498 
499 /// structure for verifying bounds and initial point for string-valued vars
500 // struct Var_scheck
501 // {
502 //   const char *name;
503 //   size_t DataVariablesRep::* n;
504 //   void (*vgen)(DataVariablesRep*, size_t);
505 //   StringArray DataVariablesRep::* L;   // when static, initialized to NULL
506 //   StringArray DataVariablesRep::* U;   // when static, initialized to NULL
507 //   StringArray DataVariablesRep::* V;   // when static, initialized to NULL
508 //   StringArray DataVariablesRep::* Lbl; // when static, initialized to NULL
509 // };
510 
511 /// structure for verifying bounds and initial point for integer-valued vars
512 struct Var_icheck
513 {
514   const char *name;
515   size_t DataVariablesRep::* n;
516   void (*vgen)(DataVariablesRep*, size_t);
517   IntVector   DataVariablesRep::* L;   // when static, initialized to NULL
518   IntVector   DataVariablesRep::* U;   // when static, initialized to NULL
519   IntVector   DataVariablesRep::* V;   // when static, initialized to NULL
520   StringArray DataVariablesRep::* Lbl; // when static, initialized to NULL
521 };
522 
523 struct Var_uinfo {
524   const char *lbl;
525   const char *vkind;
526   size_t DataVariablesRep::* n;
527   void(*vchk)(DataVariablesRep*, size_t, Var_Info*);
528 };
529 
530 struct Var_brv {
531   RealVector DataVariablesRep::* rv;
532   Real b;
533 };
534 
535 struct Var_biv {
536   IntVector DataVariablesRep::* iv;
537   int b;
538 };
539 
540 struct Var_mp_type {
541   short DataVariablesRep::* sp;
542   short type;
543 };
544 
545 void NIDRProblemDescDB::
iface_Real(const char * keyname,Values * val,void ** g,void * v)546 iface_Real(const char *keyname, Values *val, void **g, void *v)
547 {
548   (*(Iface_Info**)g)->di->**(Real DataInterfaceRep::**)v = *val->r;
549 }
550 
551 void NIDRProblemDescDB::
iface_Rlit(const char * keyname,Values * val,void ** g,void * v)552 iface_Rlit(const char *keyname, Values *val, void **g, void *v)
553 {
554   DataInterfaceRep *di = (*(Iface_Info**)g)->di;
555   Iface_mp_Rlit *R = (Iface_mp_Rlit*)v;
556   Real *r;
557   RealVector *rv;
558   size_t i, n;
559 
560   di->*R->sp = R->lit;
561   rv = &(di->*R->rv);
562   n = val->n;
563   rv->sizeUninitialized(n);
564   r = val->r;
565   for(i = 0; i < n; ++i)
566     (*rv)[i] = r[i];
567 }
568 
569 void NIDRProblemDescDB::
iface_false(const char * keyname,Values * val,void ** g,void * v)570 iface_false(const char *keyname, Values *val, void **g, void *v)
571 {
572   (*(Iface_Info**)g)->di->**(bool DataInterfaceRep::**)v = false;
573 }
574 
575 void NIDRProblemDescDB::
iface_ilit(const char * keyname,Values * val,void ** g,void * v)576 iface_ilit(const char *keyname, Values *val, void **g, void *v)
577 {
578   DataInterfaceRep *di = (*(Iface_Info**)g)->di;
579   Iface_mp_ilit *I = (Iface_mp_ilit*)v;
580 
581   di->*I->sp = I->lit;
582   di->*I->iv = *val->i;
583 }
584 
585 void NIDRProblemDescDB::
iface_int(const char * keyname,Values * val,void ** g,void * v)586 iface_int(const char *keyname, Values *val, void **g, void *v)
587 {
588   int n = *val->i;
589   (*(Iface_Info**)g)->di->**(int DataInterfaceRep::**)v = n;
590 }
591 
592 void NIDRProblemDescDB::
iface_lit(const char * keyname,Values * val,void ** g,void * v)593 iface_lit(const char *keyname, Values *val, void **g, void *v)
594 {
595   (*(Iface_Info**)g)->di->*((Iface_mp_lit*)v)->sp = ((Iface_mp_lit*)v)->lit;
596 }
597 
598 void NIDRProblemDescDB::
iface_start(const char * keyname,Values * val,void ** g,void * v)599 iface_start(const char *keyname, Values *val, void **g, void *v)
600 {
601   Iface_Info *ii;
602 
603   if (!(ii = new Iface_Info))
604   Botch:		botch("new failure in iface_start");
605   if (!(ii->di_handle = new DataInterface))
606     goto Botch;
607   ii->di = ii->di_handle->dataIfaceRep.get();
608   *g = (void*)ii;
609 }
610 
611 void NIDRProblemDescDB::
iface_true(const char * keyname,Values * val,void ** g,void * v)612 iface_true(const char *keyname, Values *val, void **g, void *v)
613 {
614   (*(Iface_Info**)g)->di->**(bool DataInterfaceRep::**)v = true;
615 }
616 
617 
618 #ifdef DEBUG_LEGACY_WORKDIR
619 
620 extern const char** arg_list_adjust(const char **, void **);
621 
622 /*
623  *  not_executable(const char *driver_name) checks whether driver_name is an
624  *  executable file appearing somewhere in $PATH and returns 0 if so,
625  *  1 if not found, 2 if found but not executable.
626  */
not_executable(const char * driver_name,const char * tdir)627 int not_executable(const char *driver_name, const char *tdir)
628 {
629   static const char *p0;
630   struct stat sb;
631   const char *p;
632   const char *p1;
633   char *b, buf[2048];
634   const char *a2[2], **al;
635   int rc, sv;
636   size_t clen, dlen, plen, tlen;
637   void *a0;
638   std::string cwd = boost::filesystem::current_path().string();
639 
640 #ifdef _WIN32
641   char dbuf[128];
642 #else
643   static uid_t myuid;
644   static gid_t mygid;
645 #endif
646 
647   /* allow shell assignments and quotes around executable names */
648   /* that may involve blanks */
649   a2[0] = driver_name;
650   a2[1] = 0;
651   al = arg_list_adjust(a2,&a0);
652   driver_name = al[0];
653 
654   rc = 0;
655   if (!p0) {
656     p0 = std::getenv("PATH");
657 #ifdef _WIN32
658     if (!p0)
659       p0 = std::getenv("Path");
660 #else
661     myuid = geteuid();
662     mygid = getegid();
663 #endif
664     if (p0)
665       while(*p0 <= ' ' && *p0)
666 	++p0;
667     else
668       p0 = "";
669   }
670 #ifdef _WIN32
671   // make sure we have a suitable suffix
672   if ((p = strrchr(driver_name, '.'))) {
673     if (std::strlen(++p) != 3)
674       p = 0;
675     else {
676       for(b = dbuf; *p; ++b, ++p)
677 	*b = tolower(*p);
678       *b = 0;
679       if (std::strcmp(dbuf, "exe") && std::strcmp(dbuf, "bat") &&
680 	  std::strcmp(dbuf, "cmd"))
681 	p = 0;
682     }
683   }
684   if (!p) {
685     dlen = std::strlen(driver_name);
686     if (dlen + 5 > sizeof(dbuf)) {
687       rc = 1;
688       goto ret;
689     }
690     std::strcpy(dbuf, driver_name);
691     std::strcpy(dbuf+dlen, ".exe");
692     driver_name = dbuf;
693   }
694 
695   // . is always implicitly in $Path under MS Windows; check it now
696   if (!stat(driver_name, &sb))
697     goto ret;
698 #endif
699 
700   cwd = boost::filesystem::current_path().string();
701   clen = cwd.size();
702   dlen = std::strlen(driver_name);
703   tlen = std::strlen(tdir);
704   rc = 1;
705   p = p0;
706   if (std::strchr(driver_name, '/')
707 #ifdef _WIN32
708       || std::strchr(driver_name, '\\')
709       || (dlen > 2 && driver_name[1] == ':')
710 #endif
711       )
712     p = "";
713 
714   else if (clen + dlen + 2 < sizeof(buf)) {
715     std::memcpy(buf,cwd.c_str(),clen);
716     buf[clen] = '/';
717     std::strcpy(buf+clen+1, driver_name);
718     sv = stat(buf,&sb);
719     if (sv == 0)
720       goto stat_check;
721   }
722   else if (tdir && *tdir && tlen + dlen + 2 < sizeof(buf)) {
723     std::memcpy(buf,tdir,tlen);
724     buf[tlen] = '/';
725     std::strcpy(buf+tlen+1, driver_name);
726     sv = stat(buf,&sb);
727     if (sv == 0)
728       goto stat_check;
729   }
730   for(;;) {
731     for(p1 = p;; ++p1) {
732       switch(*p1) {
733       case 0:
734 #ifdef _WIN32
735       case ';':
736 #else
737       case ':':
738 #endif
739 	goto break2;
740       }
741     }
742   break2:
743     if (p1 == p || (p1 == p + 1 && *p == '.'))
744       sv = stat(driver_name, &sb);
745     else {
746       plen = p1 - p;
747       while(plen && p[plen-1] <= ' ')
748 	--plen;
749       if (plen + dlen + 2 > sizeof(buf))
750 	sv = 1;
751       else {
752 	std::strncpy(buf,p,plen);
753 	b = buf + plen;
754 	*b++ = '/';
755 	std::strcpy(b, driver_name);
756 	sv = stat(buf, &sb);
757       }
758     }
759     if (!sv) {
760     stat_check:
761 #ifdef __CYGWIN__
762       rc = 2;
763       if (sb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) {
764 	rc = 0;
765 	goto ret;
766       }
767 #elif defined(_WIN32) || defined(_WIN64)
768       rc = 0;
769       goto ret;
770 #else
771       rc = 2;
772       if (sb.st_uid == myuid) {
773 	if (sb.st_mode & S_IXUSR)
774 	  goto ret0;
775       }
776       else if (sb.st_gid == mygid) {
777 	if (sb.st_mode & S_IXGRP)
778 	  goto ret0;
779       }
780       else if (sb.st_mode & S_IXOTH) {
781       ret0:                          rc = 0;
782 	goto ret;
783       }
784 #endif
785     }
786     if (p1 == 0)
787       goto ret;
788     else if (!*p1)
789       break;
790     for(p = p1 + 1; *p <= ' '; ++p)
791       while(*p <= ' ' && *p)
792 	if (!*p)
793 	  goto ret;
794   }
795  ret:
796   if (a0)
797     std::free(a0);
798   return rc;
799 }
800 
801 #endif  // DEBUG_LEGACY_WORKDIR
802 
803 
804 void NIDRProblemDescDB::
iface_stop(const char * keyname,Values * val,void ** g,void * v)805 iface_stop(const char *keyname, Values *val, void **g, void *v)
806 {
807   Iface_Info *ii = *(Iface_Info**)g;
808   DataInterfaceRep *di = ii->di;
809 
810   StringArray& analysis_drivers = di->analysisDrivers;
811   bool ife = di->inputFilter.empty();
812   bool ofe = di->outputFilter.empty();
813   int nd = analysis_drivers.size();
814   int ac = di->asynchLocalAnalysisConcurrency;
815   int ec = di->asynchLocalEvalConcurrency;
816 
817   if(di->batchEvalFlag && (nd > 1 || !ife || !ofe))
818     squawk("For batch evaluation, specification of an input_filter, output_filter,\n\t"
819         "or more than one analysis_drivers is disallowed");
820   if(di->batchEvalFlag && ec == 1) {
821     warn("batch option not required for evaluation concurrency == 1.\n\t"
822         "Sequential operation will be used");
823       di->batchEvalFlag = false;
824   }
825 
826   if(di->batchEvalFlag && ! (di->failAction == "abort" || di->failAction == "recover"))
827     squawk("For batch evaluation, only failure_capture abort and recover are supported");
828 
829   if (di->algebraicMappings == "" && nd == 0)
830     squawk("interface specification must provide algebraic_mappings,\n\t"
831 	   "analysis_drivers, or both");
832   if (nd > 0 && di->asynchLocalAnalysisConcurrency > nd) {
833     warn("analysis_concurrency specification greater than length of\n\t"
834 	 "analysis_drivers list.  Truncating analysis_concurrency to %d",nd);
835     di->asynchLocalAnalysisConcurrency = nd;
836   }
837   if (ec && ec < 2 && ac && ac < 2) {
838     warn("asynchronous option not required for evaluation and analysis.\n\t"
839 	 "Concurrency limited to %d and %d.\n\t"
840 	 "Synchronous operations will be used", ec, ac);
841     di->asynchFlag = false;
842   }
843 
844   // validate each of the analysis_drivers
845   if ( di->interfaceType == SYSTEM_INTERFACE ||
846        di->interfaceType == FORK_INTERFACE )
847     for(size_t i = 0; i < nd; ++i) {
848       // trim any leading whitespace from the driver, in place
849       boost::trim(analysis_drivers[i]);
850       check_driver(analysis_drivers[i], di->linkFiles, di->copyFiles);
851     }
852 
853   if (!di->workDir.empty()) {
854 
855 #if defined(_WIN32) || defined(_WIN64)
856     // Note: some Windows versions may support symlinks, if files and
857     // directories are managed separately.
858     if (!di->linkFiles.empty()) {
859       Cerr << "\nError: link_files not supported on Windows; use copy_files."
860 	   << std::endl;
861       ++nerr;
862     }
863 #endif
864 
865   // Check to make sure none of the linkFiles nor copyFiles are the
866   // same as the workDir (could combine into single loop with above)
867     if (WorkdirHelper::check_equivalent_dest(di->linkFiles, di->workDir) ||
868 	WorkdirHelper::check_equivalent_dest(di->copyFiles, di->workDir))
869       ++nerr;
870   }
871 
872   pDDBInstance->dataInterfaceList.push_back(*ii->di_handle);
873   delete ii->di_handle;
874   delete ii;
875 }
876 
877 /** returns 1 if not found, 2 if found, but not executable, 0 if found (no error) in case we want to return to error on not found... */
check_driver(const String & an_driver,const StringArray & link_files,const StringArray & copy_files)878 int NIDRProblemDescDB::check_driver(const String& an_driver,
879 				    const StringArray& link_files,
880 				    const StringArray& copy_files)
881 {
882   StringArray driver_and_args = WorkdirHelper::tokenize_driver(an_driver);
883   if (driver_and_args.size() == 0)
884     squawk("Empty analysis_driver string");
885   else {
886 
887     // the executable program name to check
888     const String& program_name = driver_and_args[0];
889 
890     if (program_name.empty())
891       squawk("Empty analysis_driver string");
892     else {
893 
894       // Drivers can be found in $PATH:WORKDIR(.):RUNDIR
895       // Therefore have to check PATH, link/copy files, PWD
896 
897       // check PATH and RUNDIR (since . is already on the search path)
898       bfs::path driver_found = WorkdirHelper::which(program_name);
899       if ( !driver_found.empty() )
900 	return 0;
901 
902       // check against link/copy files that will appear in workdir
903 
904       // TODO: if they are specified with ./subdirA/subdir1/foo.sh
905       // would have been found above which might be an error if subdir
906       // is not linked or copied file
907 
908       if (WorkdirHelper::find_driver(link_files, program_name))
909 	return 0;
910 
911       if (WorkdirHelper::find_driver(copy_files, program_name))
912 	return 0;
913 
914       const char* s = program_name.c_str();
915       warn("analysis driver \"%s\" %s", s, "not found");
916 
917       // BMA TODO: check whether the driver is executable and if not, return 2
918       // : "exists but is not executable");
919 
920     }
921 
922   }
923 
924   return 1;
925 }
926 
927 void NIDRProblemDescDB::
iface_str(const char * keyname,Values * val,void ** g,void * v)928 iface_str(const char *keyname, Values *val, void **g, void *v)
929 {
930   (*(Iface_Info**)g)->di->**(String DataInterfaceRep::**)v = *val->s;
931 }
932 
933 void NIDRProblemDescDB::
iface_str2D(const char * keyname,Values * val,void ** g,void * v)934 iface_str2D(const char *keyname, Values *val, void **g, void *v)
935 {
936   DataInterfaceRep *di = (*(Iface_Info**)g)->di;
937   String2DArray *sa2 = &(di->**(String2DArray DataInterfaceRep::**)v);
938   StringArray *sa;
939   const char **s = val->s;
940   size_t i, j, k, n, nc, nd;
941 
942   // This is for analysisComponents -- only String2DArray in a DataInterfaceRep
943 
944   nd = di->analysisDrivers.size();
945   n = val->n;
946   if (nd <= 0)
947     botch("num_drivers = %d in iface_str2D", (int)nd);
948   if (n % nd) {
949     squawk("number of analysis_components not evenly divisible "
950 	   "by number of analysis_drivers");
951     return;
952   }
953   nc = n / nd;
954   sa2->resize(nd);
955   for(i = k = 0; i < nd; i++) {
956     sa = &((*sa2)[i]);
957     sa->resize(nc);
958     for(j = 0; j < nc; ++j, ++k)
959       (*sa)[j] = s[k];
960   }
961 }
962 
963 void NIDRProblemDescDB::
iface_strL(const char * keyname,Values * val,void ** g,void * v)964 iface_strL(const char *keyname, Values *val, void **g, void *v)
965 {
966   StringArray *sa
967     = &((*(Iface_Info**)g)->di->**(StringArray DataInterfaceRep::**)v);
968   const char **s = val->s;
969   size_t i, n = val->n;
970 
971   sa->resize(n);
972   for(i = 0; i < n; i++)
973     (*sa)[i] = s[i];
974 }
975 
976 void NIDRProblemDescDB::
iface_type(const char * keyname,Values * val,void ** g,void * v)977 iface_type(const char *keyname, Values *val, void **g, void *v)
978 {
979   (*(Iface_Info**)g)->di->*((Iface_mp_type*)v)->sp = ((Iface_mp_type*)v)->type;
980 }
981 
982 void NIDRProblemDescDB::
method_Real(const char * keyname,Values * val,void ** g,void * v)983 method_Real(const char *keyname, Values *val, void **g, void *v)
984 {
985   (*(Meth_Info**)g)->dme->**(Real DataMethodRep::**)v = *val->r;
986 }
987 
988 void NIDRProblemDescDB::
method_Real01(const char * keyname,Values * val,void ** g,void * v)989 method_Real01(const char *keyname, Values *val, void **g, void *v)
990 {
991   Real t = *val->r;
992   if (t < 0. || t > 1.)
993     botch("%s must be in [0, 1]", keyname);
994   (*(Meth_Info**)g)->dme->**(Real DataMethodRep::**)v = t;
995 }
996 
997 void NIDRProblemDescDB::
method_RealDL(const char * keyname,Values * val,void ** g,void * v)998 method_RealDL(const char *keyname, Values *val, void **g, void *v)
999 {
1000   DataMethodRep *dm = (*(Meth_Info**)g)->dme;
1001   RealVector DataMethodRep::* sa = *(RealVector DataMethodRep::**)v;
1002   Real *r = val->r;
1003   size_t i, n = val->n;
1004 
1005   (dm->*sa).sizeUninitialized(n);
1006   for(i = 0; i < n; i++)
1007     (dm->*sa)[i] = r[i];
1008 }
1009 
1010 void NIDRProblemDescDB::
method_RealLlit(const char * keyname,Values * val,void ** g,void * v)1011 method_RealLlit(const char *keyname, Values *val, void **g, void *v)
1012 {
1013   DataMethodRep *dm = (*(Meth_Info**)g)->dme;
1014   Real *r;
1015   RealVector *rv = &(dm->*((Method_mp_litrv*)v)->rp);
1016   size_t i, n;
1017   dm->*((Method_mp_litrv*)v)->sp = ((Method_mp_litrv*)v)->lit;
1018   r = val->r;
1019   n = val->n;
1020   rv->resize(n);
1021   for(i = 0; i < n; i++)
1022     (*rv)[i] = r[i];
1023 }
1024 
1025 void NIDRProblemDescDB::
method_Realp(const char * keyname,Values * val,void ** g,void * v)1026 method_Realp(const char *keyname, Values *val, void **g, void *v)
1027 {
1028   Real t = *val->r;
1029   if (t <= 0.)
1030     botch("%s must be positive", keyname);
1031   (*(Meth_Info**)g)->dme->**(Real DataMethodRep::**)v = t;
1032 }
1033 
1034 void NIDRProblemDescDB::
method_Realz(const char * keyname,Values * val,void ** g,void * v)1035 method_Realz(const char *keyname, Values *val, void **g, void *v)
1036 {
1037   Real t = *val->r;
1038   if (t < 0.)
1039     botch("%s must be nonnegative", keyname);
1040   (*(Meth_Info**)g)->dme->**(Real DataMethodRep::**)v = t;
1041 }
1042 
1043 // MSE: This function just sets two values for one keyword.
1044 void NIDRProblemDescDB::
method_piecewise(const char * keyname,Values * val,void ** g,void * v)1045 method_piecewise(const char *keyname, Values *val, void **g, void *v)
1046 {
1047   DataMethodRep *dm = (*(Meth_Info**)g)->dme;
1048   dm->expansionType  = STD_UNIFORM_U;
1049   dm->piecewiseBasis = true;
1050 }
1051 
1052 void NIDRProblemDescDB::
method_false(const char * keyname,Values * val,void ** g,void * v)1053 method_false(const char *keyname, Values *val, void **g, void *v)
1054 {
1055   (*(Meth_Info**)g)->dme->**(bool DataMethodRep::**)v = false;
1056 }
1057 
1058 void NIDRProblemDescDB::
method_int(const char * keyname,Values * val,void ** g,void * v)1059 method_int(const char *keyname, Values *val, void **g, void *v)
1060 {
1061   (*(Meth_Info**)g)->dme->**(int DataMethodRep::**)v = *val->i;
1062 }
1063 
1064 void NIDRProblemDescDB::
method_ivec(const char * keyname,Values * val,void ** g,void * v)1065 method_ivec(const char *keyname, Values *val, void **g, void *v)
1066 {
1067   DataMethodRep *dm = (*(Meth_Info**)g)->dme;
1068   int *z = val->i;
1069   IntVector *iv = &(dm->**(IntVector DataMethodRep::**)v);
1070   size_t i, n = val->n;
1071 
1072   iv->resize(n);
1073   for(i = 0; i < n; i++)
1074     (*iv)[i] = z[i];
1075 }
1076 
1077 void NIDRProblemDescDB::
method_ilit2(const char * keyname,Values * val,void ** g,void * v)1078 method_ilit2(const char *keyname, Values *val, void **g, void *v)
1079 {
1080   DataMethodRep *dm = (*(Meth_Info**)g)->dme;
1081   dm->*((Method_mp_ilit2*)v)->sp = ((Method_mp_ilit2*)v)->lit;
1082   dm->*((Method_mp_ilit2*)v)->ip = *val->i;
1083 }
1084 
1085 void NIDRProblemDescDB::
method_ilit2p(const char * keyname,Values * val,void ** g,void * v)1086 method_ilit2p(const char *keyname, Values *val, void **g, void *v)
1087 {
1088   DataMethodRep *dm = (*(Meth_Info**)g)->dme;
1089   dm->*((Method_mp_ilit2z*)v)->sp = ((Method_mp_ilit2z*)v)->lit;
1090   if ((dm->*((Method_mp_ilit2z*)v)->ip = *val->i) <= 0)
1091     botch("%s must be positive", keyname);
1092 }
1093 
1094 void NIDRProblemDescDB::
method_lit(const char * keyname,Values * val,void ** g,void * v)1095 method_lit(const char *keyname, Values *val, void **g, void *v)
1096 {
1097   (*(Meth_Info**)g)->dme->*((Method_mp_lit*)v)->sp = ((Method_mp_lit*)v)->lit;
1098 }
1099 
1100 void NIDRProblemDescDB::
method_litc(const char * keyname,Values * val,void ** g,void * v)1101 method_litc(const char *keyname, Values *val, void **g, void *v)
1102 {
1103   DataMethodRep *dm = (*(Meth_Info**)g)->dme;
1104   if (dm->*((Method_mp_litc*)v)->rp > 0.)
1105     dm->*((Method_mp_litc*)v)->sp = ((Method_mp_litc*)v)->lit;
1106 }
1107 
1108 void NIDRProblemDescDB::
method_litp(const char * keyname,Values * val,void ** g,void * v)1109 method_litp(const char *keyname, Values *val, void **g, void *v)
1110 {
1111   DataMethodRep *dm = (*(Meth_Info**)g)->dme;
1112   Real t = *val->r;
1113   if (t <= 0.)
1114     botch("%s must be positive",keyname);
1115   dm->*((Method_mp_litc*)v)->sp = ((Method_mp_litc*)v)->lit;
1116   dm->*((Method_mp_litc*)v)->rp = t;
1117 }
1118 
1119 void NIDRProblemDescDB::
method_litz(const char * keyname,Values * val,void ** g,void * v)1120 method_litz(const char *keyname, Values *val, void **g, void *v)
1121 {
1122   DataMethodRep *dm = (*(Meth_Info**)g)->dme;
1123   Real t = *val->r;
1124   if (t < 0.)
1125     botch("%s must be nonnegative",keyname);
1126   if ((dm->*((Method_mp_litc*)v)->rp = t) == 0.)
1127     dm->*((Method_mp_litc*)v)->sp = ((Method_mp_litc*)v)->lit;
1128 }
1129 
1130 void NIDRProblemDescDB::
method_order(const char * keyname,Values * val,void ** g,void * v)1131 method_order(const char *keyname, Values *val, void **g, void *v)
1132 {
1133   (*(Meth_Info**)g)->dme->*((Method_mp_ord*)v)->sp = ((Method_mp_ord*)v)->ord;
1134 }
1135 
1136 void NIDRProblemDescDB::
method_num_resplevs(const char * keyname,Values * val,void ** g,void * v)1137 method_num_resplevs(const char *keyname, Values *val, void **g, void *v)
1138 {
1139   DataMethodRep *dm = (*(Meth_Info**)g)->dme;
1140   Real *r;
1141   RealVectorArray *rva = &(dm->**(RealVectorArray DataMethodRep::**)v);
1142   RealVector *ra = &(*rva)[0];
1143   int m, *z = val->i;
1144   size_t i, i1, j, je, k, n;
1145 
1146   n = val->n;
1147   for(i = k = 0; i < n; ++i)
1148     k += z[i];
1149   if (ra->length() != (int)k)
1150     botch("number of %s = %u does not match\n"
1151 	  "%s specification of %u response levels",
1152 	  keyname+4, (Uint)ra->length(), keyname, (Uint)k);
1153   r = new Real[k];
1154   for(i = 0; i < k; i++)
1155     r[i] = (*ra)[i];
1156   (*rva).resize(n);
1157   for(i = j = je = 0; i < n; i++) {
1158     m = z[i];
1159     (*rva)[i].resize(m);
1160     ra = &(*rva)[i];
1161     for(i1 = 0, je += m; j < je; ++i1, ++j)
1162       (*ra)[i1] = r[j];
1163   }
1164   delete[] r;
1165 }
1166 
1167 void NIDRProblemDescDB::
method_sizet(const char * keyname,Values * val,void ** g,void * v)1168 method_sizet(const char *keyname, Values *val, void **g, void *v)
1169 {
1170   int n = *val->i; // test value as int, prior to storage as size_t
1171 #ifdef REDUNDANT_INT_CHECKS
1172   if (n < 0) /* now handled by INTEGER >= 0 in the .nspec file */
1173     botch("%s must be non-negative", keyname);
1174 #endif
1175   (*(Meth_Info**)g)->dme->**(size_t DataMethodRep::**)v = n;
1176 }
1177 
1178 void NIDRProblemDescDB::
method_resplevs(const char * keyname,Values * val,void ** g,void * v)1179 method_resplevs(const char *keyname, Values *val, void **g, void *v)
1180 {
1181   DataMethodRep *dm = (*(Meth_Info**)g)->dme;
1182   Real *r = val->r;
1183   RealVector *ra;
1184   RealVectorArray *rva = &(dm->**(RealVectorArray DataMethodRep::**)v);
1185   size_t i, n = val->n;
1186 
1187   (*rva).resize(1);
1188   ra = &(*rva)[0];
1189   ra->resize(n);
1190   for(i = 0; i < n; ++i)
1191     (*ra)[i] = r[i];
1192 }
1193 
1194 void NIDRProblemDescDB::
method_resplevs01(const char * keyname,Values * val,void ** g,void * v)1195 method_resplevs01(const char *keyname, Values *val, void **g, void *v)
1196 {
1197   DataMethodRep *dm = (*(Meth_Info**)g)->dme;
1198   Real *r = val->r, t;
1199   RealVector *ra;
1200   RealVectorArray *rva = &(dm->**(RealVectorArray DataMethodRep::**)v);
1201   size_t i, n = val->n;
1202 
1203   (*rva).resize(1);
1204   ra = &(*rva)[0];
1205   ra->resize(n);
1206   for(i = 0; i < n; ++i) {
1207     (*ra)[i] = t = r[i];
1208     if (t < 0. || t > 1.)
1209       botch("%s must be between 0 and 1", keyname);
1210   }
1211 }
1212 
1213 void NIDRProblemDescDB::
method_shint(const char * keyname,Values * val,void ** g,void * v)1214 method_shint(const char *keyname, Values *val, void **g, void *v)
1215 {
1216   (*(Meth_Info**)g)->dme->**(short DataMethodRep::**)v = *val->i;
1217 }
1218 
1219 void NIDRProblemDescDB::
method_ushint(const char * keyname,Values * val,void ** g,void * v)1220 method_ushint(const char *keyname, Values *val, void **g, void *v)
1221 {
1222   (*(Meth_Info**)g)->dme->**(unsigned short DataMethodRep::**)v = *val->i;
1223 }
1224 
1225 void NIDRProblemDescDB::
method_usharray(const char * keyname,Values * val,void ** g,void * v)1226 method_usharray(const char *keyname, Values *val, void **g, void *v)
1227 {
1228   UShortArray *usa
1229     = &((*(Meth_Info**)g)->dme->**(UShortArray DataMethodRep::**)v);
1230   int *z = val->i;
1231   size_t i, n = val->n;
1232 
1233   usa->resize(n);
1234   for (i=0; i<n; ++i)
1235     if (z[i] >= 0)
1236       (*usa)[i] = z[i];
1237     else
1238       botch("%s must have non-negative values", keyname);
1239 }
1240 
1241 void NIDRProblemDescDB::
method_szarray(const char * keyname,Values * val,void ** g,void * v)1242 method_szarray(const char *keyname, Values *val, void **g, void *v)
1243 {
1244   SizetArray *sza
1245     = &((*(Meth_Info**)g)->dme->**(SizetArray DataMethodRep::**)v);
1246   int *z = val->i;
1247   size_t i, n = val->n;
1248 
1249   sza->resize(n);
1250   for (i=0; i<n; ++i)
1251     if (z[i] >= 0)
1252       (*sza)[i] = z[i];
1253     else
1254       botch("%s must have non-negative values", keyname);
1255 }
1256 
1257 void NIDRProblemDescDB::
method_slit2(const char * keyname,Values * val,void ** g,void * v)1258 method_slit2(const char *keyname, Values *val, void **g, void *v)
1259 {
1260   DataMethodRep *dm = (*(Meth_Info**)g)->dme;
1261 
1262   dm->*((Method_mp_slit2*)v)->sp  = ((Method_mp_slit2*)v)->lit;
1263   dm->*((Method_mp_slit2*)v)->sp2 = *val->s;
1264 }
1265 
1266 void NIDRProblemDescDB::
method_utype_lit(const char * keyname,Values * val,void ** g,void * v)1267 method_utype_lit(const char *keyname, Values *val, void **g, void *v)
1268 {
1269   DataMethodRep *dm = (*(Meth_Info**)g)->dme;
1270 
1271   (*(Meth_Info**)g)->dme->*((Method_mp_utype*)v)->ip
1272     = ((Method_mp_utype*)v)->utype;
1273   dm->*((Method_mp_utype_lit*)v)->sp = *val->s;
1274 }
1275 
1276 void NIDRProblemDescDB::
method_start(const char * keyname,Values * val,void ** g,void * v)1277 method_start(const char *keyname, Values *val, void **g, void *v)
1278 {
1279   Meth_Info *mi;
1280 
1281   if (!(mi = new Meth_Info))
1282   Botch:		botch("new failure in method_start");
1283   if (!(mi->dme0 = new DataMethod))
1284     goto Botch;
1285   mi->dme = mi->dme0->dataMethodRep.get();
1286   *g = (void*)mi;
1287 }
1288 
1289 static void
scale_chk(StringArray & ST,RealVector & S,const char * what,const char ** univ)1290 scale_chk(StringArray &ST, RealVector &S, const char *what, const char **univ)
1291 {
1292   const char *s, **u;
1293   size_t i, n, nbad, vseen;
1294 
1295   n = ST.size();
1296   for(i = nbad = vseen = 0; i < n; ++i) {
1297     s = ST[i].data();
1298     if (!strcmp(s,"value")) {
1299       ++vseen;
1300       goto break2;
1301     }
1302     for(u = univ; *u; ++u)
1303       if (!strcmp(s,*u)) {
1304 	goto break2;
1305       }
1306     NIDRProblemDescDB::squawk("\"%s\" cannot appear in %s_scale_types",
1307 			      s, what);
1308     ++nbad;
1309   break2:	;
1310   }
1311   if (vseen && S.length() <= 0)
1312     NIDRProblemDescDB::squawk(
1313       "\"value\" in %s_scale_types requires at least one value for %s_scales",
1314       what, what);
1315 }
1316 
1317 // scale_types beyond value to allow in some contexts
1318 static const char *auto_log_scaletypes[] = { "auto", "log", "none", 0 };
1319 
1320 void NIDRProblemDescDB::
method_stop(const char * keyname,Values * val,void ** g,void * v)1321 method_stop(const char *keyname, Values *val, void **g, void *v)
1322 {
1323   Meth_Info *mi = *(Meth_Info**)g;
1324   //DataMethodRep *dm = mi->dme;
1325   // ... any checks ...
1326   pDDBInstance->dataMethodList.push_back(*mi->dme0);
1327   delete mi->dme0;
1328   delete mi;
1329 }
1330 
1331 void NIDRProblemDescDB::
method_str(const char * keyname,Values * val,void ** g,void * v)1332 method_str(const char *keyname, Values *val, void **g, void *v)
1333 {
1334   (*(Meth_Info**)g)->dme->**(String DataMethodRep::**)v = *val->s;
1335 }
1336 
1337 void NIDRProblemDescDB::
method_strL(const char * keyname,Values * val,void ** g,void * v)1338 method_strL(const char *keyname, Values *val, void **g, void *v)
1339 {
1340   StringArray *sa = &((*(Meth_Info**)g)->dme->**(StringArray DataMethodRep::**)v);
1341   const char **s = val->s;
1342   size_t i, n = val->n;
1343 
1344   sa->resize(n);
1345   for(i = 0; i < n; i++)
1346     (*sa)[i] = s[i];
1347 }
1348 
1349 void NIDRProblemDescDB::
method_true(const char * keyname,Values * val,void ** g,void * v)1350 method_true(const char *keyname, Values *val, void **g, void *v)
1351 {
1352   (*(Meth_Info**)g)->dme->**(bool DataMethodRep::**)v = true;
1353 }
1354 
1355 void NIDRProblemDescDB::
method_type(const char * keyname,Values * val,void ** g,void * v)1356 method_type(const char *keyname, Values *val, void **g, void *v)
1357 {
1358   (*(Meth_Info**)g)->dme->*((Method_mp_type*)v)->ip
1359     = ((Method_mp_type*)v)->type;
1360 }
1361 
1362 void NIDRProblemDescDB::
method_utype(const char * keyname,Values * val,void ** g,void * v)1363 method_utype(const char *keyname, Values *val, void **g, void *v)
1364 {
1365   (*(Meth_Info**)g)->dme->*((Method_mp_utype*)v)->ip
1366     = ((Method_mp_utype*)v)->utype;
1367 }
1368 
1369 void NIDRProblemDescDB::
method_augment_utype(const char * keyname,Values * val,void ** g,void * v)1370 method_augment_utype(const char *keyname, Values *val, void **g, void *v)
1371 {
1372   (*(Meth_Info**)g)->dme->*((Method_mp_utype*)v)->ip
1373     |= ((Method_mp_utype*)v)->utype;
1374 }
1375 
1376 void NIDRProblemDescDB::
model_Real(const char * keyname,Values * val,void ** g,void * v)1377 model_Real(const char *keyname, Values *val, void **g, void *v)
1378 {
1379   (*(Mod_Info**)g)->dmo->**(Real DataModelRep::**)v = *val->r;
1380 }
1381 
1382 void NIDRProblemDescDB::
model_RealDL(const char * keyname,Values * val,void ** g,void * v)1383 model_RealDL(const char *keyname, Values *val, void **g, void *v)
1384 {
1385   Real *r = val->r;
1386   RealVector *rdv = &((*(Mod_Info**)g)->dmo->**(RealVector DataModelRep::**)v);
1387   size_t i, n = val->n;
1388 
1389   rdv->sizeUninitialized(n);
1390   for(i = 0; i < n; i++)
1391     (*rdv)[i] = r[i];
1392 }
1393 
1394 void NIDRProblemDescDB::
model_ivec(const char * keyname,Values * val,void ** g,void * v)1395 model_ivec(const char *keyname, Values *val, void **g, void *v)
1396 {
1397   DataModelRep *dm = (*(Mod_Info**)g)->dmo;
1398   IntVector *iv = &(dm->**(IntVector DataModelRep::**)v);
1399   size_t i, n = val->n;
1400   iv->sizeUninitialized(n);
1401 
1402   int *z = val->i;
1403   for(i = 0; i < n; i++)
1404     (*iv)[i] = z[i];
1405 }
1406 
1407 void NIDRProblemDescDB::
model_false(const char * keyname,Values * val,void ** g,void * v)1408 model_false(const char *keyname, Values *val, void **g, void *v)
1409 {
1410   (*(Mod_Info**)g)->dmo->**(bool DataModelRep::**)v = false;
1411 }
1412 
1413 void NIDRProblemDescDB::
model_int(const char * keyname,Values * val,void ** g,void * v)1414 model_int(const char *keyname, Values *val, void **g, void *v)
1415 {
1416   (*(Mod_Info**)g)->dmo->**(int DataModelRep::**)v = *val->i;
1417 }
1418 
1419 void NIDRProblemDescDB::
model_intsetm1(const char * keyname,Values * val,void ** g,void * v)1420 model_intsetm1(const char *keyname, Values *val, void **g, void *v)
1421 {
1422   IntSet *is = &((*(Mod_Info**)g)->dmo->**(IntSet DataModelRep::**)v);
1423   int *z = val->i;
1424   size_t i, n = val->n;
1425 
1426   for(i = 0; i < n; i++)
1427     is->insert(z[i] - 1); // model converts ids -> indices
1428 }
1429 
1430 void NIDRProblemDescDB::
model_lit(const char * keyname,Values * val,void ** g,void * v)1431 model_lit(const char *keyname, Values *val, void **g, void *v)
1432 {
1433   (*(Mod_Info**)g)->dmo->*((Model_mp_lit*)v)->sp = ((Model_mp_lit*)v)->lit;
1434 }
1435 
1436 void NIDRProblemDescDB::
model_order(const char * keyname,Values * val,void ** g,void * v)1437 model_order(const char *keyname, Values *val, void **g, void *v)
1438 {
1439   (*(Mod_Info**)g)->dmo->*((Model_mp_ord*)v)->sp = ((Model_mp_ord*)v)->ord;
1440 }
1441 
1442 void NIDRProblemDescDB::
model_type(const char * keyname,Values * val,void ** g,void * v)1443 model_type(const char *keyname, Values *val, void **g, void *v)
1444 {
1445   (*(Mod_Info**)g)->dmo->*((Model_mp_type*)v)->sp = ((Model_mp_type*)v)->type;
1446 }
1447 
1448 void NIDRProblemDescDB::
model_ushint(const char * keyname,Values * val,void ** g,void * v)1449 model_ushint(const char *keyname, Values *val, void **g, void *v)
1450 {
1451   (*(Mod_Info**)g)->dmo->**(unsigned short DataModelRep::**)v = *val->i;
1452 }
1453 
1454 void NIDRProblemDescDB::
model_usharray(const char * keyname,Values * val,void ** g,void * v)1455 model_usharray(const char *keyname, Values *val, void **g, void *v)
1456 {
1457   UShortArray *usa
1458     = &((*(Mod_Info**)g)->dmo->**(UShortArray DataModelRep::**)v);
1459   int *z = val->i;
1460   size_t i, n = val->n;
1461 
1462   usa->resize(n);
1463   for (i=0; i<n; ++i)
1464     if (z[i] >= 0)
1465       (*usa)[i] = z[i];
1466     else
1467       botch("%s must have non-negative values", keyname);
1468 }
1469 
1470 void NIDRProblemDescDB::
model_utype(const char * keyname,Values * val,void ** g,void * v)1471 model_utype(const char *keyname, Values *val, void **g, void *v)
1472 {
1473   (*(Mod_Info**)g)->dmo->*((Model_mp_utype*)v)->sp =
1474     ((Model_mp_utype*)v)->utype;
1475 }
1476 
1477 void NIDRProblemDescDB::
model_augment_utype(const char * keyname,Values * val,void ** g,void * v)1478 model_augment_utype(const char *keyname, Values *val, void **g, void *v)
1479 {
1480   (*(Mod_Info**)g)->dmo->*((Model_mp_utype*)v)->sp |=
1481     ((Model_mp_utype*)v)->utype;
1482 }
1483 
1484 void NIDRProblemDescDB::
model_shint(const char * keyname,Values * val,void ** g,void * v)1485 model_shint(const char *keyname, Values *val, void **g, void *v)
1486 {
1487   (*(Mod_Info**)g)->dmo->**(short DataModelRep::**)v = (short)*val->i;
1488 }
1489 
1490 
1491 void NIDRProblemDescDB::
model_sizet(const char * keyname,Values * val,void ** g,void * v)1492 model_sizet(const char *keyname, Values *val, void **g, void *v)
1493 {
1494   int n = *val->i; // test value as int, prior to storage as size_t
1495 #ifdef REDUNDANT_INT_CHECKS
1496   if (n < 0) /* now handled by INTEGER >= 0 in the .nspec file */
1497     botch("%s must be non-negative", keyname);
1498 #endif
1499   (*(Mod_Info**)g)->dmo->**(size_t DataModelRep::**)v = n;
1500 }
1501 
1502 
1503 void NIDRProblemDescDB::
model_start(const char * keyname,Values * val,void ** g,void * v)1504 model_start(const char *keyname, Values *val, void **g, void *v)
1505 {
1506   DataModelRep *dm;
1507   Mod_Info *mi;
1508 
1509   if (!(mi = new Mod_Info))
1510   Botch:		botch("new failure in model_start");
1511   if (!(mi->dmo0 = new DataModel))
1512     goto Botch;
1513   dm = mi->dmo = mi->dmo0->dataModelRep.get();
1514   *g = (void*)mi;
1515 }
1516 
1517 void NIDRProblemDescDB::
model_stop(const char * keyname,Values * val,void ** g,void * v)1518 model_stop(const char *keyname, Values *val, void **g, void *v)
1519 {
1520   Mod_Info *mi = *(Mod_Info**)g;
1521   pDDBInstance->dataModelList.push_back(*mi->dmo0);
1522   delete mi->dmo0;
1523   delete mi;
1524 }
1525 
1526 void NIDRProblemDescDB::
model_str(const char * keyname,Values * val,void ** g,void * v)1527 model_str(const char *keyname, Values *val, void **g, void *v)
1528 {
1529   (*(Mod_Info**)g)->dmo->**(String DataModelRep::**)v = *val->s;
1530 }
1531 
1532 
1533 void NIDRProblemDescDB::
model_strL(const char * keyname,Values * val,void ** g,void * v)1534 model_strL(const char *keyname, Values *val, void **g, void *v)
1535 {
1536   DataModelRep *dm = (*(Mod_Info**)g)->dmo;
1537   StringArray DataModelRep::* sa = *(StringArray DataModelRep::**)v;
1538   const char **s = val->s;
1539   size_t i, n = val->n;
1540 
1541   (dm->*sa).resize(n);
1542   for(i = 0; i < n; i++)
1543     (dm->*sa)[i] = s[i];
1544 }
1545 
1546 void NIDRProblemDescDB::
model_true(const char * keyname,Values * val,void ** g,void * v)1547 model_true(const char *keyname, Values *val, void **g, void *v)
1548 {
1549   (*(Mod_Info**)g)->dmo->**(bool DataModelRep::**)v = true;
1550 }
1551 
1552 void NIDRProblemDescDB::
resp_RealDL(const char * keyname,Values * val,void ** g,void * v)1553 resp_RealDL(const char *keyname, Values *val, void **g, void *v)
1554 {
1555   Real *r = val->r;
1556   RealVector *rv = &((*(Resp_Info**)g)->dr->**(RealVector DataResponsesRep::**)v);
1557   size_t i, n = val->n;
1558 
1559   rv->sizeUninitialized(n);
1560   for(i = 0; i < n; i++)
1561     (*rv)[i] = r[i];
1562 }
1563 
1564 void NIDRProblemDescDB::
resp_RealL(const char * keyname,Values * val,void ** g,void * v)1565 resp_RealL(const char *keyname, Values *val, void **g, void *v)
1566 {
1567   NIDRProblemDescDB::resp_RealDL(keyname, val, g, v);
1568 }
1569 
1570 void NIDRProblemDescDB::
resp_intset(const char * keyname,Values * val,void ** g,void * v)1571 resp_intset(const char *keyname, Values *val, void **g, void *v)
1572 {
1573   IntSet *is = &((*(Resp_Info**)g)->dr->**(IntSet DataResponsesRep::**)v);
1574   int *z = val->i;
1575   size_t i, n = val->n;
1576 
1577   for (i=0; i<n; ++i)
1578     is->insert(z[i]);
1579 }
1580 
1581 void NIDRProblemDescDB::
resp_ivec(const char * keyname,Values * val,void ** g,void * v)1582 resp_ivec(const char *keyname, Values *val, void **g, void *v)
1583 {
1584   DataResponsesRep *dr = (*(Resp_Info**)g)->dr;
1585   IntVector *iv = &(dr->**(IntVector DataResponsesRep::**)v);
1586   size_t i, n = val->n;
1587   iv->sizeUninitialized(n);
1588 
1589   int *z = val->i;
1590   for(i = 0; i < n; i++)
1591     (*iv)[i] = z[i];
1592 }
1593 
1594 void NIDRProblemDescDB::
resp_lit(const char * keyname,Values * val,void ** g,void * v)1595 resp_lit(const char *keyname, Values *val, void **g, void *v)
1596 {
1597   (*(Resp_Info**)g)->dr->*((Resp_mp_lit*)v)->sp = ((Resp_mp_lit*)v)->lit;
1598 }
1599 
1600 void NIDRProblemDescDB::
resp_utype(const char * keyname,Values * val,void ** g,void * v)1601 resp_utype(const char *keyname, Values *val, void **g, void *v)
1602 {
1603   (*(Resp_Info**)g)->dr->*((Resp_mp_utype*)v)->sp
1604     = ((Resp_mp_utype*)v)->utype;
1605 }
1606 
1607 void NIDRProblemDescDB::
resp_augment_utype(const char * keyname,Values * val,void ** g,void * v)1608 resp_augment_utype(const char *keyname, Values *val, void **g, void *v)
1609 {
1610   (*(Resp_Info**)g)->dr->*((Resp_mp_utype*)v)->sp
1611     |= ((Resp_mp_utype*)v)->utype;
1612 }
1613 
1614 
1615 void NIDRProblemDescDB::
resp_sizet(const char * keyname,Values * val,void ** g,void * v)1616 resp_sizet(const char *keyname, Values *val, void **g, void *v)
1617 {
1618   int n = *val->i; // test value as int, prior to storage as size_t
1619 #ifdef REDUNDANT_INT_CHECKS
1620   if (n < 0) /* now handled by INTEGER >= 0 in the .nspec file */
1621     botch("%s must be non-negative", keyname);
1622 #endif
1623   (*(Resp_Info**)g)->dr->**(size_t DataResponsesRep::**)v = n;
1624 }
1625 
1626 void NIDRProblemDescDB::
resp_start(const char * keyname,Values * val,void ** g,void * v)1627 resp_start(const char *keyname, Values *val, void **g, void *v)
1628 {
1629   Resp_Info *ri;
1630 
1631   if (!(ri = new Resp_Info))
1632   Botch:		botch("new failure in resp_start");
1633   if (!(ri->dr0 = new DataResponses))
1634     goto Botch;
1635   ri->dr = ri->dr0->dataRespRep.get();
1636   *g = (void*)ri;
1637 }
1638 
1639 static void
BuildLabels(StringArray * sa,size_t nsa,size_t n1,size_t n2,const char * stub)1640 BuildLabels(StringArray *sa, size_t nsa, size_t n1, size_t n2, const char *stub)
1641 {
1642   char buf[64];
1643   size_t i, n0;
1644   if (nsa)
1645     sa->resize(nsa);
1646   i = n0 = n1;
1647   while(n1 < n2) {
1648     std::sprintf(buf, "%s%lu", stub, (UL)(++n1 - n0));
1649     (*sa)[i++] = buf;
1650   }
1651 }
1652 
mixed_check(IntSet * S,int n,IntArray * iv,const char * what)1653 static int mixed_check(IntSet *S, int n, IntArray *iv, const char *what)
1654 {
1655   int nbad, j;
1656 
1657   nbad = 0;
1658   for(ISCIter it = S->begin(), ite = S->end(); it != ite; ++it) {
1659     j = *it;
1660     if (j < 1 || j > n) {
1661       if (!nbad++)
1662 	Squawk("%s values must be between 1 and %d", what, n);
1663     }
1664     else {
1665       ++(iv->operator[](j-1));
1666     }
1667   }
1668   return nbad;
1669 }
1670 
1671 static void
mixed_check2(size_t n,IntArray * iv,const char * what)1672 mixed_check2(size_t n, IntArray *iv, const char *what)
1673 {
1674   int j;
1675   size_t i;
1676 
1677   for(i = 0; i < n; ) {
1678     j = (*iv)[i++];
1679     if (j == 0)
1680       Squawk("Function %lu missing from mixed %s lists", (UL)i);
1681     else if (j > 1)
1682       Squawk("Function %lu replicated in mixed %s lists", (UL)i);
1683   }
1684 }
1685 
1686 void NIDRProblemDescDB::
resp_stop(const char * keyname,Values * val,void ** g,void * v)1687 resp_stop(const char *keyname, Values *val, void **g, void *v)
1688 {
1689   size_t k, n;
1690   static const char *log_scaletypes[] = { "log", 0 };
1691   Resp_Info *ri = *(Resp_Info**)g;
1692   DataResponsesRep *dr = ri->dr;
1693   scale_chk(dr->primaryRespFnScaleTypes, dr->primaryRespFnScales,
1694 	    dr->numLeastSqTerms ? "least_squares_term" : "objective_function", log_scaletypes);
1695   scale_chk(dr->nonlinearIneqScaleTypes, dr->nonlinearIneqScales,
1696 	    "nonlinear_inequality", auto_log_scaletypes);
1697   scale_chk(dr->nonlinearEqScaleTypes, dr->nonlinearEqScales,
1698 	    "nonlinear_equality", auto_log_scaletypes);
1699   // The following check was relevant prior to refactoring the way transformations
1700   // are applied (now in a layered manner)
1701   // if ( dr->primaryRespFnWeights.length() > 0 && dr->varianceType.size() > 0 ) {
1702   //   squawk("Specify calibration weights or experimental errors, not both.");
1703   // }
1704   if ((n = dr->responseLabels.size()) > 0) {
1705     if (!(k = dr->numResponseFunctions)) {
1706       if (!(k = dr->numObjectiveFunctions))
1707 	k = dr->numLeastSqTerms;
1708       k += dr->numNonlinearIneqConstraints + dr->numNonlinearEqConstraints;
1709     }
1710     if (n != k)
1711       squawk("Expected %ld response descriptors but found %ld",
1712 	     (long)k, (long) n);
1713   }
1714   pDDBInstance->dataResponsesList.push_back(*ri->dr0);
1715   delete ri->dr0;
1716   delete ri;
1717 }
1718 
1719 
1720 void NIDRProblemDescDB::
check_descriptors_for_repeats(const StringArray & labels)1721 check_descriptors_for_repeats(const StringArray& labels) {
1722   StringArray sorted(labels.size());
1723   std::copy(labels.begin(), labels.end(), sorted.begin());
1724   // error if descriptors are repeated
1725   std::sort(sorted.begin(), sorted.end());
1726   StringArray::iterator sorted_i =
1727     std::adjacent_find(sorted.begin(), sorted.end());
1728   if(sorted_i != sorted.end())
1729     Squawk("Repeated descriptors (\"%s\") are not permitted",sorted_i->c_str());
1730 }
1731 
1732 void NIDRProblemDescDB::
check_descriptors_for_repeats(const StringArray & cd_labels,const StringArray & ddr_labels,const StringArray & ddsi_labels,const StringArray & ddss_labels,const StringArray & ddsr_labels,const StringArray & cs_labels,const StringArray & dsr_labels,const StringArray & dssi_labels,const StringArray & dsss_labels,const StringArray & dssr_labels,const StringArray & cau_labels,const StringArray & diau_labels,const StringArray & dsau_labels,const StringArray & drau_labels,const StringArray & ceu_labels,const StringArray & dieu_labels,const StringArray & dseu_labels,const StringArray & dreu_labels)1733 check_descriptors_for_repeats(const StringArray &cd_labels,
1734                                const StringArray &ddr_labels,
1735                                const StringArray &ddsi_labels,
1736                                const StringArray &ddss_labels,
1737                                const StringArray &ddsr_labels,
1738                                const StringArray &cs_labels,
1739                                const StringArray &dsr_labels,
1740                                const StringArray &dssi_labels,
1741                                const StringArray &dsss_labels,
1742                                const StringArray &dssr_labels,
1743                                const StringArray &cau_labels,
1744                                const StringArray &diau_labels,
1745                                const StringArray &dsau_labels,
1746                                const StringArray &drau_labels,
1747                                const StringArray &ceu_labels,
1748                                const StringArray &dieu_labels,
1749                                const StringArray &dseu_labels,
1750                                const StringArray &dreu_labels) {
1751   StringArray sorted;
1752   std::copy(cd_labels.begin(),   cd_labels.end(),   std::back_inserter(sorted));
1753   std::copy(ddr_labels.begin(),  ddr_labels.end(),  std::back_inserter(sorted));
1754   std::copy(ddsi_labels.begin(), ddsi_labels.end(), std::back_inserter(sorted));
1755   std::copy(ddss_labels.begin(), ddss_labels.end(), std::back_inserter(sorted));
1756   std::copy(ddsr_labels.begin(), ddsr_labels.end(), std::back_inserter(sorted));
1757   std::copy(cs_labels.begin(),   cs_labels.end(),   std::back_inserter(sorted));
1758   std::copy(dsr_labels.begin(),  dsr_labels.end(),  std::back_inserter(sorted));
1759   std::copy(dssi_labels.begin(), dssi_labels.end(), std::back_inserter(sorted));
1760   std::copy(dsss_labels.begin(), dsss_labels.end(), std::back_inserter(sorted));
1761   std::copy(dssr_labels.begin(), dssr_labels.end(), std::back_inserter(sorted));
1762   std::copy(cau_labels.begin(),  cau_labels.end(),  std::back_inserter(sorted));
1763   std::copy(diau_labels.begin(), diau_labels.end(), std::back_inserter(sorted));
1764   std::copy(dsau_labels.begin(), dsau_labels.end(), std::back_inserter(sorted));
1765   std::copy(drau_labels.begin(), drau_labels.end(), std::back_inserter(sorted));
1766   std::copy(ceu_labels.begin(),  ceu_labels.end(),  std::back_inserter(sorted));
1767   std::copy(dieu_labels.begin(), dieu_labels.end(), std::back_inserter(sorted));
1768   std::copy(dseu_labels.begin(), dseu_labels.end(), std::back_inserter(sorted));
1769   std::copy(dreu_labels.begin(), dreu_labels.end(), std::back_inserter(sorted));
1770   std::sort(sorted.begin(), sorted.end());
1771   StringArray::iterator sorted_i =
1772     std::adjacent_find(sorted.begin(), sorted.end());
1773   if(sorted_i != sorted.end())
1774     Squawk("Repeated descriptors (\"%s\") are not permitted",sorted_i->c_str());
1775 }
1776 
1777 
1778 void NIDRProblemDescDB::
check_responses(std::list<DataResponses> * drl)1779 check_responses(std::list<DataResponses>* drl)
1780 {
1781   // TO DO: move Schk from below?
1782 
1783   // validate descriptors. The string arrays are empty unless the user
1784   // explicitly set descriptors.
1785   std::list<DataResponses>::iterator It = drl->begin(), Ite = drl->end();
1786   for(; It != Ite; ++It) {
1787     const DataResponsesRep& drr = *It->data_rep();
1788     check_descriptor_format(drr.responseLabels);
1789     check_descriptors_for_repeats(drr.responseLabels);
1790   }
1791 }
1792 
1793 void NIDRProblemDescDB::
make_response_defaults(std::list<DataResponses> * drl)1794 make_response_defaults(std::list<DataResponses>* drl)
1795 {
1796   IntArray *iv;
1797   StringArray *rl;
1798   int ni;
1799   size_t i, n, n1, nf, no, nrl;
1800   struct RespStr_chk {
1801     const char *what;
1802     size_t DataResponsesRep::* n;
1803     StringArray DataResponsesRep::* sa;
1804   } *sc;
1805   struct RespDVec_chk {
1806     const char *what;
1807     size_t DataResponsesRep::* n;
1808     RealVector DataResponsesRep::* rv;
1809   } *rdvc;
1810 #define Schk(a,b,c) {#a,&DataResponsesRep::b, &DataResponsesRep::c}
1811   static RespStr_chk Str_chk[] = {  // for StringArray checking
1812     Schk(least_squares_term_scale_types,numLeastSqTerms,primaryRespFnScaleTypes),
1813     Schk(nonlinear_equality_scale_types,numNonlinearEqConstraints,nonlinearEqScaleTypes),
1814     Schk(nonlinear_inequality_scale_types,numNonlinearIneqConstraints,nonlinearIneqScaleTypes),
1815     Schk(objective_function_scale_types,numObjectiveFunctions,primaryRespFnScaleTypes)
1816   };
1817   static RespDVec_chk RespVec_chk_Bound[] = {// Bounds:  length must be right
1818     Schk(least_squares_weights,numLeastSqTerms,primaryRespFnWeights),
1819     Schk(multi_objective_weights,numObjectiveFunctions,primaryRespFnWeights),
1820     Schk(nonlinear_equality_targets,numNonlinearEqConstraints,nonlinearEqTargets),
1821     Schk(nonlinear_inequality_lower_bounds,numNonlinearIneqConstraints,nonlinearIneqLowerBnds),
1822     Schk(nonlinear_inequality_upper_bounds,numNonlinearIneqConstraints,nonlinearIneqUpperBnds)
1823   };
1824   static RespDVec_chk RespVec_chk_Scale[] = {// Scales:  length must be right
1825     // no longer checking LSQ or Obj scales due to fields
1826     //Schk(least_squares_term_scales,numLeastSqTerms,primaryRespFnScales),
1827     Schk(nonlinear_equality_scales,numNonlinearEqConstraints,nonlinearEqScales),
1828     Schk(nonlinear_inequality_scales,numNonlinearIneqConstraints,nonlinearIneqScales) //,
1829     //Schk(objective_function_scales,numObjectiveFunctions,primaryRespFnScales)
1830   };
1831 #undef Schk
1832 #define Numberof(x) sizeof(x)/sizeof(x[0])
1833 
1834   Real dbl_inf = std::numeric_limits<Real>::infinity();
1835   std::list<DataResponses>::iterator It = drl->begin(), Ite = drl->end();
1836   for(; It != Ite; It++) {
1837 
1838     DataResponsesRep *dr = It->dataRespRep.get();
1839 
1840     for(sc = Str_chk, i = 0; i < Numberof(Str_chk); ++sc, ++i)
1841       if ((n1 = dr->*sc->n) && (n = (dr->*sc->sa).size()) > 0
1842 	  && n != n1 && n != 1)
1843 	squawk("%s must have length 1 or %lu, not %lu",
1844 	       sc->what, (UL)n1, (UL)n);
1845     rl = &dr->responseLabels;
1846     nrl = rl->size();
1847     no = dr->numObjectiveFunctions;
1848     if ((n = dr->numLeastSqTerms)) {
1849       nf = n + dr->numNonlinearEqConstraints + dr->numNonlinearIneqConstraints;
1850       if (!nrl) {
1851 	BuildLabels(rl, nf, 0, n, "least_sq_term_");
1852 	n1 = n + dr->numNonlinearIneqConstraints;
1853 	BuildLabels(rl, 0, n, n1, "nln_ineq_con_");
1854 	BuildLabels(rl, 0, n1, nf, "nln_eq_con_");
1855       }
1856     }
1857     else if ((nf = no + dr->numNonlinearEqConstraints + dr->numNonlinearIneqConstraints)) {
1858       if (!nrl) {
1859 	rl->resize(nf);
1860 	if (no == 1)
1861 	  (*rl)[0] = "obj_fn";
1862 	else
1863 	  BuildLabels(rl, 0, 0, no, "obj_fn_");
1864 	n1 = no + dr->numNonlinearIneqConstraints;
1865 	BuildLabels(rl, 0, no, n1, "nln_ineq_con_");
1866 	BuildLabels(rl, 0, n1, nf, "nln_eq_con_");
1867       }
1868     }
1869     else if ((nf = dr->numResponseFunctions)) {
1870       if (!nrl)
1871 	BuildLabels(rl, nf, 0, nf, "response_fn_");
1872     }
1873     for(rdvc = RespVec_chk_Bound, i = 0; i < Numberof(RespVec_chk_Bound); ++rdvc, ++i)
1874       if ((n1 = dr->*rdvc->n) && (n = (dr->*rdvc->rv).length()) && n != n1)
1875 	squawk("%s needs %lu elements, not %lu",
1876 	       rdvc->what, (UL)(dr->*rdvc->n), (UL)n);
1877     for(rdvc = RespVec_chk_Scale, i = 0; i < Numberof(RespVec_chk_Scale); ++rdvc, ++i)
1878       if ((n1 = dr->*rdvc->n) && (n = (dr->*rdvc->rv).length())
1879 	  && n != n1 && n != 1)
1880 	squawk("%s needs %lu elements (or just one), not %lu",
1881 	       rdvc->what, (UL)n1, (UL)n);
1882     if (dr->methodSource == "vendor" && dr->fdGradStepSize.length() > 1)
1883       squawk("vendor numerical gradients only support a single fd_gradient_step_size");
1884 
1885     ni = (int)nf;
1886     if (dr->gradientType == "mixed") {
1887       iv = new IntArray;
1888       //iv->resize(nf);
1889       iv->assign(nf, 0);
1890       if (!(mixed_check(&dr->idAnalyticGrads, ni, iv, "id_analytic_gradients")
1891 	    + mixed_check(&dr->idNumericalGrads, ni, iv, "id_numerical_gradients")))
1892 	mixed_check2(nf, iv, "gradient");
1893       delete iv;
1894     }
1895     if ((n = dr->numNonlinearEqConstraints) > 0
1896 	&& dr->nonlinearEqTargets.length() == 0) {
1897       dr->nonlinearEqTargets.sizeUninitialized(n);
1898       dr->nonlinearEqTargets = 0.;
1899     }
1900     if ((n = dr->numNonlinearIneqConstraints) > 0) {
1901       if (dr->nonlinearIneqLowerBnds.length() == 0) {
1902 	dr->nonlinearIneqLowerBnds.sizeUninitialized(n);
1903 	dr->nonlinearIneqLowerBnds = -dbl_inf;
1904       }
1905       if (dr->nonlinearIneqUpperBnds.length() == 0) {
1906 	dr->nonlinearIneqUpperBnds.sizeUninitialized(n);
1907 	dr->nonlinearIneqUpperBnds = 0.;
1908 	// default is a one-sided inequality <= 0.0
1909       }
1910     }
1911     if (dr->hessianType == "mixed") {
1912       iv = new IntArray;
1913       //iv->resize(nf);
1914       iv->assign(nf, 0);
1915       if (!(mixed_check(&dr->idAnalyticHessians, ni, iv, "id_analytic_hessians")
1916 	    + mixed_check(&dr->idNumericalHessians, ni, iv, "id_numerical_hessians")
1917 	    + mixed_check(&dr->idQuasiHessians, ni, iv, "id_quasi_hessians")))
1918 	mixed_check2(nf, iv, "Hessian");
1919       delete iv;
1920     }
1921     if (nerr)
1922       abort_handler(PARSE_ERROR);
1923   }
1924 }
1925 
1926 void NIDRProblemDescDB::
resp_str(const char * keyname,Values * val,void ** g,void * v)1927 resp_str(const char *keyname, Values *val, void **g, void *v)
1928 {
1929   (*(Resp_Info**)g)->dr->**(String DataResponsesRep::**)v = *val->s;
1930 }
1931 
1932 void NIDRProblemDescDB::
resp_strL(const char * keyname,Values * val,void ** g,void * v)1933 resp_strL(const char *keyname, Values *val, void **g, void *v)
1934 {
1935   StringArray *sa
1936     = &((*(Resp_Info**)g)->dr->**(StringArray DataResponsesRep::**)v);
1937   const char **s = val->s;
1938   size_t i, n = val->n;
1939 
1940   sa->resize(n);
1941   for(i = 0; i < n; i++)
1942     (*sa)[i] = s[i];
1943 }
1944 
1945 void NIDRProblemDescDB::
resp_false(const char * keyname,Values * val,void ** g,void * v)1946 resp_false(const char *keyname, Values *val, void **g, void *v)
1947 {
1948   (*(Resp_Info**)g)->dr->**(bool DataResponsesRep::**)v = false;
1949 }
1950 
1951 void NIDRProblemDescDB::
resp_true(const char * keyname,Values * val,void ** g,void * v)1952 resp_true(const char *keyname, Values *val, void **g, void *v)
1953 {
1954   (*(Resp_Info**)g)->dr->**(bool DataResponsesRep::**)v = true;
1955 }
1956 
1957 // void NIDRProblemDescDB::
1958 // env_Real(const char *keyname, Values *val, void **g, void *v)
1959 // {
1960 //   (*(DataEnvironmentRep**)g)->**(Real DataEnvironmentRep::**)v = *val->r;
1961 // }
1962 
1963 // void NIDRProblemDescDB::
1964 // env_RealL(const char *keyname, Values *val, void **g, void *v)
1965 // {
1966 //   Real *r = val->r;
1967 //   RealVector *rdv
1968 //     = &((*(DataEnvironmentRep**)g)->**(RealVector DataEnvironmentRep::**)v);
1969 //   size_t i, n = val->n;
1970 
1971 //   rdv->sizeUninitialized(n);
1972 //   for(i = 0; i < n; i++)
1973 //     (*rdv)[i] = r[i];
1974 // }
1975 
1976 void NIDRProblemDescDB::
env_true(const char * keyname,Values * val,void ** g,void * v)1977 env_true(const char *keyname, Values *val, void **g, void *v)
1978 {
1979   (*(DataEnvironmentRep**)g)->**(bool DataEnvironmentRep::**)v = true;
1980 }
1981 
1982 void NIDRProblemDescDB::
env_int(const char * keyname,Values * val,void ** g,void * v)1983 env_int(const char *keyname, Values *val, void **g, void *v)
1984 {
1985   (*(DataEnvironmentRep**)g)->**(int DataEnvironmentRep::**)v = *val->i;
1986 }
1987 
1988 // void NIDRProblemDescDB::
1989 // env_lit(const char *keyname, Values *val, void **g, void *v)
1990 // {
1991 //   (*(DataEnvironmentRep**)g)->*((Environment_mp_lit*)v)->sp
1992 //     = ((Environment_mp_lit*)v)->lit;
1993 // }
1994 
1995 void NIDRProblemDescDB::
env_start(const char * keyname,Values * val,void ** g,void * v)1996 env_start(const char *keyname, Values *val, void **g, void *v)
1997 {
1998   *g = (void*)pDDBInstance->environmentSpec.dataEnvRep.get();
1999 }
2000 
2001 void NIDRProblemDescDB::
env_str(const char * keyname,Values * val,void ** g,void * v)2002 env_str(const char *keyname, Values *val, void **g, void *v)
2003 {
2004   (*(DataEnvironmentRep**)g)->**(String DataEnvironmentRep::**)v = *val->s;
2005 }
2006 
2007 /// set a value for an unsigned short type
2008 void NIDRProblemDescDB::
env_utype(const char * keyname,Values * val,void ** g,void * v)2009 env_utype(const char *keyname, Values *val, void **g, void *v)
2010 {
2011   (*(DataEnvironmentRep**)g)->*((Env_mp_utype*)v)->sp =
2012     ((Env_mp_utype*)v)->utype;
2013 }
2014 
2015 /// augment an unsigned short type with |=
2016 void NIDRProblemDescDB::
env_augment_utype(const char * keyname,Values * val,void ** g,void * v)2017 env_augment_utype(const char *keyname, Values *val, void **g, void *v)
2018 {
2019   (*(DataEnvironmentRep**)g)->*((Env_mp_utype*)v)->sp |=
2020     ((Env_mp_utype*)v)->utype;
2021 }
2022 
2023 void NIDRProblemDescDB::
env_strL(const char * keyname,Values * val,void ** g,void * v)2024 env_strL(const char *keyname, Values *val, void **g, void *v)
2025 {
2026   StringArray *sa
2027     = &((*(DataEnvironmentRep**)g)->**(StringArray DataEnvironmentRep::**)v);
2028   const char **s = val->s;
2029   size_t i, n = val->n;
2030 
2031   sa->resize(n);
2032   for(i = 0; i < n; i++)
2033     (*sa)[i] = s[i];
2034 }
2035 
2036 void NIDRProblemDescDB::
method_tr_final(const char * keyname,Values * val,void ** g,void * v)2037 method_tr_final(const char *keyname, Values *val, void **g, void *v)
2038 {
2039   DataMethodRep &data_method = *(*(Meth_Info**)g)->dme;
2040 
2041   // sanity checks on trust region user-defined values
2042   size_t i, num_init = data_method.trustRegionInitSize.length();
2043   Real min_init_size = 1.;
2044   for (i=0; i<num_init; ++i) {
2045     Real init_size_i = data_method.trustRegionInitSize[i];
2046     if ( init_size_i <= 0. || init_size_i > 1. )
2047       botch("specified initial TR size must be in (0,1]");
2048     if (init_size_i < min_init_size)
2049       min_init_size = init_size_i;
2050   }
2051   if ( data_method.trustRegionMinSize > min_init_size ) {
2052     if (num_init) botch("specified initial TR size less than minimum TR size");
2053     else          botch("minimum TR size must be <= 1.");
2054   }
2055   // allow 0 for min size spec (conv control becomes inactive)
2056   if ( data_method.trustRegionMinSize < 0. ||
2057        data_method.trustRegionMinSize > 1. )
2058     botch("specified minimum TR size must be in [0,1]");
2059   if( data_method.trustRegionContractTrigger <= 0. ||
2060       data_method.trustRegionContractTrigger >
2061       data_method.trustRegionExpandTrigger         ||
2062       data_method.trustRegionExpandTrigger   >  1. )
2063     botch("expand/contract threshold values must satisfy\n\t"
2064 	  "0 < contract_threshold <= expand_threshold <= 1");
2065   if ( data_method.trustRegionContract <= 0. ||
2066        data_method.trustRegionContract >  1. )
2067     botch("contraction_factor must be in (0,1]");
2068   else if ( data_method.trustRegionContract == 1. )
2069     warn("contraction_factor = 1.0 is valid, but should be < 1\n\t"
2070 	 "to assure convergence of the surrogate_based_opt method");
2071   if ( data_method.trustRegionExpand < 1. )
2072     botch("expansion_factor must be >= 1");
2073 }
2074 
2075 
2076 void NIDRProblemDescDB::
var_RealLb(const char * keyname,Values * val,void ** g,void * v)2077 var_RealLb(const char *keyname, Values *val, void **g, void *v)
2078 {
2079   Real b, *r;
2080   RealVector *rv;
2081   Var_brv *V;
2082   size_t i, n;
2083 
2084   V = (Var_brv*)v;
2085   rv = &((*(Var_Info**)g)->dv->*V->rv);
2086   b = V->b;
2087   r = val->r;
2088   n = val->n;
2089   for(i = 0; i < n; i++)
2090     if (r[i] <= b) {
2091       squawk("%s values must be > %g", keyname, b);
2092       break;
2093     }
2094   rv->sizeUninitialized(n);
2095   for(i = 0; i < n; i++)
2096     (*rv)[i] = r[i];
2097 }
2098 
2099 void NIDRProblemDescDB::
var_RealUb(const char * keyname,Values * val,void ** g,void * v)2100 var_RealUb(const char *keyname, Values *val, void **g, void *v)
2101 {
2102   Real b, *r;
2103   RealVector *rv;
2104   Var_brv *V;
2105   size_t i, n;
2106 
2107   V = (Var_brv*)v;
2108   rv = &((*(Var_Info**)g)->dv->*V->rv);
2109   b = V->b;
2110   r = val->r;
2111   n = val->n;
2112   for(i = 0; i < n; i++)
2113     if (r[i] >= b) {
2114       squawk("%s values must be < %g", keyname, b);
2115       break;
2116     }
2117   rv->sizeUninitialized(n);
2118   for(i = 0; i < n; i++)
2119     (*rv)[i] = r[i];
2120 }
2121 
2122 void NIDRProblemDescDB::
var_IntLb(const char * keyname,Values * val,void ** g,void * v)2123 var_IntLb(const char *keyname, Values *val, void **g, void *v)
2124 {
2125   Var_biv *V = (Var_biv*)v;
2126   IntVector *iv = &((*(Var_Info**)g)->dv->*V->iv);
2127   int b = V->b;
2128   int *z = val->i;
2129   size_t i, n = val->n;
2130   for(i = 0; i < n; i++)
2131     if (z[i] <= b) {
2132       squawk("%s values must be > %g", keyname, b);
2133       break;
2134     }
2135   iv->sizeUninitialized(n);
2136   for(i = 0; i < n; i++)
2137     (*iv)[i] = z[i];
2138 }
2139 
2140 
2141 /// Map an NIDR STRINGLIST to a BoolDeque based on string values; for
2142 /// now we require user to specify all N values
2143 void NIDRProblemDescDB::
var_categorical(const char * keyname,Values * val,void ** g,void * v)2144 var_categorical(const char *keyname, Values *val, void **g, void *v)
2145 {
2146   BitArray *ba
2147     = &((*(Var_Info**)g)->dv->**(BitArray DataVariablesRep::**)v);
2148   const char **s = val->s;
2149   size_t i, n = val->n;
2150 
2151   // allow strings beginning y Y or T t (yes/true)
2152   ba->resize(n);
2153   for(i = 0; i < n; i++) {
2154     String str_lower(strtolower(s[i]));
2155     (*ba)[i] = strbegins(str_lower, "y") || strbegins(str_lower, "t");
2156   }
2157 }
2158 
2159 
2160 void NIDRProblemDescDB::
var_newrvec(const char * keyname,Values * val,void ** g,void * v)2161 var_newrvec(const char *keyname, Values *val, void **g, void *v)
2162 {
2163   Var_Info *vi = *(Var_Info**)g;
2164   RealVector *rv;
2165   size_t i, n = val->n;
2166   Real *r = val->r;
2167 
2168   if (!(rv = new RealVector(n, false)))
2169     botch("new failure in var_newrvec");
2170   vi->**(RealVector *Var_Info::**)v = rv;
2171   for(i = 0; i < n; i++)
2172     (*rv)[i] = r[i];
2173 }
2174 
2175 void NIDRProblemDescDB::
var_newivec(const char * keyname,Values * val,void ** g,void * v)2176 var_newivec(const char *keyname, Values *val, void **g, void *v)
2177 {
2178   Var_Info *vi = *(Var_Info**)g;
2179   IntVector *iv;
2180   size_t i, n = val->n;
2181   int *z = val->i;
2182 
2183   if (!(iv = new IntVector(n, false)))
2184     botch("new failure in var_newivec");
2185   vi->**(IntVector *Var_Info::**)v = iv;
2186   for(i = 0; i < n; i++)
2187     (*iv)[i] = z[i];
2188 }
2189 
2190 void NIDRProblemDescDB::
var_newiarray(const char * keyname,Values * val,void ** g,void * v)2191 var_newiarray(const char *keyname, Values *val, void **g, void *v)
2192 {
2193   Var_Info *vi = *(Var_Info**)g;
2194   IntArray *iv;
2195   size_t i, n = val->n;
2196   int *z = val->i;
2197 
2198   if (!(iv = new IntArray(n)))
2199     botch("new failure in var_intarray");
2200   vi->**(IntArray *Var_Info::**)v = iv;
2201   for(i = 0; i < n; i++)
2202     (*iv)[i] = z[i];
2203 }
2204 
2205 void NIDRProblemDescDB::
var_newsarray(const char * keyname,Values * val,void ** g,void * v)2206 var_newsarray(const char *keyname, Values *val, void **g, void *v)
2207 {
2208   Var_Info *vi = *(Var_Info**)g;
2209   StringArray *sa;
2210   size_t i, n = val->n;
2211   const char **z = val->s;
2212 
2213   if (!(sa = new StringArray(n)))
2214     botch("new failure in var_newsarray");
2215   vi->**(StringArray *Var_Info::**)v = sa;
2216   for(i = 0; i < n; i++)
2217     (*sa)[i] = z[i];
2218 }
2219 
2220 void NIDRProblemDescDB::
var_rvec(const char * keyname,Values * val,void ** g,void * v)2221 var_rvec(const char *keyname, Values *val, void **g, void *v)
2222 {
2223   RealVector *rv
2224     = &((*(Var_Info**)g)->dv->**(RealVector DataVariablesRep::**)v);
2225   size_t i, n = val->n;
2226   rv->sizeUninitialized(n);
2227 
2228   Real *r = val->r;
2229   for(i = 0; i < n; i++)
2230     (*rv)[i] = r[i];
2231 }
2232 
2233 void NIDRProblemDescDB::
var_ivec(const char * keyname,Values * val,void ** g,void * v)2234 var_ivec(const char *keyname, Values *val, void **g, void *v)
2235 {
2236   IntVector *iv = &((*(Var_Info**)g)->dv->**(IntVector DataVariablesRep::**)v);
2237   size_t i, n = val->n;
2238   iv->sizeUninitialized(n);
2239 
2240   int *z = val->i;
2241   for(i = 0; i < n; i++)
2242     (*iv)[i] = z[i];
2243 }
2244 
2245 // BMA TODO: I believe this duplicative of var_strL.
2246 // void NIDRProblemDescDB::
2247 // var_svec(const char *keyname, Values *val, void **g, void *v)
2248 // {
2249 //   StringArray *sa = &((*(Var_Info**)g)->dv->**(StringArray DataVariablesRep::**)v);
2250 //   size_t i, n = val->n;
2251 //   sa->resize(n);
2252 
2253 //   const char **z = val->s;
2254 //   for(i = 0; i < n; i++)
2255 //     (*sa)[i] = z[i];
2256 // }
2257 
2258 void NIDRProblemDescDB::
var_sizet(const char * keyname,Values * val,void ** g,void * v)2259 var_sizet(const char *keyname, Values *val, void **g, void *v)
2260 {
2261   int n = *val->i; // test value as int, prior to storage as size_t
2262 #ifdef REDUNDANT_INT_CHECKS
2263   if (n <= 0) /* now handled by INTEGER > 0 in the .nspec file */
2264     botch("%s must be positive", keyname);
2265 #endif
2266   (*(Var_Info**)g)->dv->**(size_t DataVariablesRep::**)v = n;
2267 }
2268 
2269 void NIDRProblemDescDB::
var_type(const char * keyname,Values * val,void ** g,void * v)2270 var_type(const char *keyname, Values *val, void **g, void *v)
2271 {
2272   (*(Var_Info**)g)->dv->*((Var_mp_type*)v)->sp = ((Var_mp_type*)v)->type;
2273 }
2274 
2275 void NIDRProblemDescDB::
var_start(const char * keyname,Values * val,void ** g,void * v)2276 var_start(const char *keyname, Values *val, void **g, void *v)
2277 {
2278   Var_Info *vi;
2279 
2280   if (!(vi = new Var_Info))
2281   Botch:		botch("new failure in var_start");
2282   memset(vi, 0, sizeof(Var_Info));
2283   if (!(vi->dv_handle = new DataVariables))
2284     goto Botch;
2285   vi->dv = vi->dv_handle->dataVarsRep.get();
2286   *g = (void*)vi;
2287 }
2288 
2289 void NIDRProblemDescDB::
var_true(const char * keyname,Values * val,void ** g,void * v)2290 var_true(const char *keyname, Values *val, void **g, void *v)
2291 {
2292   (*(Var_Info**)g)->dv->**(bool DataVariablesRep::**)v = true;
2293 }
2294 
wronglen(size_t n,RealVector * V,const char * what)2295 static int wronglen(size_t n, RealVector *V, const char *what)
2296 {
2297   size_t n1 = V->length();
2298   if (n != n1) {
2299     Squawk("Expected %d numbers for %s, but got %d", (int)n, what, (int)n1);
2300     return 1;
2301   }
2302   return 0;
2303 }
2304 
wronglen(size_t n,IntVector * V,const char * what)2305 static int wronglen(size_t n, IntVector *V, const char *what)
2306 {
2307   size_t n1 = V->length();
2308   if (n != n1) {
2309     Squawk("Expected %d numbers for %s, but got %d", (int)n, what, (int)n1);
2310     return 1;
2311   }
2312   return 0;
2313 }
2314 
Vcopyup(RealVector * V,RealVector * M,size_t i,size_t n)2315 static void Vcopyup(RealVector *V, RealVector *M, size_t i, size_t n)
2316 {
2317   size_t j;
2318   for(j = 0; j < n; ++i, ++j)
2319     (*V)[i] = (*M)[j];
2320 }
2321 
Set_rv(RealVector * V,double d,size_t n)2322 static void Set_rv(RealVector *V, double d, size_t n)
2323 {
2324   size_t i;
2325   V->sizeUninitialized(n);
2326   for(i = 0; i < n; ++i)
2327     (*V)[i] = d;
2328 }
2329 
Set_iv(IntVector * V,int d,size_t n)2330 static void Set_iv(IntVector *V, int d, size_t n)
2331 {
2332   size_t i;
2333   V->sizeUninitialized(n);
2334   for(i = 0; i < n; ++i)
2335     (*V)[i] = d;
2336 }
2337 
2338 static void
wrong_number(const char * what,const char * kind,size_t nsv,size_t m)2339 wrong_number(const char *what, const char *kind, size_t nsv, size_t m)
2340 {
2341   Squawk("Expected %d %s for %s, not %d", (int)nsv, what, kind, (int)m);
2342 }
2343 
too_small(const char * kind)2344 static void too_small(const char *kind)
2345 {
2346   Squawk("num_set_values values for %s must be >= 1", kind);
2347 }
2348 
not_div(const char * kind,size_t nsv,size_t m)2349 static void not_div(const char *kind, size_t nsv, size_t m)
2350 {
2351   Squawk("Number of %s set_values (%d) not evenly divisible by number of variables (%d); use num_set_values for unequal apportionment",
2352 	 kind, (int)nsv, (int)m);
2353 }
2354 
suppressed(const char * kind,int ndup,int * ip,String * sp,Real * rp)2355 static void suppressed(const char *kind, int ndup, int *ip, String *sp, Real *rp)
2356 {
2357   const char *s;
2358   int i, nother;
2359 
2360   nother = 0;
2361   if (ndup > 2) {
2362     nother = ndup - 1;
2363     ndup = 1;
2364   }
2365   for(i = 0; i < ndup; ++i)
2366     if (ip)
2367       Squawk("Duplicate %s value %d", kind, ip[i]);
2368     else if (sp)
2369       Squawk("Duplicate %s value %s", kind, sp[i].c_str());
2370     else
2371       Squawk("Duplicate %s value %.17g", kind, rp[i]);
2372   if (nother) {
2373     s = "s" + (nother == 1);
2374     Squawk("Warning%s of %d other duplicate %s value%s suppressed",
2375 	   s, nother, kind, s);
2376   }
2377 }
2378 
bad_initial_ivalue(const char * kind,int val)2379 static void bad_initial_ivalue(const char *kind, int val)
2380 {
2381   Squawk("invalid initial value %d for %s", val, kind);
2382 }
2383 
bad_initial_svalue(const char * kind,String val)2384 static void bad_initial_svalue(const char *kind, String val)
2385 {
2386   Squawk("invalid initial value %s for %s", val.c_str(), kind);
2387 }
2388 
bad_initial_rvalue(const char * kind,Real val)2389 static void bad_initial_rvalue(const char *kind, Real val)
2390 {
2391   Squawk("invalid initial value %.17g for %s", val, kind);
2392 }
2393 
2394 // *****************************************************************************
2395 //  Vchk functions called earlier from within check_variables_node(), which
2396 //   immediately {precedes,follows} DB buffer {send,receive} in broadcast().
2397 // Vgen functions called later from within make_variable_defaults()
2398 //   (from within post_process() following broadcast()).
2399 // As documented in ProblemDescDB::manage_inputs(), Vchk applies to minimal
2400 //   spec data, whereas Vgen constructs any large inferred vectors.
2401 // *****************************************************************************
Vgen_ContinuousDes(DataVariablesRep * dv,size_t offset)2402 static void Vgen_ContinuousDes(DataVariablesRep *dv, size_t offset)
2403 {
2404   RealVector *L, *U, *V;
2405   size_t i, n = dv->numContinuousDesVars;
2406   Real dbl_inf = std::numeric_limits<Real>::infinity();
2407 
2408   L = &dv->continuousDesignLowerBnds;
2409   U = &dv->continuousDesignUpperBnds;
2410   V = &dv->continuousDesignVars;
2411   if (L->length() == 0)
2412     Set_rv(L, -dbl_inf, n);
2413   if (U->length() == 0)
2414     Set_rv(U, dbl_inf, n);
2415   if (V->length() == 0) {
2416     V->sizeUninitialized(n);
2417     for(i = 0; i < n; i++) { // init to mean or 0, repairing to bounds if needed
2418       if ( -dbl_inf < (*L)[i] && (*U)[i] < dbl_inf )
2419 	(*V)[i] = ( (*L)[i] + (*U)[i] ) / 2.0;
2420       else if ((*L)[i] > 0.) (*V)[i] = (*L)[i];
2421       else if ((*U)[i] < 0.) (*V)[i] = (*U)[i];
2422       else                   (*V)[i] = 0;
2423     }
2424   }
2425 }
2426 
Vgen_DiscreteDesRange(DataVariablesRep * dv,size_t offset)2427 static void Vgen_DiscreteDesRange(DataVariablesRep *dv, size_t offset)
2428 {
2429   IntVector *L, *U, *V;
2430   size_t i, n = dv->numDiscreteDesRangeVars;
2431 
2432   L = &dv->discreteDesignRangeLowerBnds;
2433   U = &dv->discreteDesignRangeUpperBnds;
2434   V = &dv->discreteDesignRangeVars;
2435   if (L->length() == 0)
2436     Set_iv(L, INT_MIN, n);
2437   if (U->length() == 0)
2438     Set_iv(U, INT_MAX, n);
2439   if (V->length() == 0) {
2440     V->sizeUninitialized(n);
2441     for(i = 0; i < n; ++i) {
2442       // init to mean, truncating to left if needed; otherwise init to
2443       // 0, repairing to bounds if needed
2444       if ( INT_MIN < (*L)[i] && (*U)[i] < INT_MAX )
2445 	(*V)[i] = (*L)[i] + ((*U)[i] - (*L)[i])/2;
2446       else if ((*L)[i] > 0) (*V)[i] = (*L)[i];
2447       else if ((*U)[i] < 0) (*V)[i] = (*U)[i];
2448       else                  (*V)[i] = 0;
2449     }
2450   }
2451 }
2452 
Vgen_ContinuousState(DataVariablesRep * dv,size_t offset)2453 static void Vgen_ContinuousState(DataVariablesRep *dv, size_t offset)
2454 {
2455   RealVector *L, *U, *V;
2456   size_t i, n = dv->numContinuousStateVars;
2457   Real dbl_inf = std::numeric_limits<Real>::infinity();
2458 
2459   L = &dv->continuousStateLowerBnds;
2460   U = &dv->continuousStateUpperBnds;
2461   V = &dv->continuousStateVars;
2462   if (L->length() == 0)
2463     Set_rv(L, -dbl_inf, n);
2464   if (U->length() == 0)
2465     Set_rv(U, dbl_inf, n);
2466   if (V->length() == 0) {
2467     V->sizeUninitialized(n);
2468     for(i = 0; i < n; i++) { // init to mean or 0, repairing to bounds if needed
2469       if ( -dbl_inf < (*L)[i] && (*U)[i] < dbl_inf )
2470 	(*V)[i] = ( (*L)[i] + (*U)[i] ) / 2.0;
2471       else if ((*L)[i] > 0.) (*V)[i] = (*L)[i];
2472       else if ((*U)[i] < 0.) (*V)[i] = (*U)[i];
2473       else                   (*V)[i] = 0;
2474     }
2475   }
2476 }
2477 
Vgen_DiscreteStateRange(DataVariablesRep * dv,size_t offset)2478 static void Vgen_DiscreteStateRange(DataVariablesRep *dv, size_t offset)
2479 {
2480   IntVector *L, *U, *V;
2481   size_t i, n = dv->numDiscreteStateRangeVars;
2482 
2483   L = &dv->discreteStateRangeLowerBnds;
2484   U = &dv->discreteStateRangeUpperBnds;
2485   V = &dv->discreteStateRangeVars;
2486   if (L->length() == 0)
2487     Set_iv(L, INT_MIN, n);
2488   if (U->length() == 0)
2489     Set_iv(U, INT_MAX, n);
2490   if (V->length() == 0) {
2491     V->sizeUninitialized(n);
2492     for(i = 0; i < n; ++i) {
2493       // init to mean, truncating to left if needed; otherwise init to
2494       // 0, repairing to bounds if needed
2495       if ( INT_MIN < (*L)[i] && (*U)[i] < INT_MAX )
2496 	(*V)[i] = (*L)[i] + ((*U)[i] - (*L)[i])/2;
2497       else if ((*L)[i] > 0) (*V)[i] = (*L)[i];
2498       else if ((*U)[i] < 0) (*V)[i] = (*U)[i];
2499       else                  (*V)[i] = 0;
2500     }
2501   }
2502 }
2503 
Vchk_NormalUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)2504 static void Vchk_NormalUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
2505 {
2506   size_t n;
2507   RealVector *B, *M, *Sd;
2508 
2509   n = dv->numNormalUncVars;
2510   if (wronglen(n,  M = &dv->normalUncMeans,   "nuv_means") ||
2511       wronglen(n, Sd = &dv->normalUncStdDevs, "nuv_std_deviations"))
2512     return;
2513   B = &dv->normalUncLowerBnds;
2514   if (B->length() && wronglen(n, B, "nuv_lower_bounds"))
2515     return;
2516   B = &dv->normalUncUpperBnds;
2517   if (B->length() && wronglen(n, B, "nuv_upper_bounds"))
2518     return;
2519 }
2520 
Vgen_NormalUnc(DataVariablesRep * dv,size_t offset)2521 static void Vgen_NormalUnc(DataVariablesRep *dv, size_t offset)
2522 {
2523   short bds = 0; // 2 bits indicated LB and UB specs
2524   size_t i, j, n;
2525   Real mean, stdev, nudge, lower, upper;
2526   RealVector *B, *L, *M, *Sd, *U, *V, *IP;
2527   Real dbl_inf = std::numeric_limits<Real>::infinity();
2528 
2529   n  =  dv->numNormalUncVars;
2530   M  = &dv->normalUncMeans;      Sd = &dv->normalUncStdDevs;
2531   L  = &dv->normalUncLowerBnds;  U  = &dv->normalUncUpperBnds;
2532   IP = &dv->normalUncVars;       V  = &dv->continuousAleatoryUncVars;
2533 
2534   // process lower bounds
2535   B = &dv->continuousAleatoryUncLowerBnds;
2536   if (L->length()) {
2537     Vcopyup(B, L, offset, n); // global = distribution
2538     bds |= 1;
2539   }
2540   else {
2541     Set_rv(L, -dbl_inf, n); // distribution
2542     for(j = 0; j < n; ++j)
2543       (*B)[offset+j] = (*M)[j] - 3.*(*Sd)[j]; // inferred global
2544   }
2545 
2546   // process upper bounds
2547   B = &dv->continuousAleatoryUncUpperBnds;
2548   if (U->length()) {
2549     Vcopyup(B, U, offset, n); // global = distribution
2550     bds |= 2;
2551   }
2552   else {
2553     Set_rv(U, dbl_inf, n); // distribution
2554     for(j = 0; j < n; ++j)
2555       (*B)[offset+j] = (*M)[j] + 3.*(*Sd)[j]; // inferred global
2556   }
2557 
2558   // Set initial values and repair to bounds, if needed
2559   if (IP->length()) { // in this case, don't nudge since not a default value
2560 
2561     dv->uncertainVarsInitPt = true;
2562 
2563     for (i = offset, j = 0; j < n; ++i, ++j)
2564       if      ((*IP)[j] < (*L)[j]) (*V)[i] =  (*L)[j];
2565       else if ((*IP)[j] > (*U)[j]) (*V)[i] =  (*U)[j];
2566       else                         (*V)[i] = (*IP)[j];
2567   }
2568   else { // > bds is 0, 1, 2, or 3 (0 ==> no bounds given, nothing more to do)
2569 
2570     // Note: in the case of BoundedNormalRandomVariable, we are initializing
2571     // to the gaussMean parameter of the unbounded normal and repairing to the
2572     // bounds.  To assign to the mean of the bounded normal (bounds repair not
2573     // necessary), use BoundedNormalRandomVariable::mean().
2574     switch(bds) {
2575     case 0: // no bounds
2576       Vcopyup(V, M, offset, n);
2577       break;
2578     case 1: // only lower bounds given
2579       for (i = offset, j = 0; j < n; ++i, ++j)
2580 	if ((*M)[j] <= (*L)[j]) (*V)[i] = (*L)[j] + 0.5*(*Sd)[j];
2581 	else                    (*V)[i] = (*M)[j];
2582       break;
2583 
2584     case 2: // only upper bounds given
2585       for (i = offset, j = 0; j < n; ++i, ++j)
2586 	if ((*M)[j] >= (*U)[j]) (*V)[i] = (*U)[j] - 0.5*(*Sd)[j];
2587 	else                    (*V)[i] = (*M)[j];
2588       break;
2589 
2590     case 3: // both lower and upper bounds given
2591       for (i = offset, j = 0; j < n; ++i, ++j) {
2592 	lower  = (*L)[j]; upper = (*U)[j];
2593 	nudge  = 0.5 * std::min((*Sd)[j], upper - lower);
2594 	lower += nudge;   upper -= nudge;  mean = (*M)[j];
2595  	if      (mean < lower) (*V)[i] = lower;
2596 	else if (mean > upper) (*V)[i] = upper;
2597 	else                   (*V)[i] = mean;
2598       }
2599       break;
2600     }
2601   }
2602 }
2603 
Vchk_LognormalUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)2604 static void Vchk_LognormalUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
2605 {
2606   size_t n;
2607   RealVector *B, *L, *M, *Sd, *Ef, *Z;
2608 
2609   // lambda/zeta, mean/std_deviation, or mean/error_factor
2610   n = dv->numLognormalUncVars;
2611   L = &dv->lognormalUncLambdas;
2612   M = &dv->lognormalUncMeans;
2613   if (L->length()) {
2614     if (wronglen(n, L, "lnuv_lambdas"))
2615       return;
2616     if (wronglen(n, Z = &dv->lognormalUncZetas, "lnuv_zetas"))
2617       return;
2618   }
2619   else if (M->length()) {
2620     if (wronglen(n, M, "lnuv_means"))
2621       return;
2622     Sd = &dv->lognormalUncStdDevs;
2623     Ef = &dv->lognormalUncErrFacts;
2624     if (Sd->length())
2625       { if (wronglen(n, Sd, "lnuv_std_deviations")) return; }
2626     else if (Ef->length())
2627       { if (wronglen(n, Ef, "lnuv_error_factors"))  return; }
2628     //else error, but this should be trapped elsewhere
2629   }
2630 
2631   // lower bounds
2632   B = &dv->lognormalUncLowerBnds;
2633   if (B->length() && wronglen(n, B, "lnuv_lower_bounds"))
2634     return;
2635 
2636   // upper bounds
2637   B = &dv->lognormalUncUpperBnds;
2638   if (B->length() && wronglen(n, B, "lnuv_upper_bounds"))
2639     return;
2640 }
2641 
Vgen_LognormalUnc(DataVariablesRep * dv,size_t offset)2642 static void Vgen_LognormalUnc(DataVariablesRep *dv, size_t offset)
2643 {
2644   size_t i, j, n;
2645   Real mean, stdev, nudge, lower, upper;
2646   RealVector *B, *Ef, *Lam, *L, *M, *Sd, *U, *V, *Z, *IP;
2647   Real dbl_inf = std::numeric_limits<Real>::infinity();
2648 
2649   // lambda/zeta, mean/std_deviation, or mean/error_factor
2650   n  = dv->numLognormalUncVars;    Lam = &dv->lognormalUncLambdas;
2651   Z  = &dv->lognormalUncZetas;     Ef  = &dv->lognormalUncErrFacts;
2652   M  = &dv->lognormalUncMeans;     Sd  = &dv->lognormalUncStdDevs;
2653   L  = &dv->lognormalUncLowerBnds; U   = &dv->lognormalUncUpperBnds;
2654   IP = &dv->lognormalUncVars;      V   = &dv->continuousAleatoryUncVars;
2655 
2656   size_t num_Sd = Sd->length(), num_Lam = Lam->length(), num_IP = IP->length(),
2657     num_L = L->length(), num_U = U->length();
2658 
2659   if (num_IP) dv->uncertainVarsInitPt = true;
2660 
2661   // manage distribution and global bounds.  Global are inferred if
2662   // distribution are not specified.
2663   if (!num_L) L->size(n); // inits L to zeros --> default {dist,global}
2664   Vcopyup(&dv->continuousAleatoryUncLowerBnds, L, offset, n); // global = dist
2665   B = &dv->continuousAleatoryUncUpperBnds;
2666   if (num_U) Vcopyup(B, U, offset, n); // global = dist
2667   else       Set_rv(U, dbl_inf, n);    // default dist; global inferred below
2668 
2669   for (i = offset, j = 0; j < n; ++i, ++j) {
2670 
2671     // extract mean & stdev, if needed
2672     if (!num_IP || !num_U) {
2673       if (num_Lam)  // lambda/zeta
2674 	Pecos::LognormalRandomVariable::
2675 	  moments_from_params((*Lam)[j], (*Z)[j], mean, stdev);
2676       else {
2677 	mean = (*M)[j];
2678 	if (num_Sd) // mean/std_deviation
2679 	  stdev = (*Sd)[j];
2680 	else        // mean/error_factor
2681 	  Pecos::LognormalRandomVariable::
2682 	    std_deviation_from_error_factor(mean, (*Ef)[j], stdev);
2683       }
2684     }
2685 
2686     // Repair initial values to bounds, if needed
2687     if (num_IP) { // in this case, don't nudge since not a default value
2688 
2689       // TO DO: set user spec flag
2690 
2691       if      ((*IP)[j] < (*L)[j]) (*V)[i] =  (*L)[j];
2692       else if ((*IP)[j] > (*U)[j]) (*V)[i] =  (*U)[j];
2693       else                         (*V)[i] = (*IP)[j];
2694     }
2695     else {
2696       // repair bounds exceedance in default value, if needed
2697       lower  = (*L)[j]; upper = (*U)[j];
2698       nudge  = 0.5 * std::min(stdev, upper - lower);
2699       lower += nudge;   upper -= nudge;
2700       if      (mean < lower) (*V)[i] = lower;
2701       else if (mean > upper) (*V)[i] = upper;
2702       else                   (*V)[i] = mean;
2703       // Note: in the case of BoundedLognormalRandomVariable, we are
2704       // initializing to the mean parameter of the unbounded lognormal
2705       // and repairing to the bounds.  To assign to the mean of the
2706       // bounded lognormal (bounds repair not necessary), use
2707       // BoundedLognormalRandomVariable::mean().
2708     }
2709 
2710     // infer global bounds if no distribution bounds spec
2711     if (!num_U)
2712       (*B)[i] = mean + 3.*stdev;
2713   }
2714 }
2715 
Vchk_UniformUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)2716 static void Vchk_UniformUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
2717 {
2718   size_t n;
2719   RealVector *L, *U;
2720 
2721   n = dv->numUniformUncVars;
2722   L = &dv->uniformUncLowerBnds;
2723   U = &dv->uniformUncUpperBnds;
2724   if (wronglen(n, L, "uuv_lower_bounds") || wronglen(n, U, "uuv_upper_bounds"))
2725     return;
2726 }
2727 
Vgen_UniformUnc(DataVariablesRep * dv,size_t offset)2728 static void Vgen_UniformUnc(DataVariablesRep *dv, size_t offset)
2729 {
2730   size_t i, j, n;
2731   Real stdev;
2732   RealVector *L, *U, *V, *IP;
2733 
2734   n = dv->numUniformUncVars;   IP = &dv->uniformUncVars;
2735   L = &dv->uniformUncLowerBnds; U = &dv->uniformUncUpperBnds;
2736   Vcopyup(&dv->continuousAleatoryUncLowerBnds, L, offset, n);
2737   Vcopyup(&dv->continuousAleatoryUncUpperBnds, U, offset, n);
2738   V = &dv->continuousAleatoryUncVars;
2739   if (IP->length()) {
2740     dv->uncertainVarsInitPt = true;
2741     for (i = offset, j = 0; j < n; ++i, ++j)
2742       if      ((*IP)[j] < (*L)[j]) (*V)[i] =  (*L)[j];
2743       else if ((*IP)[j] > (*U)[j]) (*V)[i] =  (*U)[j];
2744       else                         (*V)[i] = (*IP)[j];
2745   }
2746   else
2747     for(i = offset, j = 0; j < n; ++i, ++j)
2748       Pecos::UniformRandomVariable::
2749 	moments_from_params((*L)[j], (*U)[j], (*V)[i], stdev);
2750 }
2751 
2752 static void
Vchk_LoguniformUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)2753 Vchk_LoguniformUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
2754 {
2755   size_t j, n;
2756   Real Lj, Uj;
2757   RealVector *L, *U;
2758   Real dbl_inf = std::numeric_limits<Real>::infinity();
2759 
2760   n = dv->numLoguniformUncVars;
2761   L = &dv->loguniformUncLowerBnds;
2762   U = &dv->loguniformUncUpperBnds;
2763   if (wronglen(n, L, "luuv_lower_bounds") ||
2764       wronglen(n, U, "luuv_upper_bounds"))
2765     return;
2766   for(j = 0; j < n; ++j) {
2767     Lj = (*L)[j];
2768     Uj = (*U)[j];
2769     if (Lj <= 0. || Uj <= 0.) {
2770       Squawk("loguniform bounds must be positive");
2771       return;
2772     }
2773     if (Lj == dbl_inf || Uj == dbl_inf) {
2774       Squawk("loguniform bounds must be finite");
2775       return;
2776     }
2777     if (Lj > Uj) {
2778       Squawk("loguniform lower bound greater than upper bound");
2779       return;
2780     }
2781   }
2782 }
2783 
Vgen_LoguniformUnc(DataVariablesRep * dv,size_t offset)2784 static void Vgen_LoguniformUnc(DataVariablesRep *dv, size_t offset)
2785 {
2786   size_t i, j, n;
2787   Real stdev;
2788   RealVector *L, *U, *V, *IP;
2789 
2790   n = dv->numLoguniformUncVars;   IP = &dv->loguniformUncVars;
2791   L = &dv->loguniformUncLowerBnds; U = &dv->loguniformUncUpperBnds;
2792   Vcopyup(&dv->continuousAleatoryUncLowerBnds, L, offset, n);
2793   Vcopyup(&dv->continuousAleatoryUncUpperBnds, U, offset, n);
2794   V = &dv->continuousAleatoryUncVars;
2795   if (IP->length()) {
2796     dv->uncertainVarsInitPt = true;
2797     for (i = offset, j = 0; j < n; ++i, ++j)
2798       if      ((*IP)[j] < (*L)[j]) (*V)[i] =  (*L)[j];
2799       else if ((*IP)[j] > (*U)[j]) (*V)[i] =  (*U)[j];
2800       else                         (*V)[i] = (*IP)[j];
2801   }
2802   else
2803     for(i = offset, j = 0; j < n; ++i, ++j)
2804       Pecos::LoguniformRandomVariable::
2805 	moments_from_params((*L)[j], (*U)[j], (*V)[i], stdev);
2806 }
2807 
Vchk_TriangularUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)2808 static void Vchk_TriangularUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
2809 {
2810   size_t j, n;
2811   Real Lj, Mj, Uj;
2812   RealVector *L, *M, *U;
2813 
2814   n = dv->numTriangularUncVars;    M = &dv->triangularUncModes;
2815   L = &dv->triangularUncLowerBnds; U = &dv->triangularUncUpperBnds;
2816   if (wronglen(n, L, "tuv_lower_bounds") || wronglen(n, M, "tuv_modes") ||
2817       wronglen(n, U, "tuv_upper_bounds"))
2818     return;
2819   for(j = 0; j < n; ++j) {
2820     Lj = (*L)[j]; Mj = (*M)[j]; Uj = (*U)[j];
2821     if (Lj > Mj || Mj > Uj) {
2822       Squawk("triangular uncertain variables must have\n\t"
2823 	     "tuv_lower_bounds <= tuv_modes <= tuv_upper_bounds");
2824       return;
2825     }
2826   }
2827 }
2828 
Vgen_TriangularUnc(DataVariablesRep * dv,size_t offset)2829 static void Vgen_TriangularUnc(DataVariablesRep *dv, size_t offset)
2830 {
2831   size_t i, j, n;
2832   Real stdev;
2833   RealVector *L, *M, *U, *V, *IP;
2834 
2835   n = dv->numTriangularUncVars;   IP = &dv->triangularUncVars;
2836   L = &dv->triangularUncLowerBnds; U = &dv->triangularUncUpperBnds;
2837   Vcopyup(&dv->continuousAleatoryUncLowerBnds, L, offset, n);
2838   Vcopyup(&dv->continuousAleatoryUncUpperBnds, U, offset, n);
2839   V = &dv->continuousAleatoryUncVars;
2840   if (IP->length()) {
2841     dv->uncertainVarsInitPt = true;
2842     for (i = offset, j = 0; j < n; ++i, ++j)
2843       if      ((*IP)[j] < (*L)[j]) (*V)[i] =  (*L)[j];
2844       else if ((*IP)[j] > (*U)[j]) (*V)[i] =  (*U)[j];
2845       else                         (*V)[i] = (*IP)[j];
2846   }
2847   else {
2848     M = &dv->triangularUncModes;
2849     for(i = offset, j = 0; j < n; ++i, ++j)
2850       Pecos::TriangularRandomVariable::
2851 	moments_from_params((*L)[j], (*M)[j], (*U)[j], (*V)[i], stdev);
2852   }
2853 }
2854 
2855 static void
Vchk_ExponentialUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)2856 Vchk_ExponentialUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
2857 {
2858   size_t n = dv->numExponentialUncVars;
2859   RealVector *B = &dv->exponentialUncBetas;
2860   if (wronglen(n, B, "euv_betas"))
2861     return;
2862 }
2863 
Vgen_ExponentialUnc(DataVariablesRep * dv,size_t offset)2864 static void Vgen_ExponentialUnc(DataVariablesRep *dv, size_t offset)
2865 {
2866   Real mean, stdev;
2867   RealVector *B, *L, *U, *V, *IP;
2868 
2869   B = &dv->exponentialUncBetas; IP = &dv->exponentialUncVars;
2870   V = &dv->continuousAleatoryUncVars;
2871   L = &dv->continuousAleatoryUncLowerBnds;
2872   U = &dv->continuousAleatoryUncUpperBnds;
2873   size_t i, j, n = dv->numExponentialUncVars, num_IP = IP->length();
2874   if (num_IP) dv->uncertainVarsInitPt = true;
2875 
2876   for(i = offset, j = 0; j < n; ++i, ++j) {
2877     Pecos::ExponentialRandomVariable::moments_from_params((*B)[j], mean, stdev);
2878     (*L)[i] = 0.;
2879     (*U)[i] = mean + 3.*stdev;
2880     if (num_IP) (*V)[i] = (*IP)[j];
2881     else        (*V)[i] = mean;
2882   }
2883 }
2884 
Vchk_BetaUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)2885 static void Vchk_BetaUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
2886 {
2887   RealVector *A, *B, *L, *U;
2888 
2889   A = &dv->betaUncAlphas;    B = &dv->betaUncBetas;
2890   L = &dv->betaUncLowerBnds; U = &dv->betaUncUpperBnds;
2891 
2892   size_t n = dv->numBetaUncVars;
2893   if (wronglen(n, A, "buv_alphas")       || wronglen(n, B, "buv_betas") ||
2894       wronglen(n, L, "buv_lower_bounds") || wronglen(n, U, "buv_upper_bounds"))
2895     return;
2896 }
2897 
Vgen_BetaUnc(DataVariablesRep * dv,size_t offset)2898 static void Vgen_BetaUnc(DataVariablesRep *dv, size_t offset)
2899 {
2900   Real stdev;
2901   RealVector *A, *B, *L, *U, *V, *IP;
2902 
2903   size_t i, j, n = dv->numBetaUncVars;
2904   A = &dv->betaUncAlphas;    B = &dv->betaUncBetas;
2905   L = &dv->betaUncLowerBnds; U = &dv->betaUncUpperBnds;
2906   V = &dv->continuousAleatoryUncVars; IP = &dv->betaUncVars;
2907   Vcopyup(&dv->continuousAleatoryUncLowerBnds, L, offset, n);
2908   Vcopyup(&dv->continuousAleatoryUncUpperBnds, U, offset, n);
2909   if (IP->length()) {
2910     dv->uncertainVarsInitPt = true;
2911     for (i = offset, j = 0; j < n; ++i, ++j)
2912       if      ((*IP)[j] < (*L)[j]) (*V)[i] =  (*L)[j];
2913       else if ((*IP)[j] > (*U)[j]) (*V)[i] =  (*U)[j];
2914       else                         (*V)[i] = (*IP)[j];
2915   }
2916   else
2917     for(i = offset, j = 0; j < n; ++i, ++j)
2918       Pecos::BetaRandomVariable::
2919 	moments_from_params((*A)[j], (*B)[j], (*L)[j], (*U)[j], (*V)[i], stdev);
2920 }
2921 
Vchk_GammaUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)2922 static void Vchk_GammaUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
2923 {
2924   RealVector *A, *B;
2925 
2926   size_t n = dv->numGammaUncVars;
2927   A = &dv->gammaUncAlphas; B = &dv->gammaUncBetas;
2928   if (wronglen(n, A, "gauv_alphas") || wronglen(n, B, "gauv_betas"))
2929     return;
2930 }
2931 
Vgen_GammaUnc(DataVariablesRep * dv,size_t offset)2932 static void Vgen_GammaUnc(DataVariablesRep *dv, size_t offset)
2933 {
2934   Real mean, stdev;
2935   RealVector *A, *B, *L, *U, *V, *IP;
2936 
2937   A = &dv->gammaUncAlphas; B = &dv->gammaUncBetas;
2938   L = &dv->continuousAleatoryUncLowerBnds;
2939   U = &dv->continuousAleatoryUncUpperBnds;
2940   V = &dv->continuousAleatoryUncVars; IP = &dv->gammaUncVars;
2941   size_t i, j, n = dv->numGammaUncVars, num_IP = IP->length();
2942   if (num_IP) dv->uncertainVarsInitPt = true;
2943 
2944   for(i = offset, j = 0; j < n; ++i, ++j) {
2945     Pecos::GammaRandomVariable::
2946       moments_from_params((*A)[j], (*B)[j], mean, stdev);
2947     (*L)[i] = 0.;
2948     (*U)[i] = mean + 3.*stdev;
2949     if (num_IP) (*V)[i] = (*IP)[j];
2950     else        (*V)[i] = mean;
2951   }
2952 }
2953 
Vchk_GumbelUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)2954 static void Vchk_GumbelUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
2955 {
2956   RealVector *A, *B;
2957 
2958   size_t n = dv->numGumbelUncVars;
2959   A = &dv->gumbelUncAlphas; B = &dv->gumbelUncBetas;
2960   if (wronglen(n, A, "guuv_alphas") || wronglen(n, B, "guuv_betas"))
2961     return;
2962 }
2963 
Vgen_GumbelUnc(DataVariablesRep * dv,size_t offset)2964 static void Vgen_GumbelUnc(DataVariablesRep *dv, size_t offset)
2965 {
2966   Real mean, stdev;
2967   RealVector *A, *B, *L, *U, *V, *IP;
2968 
2969   A = &dv->gumbelUncAlphas; B = &dv->gumbelUncBetas;
2970   L = &dv->continuousAleatoryUncLowerBnds;
2971   U = &dv->continuousAleatoryUncUpperBnds;
2972   V = &dv->continuousAleatoryUncVars; IP = &dv->gumbelUncVars;
2973   size_t i, j, n = dv->numGumbelUncVars, num_IP = IP->length();
2974   if (num_IP) dv->uncertainVarsInitPt = true;
2975 
2976   for(i = offset, j = 0; j < n; ++i, ++j) {
2977     Pecos::GumbelRandomVariable::
2978       moments_from_params((*A)[j], (*B)[j], mean, stdev);
2979     (*L)[i] = mean - 3.*stdev;
2980     (*U)[i] = mean + 3.*stdev;
2981     if (num_IP) (*V)[i] = (*IP)[j];
2982     else        (*V)[i] = mean;
2983   }
2984 }
2985 
Vchk_FrechetUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)2986 static void Vchk_FrechetUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
2987 {
2988   RealVector *A, *B;
2989 
2990   size_t n = dv->numFrechetUncVars;
2991   A = &dv->frechetUncAlphas; B = &dv->frechetUncBetas;
2992   if (wronglen(n, A, "fuv_alphas") || wronglen(n, B, "fuv_betas"))
2993     return;
2994 }
2995 
Vgen_FrechetUnc(DataVariablesRep * dv,size_t offset)2996 static void Vgen_FrechetUnc(DataVariablesRep *dv, size_t offset)
2997 {
2998   Real mean, stdev;
2999   RealVector *A, *B, *L, *U, *V, *IP;
3000 
3001   A = &dv->frechetUncAlphas; B = &dv->frechetUncBetas;
3002   L = &dv->continuousAleatoryUncLowerBnds;
3003   U = &dv->continuousAleatoryUncUpperBnds;
3004   V = &dv->continuousAleatoryUncVars; IP = &dv->frechetUncVars;
3005   size_t i, j, n = dv->numFrechetUncVars, num_IP = IP->length();
3006   if (num_IP) dv->uncertainVarsInitPt = true;
3007 
3008   for(i = offset, j = 0; j < n; ++i, ++j) {
3009     Pecos::FrechetRandomVariable::
3010       moments_from_params((*A)[j], (*B)[j], mean, stdev);
3011     (*L)[i] = 0.;
3012     (*U)[i] = mean + 3.*stdev;
3013     if (num_IP) (*V)[i] = (*IP)[j];
3014     else        (*V)[i] = mean;
3015   }
3016 }
3017 
Vchk_WeibullUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)3018 static void Vchk_WeibullUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
3019 {
3020   RealVector *A, *B;
3021 
3022   size_t n = dv->numWeibullUncVars;
3023   A = &dv->weibullUncAlphas; B = &dv->weibullUncBetas;
3024   if (wronglen(n, A, "wuv_alphas") || wronglen(n, B, "wuv_betas"))
3025     return;
3026 }
3027 
Vgen_WeibullUnc(DataVariablesRep * dv,size_t offset)3028 static void Vgen_WeibullUnc(DataVariablesRep *dv, size_t offset)
3029 {
3030   Real mean, stdev;
3031   RealVector *A, *B, *L, *U, *V, *IP;
3032 
3033   A = &dv->weibullUncAlphas; B = &dv->weibullUncBetas;
3034   L = &dv->continuousAleatoryUncLowerBnds;
3035   U = &dv->continuousAleatoryUncUpperBnds;
3036   V = &dv->continuousAleatoryUncVars; IP = &dv->weibullUncVars;
3037   size_t i, j, n = dv->numWeibullUncVars, num_IP = IP->length();
3038   if (num_IP) dv->uncertainVarsInitPt = true;
3039 
3040   for(i = offset, j = 0; j < n; ++i, ++j) {
3041     Pecos::WeibullRandomVariable::
3042       moments_from_params((*A)[j], (*B)[j], mean, stdev);
3043     (*L)[i] = 0.;
3044     (*U)[i] = mean + 3.*stdev;
3045     if (num_IP) (*V)[i] = (*IP)[j];
3046     else        (*V)[i] = mean;
3047   }
3048 }
3049 
3050 
3051 /// Check the histogram bin input data, normalize the counts and populate
3052 /// the histogramUncBinPairs map data structure; map keys are guaranteed
3053 /// unique since the abscissas must increase
3054 static void
Vchk_HistogramBinUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)3055 Vchk_HistogramBinUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
3056 {
3057   IntArray *nhbp;
3058   RealVector *hba, *hbo, *hbc;
3059   int nhbpi, avg_nhbpi;
3060   size_t i, j, num_a, num_o, num_c, m, n, tothbp, cntr;
3061   Real x, y, bin_width, count_sum;
3062 
3063   if (hba = vi->hba) { // abscissas are required
3064     num_a = hba->length();                            // abscissas
3065     hbo = vi->hbo; num_o = (hbo) ? hbo->length() : 0; // ordinates
3066     hbc = vi->hbc; num_c = (hbc) ? hbc->length() : 0; // counts
3067     if (num_o && num_o != num_a) {
3068       Squawk("Expected %d ordinates, not %d", num_a, num_o);
3069       return;
3070     }
3071     if (num_c && num_c != num_a) {
3072       Squawk("Expected %d counts, not %d", num_a, num_c);
3073       return;
3074     }
3075     bool key;
3076     if (nhbp = vi->nhbp) {
3077       key = true;
3078       m = nhbp->size();
3079       //dv->numHistogramBinUncVars = m;
3080       for (i=tothbp=0; i<m; ++i) {
3081 	tothbp += nhbpi = (*nhbp)[i];
3082 	if (nhbpi < 2) {
3083 	  Squawk("pairs_per_variable must be >= 2");
3084 	  return;
3085 	}
3086       }
3087       if (num_a != tothbp) {
3088 	Squawk("Expected %d abscissas, not %d", tothbp, num_a);
3089 	return;
3090       }
3091     }
3092     else {
3093       key = false;
3094       m = dv->numHistogramBinUncVars;
3095       if (num_a % m) {
3096 	Squawk("Number of abscissas (%d) not evenly divisible by number of variables (%d); Use pairs_per_variable for unequal apportionment", num_a, m);
3097 	return;
3098       }
3099       else
3100 	avg_nhbpi = num_a / m;
3101     }
3102     RealRealMapArray& hbp = dv->histogramUncBinPairs;
3103     hbp.resize(m);
3104     for (i=cntr=0; i<m; ++i) {
3105       nhbpi = (key) ? (*nhbp)[i] : avg_nhbpi;
3106       // hbpi is map<Real value, Real probability> for a single variable i
3107       RealRealMap& hbpi = hbp[i];
3108       count_sum = 0.;
3109       for (j=0; j<nhbpi; ++j, ++cntr) {
3110 	Real x = (*hba)[cntr];                          // abscissas
3111 	Real y = (num_c) ? (*hbc)[cntr] : (*hbo)[cntr]; // ordinates/counts
3112 	if (j<nhbpi-1) {
3113 	  Real bin_width = (*hba)[cntr+1] - x;
3114 	  if (bin_width <= 0.)
3115 	    { Squawk("histogram bin x values must increase");          return; }
3116 	  if (y <= 0.)
3117 	    { Squawk("nonpositive intermediate histogram bin y value");return; }
3118 	  if (num_c) {
3119 	    count_sum += y; // accumulate counts
3120 	    y /= bin_width; // convert counts to ordinates (probability density)
3121 	  }
3122 	  else
3123 	    count_sum += y * bin_width; // accumulate counts
3124 	}
3125 	else if (y != 0.)
3126 	  { Squawk("histogram bin y values must end with 0"); return; }
3127 	// insert without checking since keys (abscissas) must increase
3128 	hbpi[x] = y;
3129       }
3130       // normalize counts to sum to 1, omitting last value
3131       RRMCIter it_end = --(hbpi.end());
3132       for (RRMIter it = hbpi.begin(); it != it_end; ++it)
3133 	it->second /= count_sum;
3134     }
3135   }
3136 }
3137 
3138 /// Infer lower/upper bounds for histogram and set initial variable
3139 /// values based on initial_point or moments, snapping to bounds as
3140 /// needed.  (Histogram bin doesn't have lower/upper bounds specifcation)
Vgen_HistogramBinUnc(DataVariablesRep * dv,size_t offset)3141 static void Vgen_HistogramBinUnc(DataVariablesRep *dv, size_t offset)
3142 {
3143   RealVector *L, *U, *V, *IP;
3144   Real mean, stdev;
3145 
3146   L = &dv->continuousAleatoryUncLowerBnds;
3147   U = &dv->continuousAleatoryUncUpperBnds;
3148   V = &dv->continuousAleatoryUncVars; IP = &dv->histogramBinUncVars;
3149   const RealRealMapArray& A = dv->histogramUncBinPairs;
3150   size_t i, j, n = dv->numHistogramBinUncVars, num_IP = IP->length();
3151   if (num_IP) dv->uncertainVarsInitPt = true;
3152 
3153   for(i = offset, j = 0; j < n; ++i, ++j) {
3154     // the pairs are sorted, so take the first and next to last
3155     // (omitting the trailing zero)
3156     const RealRealMap& hist_bin_pairs = A[j];
3157     (*L)[i] = hist_bin_pairs.begin()->first;
3158     (*U)[i] = (--hist_bin_pairs.end())->first;
3159     if (num_IP) {
3160       if      ((*IP)[j] < (*L)[i]) (*V)[i] =  (*L)[i];
3161       else if ((*IP)[j] > (*U)[i]) (*V)[i] =  (*U)[i];
3162       else                         (*V)[i] = (*IP)[j];
3163     }
3164     else
3165       Pecos::HistogramBinRandomVariable::
3166 	moments_from_params(hist_bin_pairs, (*V)[i], stdev);
3167   }
3168 }
3169 
Vchk_PoissonUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)3170 static void Vchk_PoissonUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
3171 {
3172   size_t n = dv->numPoissonUncVars;
3173   RealVector *A = &dv->poissonUncLambdas;
3174   if (wronglen(n, A, "lambdas"))
3175     return;
3176 }
3177 
Vgen_PoissonUnc(DataVariablesRep * dv,size_t offset)3178 static void Vgen_PoissonUnc(DataVariablesRep *dv, size_t offset)
3179 {
3180   IntVector *L, *U, *V, *IP;
3181   Real mean, std_dev;
3182   RealVector *Lam;
3183 
3184   L = &dv->discreteIntAleatoryUncLowerBnds;
3185   U = &dv->discreteIntAleatoryUncUpperBnds;
3186   V = &dv->discreteIntAleatoryUncVars;
3187   IP = &dv->poissonUncVars; Lam = &dv->poissonUncLambdas;
3188   size_t i, j, n = dv->numPoissonUncVars, num_IP = IP->length();
3189   if (num_IP) dv->uncertainVarsInitPt = true;
3190 
3191   for(i = offset, j = 0; j < n; ++i, ++j) {
3192     Pecos::PoissonRandomVariable::moments_from_params((*Lam)[j], mean, std_dev);
3193     (*L)[i] = 0;
3194     (*U)[i] = (int)std::ceil(mean + 3.*std_dev);
3195     if (num_IP) (*V)[i] = (*IP)[j];
3196     else        (*V)[i] = (int)mean;
3197   }
3198 }
3199 
Vchk_BinomialUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)3200 static void Vchk_BinomialUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
3201 {
3202   size_t n = dv->numBinomialUncVars;
3203   RealVector *A = &dv->binomialUncProbPerTrial;
3204   IntVector  *B = &dv->binomialUncNumTrials;
3205   if (wronglen(n, A, "prob_per_trial") ||  wronglen(n, B, "num_trials"))
3206     return;
3207 }
3208 
Vgen_BinomialUnc(DataVariablesRep * dv,size_t offset)3209 static void Vgen_BinomialUnc(DataVariablesRep *dv, size_t offset)
3210 {
3211   IntVector *L, *NT, *U, *V, *IP;
3212   Real mean, std_dev;
3213   RealVector *Pr;
3214 
3215   L = &dv->discreteIntAleatoryUncLowerBnds;
3216   U = &dv->discreteIntAleatoryUncUpperBnds;
3217   V = &dv->discreteIntAleatoryUncVars; IP = &dv->binomialUncVars;
3218   NT = &dv->binomialUncNumTrials;      Pr = &dv->binomialUncProbPerTrial;
3219   size_t i, j, n = dv->numBinomialUncVars, num_IP = IP->length();
3220   if (num_IP) dv->uncertainVarsInitPt = true;
3221 
3222   for(i = offset, j = 0; j < n; ++i, ++j) {
3223     (*L)[i] = 0;
3224     (*U)[i] = (*NT)[j];
3225     if (num_IP) {
3226       if ((*IP)[j] > (*U)[i]) (*V)[i] =  (*U)[i];
3227       else                    (*V)[i] = (*IP)[j];
3228     }
3229     else {
3230       Pecos::BinomialRandomVariable::
3231 	moments_from_params((*NT)[j], (*Pr)[j], mean, std_dev);
3232       (*V)[i] = (int)mean;
3233     }
3234   }
3235 }
3236 
3237 static void
Vchk_NegBinomialUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)3238 Vchk_NegBinomialUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
3239 {
3240   size_t n = dv->numNegBinomialUncVars;
3241   RealVector *A = &dv->negBinomialUncProbPerTrial;
3242   IntVector  *B = &dv->negBinomialUncNumTrials;
3243   if (wronglen(n, A, "prob_per_trial") || wronglen(n, B, "num_trials"))
3244     return;
3245 }
3246 
Vgen_NegBinomialUnc(DataVariablesRep * dv,size_t offset)3247 static void Vgen_NegBinomialUnc(DataVariablesRep *dv, size_t offset)
3248 {
3249   IntVector *L, *NT, *U, *V, *IP;
3250   Real mean, std_dev;
3251   RealVector *Pr;
3252 
3253   L = &dv->discreteIntAleatoryUncLowerBnds;
3254   U = &dv->discreteIntAleatoryUncUpperBnds;
3255   V = &dv->discreteIntAleatoryUncVars; IP = &dv->negBinomialUncVars;
3256   NT = &dv->negBinomialUncNumTrials;   Pr = &dv->negBinomialUncProbPerTrial;
3257   size_t i, j, n = dv->numNegBinomialUncVars, num_IP = IP->length();
3258   if (num_IP) dv->uncertainVarsInitPt = true;
3259 
3260   for(i = offset, j = 0; j < n; ++i, ++j) {
3261     Pecos::NegBinomialRandomVariable::
3262       moments_from_params((*NT)[j], (*Pr)[j], mean, std_dev);
3263     (*L)[i] = (*NT)[j];
3264     (*U)[i] = (int)std::ceil(mean + 3.*std_dev);
3265     if (num_IP) {
3266       if ((*IP)[j] < (*L)[i]) (*V)[i] =  (*L)[i];
3267       else                    (*V)[i] = (*IP)[j];
3268     }
3269     else                      (*V)[i] = (int)mean;
3270   }
3271 }
3272 
Vchk_GeometricUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)3273 static void Vchk_GeometricUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
3274 {
3275   size_t n = dv->numGeometricUncVars;
3276   RealVector *A = &dv->geometricUncProbPerTrial;
3277   if (wronglen(n, A, "prob_per_trial"))
3278     return;
3279 }
3280 
Vgen_GeometricUnc(DataVariablesRep * dv,size_t offset)3281 static void Vgen_GeometricUnc(DataVariablesRep *dv, size_t offset)
3282 {
3283   IntVector *L, *U, *V, *IP;
3284   Real mean, std_dev;
3285   RealVector *Pr;
3286 
3287   L = &dv->discreteIntAleatoryUncLowerBnds;
3288   U = &dv->discreteIntAleatoryUncUpperBnds;
3289   V = &dv->discreteIntAleatoryUncVars; IP = &dv->geometricUncVars;
3290   Pr = &dv->geometricUncProbPerTrial;
3291   size_t i, j, n = dv->numGeometricUncVars, num_IP = IP->length();
3292   if (num_IP) dv->uncertainVarsInitPt = true;
3293 
3294   for(i = offset, j = 0; j < n; ++i, ++j) {
3295     (*L)[i] = 0;
3296     Pecos::GeometricRandomVariable::
3297       moments_from_params((*Pr)[j], mean, std_dev);
3298     (*U)[i] = (int)std::ceil(mean + 3.*std_dev);
3299     if (num_IP) (*V)[i] = (*IP)[j];
3300     else        (*V)[i] = (int)mean;
3301   }
3302 }
3303 
Vchk_HyperGeomUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)3304 static void Vchk_HyperGeomUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
3305 {
3306   IntVector *A, *B, *C;
3307 
3308   size_t n = dv->numHyperGeomUncVars;
3309   A = &dv->hyperGeomUncTotalPop;
3310   B = &dv->hyperGeomUncSelectedPop;
3311   C = &dv->hyperGeomUncNumDrawn;
3312   if (wronglen(n, A, "total_population")    ||
3313       wronglen(n, B, "selected_population") || wronglen(n, C, "num_drawn"))
3314     return;
3315 }
3316 
Vgen_HyperGeomUnc(DataVariablesRep * dv,size_t offset)3317 static void Vgen_HyperGeomUnc(DataVariablesRep *dv, size_t offset)
3318 {
3319   IntVector *L, *ND, *NS, *TP, *U, *V, *IP;
3320   Real mean, std_dev;
3321 
3322   L = &dv->discreteIntAleatoryUncLowerBnds;
3323   U = &dv->discreteIntAleatoryUncUpperBnds;
3324   V = &dv->discreteIntAleatoryUncVars;
3325   ND = &dv->hyperGeomUncNumDrawn; NS = &dv->hyperGeomUncSelectedPop;
3326   TP = &dv->hyperGeomUncTotalPop; IP = &dv->hyperGeomUncVars;
3327   size_t i, j, n = dv->numHyperGeomUncVars, num_IP = IP->length();
3328   int d, s;
3329   if (num_IP) dv->uncertainVarsInitPt = true;
3330 
3331   for(i = offset, j = 0; j < n; ++i, ++j) {
3332     (*L)[i] = 0;
3333     d = (*ND)[j]; s = (*NS)[j];
3334     (*U)[i] = std::min(d, s);
3335     if (num_IP) {
3336       if ((*IP)[j] > (*U)[i]) (*V)[i] =  (*U)[i];
3337       else                    (*V)[i] = (*IP)[j];
3338     }
3339     else {
3340       Pecos::HypergeometricRandomVariable::
3341 	moments_from_params((*TP)[j], s, d, mean, std_dev);
3342       (*V)[i] = (int)mean;
3343     }
3344   }
3345 }
3346 
3347 
3348 /// Check the histogram point integer input data, normalize the
3349 /// counts, and populate DataVariables::histogramUncPointIntPairs; map
3350 /// keys are guaranteed unique since the abscissas must increase
3351 static void
Vchk_HistogramPtIntUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)3352 Vchk_HistogramPtIntUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
3353 {
3354   IntArray *nhpip;
3355   IntVector *hpia;
3356   RealVector *hpic;
3357   int nhppi, avg_nhppi;
3358   size_t i, j, num_a, num_c, m, n, tothpp, cntr;
3359   Real y, bin_width, count_sum;
3360   int x;
3361 
3362   if (hpia = vi->hpia) {
3363     num_a = hpia->length();              // abscissas
3364     hpic = vi->hpic; num_c = hpic->length(); // counts
3365     if (num_c != num_a) {
3366       Squawk("Expected %d point counts, not %d", num_a, num_c);
3367       return;
3368     }
3369     bool key;
3370     if (nhpip = vi->nhpip) {
3371       key = true;
3372       m = nhpip->size();
3373       //dv->numHistogramPtUncVars = m;
3374       for(i=tothpp=0; i<m; ++i) {
3375 	tothpp += nhppi = (*nhpip)[i];
3376 	if (nhppi < 1) {
3377 	  Squawk("pairs_per_variable must be >= 1");
3378 	  return;
3379 	}
3380       }
3381       if (num_a != tothpp) {
3382 	Squawk("Expected %d point abscissas, not %d", tothpp, num_a);
3383 	return;
3384       }
3385     }
3386     else {
3387       key = false;
3388       m = dv->numHistogramPtIntUncVars;
3389       if (num_a % m) {
3390 	Squawk("Number of abscissas (%d) not evenly divisible by number of variables (%d); Use pairs_per_variable for unequal apportionment", num_a, m);
3391 	return;
3392       }
3393       else
3394 	avg_nhppi = num_a / m;
3395     }
3396     IntRealMapArray& hpp = dv->histogramUncPointIntPairs;
3397     hpp.resize(m);
3398     for (i=cntr=0; i<m; ++i) {
3399       nhppi = (key) ? (*nhpip)[i] : avg_nhppi;
3400       // hbpi is map<Int value, Real probability> for a single variable i
3401       IntRealMap& hppi = hpp[i];
3402       count_sum = 0.;
3403       for (j=0; j<nhppi; ++j, ++cntr) {
3404 	int x = (*hpia)[cntr]; // abscissas
3405 	Real y = (*hpic)[cntr]; // counts
3406 	if (j<nhppi-1 && x >= (*hpia)[cntr+1]) {
3407 	  Squawk("histogram point x values must increase");
3408 	  return;
3409 	}
3410 	if (y <= 0.) {
3411 	  Squawk("nonpositive intermediate histogram point y value");
3412 	  return;
3413 	}
3414 	hppi[x] = y;
3415 	count_sum += y;
3416       }
3417       // normalize counts to sum to 1
3418       IRMCIter it_end = hppi.end();
3419       for (IRMIter it = hppi.begin(); it != it_end; ++it)
3420 	it->second /= count_sum;
3421     }
3422   }
3423 }
3424 
3425 
3426 /// Use the integer-valued point histogram data to initialize the lower,
3427 /// upper, and initial values of the variables, using value closest to
3428 /// mean if no initial point.
Vgen_HistogramPtIntUnc(DataVariablesRep * dv,size_t offset)3429 static void Vgen_HistogramPtIntUnc(DataVariablesRep *dv, size_t offset)
3430 {
3431   const IntRealMapArray& A = dv->histogramUncPointIntPairs;
3432 
3433   IntVector& L = dv->discreteIntAleatoryUncLowerBnds;
3434   IntVector& U = dv->discreteIntAleatoryUncUpperBnds;
3435   IntVector& V = dv->discreteIntAleatoryUncVars;
3436   IntVector&  IP = dv->histogramPointIntUncVars;
3437 
3438   size_t i, j, k, last, n = dv->numHistogramPtIntUncVars;
3439   size_t num_IP = IP.length();
3440   if (num_IP) dv->uncertainVarsInitPt = true;
3441 
3442   for(i = offset, j = 0; j < n; ++i, ++j) {
3443     const IntRealMap& hist_pt_prs = A[j];
3444     L[i] = hist_pt_prs.begin()->first;
3445     U[i] = (--hist_pt_prs.end())->first;
3446     if (num_IP) {
3447       if      (IP[j] < L[i]) V[i] =  L[i];
3448       else if (IP[j] > U[i]) V[i] =  U[i];
3449       else                   V[i] = IP[j];
3450     }
3451     else {
3452       Real mean, stdev;
3453       Pecos::DiscreteSetRandomVariable<int>::
3454 	moments_from_params(hist_pt_prs, mean, stdev);
3455       if (hist_pt_prs.size() == 1)
3456 	V[i] = hist_pt_prs.begin()->first;
3457       else {
3458 	IRMCIter it = hist_pt_prs.begin(), it_end = hist_pt_prs.end();
3459 	// find value immediately right of mean (can't be past the end)
3460 	for( ; it != it_end, it->first <= mean; ++it);
3461 	// bracket the mean
3462 	int right_val = it->first;
3463 	int left_val = (--it)->first;
3464 	// initialize with value closest to mean
3465 	V[i] = (mean - right_val < left_val - mean) ? right_val : left_val;
3466       }
3467     }
3468   }
3469 }
3470 
3471 /// Check the histogram point string input data, normalize the counts,
3472 /// and populate DataVariables::histogramUncPointStrPairs; map keys
3473 /// are guaranteed unique since the abscissas must increase
3474 /// (lexicographically)
3475 static void
Vchk_HistogramPtStrUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)3476 Vchk_HistogramPtStrUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
3477 {
3478   IntArray *nhpsp;
3479   StringArray *hpsa;
3480   RealVector *hpsc;
3481   int nhppi, avg_nhppi;
3482   size_t i, j, num_a, num_c, m, n, tothpp, cntr;
3483   Real x, y, bin_width, count_sum;
3484 
3485   if (hpsa = vi->hpsa) {
3486     num_a = hpsa->size();              // abscissas
3487     hpsc = vi->hpsc; num_c = hpsc->length(); // counts
3488     if (num_c != num_a) {
3489       Squawk("Expected %d point counts, not %d", num_a, num_c);
3490       return;
3491     }
3492     bool key;
3493     if (nhpsp = vi->nhpsp) {
3494       key = true;
3495       m = nhpsp->size();
3496       //dv->numHistogramPtUncVars = m;
3497       for(i=tothpp=0; i<m; ++i) {
3498 	tothpp += nhppi = (*nhpsp)[i];
3499 	if (nhppi < 1) {
3500 	  Squawk("pairs_per_variable must be >= 1");
3501 	  return;
3502 	}
3503       }
3504       if (num_a != tothpp) {
3505 	Squawk("Expected %d point abscissas, not %d", tothpp, num_a);
3506 	return;
3507       }
3508     }
3509     else {
3510       key = false;
3511       m = dv->numHistogramPtStrUncVars;
3512       if (num_a % m) {
3513 	Squawk("Number of abscissas (%d) not evenly divisible by number of variables (%d); Use pairs_per_variable for unequal apportionment", num_a, m);
3514 	return;
3515       }
3516       else
3517 	avg_nhppi = num_a / m;
3518     }
3519     StringRealMapArray& hpp = dv->histogramUncPointStrPairs;
3520     hpp.resize(m);
3521     for (i=cntr=0; i<m; ++i) {
3522       nhppi = (key) ? (*nhpsp)[i] : avg_nhppi;
3523       // hbpi is map<Real value, Real probability> for a single variable i
3524       StringRealMap& hppi = hpp[i];
3525       count_sum = 0.;
3526       for (j=0; j<nhppi; ++j, ++cntr) {
3527 	String x = (*hpsa)[cntr]; // abscissas
3528 	Real y = (*hpsc)[cntr]; // counts
3529 	if (j<nhppi-1 && x >= (*hpsa)[cntr+1]) {
3530 	  Squawk("histogram point x values must increase");
3531 	  return;
3532 	}
3533 	if (y <= 0.) {
3534 	  Squawk("nonpositive intermediate histogram point y value");
3535 	  return;
3536 	}
3537 	hppi[x] = y;
3538 	count_sum += y;
3539       }
3540       // normalize counts to sum to 1
3541       SRMCIter it_end = hppi.end();
3542       for (SRMIter it = hppi.begin(); it != it_end; ++it)
3543 	it->second /= count_sum;
3544     }
3545   }
3546 }
3547 
3548 
3549 /// Use the string-valued point histogram data to initialize the lower,
3550 /// upper, and initial values of the variables, using index closest to
3551 /// mean index if no initial point.
Vgen_HistogramPtStrUnc(DataVariablesRep * dv,size_t offset)3552 static void Vgen_HistogramPtStrUnc(DataVariablesRep *dv, size_t offset)
3553 {
3554   const StringRealMapArray& A = dv->histogramUncPointStrPairs;
3555 
3556   StringArray& L = dv->discreteStrAleatoryUncLowerBnds;
3557   StringArray& U = dv->discreteStrAleatoryUncUpperBnds;
3558   StringArray& V = dv->discreteStrAleatoryUncVars;
3559   StringArray& IP = dv->histogramPointStrUncVars;
3560 
3561   size_t i, j, k, last, n = dv->numHistogramPtStrUncVars;
3562   size_t num_IP = IP.size();
3563 
3564   if (num_IP) dv->uncertainVarsInitPt = true;
3565 
3566   for(i = offset, j = 0; j < n; ++i, ++j) {
3567     const StringRealMap& hist_pt_prs = A[j];
3568     L[i] = hist_pt_prs.begin()->first;
3569     U[i] = (--hist_pt_prs.end())->first;
3570     if (num_IP) {
3571       if      (IP[j] < L[i]) V[i] =  L[i];
3572       else if (IP[j] > U[i]) V[i] =  U[i];
3573       else                   V[i] = IP[j];
3574     }
3575     else {
3576       // for string-valued histograms, mean and stddev are of
3577       // zero-based indices from beginning of the map
3578       Real mean, stdev;
3579       Pecos::DiscreteSetRandomVariable<String>::
3580 	moments_from_params(hist_pt_prs, mean, stdev);
3581       if (hist_pt_prs.size() == 1)
3582 	V[i] = hist_pt_prs.begin()->first;
3583       else {
3584 	size_t mean_index = boost::math::iround(mean);
3585 	SRMCIter it = hist_pt_prs.begin();
3586 	std::advance(it, mean_index);
3587 	// initialize with value closest to mean
3588 	V[i] = it->first;
3589       }
3590     }
3591   }
3592 }
3593 
3594 
3595 /// Check the histogram point integer real data, normalize the counts,
3596 /// and populate DataVariables::histogramUncPointRealPairs; map keys
3597 /// are guaranteed unique since the abscissas must increase
3598 static void
Vchk_HistogramPtRealUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)3599 Vchk_HistogramPtRealUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
3600 {
3601   IntArray *nhprp;
3602   RealVector *hpra, *hprc;
3603   int nhppi, avg_nhppi;
3604   size_t i, j, num_a, num_c, m, n, tothpp, cntr;
3605   Real x, y, bin_width, count_sum;
3606 
3607   if (hpra = vi->hpra) {
3608     num_a = hpra->length();              // abscissas
3609     hprc = vi->hprc; num_c = hprc->length(); // counts
3610     if (num_c != num_a) {
3611       Squawk("Expected %d point counts, not %d", num_a, num_c);
3612       return;
3613     }
3614     bool key;
3615     if (nhprp = vi->nhprp) {
3616       key = true;
3617       m = nhprp->size();
3618       //dv->numHistogramPtUncVars = m;
3619       for(i=tothpp=0; i<m; ++i) {
3620 	tothpp += nhppi = (*nhprp)[i];
3621 	if (nhppi < 1) {
3622 	  Squawk("pairs_per_variable must be >= 1");
3623 	  return;
3624 	}
3625       }
3626       if (num_a != tothpp) {
3627 	Squawk("Expected %d point abscissas, not %d", tothpp, num_a);
3628 	return;
3629       }
3630     }
3631     else {
3632       key = false;
3633       m = dv->numHistogramPtRealUncVars;
3634       if (num_a % m) {
3635 	Squawk("Number of abscissas (%d) not evenly divisible by number of variables (%d); Use pairs_per_variable for unequal apportionment", num_a, m);
3636 	return;
3637       }
3638       else
3639 	avg_nhppi = num_a / m;
3640     }
3641     RealRealMapArray& hpp = dv->histogramUncPointRealPairs;
3642     hpp.resize(m);
3643     for (i=cntr=0; i<m; ++i) {
3644       nhppi = (key) ? (*nhprp)[i] : avg_nhppi;
3645       // hbpi is map<Real value, Real probability> for a single variable i
3646       RealRealMap& hppi = hpp[i];
3647       count_sum = 0.;
3648       for (j=0; j<nhppi; ++j, ++cntr) {
3649 	Real x = (*hpra)[cntr]; // abscissas
3650 	Real y = (*hprc)[cntr]; // counts
3651 	if (j<nhppi-1 && x >= (*hpra)[cntr+1]) {
3652 	  Squawk("histogram point x values must increase");
3653 	  return;
3654 	}
3655 	if (y <= 0.) {
3656 	  Squawk("nonpositive intermediate histogram point y value");
3657 	  return;
3658 	}
3659 	hppi[x] = y;
3660 	count_sum += y;
3661       }
3662       // normalize counts to sum to 1
3663       RRMCIter it_end = hppi.end();
3664       for (RRMIter it = hppi.begin(); it != it_end; ++it)
3665 	it->second /= count_sum;
3666     }
3667   }
3668 }
3669 
3670 
3671 /// Use the real-valued point histogram data to initialize the lower,
3672 /// upper, and initial values of the variables, using value closest to
3673 /// mean if no initial point.
Vgen_HistogramPtRealUnc(DataVariablesRep * dv,size_t offset)3674 static void Vgen_HistogramPtRealUnc(DataVariablesRep *dv, size_t offset)
3675 {
3676   const RealRealMapArray& A = dv->histogramUncPointRealPairs;
3677 
3678   RealVector& L = dv->discreteRealAleatoryUncLowerBnds;
3679   RealVector& U = dv->discreteRealAleatoryUncUpperBnds;
3680   RealVector& V = dv->discreteRealAleatoryUncVars;
3681   RealVector& IP = dv->histogramPointRealUncVars;
3682 
3683   size_t i, j, k, last, n = dv->numHistogramPtRealUncVars;
3684   size_t num_IP = IP.length();
3685   if (num_IP) dv->uncertainVarsInitPt = true;
3686 
3687   for(i = offset, j = 0; j < n; ++i, ++j) {
3688     const RealRealMap& hist_pt_prs = A[j];
3689     L[i] = hist_pt_prs.begin()->first;
3690     U[i] = (--hist_pt_prs.end())->first;
3691     if (num_IP) {
3692       if      (IP[j] < L[i]) V[i] =  L[i];
3693       else if (IP[j] > U[i]) V[i] =  U[i];
3694       else                   V[i] = IP[j];
3695     }
3696     else {
3697       Real mean, stdev;
3698       Pecos::DiscreteSetRandomVariable<Real>::
3699 	moments_from_params(hist_pt_prs, mean, stdev);
3700       if (hist_pt_prs.size() == 1)
3701 	V[i] = hist_pt_prs.begin()->first;
3702       else {
3703 	RRMCIter it = hist_pt_prs.begin(), it_end = hist_pt_prs.end();
3704 	// find value immediately right of mean (can't be past the end)
3705 	for( ; it != it_end, it->first <= mean; ++it);
3706 	// bracket the mean
3707 	Real right_val = it->first;
3708 	Real left_val = (--it)->first;
3709 	// initialize with value closest to mean
3710 	V[i] = (mean - right_val < left_val - mean) ? right_val : left_val;
3711       }
3712     }
3713   }
3714 }
3715 
3716 
3717 /// Check the continuous interval uncertain input data and populate
3718 /// DataVariables::continuousIntervalUncBasicProbs; map keys (real
3719 /// intervals) are checked for uniqueness because we don't have a
3720 /// theoretically sound way to combine duplicate intervals
3721 static void
Vchk_ContinuousIntervalUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)3722 Vchk_ContinuousIntervalUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
3723 {
3724   size_t i, j, k, m, num_p = 0, num_lb, num_ub;
3725   IntArray *nI;
3726   int tot_nI, nIi, avg_nI;
3727   Real lb, lbj, ub, ubj, default_p;
3728   RealVector *Ilb, *Iub, *Ip;
3729   Real dbl_inf = std::numeric_limits<Real>::infinity();
3730 
3731   if ((Ilb = vi->CIlb) && (Iub = vi->CIub)) {
3732     num_lb = Ilb->length(); // interval lower_bounds
3733     num_ub = Iub->length(); // interval upper_bounds
3734 
3735     // error check on array lengths; bounds are reqd, probs are optional
3736     if ((Ip = vi->CIp)) {
3737       num_p = Ip->length(); // interval_probs
3738       if (num_lb != num_p || num_ub != num_p) {
3739 	Squawk("Expected as many lower bounds (%d) and upper bounds (%d) as probabilities (%d)", num_lb, num_ub, num_p);
3740 	return;
3741       }
3742     }
3743     else if (num_lb != num_ub) {
3744       Squawk("Expected as many lower bounds (%d) as upper bounds (%d)", num_lb, num_ub);
3745       return;
3746     }
3747 
3748     // define apportionment
3749     bool key;
3750     if (nI = vi->nCI) {
3751       key = true;
3752       m = nI->size();
3753       if (m != dv->numContinuousIntervalUncVars) {
3754 	Squawk("Expected %d numbers for num_intervals, but got %d",
3755 	       dv->numContinuousIntervalUncVars, m);
3756 	return;
3757       }
3758       for(i=tot_nI=0; i<m; ++i) {
3759 	tot_nI += nIi = (*nI)[i];
3760 	if (nIi < 1) {
3761 	  Squawk("num_intervals values should be positive");
3762 	  return;
3763 	}
3764       }
3765       if ( (num_p && wronglen(tot_nI,  Ip, "interval_probs") ) ||
3766 	   wronglen(tot_nI, Ilb, "interval lower_bounds") ||
3767 	   wronglen(tot_nI, Iub, "interval upper_bounds"))
3768 	return;
3769     }
3770     else {
3771       key = false;
3772       m = dv->numContinuousIntervalUncVars;
3773       if (num_lb % m) {
3774 	Squawk("Number of bounds (%d) not evenly divisible by number of variables (%d); Use num_intervals for unequal apportionment", num_lb, m);
3775 	return;
3776       }
3777       else
3778 	avg_nI = num_lb / m;
3779     }
3780     RealRealPairRealMapArray& P = dv->continuousIntervalUncBasicProbs;
3781     P.resize(m);
3782     for(i = k = 0; i < m; ++i) {
3783       nIi = (key) ? (*nI)[i] : avg_nI;
3784       RealRealPairRealMap& Pi = P[i];  // map from an interval to a probability
3785       lb = dbl_inf;
3786       ub = -dbl_inf;
3787       if (!num_p)
3788         default_p = 1./nIi; // default = equal probability per cell
3789       else {
3790         double total_prob=0.0;
3791         size_t s = k;
3792         for(j=0; j<nIi; ++j, ++s) {  // normalize the probabilities to sum to one
3793           total_prob+=(*Ip)[s];
3794         }
3795         if (fabs(total_prob-1.0) > 1.E-10) {
3796           s = k;
3797           {for(j=0; j<nIi; ++j,++s)  // normalize the probabilities to sum to one
3798             (*Ip)[s]/=total_prob;
3799           }
3800           Warn("Renormalized probability assignments to sum to one for variable %d",i);
3801         }
3802       }
3803       for(j=0; j<nIi; ++j, ++k) {
3804 	lbj = (*Ilb)[k];
3805 	ubj = (*Iub)[k];
3806 	RealRealPair interval(lbj, ubj);
3807 	Real probability = (num_p) ? (*Ip)[k] : default_p;
3808 	if (!Pi.insert(make_pair(interval, probability)).second)
3809 	  Squawk("Continuous interval [%g, %g] specified more than once for variable %d", interval.first, interval.second, i);
3810 	if (lb > lbj) lb = lbj;
3811 	if (ub < ubj) ub = ubj;
3812         if (lbj > ubj)
3813 	  Squawk("Upper bound less than lower bound: [%g, %g] for interval variable %d", lbj, ubj,i);
3814       }
3815       if (lb > ub)
3816 	Squawk("Inconsistent interval uncertain bounds: %g > %g", lb, ub);
3817     }
3818   }
3819 }
3820 
Vgen_ContinuousIntervalUnc(DataVariablesRep * dv,size_t offset)3821 static void Vgen_ContinuousIntervalUnc(DataVariablesRep *dv, size_t offset)
3822 {
3823   Real lb, lbk, ub, ubk, stdev;
3824   RealVector *ceuLB, *ceuUB, *V, *IP;
3825   Real dbl_inf = std::numeric_limits<Real>::infinity();
3826 
3827   ceuLB = &dv->continuousEpistemicUncLowerBnds;
3828   ceuUB = &dv->continuousEpistemicUncUpperBnds;
3829   V     = &dv->continuousEpistemicUncVars;
3830   const RealRealPairRealMapArray& P = dv->continuousIntervalUncBasicProbs;
3831   IP    = &dv->continuousIntervalUncVars;
3832   size_t i, j, n = dv->numContinuousIntervalUncVars,
3833     num_IP = IP->length();
3834   if (num_IP) dv->uncertainVarsInitPt = true;
3835 
3836   for(i = offset, j = 0; j < n; ++i, ++j) {
3837     lb = dbl_inf; ub = -dbl_inf;
3838     const RealRealPairRealMap& Pj = P[j];
3839     RealRealPairRealMap::const_iterator it = Pj.begin();
3840     RealRealPairRealMap::const_iterator it_end = Pj.end();
3841     for ( ; it != it_end; ++it) {
3842       const RealRealPair& interval = it->first;
3843       lbk = interval.first;
3844       ubk = interval.second;
3845       if (lb > lbk) lb = lbk;
3846       if (ub < ubk) ub = ubk;
3847     }
3848     (*ceuLB)[i] = lb; (*ceuUB)[i] = ub;
3849     if (num_IP) {
3850       if      ((*IP)[j] < lb) (*V)[i] = lb;
3851       else if ((*IP)[j] > ub) (*V)[i] = ub;
3852       else                    (*V)[i] = (*IP)[j];
3853     }
3854     else
3855       Pecos::UniformRandomVariable::moments_from_params(lb, ub, (*V)[i], stdev);
3856     // TO DO: if disjoint cells, repair V[i] to lie inside nearest cell
3857   }
3858 }
3859 
3860 /// Check the discrete interval uncertain input data and populate
3861 /// DataVariables::discreteIntervalUncBasicProbs; map keys (integer
3862 /// intervals) are checked for uniqueness because we don't have a
3863 /// theoretically sound way to combine duplicate intervals
3864 static void
Vchk_DiscreteIntervalUnc(DataVariablesRep * dv,size_t offset,Var_Info * vi)3865 Vchk_DiscreteIntervalUnc(DataVariablesRep *dv, size_t offset, Var_Info *vi)
3866 {
3867   size_t i, j, k, m, num_p = 0, num_lb, num_ub;
3868   IntArray *nI;
3869   int tot_nI, nIi, avg_nI, lb, lbj, ub, ubj;
3870   Real default_p;
3871   RealVector *Ip;
3872   IntVector *Ilb, *Iub;
3873 
3874   if ((Ilb = vi->DIlb) && (Iub = vi->DIub)) {
3875     num_lb = Ilb->length(); // interval lower_bounds
3876     num_ub = Iub->length(); // interval upper_bounds
3877 
3878     // error check on array lengths; bounds are reqd, probs are optional
3879     if ((Ip = vi->DIp)) {
3880       num_p = Ip->length(); // interval_probs
3881       if (num_lb != num_p || num_ub != num_p) {
3882 	Squawk("Expected as many lower bounds (%d) and upper bounds (%d) as probabilities (%d)", num_lb, num_ub, num_p);
3883 	return;
3884       }
3885     }
3886     else if (num_lb != num_ub) {
3887       Squawk("Expected as many lower bounds (%d) as upper bounds (%d)", num_lb, num_ub);
3888       return;
3889     }
3890 
3891     // define apportionment
3892     bool key;
3893     if (nI = vi->nDI) {
3894       key = true;
3895       m = nI->size();
3896       if (m != dv->numDiscreteIntervalUncVars) {
3897 	Squawk("Expected %d numbers for num_intervals, but got %d",
3898 	       dv->numDiscreteIntervalUncVars, m);
3899 	return;
3900       }
3901       for(i=tot_nI=0; i<m; ++i) {
3902 	tot_nI += nIi = (*nI)[i];
3903 	if (nIi < 1) {
3904 	  Squawk("num_intervals values should be positive");
3905 	  return;
3906 	}
3907       }
3908       if ( (num_p && wronglen(tot_nI,  Ip, "interval_probs") ) ||
3909 	   wronglen(tot_nI, Ilb, "interval lower_bounds") ||
3910 	   wronglen(tot_nI, Iub, "interval upper_bounds"))
3911 	return;
3912     }
3913     else {
3914       key = false;
3915       m = dv->numDiscreteIntervalUncVars;
3916       if (num_lb % m) {
3917 	Squawk("Number of bounds (%d) not evenly divisible by number of variables (%d); Use num_intervals for unequal apportionment", num_lb, m);
3918 	return;
3919       }
3920       else
3921 	avg_nI = num_lb / m;
3922     }
3923     IntIntPairRealMapArray& P = dv->discreteIntervalUncBasicProbs;
3924     P.resize(m);
3925     for(i = k = 0; i < m; ++i) {
3926       nIi = (key) ? (*nI)[i] : avg_nI;
3927       IntIntPairRealMap& Pi = P[i];   // map from an interval to a probability
3928       lb = INT_MAX; ub = INT_MIN;
3929       if (!num_p) default_p = 1./nIi; // default = equal probability per cell
3930       for(j=0; j<nIi; ++j, ++k) {
3931 	lbj = (*Ilb)[k];
3932 	ubj = (*Iub)[k];
3933 	IntIntPair interval(lbj, ubj);
3934 	Real probability =  (num_p) ? (*Ip)[k] : default_p;
3935 	if (!Pi.insert(make_pair(interval, probability)).second)
3936 	  Squawk("Discrete interval [%d, %d] specified more than once for variable %d", interval.first, interval.second, i);
3937 	if (lb > lbj) lb = lbj;
3938 	if (ub < ubj) ub = ubj;
3939       }
3940       if (lb > ub)
3941 	Squawk("Inconsistent interval uncertain bounds: %g > %g", lb, ub);
3942     }
3943   }
3944 }
3945 
Vgen_DiscreteIntervalUnc(DataVariablesRep * dv,size_t offset)3946 static void Vgen_DiscreteIntervalUnc(DataVariablesRep *dv, size_t offset)
3947 {
3948   int lb, lbk, ub, ubk, stdev;
3949   IntVector *deuLB, *deuUB, *V, *IP;
3950 
3951   deuLB = &dv->discreteIntEpistemicUncLowerBnds;
3952   deuUB = &dv->discreteIntEpistemicUncUpperBnds;
3953   V     = &dv->discreteIntEpistemicUncVars;
3954   const IntIntPairRealMapArray& P = dv->discreteIntervalUncBasicProbs;
3955   IP    = &dv->discreteIntervalUncVars;
3956   size_t i, j, n = dv->numDiscreteIntervalUncVars, num_IP = IP->length();
3957   if (num_IP) dv->uncertainVarsInitPt = true;
3958 
3959   for(i = offset, j = 0; j < n; ++i, ++j) {
3960     ub = INT_MIN; lb = INT_MAX;
3961     const IntIntPairRealMap& Pj = P[j];
3962     IntIntPairRealMap::const_iterator it = Pj.begin();
3963     IntIntPairRealMap::const_iterator it_end = Pj.end();
3964     for ( ; it != it_end; ++it) {
3965       const IntIntPair& interval = it->first;
3966       lbk = interval.first;
3967       ubk = interval.second;
3968       if (lb > lbk) lb = lbk;
3969       if (ub < ubk) ub = ubk;
3970     }
3971     (*deuLB)[i] = lb; (*deuUB)[i] = ub;
3972     if (num_IP) {
3973       if      ((*IP)[j] < lb) (*V)[i] = lb;
3974       else if ((*IP)[j] > ub) (*V)[i] = ub;
3975       else                    (*V)[i] = (*IP)[j];
3976     }
3977     else
3978       (*V)[i] = (lb + ub) / 2; // int truncation if odd sum
3979     // TO DO: if disjoint cells, repair V[offset] to lie inside nearest cell
3980   }
3981 }
3982 
3983 
3984 /// validate the number of set elements (values) given the number of
3985 /// variables and an optional apportionment with
3986 /// elements_per_variable; return the average number per variable if
3987 /// equally distributed
3988 static bool
check_set_keys(size_t num_v,size_t ds_len,const char * kind,IntArray * input_nds,int & avg_num_ds)3989 check_set_keys(size_t num_v, size_t ds_len, const char *kind,
3990 	       IntArray *input_nds, int& avg_num_ds)
3991 {
3992   // Process num_set_values key or define default allocation
3993   bool key = (input_nds);
3994   if (key) {
3995     if (input_nds->size() != num_v) {
3996       wrong_number("num_set_values value(s)", kind, num_v, input_nds->size());
3997       return key;
3998     }
3999     int num_ds_i, total_ds = 0;
4000     for (size_t i=0; i<num_v; ++i) {
4001       total_ds += num_ds_i = (*input_nds)[i];
4002       if (num_ds_i < 1)
4003 	{ too_small(kind); return key; }
4004     }
4005     if (ds_len != total_ds)
4006       { wrong_number("set_values", kind, total_ds, ds_len); return key; }
4007   }
4008   else { // num_set_values is optional; use average len if no spec
4009     if (ds_len % num_v)
4010       { not_div(kind, ds_len, num_v); return key; }
4011     else
4012       avg_num_ds = ds_len / num_v;
4013   }
4014   return key;
4015 }
4016 
4017 
4018 /// check discrete sets of integers (design and state variables);
4019 /// error if a duplicate value is specified
4020 /// error if not ordered to prevent user confusion
4021 static void
Vchk_DIset(size_t num_v,const char * kind,IntArray * input_ndsi,IntVector * input_dsi,IntSetArray & dsi_all,IntVector & dsi_init_pt)4022 Vchk_DIset(size_t num_v, const char *kind, IntArray *input_ndsi,
4023 	   IntVector *input_dsi, IntSetArray& dsi_all, IntVector& dsi_init_pt)
4024 {
4025   if (!input_dsi)
4026     return;
4027 
4028   bool misordered = false;
4029   int avg_num_dsi, ndup, dupval[2], num_dsi_i, val;
4030   size_t i, j, cntr, dsi_len = input_dsi->length();
4031 
4032   // Process num_set_values key or define default allocation
4033   bool key = check_set_keys(num_v, dsi_len, kind, input_ndsi, avg_num_dsi);
4034 
4035   // Insert values into the IntSetArray
4036   dsi_all.resize(num_v);
4037   for (i=cntr=ndup=0; i<num_v; ++i) {
4038     num_dsi_i = (key) ? (*input_ndsi)[i] : avg_num_dsi;
4039     IntSet& dsi_all_i = dsi_all[i];
4040     for (j=0; j<num_dsi_i; ++j, ++cntr) {
4041       val = (*input_dsi)[cntr];
4042       if (!dsi_all_i.insert(val).second) { // insert returns pair<iterator,bool>
4043 	if (++ndup <= 2) // warnings suppressed beyond two duplicates
4044 	  dupval[ndup-1] = val;
4045       }
4046       if (j<num_dsi_i-1 && val >= (*input_dsi)[cntr+1])
4047 	misordered = true;
4048     }
4049   }
4050   if (ndup)
4051     suppressed(kind, ndup, dupval, 0, 0);
4052   if (misordered)
4053     Squawk("Set values for each %s variable must increase", kind);
4054 
4055   // Checks on user-specified initial pt array
4056   if (!dsi_init_pt.empty()) {
4057     if (dsi_init_pt.length() != num_v)
4058       wrong_number("initial_point value(s)", kind, num_v, dsi_init_pt.length());
4059     else // check within admissible set for specified initial pt
4060       for (i=0; i<num_v; ++i) {
4061 	IntSet& dsi_all_i = dsi_all[i]; val = dsi_init_pt[i];
4062 	if (dsi_all_i.find(val) == dsi_all_i.end())
4063 	  bad_initial_ivalue(kind, val);
4064       }
4065   }
4066   //else: default initialization performed in Vgen_DIset
4067 }
4068 
4069 /// check discrete sets of integers (uncertain variables);
4070 /// error if a duplicate value is specified
4071 /// error if not ordered to prevent user confusion
4072 static void
Vchk_DIset(size_t num_v,const char * kind,IntArray * input_ndsi,IntVector * input_dsi,RealVector * input_dsip,IntRealMapArray & dsi_vals_probs,IntVector & dsi_init_pt)4073 Vchk_DIset(size_t num_v, const char *kind, IntArray *input_ndsi,
4074 	   IntVector *input_dsi, RealVector *input_dsip,
4075 	   IntRealMapArray& dsi_vals_probs, IntVector& dsi_init_pt)
4076 {
4077   if (!input_dsi)
4078     return;
4079 
4080   bool misordered = false;
4081   int avg_num_dsi, ndup, dupval[2], num_dsi_i, val;
4082   size_t i, j, cntr, dsi_len = input_dsi->length(),
4083     num_p = (input_dsip) ? input_dsip->length() : 0;
4084   if (num_p && num_p != dsi_len)
4085     wrong_number("set_probabilities", kind, dsi_len, num_p);
4086   Real prob, default_p;
4087 
4088   // Process num_set_values key or define default allocation
4089   bool key = check_set_keys(num_v, dsi_len, kind, input_ndsi, avg_num_dsi);
4090 
4091   // Insert values into the IntRealMapArray
4092   dsi_vals_probs.resize(num_v);
4093   for (i=cntr=ndup=0; i<num_v; ++i) {
4094     IntRealMap& dsi_v_p_i = dsi_vals_probs[i];
4095     num_dsi_i = (key) ? (*input_ndsi)[i] : avg_num_dsi;
4096     if (!num_p) default_p = 1./num_dsi_i;
4097     for (j=0; j<num_dsi_i; ++j, ++cntr) {
4098       val  = (*input_dsi)[cntr];
4099       prob = (num_p) ? (*input_dsip)[cntr] : default_p;
4100       if (dsi_v_p_i.find(val) == dsi_v_p_i.end())
4101 	dsi_v_p_i[val]  = prob; // insert new
4102       else {
4103 	// don't want to aggregate the probability; just error
4104 	//dsi_v_p_i[val] += prob; // add to existing
4105 	if (++ndup <= 2) // warnings suppressed beyond two duplicates
4106 	  dupval[ndup-1] = val;
4107       }
4108       if (j<num_dsi_i-1 && val >= (*input_dsi)[cntr+1])
4109 	misordered = true;
4110     }
4111   }
4112   if (ndup)
4113     suppressed(kind, ndup, dupval, 0, 0);
4114   if (misordered)
4115     Squawk("Set values for each %s variable must increase", kind);
4116 
4117   // Checks on user-specified initial pt array
4118   if (!dsi_init_pt.empty()) {
4119     if (dsi_init_pt.length() != num_v)
4120       wrong_number("initial_point value(s)", kind, num_v, dsi_init_pt.length());
4121     else // check within admissible set for specified initial pt
4122       for (i=0; i<num_v; ++i) {
4123 	IntRealMap& dsi_v_p_i = dsi_vals_probs[i]; val = dsi_init_pt[i];
4124 	if (dsi_v_p_i.find(val) == dsi_v_p_i.end())
4125 	  bad_initial_ivalue(kind, val);
4126       }
4127   }
4128   //else: default initialization performed in Vgen_DIset
4129 }
4130 
4131 static void
Vchk_DSset(size_t num_v,const char * kind,IntArray * input_ndss,StringArray * input_dss,StringSetArray & dss_all,StringArray & dss_init_pt)4132 Vchk_DSset(size_t num_v, const char *kind, IntArray *input_ndss,
4133 	   StringArray *input_dss, StringSetArray& dss_all, StringArray& dss_init_pt)
4134 {
4135   if (!input_dss)
4136     return;
4137 
4138   std::vector<bool> misordered(num_v,false);
4139   int avg_num_dss, ndup, num_dss_i;
4140   String dupval[2], val;
4141   size_t i, j, cntr, dss_len = input_dss->size();
4142 
4143   // Process num_set_values key or define default allocation
4144   bool key = check_set_keys(num_v, dss_len, kind, input_ndss, avg_num_dss);
4145 
4146   // Insert values into the StringSetArray
4147   dss_all.resize(num_v);
4148   for (i=cntr=ndup=0; i<num_v; ++i) {
4149     num_dss_i = (key) ? (*input_ndss)[i] : avg_num_dss;
4150     StringSet& dss_all_i = dss_all[i];
4151     for (j=0; j<num_dss_i; ++j, ++cntr) {
4152       val = (*input_dss)[cntr];
4153       if (!dss_all_i.insert(val).second) { // insert returns pair<iterator,bool>
4154 	if (++ndup <= 2) // warnings suppressed beyond two duplicates
4155 	  dupval[ndup-1] = val;
4156       }
4157       if (j<num_dss_i-1 && val >= (*input_dss)[cntr+1])
4158 	misordered[i] = true;
4159     }
4160   }
4161   if (ndup)
4162     suppressed(kind, ndup, 0, dupval, 0);
4163   // Check for misordered elements and print out in the expected order
4164   for(i=0; i<num_v; ++i) {
4165     if (misordered[i]) {
4166 	std::stringstream mss;
4167 	std::copy(dss_all[i].begin(), dss_all[i].end(),
4168 	    std::ostream_iterator<std::string>(mss, " "));
4169         Squawk("Elements of %s variables must be provided in ascending order ( %s)",
4170 	    kind, mss.str().c_str());
4171      }
4172   }
4173 
4174   // Checks on user-specified initial pt array
4175   if (!dss_init_pt.empty()) {
4176     if (dss_init_pt.size() != num_v)
4177       wrong_number("initial_point value(s)", kind, num_v, dss_init_pt.size());
4178     else // check within admissible set for specified initial pt
4179       for (i=0; i<num_v; ++i) {
4180 	StringSet& dss_all_i = dss_all[i]; val = dss_init_pt[i];
4181 	if (dss_all_i.find(val) == dss_all_i.end())
4182 	  bad_initial_svalue(kind, val);
4183       }
4184   }
4185   //else: default initialization performed in Vgen_DSset
4186 }
4187 
4188 static void
Vchk_DSset(size_t num_v,const char * kind,IntArray * input_ndss,StringArray * input_dss,RealVector * input_dssp,StringRealMapArray & dss_vals_probs,StringArray & dss_init_pt)4189 Vchk_DSset(size_t num_v, const char *kind, IntArray *input_ndss,
4190 	   StringArray *input_dss, RealVector *input_dssp,
4191 	   StringRealMapArray& dss_vals_probs, StringArray& dss_init_pt)
4192 {
4193   if (!input_dss)
4194     return;
4195 
4196   bool misordered = false;
4197   int avg_num_dss, ndup, num_dss_i;
4198   String dupval[2], val;
4199   size_t i, j, cntr, dss_len = input_dss->size(),
4200     num_p = (input_dssp) ? input_dssp->length() : 0;
4201   if (num_p && num_p != dss_len)
4202     wrong_number("set_probabilities", kind, dss_len, num_p);
4203   Real prob, default_p;
4204 
4205   // Process num_set_values key or define default allocation
4206   bool key = check_set_keys(num_v, dss_len, kind, input_ndss, avg_num_dss);
4207 
4208   // Insert values into the StringRealMapArray
4209   dss_vals_probs.resize(num_v);
4210   for (i=cntr=ndup=0; i<num_v; ++i) {
4211     StringRealMap& dss_v_p_i = dss_vals_probs[i];
4212     num_dss_i = (key) ? (*input_ndss)[i] : avg_num_dss;
4213     if (!num_p) default_p = 1./num_dss_i;
4214     for (j=0; j<num_dss_i; ++j, ++cntr) {
4215       val  = (*input_dss)[cntr];
4216       prob = (num_p) ? (*input_dssp)[cntr] : default_p;
4217       if (dss_v_p_i.find(val) == dss_v_p_i.end())
4218 	dss_v_p_i[val]  = prob; // insert new
4219       else {
4220 	// don't want to aggregate the probability; just error
4221 	//dss_v_p_i[val] += prob; // add to existing
4222 	if (++ndup <= 2) // warnings suppressed beyond two duplicates
4223 	  dupval[ndup-1] = val;
4224       }
4225       if (j<num_dss_i-1 && val >= (*input_dss)[cntr+1])
4226 	misordered = true;
4227     }
4228   }
4229   if (ndup)
4230     suppressed(kind, ndup, 0, dupval, 0);
4231   if (misordered)
4232     Squawk("Set values for each %s variable must increase", kind);
4233 
4234   // Checks on user-specified initial pt array
4235   if (!dss_init_pt.empty()) {
4236     if (dss_init_pt.size() != num_v)
4237       wrong_number("initial_point value(s)", kind, num_v, dss_init_pt.size());
4238     else // check within admissible set for specified initial pt
4239       for (i=0; i<num_v; ++i) {
4240 	StringRealMap& dss_v_p_i = dss_vals_probs[i]; val = dss_init_pt[i];
4241 	if (dss_v_p_i.find(val) == dss_v_p_i.end())
4242 	  bad_initial_svalue(kind, val);
4243       }
4244   }
4245   //else: default initialization performed in Vgen_DIset
4246 }
4247 
4248 
4249 static void
Vchk_DRset(size_t num_v,const char * kind,IntArray * input_ndsr,RealVector * input_dsr,RealSetArray & dsr_all,RealVector & dsr_init_pt)4250 Vchk_DRset(size_t num_v, const char *kind, IntArray  *input_ndsr,
4251 	   RealVector *input_dsr, RealSetArray& dsr_all,
4252 	   RealVector& dsr_init_pt)
4253 {
4254   if (!input_dsr)
4255     return;
4256 
4257   bool misordered = false;
4258   int avg_num_dsr, ndup, num_dsr_i;
4259   Real dupval[2], val;
4260   size_t i, j, cntr, dsr_len = input_dsr->length();
4261 
4262   // Process num_set_values key or define default allocation
4263   bool key = check_set_keys(num_v, dsr_len, kind, input_ndsr, avg_num_dsr);
4264 
4265   // Insert values into the RealSetArray
4266   dsr_all.resize(num_v);
4267   for (i=cntr=ndup=0; i<num_v; ++i) {
4268     num_dsr_i = (key) ? (*input_ndsr)[i] : avg_num_dsr;
4269     RealSet& dsr_all_i = dsr_all[i];
4270     for (j=0; j<num_dsr_i; ++j, ++cntr) {
4271       val = (*input_dsr)[cntr];
4272       if (!dsr_all_i.insert(val).second) { // insert returns pair<iterator,bool>
4273 	if (++ndup <= 2) // warnings suppressed beyond two duplicates
4274 	  dupval[ndup-1] = val;
4275       }
4276       if (j<num_dsr_i-1 && val >= (*input_dsr)[cntr+1])
4277 	misordered = true;
4278     }
4279   }
4280   if (ndup)
4281     suppressed(kind, ndup, 0, 0, dupval);
4282   if (misordered)
4283     Squawk("Set values for each %s variable must increase", kind);
4284 
4285   // Checks on user-specified initial pt array
4286   if (!dsr_init_pt.empty()) {
4287     if (dsr_init_pt.length() != num_v)
4288       wrong_number("initial_point value(s)", kind, num_v, dsr_init_pt.length());
4289     else
4290       for (i=0; i<num_v; ++i) {
4291 	RealSet& dsr_all_i = dsr_all[i]; val = dsr_init_pt[i];
4292 	if (dsr_all_i.find(val) == dsr_all_i.end())
4293 	  bad_initial_rvalue(kind, val);
4294       }
4295   }
4296   //else: default initialization performed in Vgen_DRset
4297 }
4298 
4299 static void
Vchk_DRset(size_t num_v,const char * kind,IntArray * input_ndsr,RealVector * input_dsr,RealVector * input_dsrp,RealRealMapArray & dsr_vals_probs,RealVector & dsr_init_pt)4300 Vchk_DRset(size_t num_v, const char *kind, IntArray  *input_ndsr,
4301 	   RealVector *input_dsr, RealVector* input_dsrp,
4302 	   RealRealMapArray& dsr_vals_probs, RealVector& dsr_init_pt)
4303 {
4304   if (!input_dsr)
4305     return;
4306 
4307   bool misordered = false;
4308   size_t i, j, cntr, dsr_len = input_dsr->length();
4309   int avg_num_dsr, ndup, num_dsr_i,
4310     num_p = (input_dsrp) ? input_dsrp->length() : 0;
4311   if (num_p && num_p != dsr_len)
4312     wrong_number("set_probabilities", kind, dsr_len, num_p);
4313   Real prob, default_p, dupval[2], val;
4314 
4315   // Process num_set_values key or define default allocation
4316   bool key = check_set_keys(num_v, dsr_len, kind, input_ndsr, avg_num_dsr);
4317 
4318   // Insert values into the RealRealMapArray
4319   dsr_vals_probs.resize(num_v);
4320   for (i=cntr=ndup=0; i<num_v; ++i) {
4321     RealRealMap& dsr_v_p_i = dsr_vals_probs[i];
4322     num_dsr_i = (key) ? (*input_ndsr)[i] : avg_num_dsr;
4323     if (!num_p) default_p = 1./num_dsr_i;
4324     for (j=0; j<num_dsr_i; ++j, ++cntr) {
4325       val  = (*input_dsr)[cntr];
4326       prob = (num_p) ? (*input_dsrp)[cntr] : default_p;
4327       if (dsr_v_p_i.find(val) == dsr_v_p_i.end())
4328 	dsr_v_p_i[val]  = prob; // insert new
4329       else {
4330 	// don't want to aggregate the probability; just error
4331 	//dsr_v_p_i[val] += prob; // add to existing
4332 	if (++ndup <= 2) // warnings suppressed beyond two duplicates
4333 	  dupval[ndup-1] = val;
4334       }
4335       if (j<num_dsr_i-1 && val >= (*input_dsr)[cntr+1])
4336 	misordered = true;
4337     }
4338   }
4339   if (ndup)
4340     suppressed(kind, ndup, 0, 0, dupval);
4341   if (misordered)
4342     Squawk("Set values for each %s variable must increase", kind);
4343 
4344   // Checks on user-specified initial pt array
4345   if (!dsr_init_pt.empty()) {
4346     if (dsr_init_pt.length() != num_v)
4347       wrong_number("initial_point value(s)", kind, num_v, dsr_init_pt.length());
4348     else // check within admissible set for specified initial pt
4349       for (i=0; i<num_v; ++i) {
4350 	RealRealMap& dsr_v_p_i = dsr_vals_probs[i]; val = dsr_init_pt[i];
4351 	if (dsr_v_p_i.find(val) == dsr_v_p_i.end())
4352 	  bad_initial_rvalue(kind, val);
4353       }
4354   }
4355   //else: default initialization performed in Vgen_DIset
4356 }
4357 
4358 // Validate adjacency matrices
4359 static void
Vchk_Adjacency(size_t num_v,const char * kind,const IntArray & num_e,const IntVector & input_ddsa,RealMatrixArray & dda_all)4360 Vchk_Adjacency(size_t num_v, const char *kind, const IntArray &num_e,
4361 		const IntVector &input_ddsa, RealMatrixArray &dda_all)  {
4362   size_t expected_size = 0;
4363   for(size_t i = 0; i < num_v; ++i)
4364     expected_size += num_e[i]*num_e[i];
4365     if(expected_size != input_ddsa.length())
4366       Squawk("adjacency list for %s has incorrect length", kind);
4367     else {
4368       size_t e_ctr = 0;
4369       for(size_t i = 0; i < num_v; ++i) {
4370         RealMatrix a_tmp(num_e[i],num_e[i]);
4371         for(size_t j = 0; j < num_e[i]; ++j)
4372           for(size_t k = 0; k < num_e[i]; ++k)
4373             a_tmp[j][k] = input_ddsa[e_ctr++];
4374         dda_all.push_back(a_tmp);
4375       }
4376     }
4377 }
4378 
4379 static bool
check_LUV_size(size_t num_v,IntVector & L,IntVector & U,IntVector & V,bool aggregate_LUV,size_t offset)4380 check_LUV_size(size_t num_v, IntVector& L, IntVector& U, IntVector& V,
4381 	       bool aggregate_LUV, size_t offset)
4382 {
4383   bool init_V = true;
4384   if (aggregate_LUV) {
4385     int max_index = offset + num_v - 1;
4386     if (max_index >= L.length() || max_index >= U.length() ||
4387 	max_index >= V.length())
4388       Squawk("max index %d out of range for aggregate updates in Vgen_DIset",
4389 	     max_index);
4390   }
4391   else {
4392     if (offset)
4393       Squawk("unexpected offset (%d) for non-aggregate mode in Vgen_DIset",
4394 	     (int)offset);
4395     L.sizeUninitialized(num_v);
4396     U.sizeUninitialized(num_v);
4397     if (V.length() == num_v) // user spec --> already assigned by var_ivec()
4398       init_V = false;
4399     else
4400       V.sizeUninitialized(num_v);
4401   }
4402   return init_V;
4403 }
4404 
4405 static bool
check_LUV_size(size_t num_v,StringArray & L,StringArray & U,StringArray & V,bool aggregate_LUV,size_t offset)4406 check_LUV_size(size_t num_v, StringArray& L, StringArray& U, StringArray& V,
4407 	       bool aggregate_LUV, size_t offset)
4408 {
4409   bool init_V = true;
4410   if (aggregate_LUV) {
4411     int max_index = offset + num_v - 1;
4412     if (max_index >= L.size() || max_index >= U.size() ||
4413 	max_index >= V.size())
4414       Squawk("max index %d out of range for aggregate updates in Vgen_DSset",
4415 	     max_index);
4416   }
4417   else {
4418     if (offset)
4419       Squawk("unexpected offset (%d) for non-aggregate mode in Vgen_DSset",
4420 	     (int)offset);
4421     L.resize(num_v);
4422     U.resize(num_v);
4423     if (V.size() == num_v) // user spec --> already assigned by var_ivec()
4424       init_V = false;
4425     else
4426       V.resize(num_v);
4427   }
4428   return init_V;
4429 }
4430 
4431 
4432 
4433 static bool
check_LUV_size(size_t num_v,RealVector & L,RealVector & U,RealVector & V,bool aggregate_LUV,size_t offset)4434 check_LUV_size(size_t num_v, RealVector& L, RealVector& U, RealVector& V,
4435 	       bool aggregate_LUV, size_t offset)
4436 {
4437   bool init_V = true;
4438   if (aggregate_LUV) {
4439     int max_index = offset + num_v - 1;
4440     if (max_index >= L.length() || max_index >= U.length() ||
4441 	max_index >= V.length())
4442       Squawk("max index %d out of range for aggregate updates in Vgen_DRset",
4443 	     max_index);
4444   }
4445   else {
4446     if (offset)
4447       Squawk("unexpected offset (%d) for non-aggregate mode in Vgen_DRset",
4448 	     (int)offset);
4449     L.sizeUninitialized(num_v);
4450     U.sizeUninitialized(num_v);
4451     if (V.length() == num_v) // user spec --> already assigned by var_rvec()
4452       init_V = false;
4453     else
4454       V.sizeUninitialized(num_v);
4455   }
4456   return init_V;
4457 }
4458 
4459 
4460 /// Compute the midpoint of floating-point or integer range [a, b] (a <= b),
4461 /// possibly indices, rounding toward a if needed. (Eventually replace
4462 /// with C++20 midpoint, which is more general.)
4463 template<typename T>
midpoint(T a,T b)4464 T midpoint(T a, T b)
4465 {
4466   // avoid overflow and possibly integer truncate the middle value
4467   // (b-a)/2 toward 0:
4468   return a + (b-a)/2;
4469 }
4470 
4471 
4472 /// get the middle or left-of-middle index among indices [0,num_inds-1]
mid_or_next_lower_index(const size_t num_inds)4473 static size_t mid_or_next_lower_index(const size_t num_inds)
4474 {
4475   return midpoint((size_t)0, num_inds-1);
4476 }
4477 
4478 
4479 static void
Vgen_DIset(size_t num_v,IntSetArray & sets,IntVector & L,IntVector & U,IntVector & V,bool aggregate_LUV=false,size_t offset=0)4480 Vgen_DIset(size_t num_v, IntSetArray& sets, IntVector& L, IntVector& U,
4481 	   IntVector& V, bool aggregate_LUV = false, size_t offset = 0)
4482 {
4483   ISCIter ie, it;
4484   size_t i, num_set_i;
4485 
4486   bool init_V = check_LUV_size(num_v, L, U, V, aggregate_LUV, offset);
4487 
4488   for(i = 0; i < num_v; ++i, ++offset) {
4489     IntSet& set_i = sets[i];
4490     it = set_i.begin(); ie = set_i.end(); num_set_i = set_i.size();
4491     if (num_set_i == 0) // should not occur
4492       L[offset] = U[offset] = V[offset] = 0;
4493     else if (num_set_i == 1)
4494       L[offset] = U[offset] = V[offset] = *it;
4495     else {
4496       L[offset] = *it;     // lower bound is first value
4497       U[offset] = *(--ie); // upper bound is final value
4498       if (init_V) {
4499 	// OLD: Select the initial value to be closest set value to mean value
4500 	// NEW: Select value from middle or next lower index
4501 	std::advance(it, mid_or_next_lower_index(num_set_i));
4502 	V[offset] = *it;
4503       }
4504     }
4505   }
4506 }
4507 
4508 
4509 /// generate lower, upper, and initial point for string-valued sets
4510 static void
Vgen_DSset(size_t num_v,StringSetArray & sets,StringArray & L,StringArray & U,StringArray & V,bool aggregate_LUV=false,size_t offset=0)4511 Vgen_DSset(size_t num_v, StringSetArray& sets, StringArray& L, StringArray& U,
4512 	   StringArray& V, bool aggregate_LUV = false, size_t offset = 0)
4513 {
4514   SSCIter ie, it;
4515   int i;
4516   size_t num_set_i;
4517 
4518   bool init_V = check_LUV_size(num_v, L, U, V, aggregate_LUV, offset);
4519 
4520   for(i = 0; i < num_v; ++i, ++offset) {
4521     StringSet& set_i = sets[i];
4522     it = set_i.begin(); ie = set_i.end(); num_set_i = set_i.size();
4523     if (num_set_i == 0) // should not occur
4524       L[offset] = U[offset] = V[offset] = "";
4525     else if (num_set_i == 1)
4526       L[offset] = U[offset] = V[offset] = *it;
4527     else {
4528       L[offset] = *it;     // lower bound is first value
4529       U[offset] = *(--ie); // upper bound is final value
4530       if (init_V) {
4531 	std::advance(it, mid_or_next_lower_index(num_set_i));
4532 	V[offset] = *it;
4533       }
4534     }
4535   }
4536 }
4537 
4538 static void
Vgen_DIset(size_t num_v,IntRealMapArray & vals_probs,IntVector & IP,IntVector & L,IntVector & U,IntVector & V,bool aggregate_LUV=false,size_t offset=0)4539 Vgen_DIset(size_t num_v, IntRealMapArray& vals_probs, IntVector& IP,
4540 	   IntVector& L, IntVector& U, IntVector& V,
4541 	   bool aggregate_LUV = false, size_t offset = 0)
4542 {
4543   IRMCIter ite, it;
4544   size_t i, j, num_vp_j, num_IP = IP.length();
4545 
4546   bool init_V = check_LUV_size(num_v, L, U, V, aggregate_LUV, offset);
4547 
4548   for(i = offset, j = 0; j < num_v; ++i, ++j) {
4549     IntRealMap& vp_j = vals_probs[j];
4550     it = vp_j.begin(); ite = vp_j.end(); num_vp_j = vp_j.size();
4551     if (num_vp_j == 0) { // should not occur
4552       L[i] = U[i] = 0;
4553       V[i] = (num_IP) ? IP[j] : 0;
4554     }
4555     else if (num_vp_j == 1) {
4556       L[i] = U[i] = it->first;
4557       V[i] = (num_IP) ? IP[j] : it->first;
4558     }
4559     else {
4560       L[i] = it->first;      // lower bound is first value
4561       U[i] = (--ite)->first; // upper bound is final value
4562       if (num_IP) V[i] = IP[j]; // presence of value w/i set already checked
4563       else if (init_V) {
4564 	// OLD: Select the initial value to be closest set value to mean value
4565 	// NEW: Select value from middle or next lower index
4566 	std::advance(it, mid_or_next_lower_index(num_vp_j));
4567 	V[i] = it->first;
4568       }
4569     }
4570   }
4571 }
4572 
4573 static void
Vgen_DRset(size_t num_v,RealSetArray & sets,RealVector & L,RealVector & U,RealVector & V,bool aggregate_LUV=false,size_t offset=0)4574 Vgen_DRset(size_t num_v, RealSetArray& sets, RealVector& L, RealVector& U,
4575 	   RealVector& V, bool aggregate_LUV = false, size_t offset = 0)
4576 {
4577   RSCIter ie, it;
4578   size_t i, num_set_i;
4579 
4580   bool init_V = check_LUV_size(num_v, L, U, V, aggregate_LUV, offset);
4581 
4582   for(i = 0; i < num_v; ++i, ++offset) {
4583     RealSet& set_i = sets[i];
4584     it = set_i.begin(); ie = set_i.end(); num_set_i = set_i.size();
4585     if (num_set_i == 0) // should not occur
4586       L[offset] = U[offset] = V[offset] = 0.;
4587     else if (num_set_i == 1)
4588       L[offset] = U[offset] = V[offset] = *it;
4589     else {
4590       L[offset] = *it;     // lower bound is first value
4591       U[offset] = *(--ie); // upper bound is final value
4592       if (init_V) {
4593 	// OLD: Select the initial value to be closest set value to mean value
4594 	// NEW: Select value from middle or next lower index
4595 	std::advance(it, mid_or_next_lower_index(num_set_i));
4596 	V[offset] = *it;
4597       }
4598     }
4599   }
4600 }
4601 
4602 static void
Vgen_DRset(size_t num_v,RealRealMapArray & vals_probs,RealVector & IP,RealVector & L,RealVector & U,RealVector & V,bool aggregate_LUV=false,size_t offset=0)4603 Vgen_DRset(size_t num_v, RealRealMapArray& vals_probs, RealVector& IP,
4604 	   RealVector& L, RealVector& U, RealVector& V,
4605 	   bool aggregate_LUV = false, size_t offset = 0)
4606 {
4607   RRMCIter ite, it;
4608   size_t i, j, num_vp_j, num_IP = IP.length();
4609 
4610   bool init_V = check_LUV_size(num_v, L, U, V, aggregate_LUV, offset);
4611 
4612   for(i = offset, j = 0; j < num_v; ++i, ++j) {
4613     RealRealMap& vp_j = vals_probs[j];
4614     it = vp_j.begin(); ite = vp_j.end(); num_vp_j = vp_j.size();
4615     if (num_vp_j == 0) { // should not occur
4616       L[i] = U[i] = V[i] = 0.;
4617       V[i] = (num_IP) ? IP[j] : 0.;
4618     }
4619     else if (num_vp_j == 1) {
4620       L[i] = U[i] = it->first;
4621       V[i] = (num_IP) ? IP[j] : it->first;
4622     }
4623     else {
4624       L[i] = it->first;      // lower bound is first value
4625       U[i] = (--ite)->first; // upper bound is final value
4626       if (num_IP) V[i] = IP[j];
4627       else if (init_V) {
4628 	// OLD: Select the initial value to be closest set value to mean value
4629 	// NEW: Select value from middle or next lower index
4630 	std::advance(it, mid_or_next_lower_index(num_vp_j));
4631 	V[i] = it->first;
4632       }
4633     }
4634   }
4635 }
4636 
4637 static void
Vgen_DSset(size_t num_v,StringRealMapArray & vals_probs,StringArray & IP,StringArray & L,StringArray & U,StringArray & V,bool aggregate_LUV=false,size_t offset=0)4638 Vgen_DSset(size_t num_v, StringRealMapArray& vals_probs, StringArray& IP,
4639 	   StringArray& L, StringArray& U, StringArray& V,
4640 	   bool aggregate_LUV = false, size_t offset = 0)
4641 {
4642   Real avg_val, set_val, s_left, s_right;
4643   SRMCIter ite, it;
4644   size_t i, j, num_vp_j, num_IP = IP.size();
4645 
4646   bool init_V = check_LUV_size(num_v, L, U, V, aggregate_LUV, offset);
4647 
4648   for(i = offset, j = 0; j < num_v; ++i, ++j) {
4649     StringRealMap& vp_j = vals_probs[j];
4650     it = vp_j.begin(); ite = vp_j.end(); num_vp_j = vp_j.size();
4651     if (num_vp_j == 0) { // should not occur
4652       L[i] = U[i] = V[i] = "";
4653       V[i] = (num_IP) ? IP[j] : "";
4654     }
4655     else if (num_vp_j == 1) {
4656       L[i] = U[i] = it->first;
4657       V[i] = (num_IP) ? IP[j] : it->first;
4658     }
4659     else {
4660       L[i] = it->first;     // lower bound is first value
4661       U[i] = (--ite)->first; // upper bound is final value
4662       if (num_IP) V[i] = IP[j];
4663       else if (init_V) {
4664 	std::advance(it, mid_or_next_lower_index(num_vp_j));
4665 	V[i] = it->first;
4666       }
4667     }
4668   }
4669 }
4670 
4671 static void
Vchk_DiscreteDesSetInt(DataVariablesRep * dv,size_t offset,Var_Info * vi)4672 Vchk_DiscreteDesSetInt(DataVariablesRep *dv, size_t offset, Var_Info *vi)
4673 {
4674   static char kind[] = "discrete_design_set_integer";
4675   Vchk_DIset(dv->numDiscreteDesSetIntVars, kind, vi->nddsi, vi->ddsi,
4676 	     dv->discreteDesignSetInt, dv->discreteDesignSetIntVars);
4677   if(vi->ddsia) {
4678     IntArray num_e;
4679     for(size_t i = 0; i < dv->numDiscreteDesSetIntVars; i++)
4680       num_e.push_back(dv->discreteDesignSetInt[i].size());
4681       Vchk_Adjacency(dv->numDiscreteDesSetIntVars, kind, num_e, *vi->ddsia,
4682 		      dv->discreteDesignSetIntAdj);
4683   }
4684 }
4685 
Vgen_DiscreteDesSetInt(DataVariablesRep * dv,size_t offset)4686 static void Vgen_DiscreteDesSetInt(DataVariablesRep *dv, size_t offset)
4687 {
4688   Vgen_DIset(dv->numDiscreteDesSetIntVars, dv->discreteDesignSetInt,
4689 	     dv->discreteDesignSetIntLowerBnds,
4690 	     dv->discreteDesignSetIntUpperBnds,
4691 	     dv->discreteDesignSetIntVars); // no offset, not aggregate L/U/V
4692 }
4693 
4694 static void
Vchk_DiscreteDesSetStr(DataVariablesRep * dv,size_t offset,Var_Info * vi)4695 Vchk_DiscreteDesSetStr(DataVariablesRep *dv, size_t offset, Var_Info *vi)
4696 {
4697   static char kind[] = "discrete_design_set_string";
4698   Vchk_DSset(dv->numDiscreteDesSetStrVars, kind, vi->nddss, vi->ddss,
4699 	     dv->discreteDesignSetStr, dv->discreteDesignSetStrVars);
4700   if(vi->ddssa) {
4701     IntArray num_e;
4702     for(size_t i = 0; i < dv->numDiscreteDesSetStrVars; i++)
4703 	    num_e.push_back(dv->discreteDesignSetStr[i].size());
4704     Vchk_Adjacency(dv->numDiscreteDesSetStrVars, kind, num_e, *vi->ddssa,
4705 		    dv->discreteDesignSetStrAdj);
4706   }
4707 }
4708 
Vgen_DiscreteDesSetStr(DataVariablesRep * dv,size_t offset)4709 static void Vgen_DiscreteDesSetStr(DataVariablesRep *dv, size_t offset)
4710 {
4711   Vgen_DSset(dv->numDiscreteDesSetStrVars, dv->discreteDesignSetStr,
4712 	     dv->discreteDesignSetStrLowerBnds,
4713 	     dv->discreteDesignSetStrUpperBnds,
4714 	     dv->discreteDesignSetStrVars); // no offset, not aggregate L/U/V
4715 }
4716 
4717 static void
Vchk_DiscreteDesSetReal(DataVariablesRep * dv,size_t offset,Var_Info * vi)4718 Vchk_DiscreteDesSetReal(DataVariablesRep *dv, size_t offset, Var_Info *vi)
4719 {
4720   static char kind[] = "discrete_design_set_real";
4721   Vchk_DRset(dv->numDiscreteDesSetRealVars, kind, vi->nddsr, vi->ddsr,
4722 	     dv->discreteDesignSetReal, dv->discreteDesignSetRealVars);
4723   if(vi->ddsra) {
4724     IntArray num_e;
4725     for(size_t i = 0; i < dv->numDiscreteDesSetRealVars; i++)
4726       num_e.push_back(dv->discreteDesignSetReal[i].size());
4727     Vchk_Adjacency(dv->numDiscreteDesSetRealVars, kind, num_e,
4728 		    *vi->ddsra, dv->discreteDesignSetRealAdj);
4729   }
4730 }
4731 
Vgen_DiscreteDesSetReal(DataVariablesRep * dv,size_t offset)4732 static void Vgen_DiscreteDesSetReal(DataVariablesRep *dv, size_t offset)
4733 {
4734   Vgen_DRset(dv->numDiscreteDesSetRealVars, dv->discreteDesignSetReal,
4735 	     dv->discreteDesignSetRealLowerBnds,
4736 	     dv->discreteDesignSetRealUpperBnds,
4737 	     dv->discreteDesignSetRealVars); // no offset, not aggregate L/U/V
4738 }
4739 
4740 static void
Vchk_DiscreteUncSetInt(DataVariablesRep * dv,size_t offset,Var_Info * vi)4741 Vchk_DiscreteUncSetInt(DataVariablesRep *dv, size_t offset, Var_Info *vi)
4742 {
4743   static char kind[] = "discrete_uncertain_set_integer";
4744   Vchk_DIset(dv->numDiscreteUncSetIntVars, kind, vi->ndusi, vi->dusi, vi->DSIp,
4745 	     dv->discreteUncSetIntValuesProbs, dv->discreteUncSetIntVars);
4746 }
4747 
Vgen_DiscreteUncSetInt(DataVariablesRep * dv,size_t offset)4748 static void Vgen_DiscreteUncSetInt(DataVariablesRep *dv, size_t offset)
4749 {
4750   Vgen_DIset(dv->numDiscreteUncSetIntVars, dv->discreteUncSetIntValuesProbs,
4751 	     dv->discreteUncSetIntVars, dv->discreteIntEpistemicUncLowerBnds,
4752 	     dv->discreteIntEpistemicUncUpperBnds,
4753 	     dv->discreteIntEpistemicUncVars, true, offset);
4754   if (dv->discreteUncSetIntVars.length()) dv->uncertainVarsInitPt = true;
4755 }
4756 
4757 static void
Vchk_DiscreteUncSetStr(DataVariablesRep * dv,size_t offset,Var_Info * vi)4758 Vchk_DiscreteUncSetStr(DataVariablesRep *dv, size_t offset, Var_Info *vi)
4759 {
4760   static char kind[] = "discrete_uncertain_set_string";
4761   Vchk_DSset(dv->numDiscreteUncSetStrVars, kind, vi->nduss, vi->duss, vi->DSSp,
4762 	     dv->discreteUncSetStrValuesProbs, dv->discreteUncSetStrVars);
4763 }
4764 
Vgen_DiscreteUncSetStr(DataVariablesRep * dv,size_t offset)4765 static void Vgen_DiscreteUncSetStr(DataVariablesRep *dv, size_t offset)
4766 {
4767   Vgen_DSset(dv->numDiscreteUncSetStrVars, dv->discreteUncSetStrValuesProbs,
4768 	     dv->discreteUncSetStrVars, dv->discreteStrEpistemicUncLowerBnds,
4769 	     dv->discreteStrEpistemicUncUpperBnds,
4770 	     dv->discreteStrEpistemicUncVars, true, offset);
4771   if (dv->discreteUncSetStrVars.size()) dv->uncertainVarsInitPt = true;
4772 }
4773 
4774 static void
Vchk_DiscreteUncSetReal(DataVariablesRep * dv,size_t offset,Var_Info * vi)4775 Vchk_DiscreteUncSetReal(DataVariablesRep *dv, size_t offset, Var_Info *vi)
4776 {
4777   static char kind[] = "discrete_uncertain_set_real";
4778   Vchk_DRset(dv->numDiscreteUncSetRealVars, kind, vi->ndusr, vi->dusr, vi->DSRp,
4779 	     dv->discreteUncSetRealValuesProbs, dv->discreteUncSetRealVars);
4780 }
4781 
Vgen_DiscreteUncSetReal(DataVariablesRep * dv,size_t offset)4782 static void Vgen_DiscreteUncSetReal(DataVariablesRep *dv, size_t offset)
4783 {
4784   Vgen_DRset(dv->numDiscreteUncSetRealVars, dv->discreteUncSetRealValuesProbs,
4785 	     dv->discreteUncSetRealVars, dv->discreteRealEpistemicUncLowerBnds,
4786 	     dv->discreteRealEpistemicUncUpperBnds,
4787 	     dv->discreteRealEpistemicUncVars, true, offset);
4788   if (dv->discreteUncSetRealVars.length()) dv->uncertainVarsInitPt = true;
4789 }
4790 
4791 static void
Vchk_DiscreteStateSetInt(DataVariablesRep * dv,size_t offset,Var_Info * vi)4792 Vchk_DiscreteStateSetInt(DataVariablesRep *dv, size_t offset, Var_Info *vi)
4793 {
4794   static char kind[] = "discrete_state_set_integer";
4795   Vchk_DIset(dv->numDiscreteStateSetIntVars, kind, vi->ndssi, vi->dssi,
4796 	     dv->discreteStateSetInt, dv->discreteStateSetIntVars);
4797 }
4798 
Vgen_DiscreteStateSetInt(DataVariablesRep * dv,size_t offset)4799 static void Vgen_DiscreteStateSetInt(DataVariablesRep *dv, size_t offset)
4800 {
4801   Vgen_DIset(dv->numDiscreteStateSetIntVars, dv->discreteStateSetInt,
4802 	     dv->discreteStateSetIntLowerBnds,
4803 	     dv->discreteStateSetIntUpperBnds,
4804 	     dv->discreteStateSetIntVars); // no offset, not aggregate L/U/V
4805 }
4806 
4807 static void
Vchk_DiscreteStateSetStr(DataVariablesRep * dv,size_t offset,Var_Info * vi)4808 Vchk_DiscreteStateSetStr(DataVariablesRep *dv, size_t offset, Var_Info *vi)
4809 {
4810   static char kind[] = "discrete_state_set_string";
4811   Vchk_DSset(dv->numDiscreteStateSetStrVars, kind, vi->ndsss, vi->dsss,
4812 	     dv->discreteStateSetStr, dv->discreteStateSetStrVars);
4813 }
4814 
Vgen_DiscreteStateSetStr(DataVariablesRep * dv,size_t offset)4815 static void Vgen_DiscreteStateSetStr(DataVariablesRep *dv, size_t offset)
4816 {
4817   Vgen_DSset(dv->numDiscreteStateSetStrVars, dv->discreteStateSetStr,
4818 	     dv->discreteStateSetStrLowerBnds,
4819 	     dv->discreteStateSetStrUpperBnds,
4820 	     dv->discreteStateSetStrVars); // no offset, not aggregate L/U/V
4821 }
4822 
4823 static void
Vchk_DiscreteStateSetReal(DataVariablesRep * dv,size_t offset,Var_Info * vi)4824 Vchk_DiscreteStateSetReal(DataVariablesRep *dv, size_t offset, Var_Info *vi)
4825 {
4826   static char kind[] = "discrete_state_set_real";
4827   Vchk_DRset(dv->numDiscreteStateSetRealVars, kind, vi->ndssr, vi->dssr,
4828 	     dv->discreteStateSetReal, dv->discreteStateSetRealVars);
4829 }
4830 
Vgen_DiscreteStateSetReal(DataVariablesRep * dv,size_t offset)4831 static void Vgen_DiscreteStateSetReal(DataVariablesRep *dv, size_t offset)
4832 {
4833   Vgen_DRset(dv->numDiscreteStateSetRealVars, dv->discreteStateSetReal,
4834 	     dv->discreteStateSetRealLowerBnds,
4835 	     dv->discreteStateSetRealUpperBnds,
4836 	     dv->discreteStateSetRealVars); // no offset, not aggregate L/U/V
4837 }
4838 
4839 static const char *
Var_Name(StringArray * sa,char * buf,size_t i)4840 Var_Name(StringArray *sa, char *buf, size_t i)
4841 {
4842   if (sa)
4843     return (*sa)[i].data();
4844   std::sprintf(buf,"%lu", (UL)(i+1));
4845   return (const char*)buf;
4846 }
4847 
4848 /// For real-valued variables: verify lengths of bounds and initial
4849 /// point, validate bounds and adjust initial point to bounds
Var_RealBoundIPCheck(DataVariablesRep * dv,Var_rcheck * b)4850 static void Var_RealBoundIPCheck(DataVariablesRep *dv, Var_rcheck *b)
4851 {
4852   RealVector *L, *U, *V;
4853   StringArray *sa;
4854   char namebuf[32];
4855   int i, n, ndflt; // length() values here are int rather than size_t
4856 
4857   if ((n = dv->*b->n) == 0)
4858     return;
4859   ndflt = -1;
4860   if (b->L) {
4861     ndflt = 0;
4862     L = &(dv->*b->L);
4863     if (L->length() == 0)
4864       ++ndflt;
4865     else if (L->length() != n) {
4866       Squawk("%s_lower_bounds needs %lu elements, not %lu",
4867 	     b->name, (UL)n, (UL)L->length());
4868       return;
4869     }
4870     U = &(dv->*b->U);
4871     if (U->length() == 0)
4872       ++ndflt;
4873     else if (U->length() != n) {
4874       Squawk("%s_upper_bounds needs %lu elements, not %lu",
4875 	     b->name, (UL)n, (UL)U->length());
4876       return;
4877     }
4878   }
4879   sa = 0;
4880   if (b->Lbl) {
4881     sa = &(dv->*b->Lbl);
4882     if (sa->size() == 0)
4883       sa = 0;
4884   }
4885   if (ndflt == 0)
4886     for(i = 0; i < n; i++) {
4887       if ((*L)[i] > (*U)[i])
4888 	Squawk("lower bound for %s variable %s exceeds upper bound",
4889 	       b->name, Var_Name(sa,namebuf,i));
4890     }
4891   if (b->V == 0)
4892     return;
4893   V = &(dv->*b->V);
4894   if (V->length() == 0)
4895     return;
4896   if (V->length() != n) {
4897     Squawk("initial point for %s needs %lu elements, not %lu",
4898 	   b->name, (UL)n, (UL)V->length());
4899     return;
4900   }
4901   if (L->length() > 0) {
4902     for(i = 0; i < n; i++)
4903       if ((*V)[i] < (*L)[i]) {
4904 	Warn("Setting initial_value for %s variable %s to its lower bound",
4905 	     b->name, Var_Name(sa,namebuf,i));
4906 	(*V)[i] = (*L)[i];
4907       }
4908   }
4909   if (U->length() > 0) {
4910     for(i = 0; i < n; i++)
4911       if ((*V)[i] > (*U)[i]) {
4912 	Warn("Setting initial_value for %s variable %s to its upper bound",
4913 	     b->name, Var_Name(sa,namebuf,i));
4914 	(*V)[i] = (*U)[i];
4915       }
4916   }
4917 }
4918 
4919 /// For integer-valued variables: verify lengths of bounds and initial
4920 /// point, validate bounds and initial point against bounds
Var_IntBoundIPCheck(DataVariablesRep * dv,Var_icheck * ib)4921 static void Var_IntBoundIPCheck(DataVariablesRep *dv, Var_icheck *ib)
4922 {
4923   IntVector *L, *U, *V;
4924   StringArray *sa;
4925   char namebuf[32];
4926   int i, n, ndflt; // length() values here are int rather than size_t
4927 
4928   if ((n = dv->*ib->n) == 0)
4929     return;
4930   L = &(dv->*ib->L);
4931   ndflt = 0;
4932   if (L->length() == 0)
4933     ++ndflt;
4934   else if (L->length() != n) {
4935     Squawk("%s_lower_bounds needs %lu elements, not %lu",
4936 	   ib->name, (UL)n, (UL)L->length());
4937     return;
4938   }
4939   U = &(dv->*ib->U);
4940   if (U->length() == 0)
4941     ++ndflt;
4942   else if (U->length() != n) {
4943     Squawk("%s_upper_bounds needs %lu elements, not %lu",
4944 	   ib->name, (UL)n, (UL)L->length());
4945     return;
4946   }
4947   sa = 0;
4948   if (ib->Lbl) {
4949     sa = &(dv->*ib->Lbl);
4950     if (sa->size() == 0)
4951       sa = 0;
4952   }
4953   if (ndflt == 0)
4954     for(i = 0; i < n; i++) {
4955       if ((*L)[i] > (*U)[i])
4956 	Squawk("lower bound for %s variable %s exceeds upper bound",
4957 	       ib->name, Var_Name(sa,namebuf,i));
4958     }
4959   if (ib->V == 0)
4960     return;	// won't happen for discrete variables
4961   V = &(dv->*ib->V);
4962   if (V->length() == 0)
4963     return;
4964   if (V->length() != n) {
4965     Squawk("initial point for %s needs %lu elements, not %lu",
4966 	   ib->name, (UL)n, (UL)V->length());
4967     return;
4968   }
4969   if (L->length() > 0) {
4970     for(i = 0; i < n; i++)
4971       if ((*V)[i] < (*L)[i]) {
4972 	Warn("Setting initial_value for %s variable %s to its lower bound",
4973 	     ib->name, Var_Name(sa,namebuf,i));
4974 	(*V)[i] = (*L)[i];
4975       }
4976   }
4977   if (U->length() > 0) {
4978     for(i = 0; i < n; i++)
4979       if ((*V)[i] > (*U)[i]) {
4980 	Warn("Setting initial_value for %s variable %s to its upper bound",
4981 	     ib->name, Var_Name(sa,namebuf,i));
4982 	(*V)[i] = (*U)[i];
4983       }
4984   }
4985 }
4986 
4987 
4988 // setup the Vchk functions for each of the uncertain variable
4989 // contiguous containers, and for discrete sets
4990 #define VarLabelInfo(a,b)     { #a, #b, &DataVariablesRep::num##b##Vars, Vchk_##b }
4991 static Var_uinfo CAUVLbl[CAUVar_Nkinds] = {
4992   VarLabelInfo(nuv_, NormalUnc),
4993   VarLabelInfo(lnuv_, LognormalUnc),
4994   VarLabelInfo(uuv_, UniformUnc),
4995   VarLabelInfo(luuv_, LoguniformUnc),
4996   VarLabelInfo(tuv_, TriangularUnc),
4997   VarLabelInfo(euv_, ExponentialUnc),
4998   VarLabelInfo(beuv_, BetaUnc),
4999   VarLabelInfo(gauv_, GammaUnc),
5000   VarLabelInfo(guuv_, GumbelUnc),
5001   VarLabelInfo(fuv_, FrechetUnc),
5002   VarLabelInfo(wuv_, WeibullUnc),
5003   VarLabelInfo(hbuv_, HistogramBinUnc)
5004 };
5005 static Var_uinfo DAUIVLbl[DAUIVar_Nkinds] = {
5006   VarLabelInfo(puv_, PoissonUnc),
5007   VarLabelInfo(biuv_, BinomialUnc),
5008   VarLabelInfo(nbuv_, NegBinomialUnc),
5009   VarLabelInfo(geuv_, GeometricUnc),
5010   VarLabelInfo(hguv_, HyperGeomUnc),
5011   VarLabelInfo(hpiuv_, HistogramPtIntUnc)
5012 };
5013 static Var_uinfo DAUSVLbl[DAUSVar_Nkinds] = {
5014   VarLabelInfo(hpsuv_, HistogramPtStrUnc)
5015 };
5016 static Var_uinfo DAURVLbl[DAURVar_Nkinds] = {
5017   VarLabelInfo(hpruv_, HistogramPtRealUnc)
5018 };
5019 static Var_uinfo CEUVLbl[CEUVar_Nkinds] = {
5020   VarLabelInfo(ciuv_, ContinuousIntervalUnc)
5021 };
5022 static Var_uinfo DEUIVLbl[DEUIVar_Nkinds] = {
5023   VarLabelInfo(diuv_, DiscreteIntervalUnc),
5024   VarLabelInfo(dusiv_, DiscreteUncSetInt)
5025 };
5026 static Var_uinfo DEUSVLbl[DEUSVar_Nkinds] = {
5027   VarLabelInfo(dussv_, DiscreteUncSetStr)
5028 };
5029 static Var_uinfo DEURVLbl[DEURVar_Nkinds] = {
5030   VarLabelInfo(dusrv_, DiscreteUncSetReal)
5031 };
5032 static Var_uinfo DiscSetLbl[DiscSetVar_Nkinds] = {
5033   VarLabelInfo(ddsiv_, DiscreteDesSetInt),
5034   VarLabelInfo(ddssv_, DiscreteDesSetStr),
5035   VarLabelInfo(ddsrv_, DiscreteDesSetReal),
5036   VarLabelInfo(dssiv_, DiscreteStateSetInt),
5037   VarLabelInfo(dsssv_, DiscreteStateSetStr),
5038   VarLabelInfo(dssrv_, DiscreteStateSetReal)
5039 };
5040 #undef VarLabelInfo
5041 
5042 void NIDRProblemDescDB::
var_stop(const char * keyname,Values * val,void ** g,void * v)5043 var_stop(const char *keyname, Values *val, void **g, void *v)
5044 {
5045   // scale_types beyond value to allow for linear constraints
5046   static const char *lincon_scaletypes[] = { "auto", 0 };
5047 
5048   Var_Info *vi = *(Var_Info**)g;
5049   DataVariablesRep *dv = vi->dv;
5050 
5051   scale_chk(dv->continuousDesignScaleTypes, dv->continuousDesignScales,
5052 	    "cdv", auto_log_scaletypes);
5053   scale_chk(dv->linearIneqScaleTypes, dv->linearIneqScales,
5054 	    "linear_inequality", lincon_scaletypes);
5055   scale_chk(dv->linearEqScaleTypes, dv->linearEqScales,
5056 	    "linear_equality", lincon_scaletypes);
5057 
5058   pDDBInstance->VIL.push_back(vi);
5059   pDDBInstance->dataVariablesList.push_back(*vi->dv_handle);
5060   delete vi->dv_handle;
5061 }
5062 
5063 struct VarLabelChk {
5064   size_t DataVariablesRep::* n;
5065   StringArray DataVariablesRep::* sa;
5066   const char *stub;
5067   const char *name;
5068 };
5069 
5070 
5071 // For validation to be performed in check_variables_node(), each
5072 // variable type must be included in at least one of
5073 // DesignAndStateLabelsCheck, LUncertainInt, VLUncertainStr,
5074 // VLUncertainReal, or DiscSetLbl.  Design and state discrete sets are
5075 // currently in both DesignAndStateLabelsCheck (check lengths) and
5076 // DiscSetLbl (duplicates and STL population)
5077 
5078 #define AVI &DataVariablesRep::
5079 /// Variables label array designations for design and state.  All
5080 /// non-uncertain variables need to be in this array.  Used in
5081 /// check_variables_node to check lengths and make_variable_defaults
5082 /// to build labels.
5083 static VarLabelChk DesignAndStateLabelsCheck[] = {
5084   { AVI numContinuousDesVars, AVI continuousDesignLabels, "cdv_", "cdv_descriptors" },
5085   { AVI numDiscreteDesRangeVars, AVI discreteDesignRangeLabels, "ddriv_", "ddriv_descriptors" },
5086   { AVI numDiscreteDesSetIntVars, AVI discreteDesignSetIntLabels, "ddsiv_", "ddsiv_descriptors" },
5087   { AVI numDiscreteDesSetStrVars, AVI discreteDesignSetStrLabels, "ddssv_", "ddssv_descriptors" },
5088   { AVI numDiscreteDesSetRealVars, AVI discreteDesignSetRealLabels, "ddsrv_", "ddsrv_descriptors" },
5089   { AVI numContinuousStateVars, AVI continuousStateLabels, "csv_", "csv_descriptors" },
5090   { AVI numDiscreteStateRangeVars, AVI discreteStateRangeLabels, "dsriv_", "dsriv_descriptors" },
5091   { AVI numDiscreteStateSetIntVars, AVI discreteStateSetIntLabels, "dssiv_", "dssiv_descriptors" },
5092   { AVI numDiscreteStateSetStrVars, AVI discreteStateSetStrLabels, "dsssv_", "dsssv_descriptors" },
5093   { AVI numDiscreteStateSetRealVars, AVI discreteStateSetRealLabels, "dssrv_", "dssrv_descriptors" },
5094   { AVI numContinuousDesVars, AVI continuousDesignScaleTypes, 0, "cdv_scale_types" }
5095 };
5096 #undef  AVI
5097 
5098 /// structure for validating real uncertain variable labels, bounds, values
5099 struct VLreal {
5100   int n;
5101   VarLabel Var_Info::* VL; // should be "VarLabel *Var_Info::* VL"
5102   // but g++ is buggy (versions 4.3.1, 4.4.2 anyway)
5103   Var_uinfo *vui;
5104   StringArray DataVariablesRep::* Labels;
5105   RealVector DataVariablesRep::* LowerBnds;
5106   RealVector DataVariablesRep::* UpperBnds;
5107   RealVector DataVariablesRep::* UncVars;
5108 };
5109 
5110 /// structure for validating integer uncertain variable labels, bounds, values
5111 struct VLint {
5112   int n;
5113   VarLabel Var_Info::* VL; // should be "VarLabel *Var_Info::* VL"
5114   // but g++ is buggy (versions 4.3.1, 4.4.2 anyway)
5115   Var_uinfo *vui;
5116   StringArray DataVariablesRep::* Labels;
5117   IntVector DataVariablesRep::* LowerBnds;
5118   IntVector DataVariablesRep::* UpperBnds;
5119   IntVector DataVariablesRep::* UncVars;
5120 };
5121 
5122 /// structure for validating string uncertain variable labels, bounds, values
5123 struct VLstr {
5124   int n;
5125   VarLabel Var_Info::* VL; // should be "VarLabel *Var_Info::* VL"
5126   // but g++ is buggy (versions 4.3.1, 4.4.2 anyway)
5127   Var_uinfo *vui;
5128   StringArray DataVariablesRep::* Labels;
5129   StringArray DataVariablesRep::* LowerBnds;
5130   StringArray DataVariablesRep::* UpperBnds;
5131   StringArray DataVariablesRep::* UncVars;
5132 };
5133 
5134 /// number of real-valued   uncertain contiguous containers
5135 enum { NUM_UNC_REAL_CONT = 4 };
5136 /// number of int-valued    uncertain contiguous containers
5137 enum { NUM_UNC_INT_CONT = 2 };
5138 /// number of string-valued uncertain contiguous containers
5139 enum { NUM_UNC_STR_CONT = 2 };
5140 
5141 #define AVI (VarLabel Var_Info::*) &Var_Info::	// cast to bypass g++ bug
5142 #define DVR &DataVariablesRep::
5143 
5144 /// Variables labels/bounds/values check array for real-valued
5145 /// uncertain variables; one array entry per contiguous container.
5146 /// These associate the individual variables given by, e.g., CAUVLbl,
5147 /// with the contiguous container in which they are stored.
5148 static VLreal VLUncertainReal[NUM_UNC_REAL_CONT] = {
5149  {CAUVar_Nkinds,  AVI CAUv,  CAUVLbl,
5150 	DVR continuousAleatoryUncLabels,
5151 	DVR continuousAleatoryUncLowerBnds,
5152 	DVR continuousAleatoryUncUpperBnds,
5153 	DVR continuousAleatoryUncVars},
5154  {CEUVar_Nkinds,  AVI CEUv,  CEUVLbl,
5155 	DVR continuousEpistemicUncLabels,
5156 	DVR continuousEpistemicUncLowerBnds,
5157 	DVR continuousEpistemicUncUpperBnds,
5158 	DVR continuousEpistemicUncVars},
5159  {DAURVar_Nkinds, AVI DAURv, DAURVLbl,
5160 	DVR discreteRealAleatoryUncLabels,
5161 	DVR discreteRealAleatoryUncLowerBnds,
5162 	DVR discreteRealAleatoryUncUpperBnds,
5163         DVR discreteRealAleatoryUncVars},
5164  {DEURVar_Nkinds, AVI DEURv, DEURVLbl,
5165 	DVR discreteRealEpistemicUncLabels,
5166 	DVR discreteRealEpistemicUncLowerBnds,
5167 	DVR discreteRealEpistemicUncUpperBnds,
5168 	DVR discreteRealEpistemicUncVars}};
5169 
5170 /// Variables labels/bounds/values check array for integer-valued
5171 /// uncertain variables; one array entry per contiguous container.
5172 /// These associate the individual variables given by, e.g., DAUIVLbl,
5173 /// with the contiguous container in which they are stored.
5174 static VLint VLUncertainInt[NUM_UNC_INT_CONT] = {
5175  {DAUIVar_Nkinds, AVI DAUIv, DAUIVLbl,
5176 	DVR discreteIntAleatoryUncLabels,
5177         DVR discreteIntAleatoryUncLowerBnds,
5178         DVR discreteIntAleatoryUncUpperBnds,
5179         DVR discreteIntAleatoryUncVars},
5180  {DEUIVar_Nkinds, AVI DEUIv, DEUIVLbl,
5181 	DVR discreteIntEpistemicUncLabels,
5182 	DVR discreteIntEpistemicUncLowerBnds,
5183 	DVR discreteIntEpistemicUncUpperBnds,
5184 	DVR discreteIntEpistemicUncVars}};
5185 
5186 /// Variables labels/bounds/values check array for string-valued
5187 /// uncertain variables; one array entry per contiguous container.
5188 /// These associate the individual variables given by, e.g., DAUSVLbl,
5189 /// with the contiguous container in which they are stored.
5190 static VLstr VLUncertainStr[NUM_UNC_STR_CONT] = {
5191  {DAUSVar_Nkinds, AVI DAUSv, DAUSVLbl,
5192 	DVR discreteStrAleatoryUncLabels,
5193 	DVR discreteStrAleatoryUncLowerBnds,
5194 	DVR discreteStrAleatoryUncUpperBnds,
5195         DVR discreteStrAleatoryUncVars},
5196  {DEUSVar_Nkinds, AVI DEUSv, DEUSVLbl,
5197 	DVR discreteStrEpistemicUncLabels,
5198 	DVR discreteStrEpistemicUncLowerBnds,
5199 	DVR discreteStrEpistemicUncUpperBnds,
5200 	DVR discreteStrEpistemicUncVars}};
5201 
5202 //#undef RDVR
5203 #undef	DVR
5204 #undef  AVI
5205 
5206 /// which uncertain real check array containers are aleatory (true = 1)
5207 static int VLR_aleatory[NUM_UNC_REAL_CONT] = { 1, 0, 1, 0 };
5208 /// which uncertain integer check array containers are aleatory (true = 1)
5209 static int VLI_aleatory[NUM_UNC_INT_CONT] = { 1, 0 };
5210 /// which uncertain string check array containers are aleatory (true = 1)
5211 static int VLS_aleatory[NUM_UNC_STR_CONT] = { 1, 0 };
5212 
5213 
5214 /// Generate check data for variables with just name, size, and a
5215 /// generator function
5216 #define Vchk_3(x,y) {#x,&DataVariablesRep::num##y##Vars,Vgen_##y}
5217 
5218 /// Generate check data for variables additionally with lower and
5219 /// upper bounds.  Some compilers in debug mode (MSVC) don't
5220 /// initialize the trailing two entries to NULL, so be explicit:
5221 #define Vchk_5(x,y,z) {#x,&DataVariablesRep::num##y##Vars,Vgen_##y,&DataVariablesRep::z##LowerBnds,&DataVariablesRep::z##UpperBnds,NULL,NULL}
5222 
5223 /// Generate check data for variables additionally with initial point
5224 /// and labels
5225 #define Vchk_7(x,y,z) {#x,&DataVariablesRep::num##y##Vars,Vgen_##y,&DataVariablesRep::z##LowerBnds,&DataVariablesRep::z##UpperBnds,&DataVariablesRep::z##Vars,&DataVariablesRep::z##Labels}
5226 
5227 // Trailing pointers in these initialization lists will be NULL. From C++ 2003:
5228 //   "Objects with static storage shall be zero-initialized before any other
5229 //    initialization takes place."
5230 
5231 // These are used within make_variable_defaults(): Vgen_##y is applied
5232 // to generate bounds, adjust initial values
5233 static Var_check
5234   var_mp_check_cv[] = {
5235 	Vchk_3(continuous_design,ContinuousDes),
5236 	Vchk_3(continuous_state,ContinuousState) },
5237   var_mp_check_dset[] = {
5238 	Vchk_3(discrete_design_set_integer,DiscreteDesSetInt),
5239 	Vchk_3(discrete_design_set_string,DiscreteDesSetStr),
5240 	Vchk_3(discrete_design_set_real,DiscreteDesSetReal),
5241 	Vchk_3(discrete_state_set_integer,DiscreteStateSetInt),
5242 	Vchk_3(discrete_state_set_string,DiscreteStateSetStr),
5243 	Vchk_3(discrete_state_set_real,DiscreteStateSetReal) },
5244   var_mp_check_cau[] = {
5245 	Vchk_3(normal_uncertain,NormalUnc),
5246 	Vchk_3(lognormal_uncertain,LognormalUnc),
5247 	Vchk_3(uniform_uncertain,UniformUnc),
5248 	Vchk_3(loguniform_uncertain,LoguniformUnc),
5249 	Vchk_3(triangular_uncertain,TriangularUnc),
5250 	Vchk_3(exponential_uncertain,ExponentialUnc),
5251 	Vchk_3(beta_uncertain,BetaUnc),
5252 	Vchk_3(gamma_uncertain,GammaUnc),
5253 	Vchk_3(gumbel_uncertain,GumbelUnc),
5254 	Vchk_3(frechet_uncertain,FrechetUnc),
5255 	Vchk_3(weibull_uncertain,WeibullUnc),
5256 	Vchk_3(histogram_bin_uncertain,HistogramBinUnc) },
5257   var_mp_check_daui[] = {
5258 	Vchk_3(poisson_uncertain,PoissonUnc),
5259 	Vchk_3(binomial_uncertain,BinomialUnc),
5260 	Vchk_3(negative_binomial_uncertain,NegBinomialUnc),
5261 	Vchk_3(geometric_uncertain,GeometricUnc),
5262 	Vchk_3(hypergeometric_uncertain,HyperGeomUnc),
5263 	Vchk_3(histogram_point_int_uncertain,HistogramPtIntUnc) },
5264   var_mp_check_daus[] = {
5265 	Vchk_3(histogram_point_str_uncertain,HistogramPtStrUnc) },
5266   var_mp_check_daur[] = {
5267 	Vchk_3(histogram_point_real_uncertain,HistogramPtRealUnc) },
5268   var_mp_check_ceu[] = {
5269 	Vchk_3(continuous_interval_uncertain,ContinuousIntervalUnc) },
5270   var_mp_check_deui[] = {
5271 	Vchk_3(discrete_interval_uncertain,DiscreteIntervalUnc),
5272 	Vchk_3(discrete_uncertain_set_integer,DiscreteUncSetInt) },
5273   var_mp_check_deus[] = {
5274         Vchk_3(discrete_uncertain_set_string,DiscreteUncSetStr) },
5275   var_mp_check_deur[] = {
5276 	Vchk_3(discrete_uncertain_set_real,DiscreteUncSetReal) };
5277 
5278 /// This is used within check_variables_node(): Var_RealBoundIPCheck()
5279 /// is applied to validate bounds and initial points
5280 static Var_rcheck
5281   var_mp_cbound[] = {
5282 	Vchk_7(continuous_design,ContinuousDes,continuousDesign),
5283 	Vchk_7(continuous_state,ContinuousState,continuousState),
5284 	// BMA: I believe these should these be promoted to Vchk_7,
5285 	// but not easy to do since their labels are stored in an
5286 	// aggregated array instead of individually
5287 	Vchk_5(normal_uncertain,NormalUnc,normalUnc),
5288 	Vchk_5(lognormal_uncertain,LognormalUnc,lognormalUnc),
5289 	Vchk_5(uniform_uncertain,UniformUnc,uniformUnc),
5290 	Vchk_5(loguniform_uncertain,LoguniformUnc,loguniformUnc),
5291 	Vchk_5(triangular_uncertain,TriangularUnc,triangularUnc),
5292 	Vchk_5(beta_uncertain,BetaUnc,betaUnc) };
5293 // gamma, gumbel, frechet, weibull, histogram_bin don't support bounds
5294 // from user input, so are omitted here?
5295 
5296 /// This is used in check_variables_node(): Var_IntBoundIPCheck() is
5297 /// applied to validate bounds and initial points, and in
5298 /// make_variable_defaults(): Vgen_* is called to infer bounds.
5299 static Var_icheck
5300   var_mp_drange[] = {
5301 	Vchk_7(discrete_design_range,DiscreteDesRange,discreteDesignRange),
5302 	Vchk_7(discrete_state_range,DiscreteStateRange,discreteStateRange) };
5303 
5304 // would be used to check initial point, but those are covered in DiscSetLbl
5305 // static Var_scheck
5306 // var_mp_sbound[] = {
5307 //   Vchk_7(discrete_design_set_string,DiscreteDesSetStr,discreteDesignSetStr) };
5308 
5309 #undef Vchk_7
5310 #undef Vchk_5
5311 #undef Vchk_3
5312 
5313 
5314 /** Size arrays for contiguous storage of aggregated uncertain types.
5315     For each variable type, call Vgen_* to generate inferred bounds
5316     and initial point, repairing initial if needed. */
5317 void NIDRProblemDescDB::
make_variable_defaults(std::list<DataVariables> * dvl)5318 make_variable_defaults(std::list<DataVariables>* dvl)
5319 {
5320   DataVariablesRep *dv;
5321   IntVector *IL, *IU, *IV;
5322   StringArray *SL, *SU, *SV;
5323   RealVector *L, *U, *V;
5324   StringArray *sa;
5325   VLreal *vlr;
5326   VLint  *vli;
5327   VLstr  *vls;
5328   VarLabel *vl;
5329   VarLabelChk *vlc, *vlce;
5330   Var_uinfo *vui, *vuie;
5331   char buf[32];
5332   size_t i, j, k, n, nu, nursave[NUM_UNC_REAL_CONT], nuisave[NUM_UNC_INT_CONT],
5333     nussave[NUM_UNC_STR_CONT];
5334   static const char Inconsistent_bounds[] =
5335     "Inconsistent bounds on %s uncertain variables";
5336 
5337   /// size the aggregate arrays for uncertain (design and state are
5338   /// stored separately
5339   std::list<DataVariables>::iterator It = dvl->begin(), Ite = dvl->end();
5340   for(; It != Ite; ++It) {
5341     dv = It->dataVarsRep.get();
5342     // size the aggregate labels, bounds, values arrays for
5343     // real-valued uncertain
5344     for(k = 0; k < NUM_UNC_REAL_CONT; ++k) {
5345       vlr = &VLUncertainReal[k];
5346       vui = vlr->vui;
5347       vuie = vui + vlr->n;
5348       for(nu = 0; vui < vuie; ++vui)
5349 	nu += dv->*vui->n;
5350       nursave[k] = nu;
5351       if (!nu)
5352 	continue;
5353       L = &(dv->*vlr->LowerBnds);
5354       U = &(dv->*vlr->UpperBnds);
5355       V = &(dv->*vlr->UncVars);
5356       L->sizeUninitialized(nu);
5357       U->sizeUninitialized(nu);
5358       V->sizeUninitialized(nu);
5359     }
5360     // size the aggregate labels, bounds, values arrays for
5361     // integer-valued uncertain
5362     for(k = 0; k < NUM_UNC_INT_CONT; ++k) {
5363       vli = &VLUncertainInt[k];
5364       vui = vli->vui;
5365       vuie = vui + vli->n;
5366       for(nu = 0; vui < vuie; ++vui)
5367 	nu += dv->*vui->n;
5368       nuisave[k] = nu;
5369       if (!nu)
5370 	continue;
5371       IL = &(dv->*vli->LowerBnds);
5372       IU = &(dv->*vli->UpperBnds);
5373       IV = &(dv->*vli->UncVars);
5374       IL->sizeUninitialized(nu);
5375       IU->sizeUninitialized(nu);
5376       IV->sizeUninitialized(nu);
5377     }
5378     // size the aggregate labels, bounds, values arrays for
5379     // string-valued uncertain
5380     for(k = 0; k < NUM_UNC_STR_CONT; ++k) {
5381       vls = &VLUncertainStr[k];
5382       vui = vls->vui;
5383       vuie = vui + vls->n;
5384       for(nu = 0; vui < vuie; ++vui)
5385 	nu += dv->*vui->n;
5386       nussave[k] = nu;
5387       if (!nu)
5388 	continue;
5389       SL = &(dv->*vls->LowerBnds);
5390       SU = &(dv->*vls->UpperBnds);
5391       SV = &(dv->*vls->UncVars);
5392       SL->resize(nu);
5393       SU->resize(nu);
5394       SV->resize(nu);
5395     }
5396 
5397     // inferred bound generation for continuous variable types;
5398     // populates the individual (design/state) or aggregate
5399     // (uncertain) arrays of bounds and values by applying Vgen_##y
5400 
5401     // loop over cdv/csv
5402     Var_check *c, *ce;
5403     // the offset into the aggregated aleatory or epistemic arrays
5404     size_t offset = 0;
5405     for(c=var_mp_check_cv, ce = c + Numberof(var_mp_check_cv); c < ce; ++c)
5406       if ((n = dv->*c->n) > 0)
5407 	(*c->vgen)(dv, offset); // offset not used
5408     // loop over nuv/lnuv/uuv/luuv/truv/euv/buv/gauv/guuv/fuv/wuv/hbuv
5409     // working with continuousAleatoryUnc*
5410     for(c=var_mp_check_cau, ce=c + Numberof(var_mp_check_cau); c < ce; ++c)
5411       if ((n = dv->*c->n) > 0)
5412 	{ (*c->vgen)(dv, offset); offset += n; }
5413     // continuous epistemic uncertain
5414     offset = 0; // reset for combined epistemic arrays
5415     for(c=var_mp_check_ceu, ce=c + Numberof(var_mp_check_ceu); c < ce; ++c)
5416       if ((n = dv->*c->n) > 0)
5417 	{ (*c->vgen)(dv, offset); offset += n; }
5418 
5419     // inferred bound generation for discrete variable types;
5420     // populates the individual (design/state) or aggregate
5421     // (uncertain) arrays of bounds and values by applying Vgen_##y
5422 
5423     // discrete design,state ranges
5424     Var_icheck *ic, *ice;
5425     offset = 0;
5426     for(ic=var_mp_drange, ice=ic + Numberof(var_mp_drange); ic<ice; ++ic)
5427       if ((n = dv->*ic->n) > 0)
5428 	{ (*ic->vgen)(dv, offset); } // offset not used
5429     // discrete int aleatory uncertain use offset passed into Vgen_*Unc
5430     // working with discreteIntAleatoryUnc*
5431     for(c=var_mp_check_daui, ce=c + Numberof(var_mp_check_daui); c<ce; ++c)
5432       if ((n = dv->*c->n) > 0)
5433 	{ (*c->vgen)(dv, offset); offset += n; }
5434     // discrete string aleatory uncertain use offset passed into Vgen_*Unc
5435     // working with discreteStrAleatoryUnc*
5436     offset = 0.;
5437     for(c=var_mp_check_daus, ce=c + Numberof(var_mp_check_daus); c<ce; ++c)
5438       if ((n = dv->*c->n) > 0)
5439 	{ (*c->vgen)(dv, offset); offset += n; }
5440     // discrete real aleatory uncertain use offset passed into Vgen_*Unc
5441     // working with discreteRealAleatoryUnc*
5442     offset = 0;
5443     for(c=var_mp_check_daur, ce=c + Numberof(var_mp_check_daur); c<ce; ++c)
5444       if ((n = dv->*c->n) > 0)
5445 	{ (*c->vgen)(dv, offset); offset += n; }
5446     // discrete int epistemic uncertain use offset passed into Vgen_*Unc
5447     // working with discreteIntEpistemicUnc*
5448     offset = 0;
5449     for(c=var_mp_check_deui, ce=c + Numberof(var_mp_check_deui); c<ce; ++c)
5450       if ((n = dv->*c->n) > 0)
5451 	{ (*c->vgen)(dv, offset); offset += n; }
5452     // discrete string epistemic uncertain use offset passed into Vgen_*Unc
5453     // working with discreteStrEpistemicUnc*
5454     offset = 0;
5455     for(c=var_mp_check_deus, ce=c + Numberof(var_mp_check_deus); c<ce; ++c)
5456       if ((n = dv->*c->n) > 0)
5457 	{ (*c->vgen)(dv, offset); offset += n; }
5458     // discrete real epistemic uncertain use offset passed into Vgen_*Unc
5459     // working with discreteRealEpistemicUnc*
5460     offset = 0;
5461     for(c=var_mp_check_deur, ce=c + Numberof(var_mp_check_deur); c<ce; ++c)
5462       if ((n = dv->*c->n) > 0)
5463 	{ (*c->vgen)(dv, offset); offset += n; }
5464 
5465     // check discrete design and state set types
5466     // these don't use an offset passed into Vgen_Discrete*Set*
5467     offset = 0;
5468     for(c=var_mp_check_dset, ce=c + Numberof(var_mp_check_dset); c<ce; ++c)
5469       if ((n = dv->*c->n) > 0)
5470 	(*c->vgen)(dv, offset); // offset not used
5471 
5472 
5473     // Validate bounds (again?) for uncertain variables and set
5474     // default labels for all types
5475 
5476     // uncertain real: bounds check and label generation
5477     for(k = 0; k < NUM_UNC_REAL_CONT; ++k) {
5478       nu = nursave[k];
5479       if (!nu)
5480 	continue;
5481       vlr = &VLUncertainReal[k];
5482       vui = vlr->vui;
5483       vuie = vui + vlr->n;
5484       L = &(dv->*vlr->LowerBnds);
5485       U = &(dv->*vlr->UpperBnds);
5486       V = &(dv->*vlr->UncVars);
5487       sa = &(dv->*vlr->Labels);
5488       if (!sa->size())
5489 	sa->resize(nu);
5490       i = 0;
5491       for(vui = vlr->vui; vui < vuie; ++vui) {
5492 	if ((n = dv->*vui->n) == 0)
5493 	  continue;
5494 	for(j = 0; j < n; ++j)
5495 	  if ((*L)[i+j] > (*U)[i+j]) {
5496 	    squawk(Inconsistent_bounds, vui->vkind);
5497 	    break;
5498 	  }
5499 	if ((*sa)[i] == "")
5500 	  for(j = 1; j <= n; ++j) {
5501 	    std::sprintf(buf, "%s%d", vui->lbl, (int)j);
5502 	    (*sa)[i++] = buf;
5503 	  }
5504 	else
5505 	  i += n;
5506       }
5507     }
5508 
5509     // uncertain integer: bounds check and label generation
5510     for(k = 0; k < NUM_UNC_INT_CONT; ++k) {
5511       nu = nuisave[k];
5512       if (!nu)
5513 	continue;
5514       vli = &VLUncertainInt[k];
5515       vui = vli->vui;
5516       vuie = vui + vli->n;
5517       IL = &(dv->*vli->LowerBnds);
5518       IU = &(dv->*vli->UpperBnds);
5519       IV = &(dv->*vli->UncVars);
5520       sa = &(dv->*vli->Labels);
5521       if (!sa->size())
5522 	sa->resize(nu);
5523       i = 0;
5524       for(vui = vli->vui; vui < vuie; ++vui) {
5525 	if ((n = dv->*vui->n) == 0)
5526 	  continue;
5527 	for(j = 0; j < n; ++j)
5528 	  if ((*IL)[i+j] > (*IU)[i+j]) {
5529 	    squawk(Inconsistent_bounds, vui->vkind);
5530 	    break;
5531 	  }
5532 	if ((*sa)[i] == "")
5533 	  for(j = 1; j <= n; ++j) {
5534 	    std::sprintf(buf, "%s%d", vui->lbl, (int)j);
5535 	    (*sa)[i++] = buf;
5536 	  }
5537 	else
5538 	  i += n;
5539       }
5540     }
5541 
5542     // uncertain string: bounds check and label generation
5543     for(k = 0; k < NUM_UNC_STR_CONT; ++k) {
5544       nu = nussave[k];
5545       if (!nu)
5546 	continue;
5547       vls = &VLUncertainStr[k];
5548       vui = vls->vui;
5549       vuie = vui + vls->n;
5550       SL = &(dv->*vls->LowerBnds);
5551       SU = &(dv->*vls->UpperBnds);
5552       SV = &(dv->*vls->UncVars);
5553       sa = &(dv->*vls->Labels);
5554       if (!sa->size())
5555 	sa->resize(nu);
5556       i = 0;
5557       for(vui = vls->vui; vui < vuie; ++vui) {
5558 	if ((n = dv->*vui->n) == 0)
5559 	  continue;
5560 	for(j = 0; j < n; ++j)
5561 	  if ((*SL)[i+j] > (*SU)[i+j]) {
5562 	    squawk(Inconsistent_bounds, vui->vkind);
5563 	    break;
5564 	  }
5565 	if ((*sa)[i] == "")
5566 	  for(j = 1; j <= n; ++j) {
5567 	    std::sprintf(buf, "%s%d", vui->lbl, (int)j);
5568 	    (*sa)[i++] = buf;
5569 	  }
5570 	else
5571 	  i += n;
5572       }
5573     }
5574 
5575     // build labels for all design and state variables if needed
5576     for(vlc = DesignAndStateLabelsCheck, vlce = vlc + Numberof(DesignAndStateLabelsCheck); vlc < vlce; ++vlc)
5577       if (vlc->stub && (n = dv->*vlc->n)) {
5578 	sa = &(dv->*vlc->sa);
5579 	if (sa->size() == 0)
5580 	  BuildLabels(sa, n, 0, n, vlc->stub);
5581       }
5582   }
5583 }
5584 
check_variables_node(void * v)5585 void NIDRProblemDescDB::check_variables_node(void *v)
5586 {
5587   IntArray *Ia; IntVector *Iv; RealVector *Rv; RealSymMatrix *Rm;
5588   StringArray *sa, *Sa;
5589   VLreal *vlr; VLint *vli; VLstr *vls;
5590   VarLabel *vl;
5591   VarLabelChk *vlc, *vlce;
5592   Var_uinfo *vui, *vuie;
5593   const char **sp;
5594   int havelabels;
5595   size_t i, j, k, n, nd, nu, nuk, nutot, nv;
5596 
5597 #define AVI &Var_Info::
5598   // Used for deallocation of Var_Info temporary data
5599   static IntArray   *Var_Info::* Ia_delete[]
5600     = { AVI nddsi, AVI nddss, AVI nddsr, AVI nCI, AVI nDI, AVI nhbp,
5601 	AVI nhpip, AVI nhpsp, AVI nhprp, AVI ndusi, AVI nduss, AVI ndusr,
5602 	AVI ndssi, AVI ndsss, AVI ndssr };
5603   static RealVector *Var_Info::* Rv_delete[]
5604     = { AVI ddsr, AVI CIlb, AVI CIub, AVI CIp, AVI DIp,
5605 	AVI DSIp, AVI DSSp, AVI DSRp,
5606 	AVI dusr, AVI hba, AVI hbo, AVI hbc,
5607 	AVI hpic, AVI hpsc, AVI hpra, AVI hprc,
5608 	AVI ucm,
5609 	AVI dssr };
5610   static IntVector *Var_Info::* Iv_delete[]
5611     = { AVI ddsi, AVI DIlb, AVI DIub, AVI hpia, AVI dusi, AVI dssi,
5612         AVI ddsia, AVI ddssa, AVI ddsra };
5613   static StringArray *Var_Info::* Sa_delete[]
5614     = { AVI ddss, AVI hpsa, AVI duss, AVI dsss };
5615 #undef AVI
5616 
5617   Var_Info *vi = (Var_Info*)v;
5618   DataVariablesRep *dv = vi->dv;
5619 
5620   // check label lengths for design and state variables, if present
5621   nv = 0;
5622   for(vlc = DesignAndStateLabelsCheck, vlce = vlc + Numberof(DesignAndStateLabelsCheck);
5623       vlc < vlce; ++vlc)
5624     if ((n = dv->*vlc->n)) {
5625       if (vlc->stub)
5626 	nv += n;
5627       sa = &(dv->*vlc->sa);
5628       if ((nu = sa->size())) {
5629 	if (nu != n) {
5630 	  if (nu == 1 && !vlc->stub)
5631 	    continue;
5632 	  squawk("Found %lu rather than %lu %s values",
5633 		 (UL)nu, (UL)n, vlc->name);
5634 	}
5635       }
5636     }
5637 
5638   // Now check uncertain variables.  Calls the Vchk_* functions (e.g.,
5639   // Vchk_NormalUnc) for each variable type, which for simple
5640   // uncertain variables check lengths, but for histograms and
5641   // intervals, translate the data into arrays of maps.
5642 
5643   // uncertain real
5644   for(k = nu = nutot = 0; k < NUM_UNC_REAL_CONT; ++k) {
5645     vlr = &VLUncertainReal[k];
5646     havelabels = 0;
5647     nuk = 0;
5648     vl = &(vi->*vlr->VL);	// "&(...)" to bypass a g++ bug
5649     vui = vlr->vui;
5650     for(vuie = vui + vlr->n; vui < vuie; ++vl, ++vui) {
5651       nuk += dv->*vui->n;
5652       if (vl->s)
5653 	++havelabels;
5654     }
5655     if (nuk > 0) {
5656       nutot += nuk;
5657       if (VLR_aleatory[k])
5658 	nu += nuk;
5659       if (havelabels)
5660 	(sa = &(dv->*vlr->Labels))->resize(nuk);
5661       i = 0;
5662       vl = &(vi->*vlr->VL);
5663       for(vui = vlr->vui; vui < vuie; ++vl, ++vui) {
5664 	if ((n = dv->*vui->n) == 0)
5665 	  continue;
5666 	vui->vchk(dv,i,vi);
5667 	if ((sp = vl->s)) {
5668 	  if (vl->n != n)
5669 	    squawk("Expected %d %s_descriptors, but got %d",
5670 		   n, vui->lbl, vl->n);
5671 	  else
5672 	    for(j = 0; j < n; ++i, ++j)
5673 	      (*sa)[i] = sp[j];
5674 	  free(sp);
5675 	}
5676 	else
5677 	  i += n;
5678       }
5679     }
5680   }
5681 
5682   // uncertain integer
5683   for(k = 0; k < NUM_UNC_INT_CONT; ++k) {
5684     vli = &VLUncertainInt[k];
5685     havelabels = 0;
5686     nuk = 0;
5687     vl = &(vi->*vli->VL);	// "&(...)" to bypass a g++ bug
5688     vui = vli->vui;
5689     for(vuie = vui + vli->n; vui < vuie; ++vl, ++vui) {
5690       nuk += dv->*vui->n;
5691       if (vl->s)
5692 	++havelabels;
5693     }
5694     if (nuk > 0) {
5695       nutot += nuk;
5696       if (VLI_aleatory[k])
5697 	nu += nuk;
5698       if (havelabels)
5699 	(sa = &(dv->*vli->Labels))->resize(nuk);
5700       i = 0;
5701       vl = &(vi->*vli->VL);
5702       for(vui = vli->vui; vui < vuie; ++vl, ++vui) {
5703 	if ((n = dv->*vui->n) == 0)
5704 	  continue;
5705 	vui->vchk(dv,i,vi);
5706 	if ((sp = vl->s)) {
5707 	  if (vl->n != n)
5708 	    squawk("Expected %d %s_descriptors, but got %d",
5709 		   n, vui->lbl, vl->n);
5710 	  else
5711 	    for(j = 0; j < n; ++i, ++j)
5712 	      (*sa)[i] = sp[j];
5713 	  free(sp);
5714 	}
5715 	else
5716 	  i += n;
5717       }
5718     }
5719   }
5720 
5721   // uncertain string
5722   for(k = 0; k < NUM_UNC_STR_CONT; ++k) {
5723     vls = &VLUncertainStr[k];
5724     havelabels = 0;
5725     nuk = 0;
5726     vl = &(vi->*vls->VL);	// "&(...)" to bypass a g++ bug
5727     vui = vls->vui;
5728     for(vuie = vui + vls->n; vui < vuie; ++vl, ++vui) {
5729       nuk += dv->*vui->n;
5730       if (vl->s)
5731 	++havelabels;
5732     }
5733     if (nuk > 0) {
5734       nutot += nuk;
5735       if (VLS_aleatory[k])
5736 	nu += nuk;
5737       if (havelabels)
5738 	(sa = &(dv->*vls->Labels))->resize(nuk);
5739       i = 0;
5740       vl = &(vi->*vls->VL);
5741       for(vui = vls->vui; vui < vuie; ++vl, ++vui) {
5742 	if ((n = dv->*vui->n) == 0)
5743 	  continue;
5744 	vui->vchk(dv,i,vi);
5745 	if ((sp = vl->s)) {
5746 	  if (vl->n != n)
5747 	    squawk("Expected %d %s_descriptors, but got %d",
5748 		   n, vui->lbl, vl->n);
5749 	  else
5750 	    for(j = 0; j < n; ++i, ++j)
5751 	      (*sa)[i] = sp[j];
5752 	  free(sp);
5753 	}
5754 	else
5755 	  i += n;
5756       }
5757     }
5758   }
5759 
5760   // uncertain_correlation_matrix
5761   if ((Rv = vi->ucm)) {
5762     n = Rv->length();
5763     if (n != nu*nu) {
5764       static char ucmerr[]
5765 	= "Got %lu entries for the uncertain_correlation_matrix\n\
5766 	but needed %lu for %lu uncertain variables";
5767       squawk(ucmerr, (UL)n, (UL)nu*nu, (UL)nu);
5768     }
5769     else {
5770       Rm = &dv->uncertainCorrelations;
5771       Rm->reshape(nu);
5772       for(i = k = 0; i < nu; ++i) {
5773 	for(j = 0; j < nu;)
5774 	  (*Rm)(i,j++) = (*Rv)[k++];
5775       }
5776     }
5777   }
5778 
5779   // The above functions don't validate the initial point; those below do
5780 
5781   // Check discrete design and state set variables.  Call Vchk_* for
5782   // the discrete design and state set (integer/string/real) types,
5783   // e.g, Vchk_DiscreteDesSetInt; these check for duplicates, validate
5784   // that initial point is in the set, and populate arrays of maps or
5785   // other STL data structures
5786   nd = 0;
5787   for(vui=DiscSetLbl, vuie=DiscSetLbl+DiscSetVar_Nkinds; vui<vuie; ++vui) {
5788     if ((n = dv->*vui->n) > 0) {
5789       vui->vchk(dv,0,vi);
5790       nd += n;
5791     }
5792   }
5793 
5794   if (nd + nv + nutot == 0)
5795     squawk("at least one variable must be specified");
5796 
5797   // Check bounds and adjust initial points for remaining types.
5798 
5799   // TODO: these don't support explicit bounds, but their initial
5800   // point could still be validated here: gamma, gumbel, frechet,
5801   // weibull, histogram_bin, poisson, binomial, negative_binomial,
5802   // geometric, hypergeometric, histogram_point
5803 
5804   // TODO: continuous and discrete interval, initial point not checked
5805   // anywhere?
5806 
5807   // Continuous bound specs: cdv/csv/nuv/lnuv/uuv/luuv/truv/buv
5808   Var_rcheck *rc, *rce;
5809   for(rc = var_mp_cbound, rce = rc + Numberof(var_mp_cbound); rc < rce; ++rc)
5810     Var_RealBoundIPCheck(dv, rc);
5811 
5812   // Discrete bound specs: design,state ranges
5813   Var_icheck *ic, *ice;
5814   for(ic = var_mp_drange, ice = ic + Numberof(var_mp_drange); ic < ice; ++ic)
5815     Var_IntBoundIPCheck(dv, ic);
5816 
5817 
5818   // deallocate temporary Var_Info data
5819   n = Numberof(Ia_delete);
5820   for(i = 0; i < n; i++)
5821     if ((Ia = vi->*Ia_delete[i]))
5822       delete Ia;
5823   n = Numberof(Rv_delete);
5824   for(i = 0; i < n; i++)
5825     if ((Rv = vi->*Rv_delete[i]))
5826       delete Rv;
5827   n = Numberof(Iv_delete);
5828   for(i = 0; i < n; i++)
5829     if ((Iv = vi->*Iv_delete[i]))
5830       delete Iv;
5831   n = Numberof(Sa_delete);
5832   for(i = 0; i < n; i++)
5833     if ((Sa = vi->*Sa_delete[i]))
5834       delete Sa;
5835   delete vi;
5836 
5837   if (nerr)
5838     abort_handler(PARSE_ERROR);
5839 }
5840 
5841 
flatten_rva(RealVectorArray * rva,RealVector ** prv)5842 static void flatten_rva(RealVectorArray *rva, RealVector **prv)
5843 {
5844   size_t i, j, k, m, n;
5845   RealVector *rv, *rv_i;
5846 
5847   m = rva->size();
5848   for(i = n = 0; i < m; ++i)
5849     n += (*rva)[i].length();
5850   *prv = rv = new RealVector(n, false);
5851   for(i = k = 0; i < m; ++i) {
5852     rv_i = &(*rva)[i];
5853     n = rv_i->length();
5854     for(j = 0; j < n; ++j, ++k)
5855       (*rv)[k] = (*rv_i)[j];
5856   }
5857 }
5858 
flatten_iva(IntVectorArray * iva,IntVector ** piv)5859 static void flatten_iva(IntVectorArray *iva, IntVector **piv)
5860 {
5861   size_t i, j, k, m, n;
5862   IntVector *iv, *iv_i;
5863 
5864   m = iva->size();
5865   for(i = n = 0; i < m; ++i)
5866     n += (*iva)[i].length();
5867   *piv = iv = new IntVector(n, false);
5868   for(i = k = 0; i < m; ++i) {
5869     iv_i = &(*iva)[i];
5870     n = iv_i->length();
5871     for(j = 0; j < n; ++j, ++k)
5872       (*iv)[k] = (*iv_i)[j];
5873   }
5874 }
5875 
flatten_rsm(RealSymMatrix * rsm,RealVector ** prv)5876 static void flatten_rsm(RealSymMatrix *rsm, RealVector **prv)
5877 {
5878   size_t i, j, m, n;
5879   RealVector *rv;
5880 
5881   m = rsm->numRows();
5882   *prv = rv = new RealVector(m*m, false);
5883   for(i = n = 0; i < m; ++i)
5884     for(j = 0; j < m; ++j, ++n)
5885       (*rv)[n] = (*rsm)(i,j);
5886 }
5887 
flatten_rsa(RealSetArray * rsa,RealVector ** prv)5888 static void flatten_rsa(RealSetArray *rsa, RealVector **prv)
5889 {
5890   size_t i, j, k, m, n;
5891   RealVector *rv;
5892   RealSet *rs_i;
5893   RealSet::iterator rs_it, rs_ite;
5894 
5895   m = rsa->size();
5896   for(i = n = 0; i < m; ++i)
5897     n += (*rsa)[i].size();
5898   *prv = rv = new RealVector(n, false);
5899   for(i = k = 0; i < m; ++i) {
5900     rs_i = &(*rsa)[i];
5901     for(rs_it=rs_i->begin(), rs_ite=rs_i->end(); rs_it!=rs_ite; ++rs_it, ++k)
5902       (*rv)[k] = *rs_it;
5903   }
5904 }
5905 
flatten_ssa(StringSetArray * ssa,StringArray ** psa)5906 static void flatten_ssa(StringSetArray *ssa, StringArray **psa)
5907 {
5908   size_t i, j, k, m, n;
5909   StringArray *sa;
5910   StringSet *ss_i;
5911   SSIter ss_it, ss_ite;
5912 
5913   m = ssa->size();
5914   for(i = n = 0; i < m; ++i)
5915     n += (*ssa)[i].size();
5916   *psa = sa = new StringArray(n);
5917   for(i = k = 0; i < m; ++i) {
5918     ss_i = &(*ssa)[i];
5919     for(ss_it=ss_i->begin(), ss_ite=ss_i->end(); ss_it!=ss_ite; ++ss_it, ++k)
5920       (*sa)[k] = *ss_it;
5921   }
5922 }
5923 
flatten_isa(IntSetArray * isa,IntVector ** piv)5924 static void flatten_isa(IntSetArray *isa, IntVector **piv)
5925 {
5926   size_t i, j, k, m, n;
5927   IntVector *iv;
5928   IntSet *is_i;
5929   IntSet::iterator is_it, is_ite;
5930 
5931   m = isa->size();
5932   for(i = n = 0; i < m; ++i)
5933     n += (*isa)[i].size();
5934   *piv = iv = new IntVector(n, false);
5935   for(i = k = 0; i < m; ++i) {
5936     is_i = &(*isa)[i];
5937     for(is_it=is_i->begin(), is_ite=is_i->end(); is_it!=is_ite; ++is_it, ++k)
5938       (*iv)[k] = *is_it;
5939   }
5940 }
5941 
flatten_rrma_keys(RealRealMapArray * rrma,RealVector ** prv)5942 static void flatten_rrma_keys(RealRealMapArray *rrma, RealVector **prv)
5943 {
5944   size_t i, j, k, m, n;
5945   RealVector *rv;
5946   RealRealMap *rrm_i;
5947   RealRealMap::iterator rrm_it, rrm_ite;
5948 
5949   m = rrma->size();
5950   for(i = n = 0; i < m; ++i)
5951     n += (*rrma)[i].size();
5952   *prv = rv = new RealVector(n, false);
5953   for(i = k = 0; i < m; ++i) {
5954     rrm_i = &(*rrma)[i];
5955     for (rrm_it=rrm_i->begin(), rrm_ite=rrm_i->end();
5956 	 rrm_it!=rrm_ite; ++rrm_it, ++k)
5957       (*rv)[k] = rrm_it->first;
5958   }
5959 }
5960 
flatten_rrma_values(RealRealMapArray * rrma,RealVector ** prv)5961 static void flatten_rrma_values(RealRealMapArray *rrma, RealVector **prv)
5962 {
5963   size_t i, j, k, m, n;
5964   RealVector *rv;
5965   RealRealMap *rrm_i;
5966   RealRealMap::iterator rrm_it, rrm_ite;
5967 
5968   m = rrma->size();
5969   for(i = n = 0; i < m; ++i)
5970     n += (*rrma)[i].size();
5971   *prv = rv = new RealVector(n, false);
5972   for(i = k = 0; i < m; ++i) {
5973     rrm_i = &(*rrma)[i];
5974     for (rrm_it=rrm_i->begin(), rrm_ite=rrm_i->end();
5975 	 rrm_it!=rrm_ite; ++rrm_it, ++k)
5976       (*rv)[k] = rrm_it->second;
5977   }
5978 }
5979 
flatten_irma_keys(IntRealMapArray * irma,IntVector ** piv)5980 static void flatten_irma_keys(IntRealMapArray *irma, IntVector **piv)
5981 {
5982   size_t i, j, k, m, n;
5983   IntVector *iv;
5984   IntRealMap *irm_i;
5985   IntRealMap::iterator irm_it, irm_ite;
5986 
5987   m = irma->size();
5988   for(i = n = 0; i < m; ++i)
5989     n += (*irma)[i].size();
5990   *piv = iv = new IntVector(n, false);
5991   for(i = k = 0; i < m; ++i) {
5992     irm_i = &(*irma)[i];
5993     for (irm_it=irm_i->begin(), irm_ite=irm_i->end();
5994 	 irm_it!=irm_ite; ++irm_it, ++k)
5995       (*iv)[k] = irm_it->first;
5996   }
5997 }
5998 
flatten_irma_values(IntRealMapArray * irma,RealVector ** prv)5999 static void flatten_irma_values(IntRealMapArray *irma, RealVector **prv)
6000 {
6001   size_t i, j, k, m, n;
6002   RealVector *rv;
6003   IntRealMap *irm_i;
6004   IntRealMap::iterator irm_it, irm_ite;
6005 
6006   m = irma->size();
6007   for(i = n = 0; i < m; ++i)
6008     n += (*irma)[i].size();
6009   *prv = rv = new RealVector(n, false);
6010   for(i = k = 0; i < m; ++i) {
6011     irm_i = &(*irma)[i];
6012     for (irm_it=irm_i->begin(), irm_ite=irm_i->end();
6013 	 irm_it!=irm_ite; ++irm_it, ++k)
6014       (*rv)[k] = irm_it->second;
6015   }
6016 }
6017 
flatten_srma_keys(StringRealMapArray * srma,StringArray ** psa)6018 static void flatten_srma_keys(StringRealMapArray *srma, StringArray **psa)
6019 {
6020   size_t i, j, k, m, n;
6021   StringArray *sa;
6022   StringRealMap *srm_i;
6023   StringRealMap::iterator srm_it, srm_ite;
6024 
6025   m = srma->size();
6026   for(i = n = 0; i < m; ++i)
6027     n += (*srma)[i].size();
6028   *psa = sa = new StringArray(n);
6029   for(i = k = 0; i < m; ++i) {
6030     srm_i = &(*srma)[i];
6031     for (srm_it=srm_i->begin(), srm_ite=srm_i->end();
6032 	 srm_it!=srm_ite; ++srm_it, ++k)
6033       (*sa)[k] = srm_it->first;
6034   }
6035 }
6036 
flatten_srma_values(StringRealMapArray * srma,RealVector ** prv)6037 static void flatten_srma_values(StringRealMapArray *srma, RealVector **prv)
6038 {
6039   size_t i, j, k, m, n;
6040   RealVector *rv;
6041   StringRealMap *srm_i;
6042   StringRealMap::iterator srm_it, srm_ite;
6043 
6044   m = srma->size();
6045   for(i = n = 0; i < m; ++i)
6046     n += (*srma)[i].size();
6047   *prv = rv = new RealVector(n, false);
6048   for(i = k = 0; i < m; ++i) {
6049     srm_i = &(*srma)[i];
6050     for (srm_it=srm_i->begin(), srm_ite=srm_i->end();
6051 	 srm_it!=srm_ite; ++srm_it, ++k)
6052       (*rv)[k] = srm_it->second;
6053   }
6054 }
6055 
6056 /// Flatten real-valued interval uncertain variable intervals and
6057 /// probabilities back into separate arrays.
flatten_real_intervals(const RealRealPairRealMapArray & rrprma,RealVector ** probs,RealVector ** lb,RealVector ** ub)6058 static void flatten_real_intervals(const RealRealPairRealMapArray& rrprma,
6059 				   RealVector **probs,
6060 				   RealVector **lb, RealVector** ub)
6061 {
6062   size_t i, j, k, m, n;
6063   RealVector *rvp, *rvlb, *rvub;
6064 
6065   m = rrprma.size();
6066   for(i = n = 0; i < m; ++i)
6067     n += rrprma[i].size();
6068   *probs = rvp = new RealVector(n, false);
6069   *lb = rvlb = new RealVector(n, false);
6070   *ub = rvub = new RealVector(n, false);
6071   for(i = k = 0; i < m; ++i) {
6072     const RealRealPairRealMap& rrprm_i = (rrprma)[i];
6073     RealRealPairRealMap::const_iterator rrprm_it = rrprm_i.begin();
6074     RealRealPairRealMap::const_iterator rrprm_ite = rrprm_i.end();
6075     for ( ; rrprm_it != rrprm_ite; ++rrprm_it, ++k) {
6076       const RealRealPair& interval = rrprm_it->first;
6077       Real prob = rrprm_it->second;
6078       (*rvp)[k] = prob;
6079       (*rvlb)[k] = interval.first;
6080       (*rvub)[k] = interval.second;
6081     }
6082   }
6083 }
6084 
6085 /// Flatten integer-valued interval uncertain variable intervals and
6086 /// probabilities back into separate arrays.
flatten_int_intervals(const IntIntPairRealMapArray & iiprma,RealVector ** probs,IntVector ** lb,IntVector ** ub)6087 static void flatten_int_intervals(const IntIntPairRealMapArray& iiprma,
6088 				  RealVector **probs,
6089 				  IntVector **lb, IntVector** ub)
6090 {
6091   size_t i, j, k, m, n;
6092   RealVector *ivp;
6093   IntVector *ivlb, *ivub;
6094 
6095   m = iiprma.size();
6096   for(i = n = 0; i < m; ++i)
6097     n += iiprma[i].size();
6098   *probs = ivp = new RealVector(n, false);
6099   *lb = ivlb = new IntVector(n, false);
6100   *ub = ivub = new IntVector(n, false);
6101   for(i = k = 0; i < m; ++i) {
6102     const IntIntPairRealMap& iiprm_i = iiprma[i];
6103     IntIntPairRealMap::const_iterator iiprm_it = iiprm_i.begin();
6104     IntIntPairRealMap::const_iterator iiprm_ite = iiprm_i.end();
6105     for ( ; iiprm_it != iiprm_ite; ++iiprm_it, ++k) {
6106       const IntIntPair& interval = iiprm_it->first;
6107       Real prob = iiprm_it->second;
6108       (*ivp)[k] = prob;
6109       (*ivlb)[k] = interval.first;
6110       (*ivub)[k] = interval.second;
6111     }
6112   }
6113 }
6114 
6115 
6116 void NIDRProblemDescDB::
check_descriptor_format(const StringArray & labels)6117 check_descriptor_format(const StringArray& labels) {
6118   StringArray::const_iterator li = labels.begin();
6119   String::const_iterator si;
6120   for(; li != labels.end(); ++li) {
6121     // error if descriptor contains whitespace
6122     for(si = li->begin(); si != li->end(); ++si) {
6123       if(isspace(*si)) {
6124         Squawk("Descriptor \"%s\" is invalid: whitespace not permitted",
6125               li->c_str());
6126         break;
6127       }
6128     }
6129     if(isfloat(*li))
6130       Squawk("Descriptor \"%s\" is invalid: floating point numbers not permitted",
6131           li->c_str());
6132     if(li->empty())
6133       Squawk("Empty variable or response descriptors are not permitted");
6134   }
6135 }
6136 
6137 
6138 void NIDRProblemDescDB::
check_variables(std::list<DataVariables> * dvl)6139 check_variables(std::list<DataVariables>* dvl)
6140 {
6141   // BMA: If parse was called, then the Var_Info objects have already
6142   // been populated; in the case of pure DB insertion in library mode,
6143   // they won't be and some data from DataVariables needs to be mapped
6144   // back to the flat Var_Info structures.  Not sure if this correctly
6145   // captures the case where a parse is followed by a DB update of an
6146   // input keyword not accounted for below
6147 
6148 
6149   if (pDDBInstance) {
6150     std::list<void*>::iterator It, Ite = pDDBInstance->VIL.end();
6151     for(It = pDDBInstance->VIL.begin(); It != Ite; ++It)
6152       check_variables_node(*It);
6153     pDDBInstance->VIL.clear();
6154   }
6155   else {
6156     // library mode with manual provision of everything
6157     // map all data back into NIDR Var_Info structures
6158     DataVariablesRep *dv;
6159     IntArray *ia;
6160     RealSymMatrix *rsm;
6161     IntVector *iv_a;
6162     StringArray *sa_a;
6163     RealVector *rv, *rv_a, *rv_o, *rv_c;
6164     RealVectorArray *rva;
6165     Var_Info *vi;
6166     size_t i, j, m, n, cntr;
6167     int num_prs_i, total_prs;
6168 
6169     // copy from DataVariables into Var_Info so that check_variables_node() can
6170     // go the other direction.  TO DO: can we eliminate this circular update?
6171     std::list<DataVariables>::iterator It = dvl->begin(), Ite = dvl->end();
6172     for(; It != Ite; ++It) {
6173 
6174       // create new Var_Info instance to hold DataVariables data
6175 
6176       vi = new Var_Info;
6177       memset(vi, 0, sizeof(Var_Info));
6178       vi->dv_handle = &*It;
6179       vi->dv = dv = It->dataVarsRep.get();
6180 
6181       // flatten 2D {Real,Int}{Vector,Set}Arrays back into Var_Info 1D arrays
6182 
6183       // discrete design set int vars
6184       if ((n = dv->numDiscreteDesSetIntVars)) {
6185 	// Note: set consolidation and/or reordering cannot be undone
6186 	flatten_num_array(dv->discreteDesignSetInt, &vi->nddsi);
6187 	flatten_isa(&dv->discreteDesignSetInt,     &vi->ddsi);
6188       }
6189       // discrete design set string vars
6190       if ((n = dv->numDiscreteDesSetStrVars)) {
6191 	// Note: set consolidation and/or reordering cannot be undone
6192 	flatten_num_array(dv->discreteDesignSetStr, &vi->nddss);
6193 	flatten_ssa(&dv->discreteDesignSetStr,     &vi->ddss);
6194       }
6195       // discrete design set real vars
6196       if ((n = dv->numDiscreteDesSetRealVars)) {
6197 	// Note: set consolidation and/or reordering cannot be undone
6198 	flatten_num_array(dv->discreteDesignSetReal, &vi->nddsr);
6199 	flatten_rsa(&dv->discreteDesignSetReal,     &vi->ddsr);
6200       }
6201       // histogram bin uncertain vars
6202       // convert RealRealMapArray to RealVectors of abscissas + ordinates
6203       const RealRealMapArray& hbp = dv->histogramUncBinPairs;
6204       if ((m = hbp.size())) {
6205 	vi->nhbp = ia = new IntArray(m);
6206 	for(i = 0; i < m; ++i)
6207 	  total_prs += (*ia)[i] = hbp[i].size();
6208 	vi->hba = rv_a = new RealVector(total_prs); // abscissas
6209 	vi->hbo = rv_o = new RealVector(total_prs); // ordinates only
6210 	vi->hbc = NULL;                             // no counts
6211 	for(i = cntr = 0; i < m; ++i) {
6212 	  RRMCIter it = hbp[i].begin(), it_end = hbp[i].end();
6213 	  for( ; it != it_end; ++cntr) {
6214 	    (*rv_a)[cntr] = it->first;  // abscissas
6215 	    (*rv_o)[cntr] = it->second; // ordinates only (no counts)
6216 	  }
6217 	  // normalization occurs in Vchk_HistogramBinUnc going other direction
6218 	}
6219       }
6220 
6221       // histogram point int uncertain vars
6222       // convert IntRealMapArray to Int/RealVectors of abscissas + counts
6223       const IntRealMapArray& hpip = dv->histogramUncPointIntPairs;
6224       if ((m = hpip.size())) {
6225 	vi->nhpip = ia = new IntArray(m);
6226 	for(i = 0; i < m; ++i)
6227 	  total_prs += (*ia)[i] = hpip[i].size();
6228 	vi->hpia = iv_a = new IntVector(total_prs); // abscissas
6229 	vi->hpic = rv_c = new RealVector(total_prs); // counts
6230 	for(i = cntr = 0; i < m; ++i) {
6231 	  IRMCIter it = hpip[i].begin();
6232 	  IRMCIter it_end = hpip[i].end();
6233 	  for( ; it != it_end; ++cntr) {
6234 	    (*iv_a)[cntr] = it->first;  // abscissas
6235 	    (*rv_c)[cntr] = it->second; // counts only (no ordinates)
6236 	  }
6237 	  // normalization occurs in Vchk_HistogramPtUnc going other direction
6238 	}
6239       }
6240 
6241       // histogram point string uncertain vars
6242       // convert StringRealMapArray to String/RealVectors of abscissas + counts
6243       const StringRealMapArray& hpsp = dv->histogramUncPointStrPairs;
6244       if ((m = hpsp.size())) {
6245 	vi->nhpsp = ia = new IntArray(m);
6246 	for(i = 0; i < m; ++i)
6247 	  total_prs += (*ia)[i] = hpsp[i].size();
6248 	vi->hpsa = sa_a = new StringArray(total_prs); // abscissas
6249 	vi->hpsc = rv_c = new RealVector(total_prs); // counts
6250 	for(i = cntr = 0; i < m; ++i) {
6251 	  SRMCIter it = hpsp[i].begin();
6252 	  SRMCIter it_end = hpsp[i].end();
6253 	  for( ; it != it_end; ++cntr) {
6254 	    (*sa_a)[cntr] = it->first;  // abscissas
6255 	    (*rv_c)[cntr] = it->second; // counts only (no ordinates)
6256 	  }
6257 	  // normalization occurs in Vchk_HistogramPtUnc going other direction
6258 	}
6259       }
6260 
6261       // histogram point real uncertain vars
6262       // convert RealRealMapArray to RealVectors of abscissas + counts
6263       const RealRealMapArray& hprp = dv->histogramUncPointRealPairs;
6264       if ((m = hprp.size())) {
6265 	vi->nhprp = ia = new IntArray(m);
6266 	for(i = 0; i < m; ++i)
6267 	  total_prs += (*ia)[i] = hprp[i].size();
6268 	vi->hpra = rv_a = new RealVector(total_prs); // abscissas
6269 	vi->hprc = rv_c = new RealVector(total_prs); // counts
6270 	for(i = cntr = 0; i < m; ++i) {
6271 	  RRMCIter it = hprp[i].begin();
6272 	  RRMCIter it_end = hprp[i].end();
6273 	  for( ; it != it_end; ++cntr) {
6274 	    (*rv_a)[cntr] = it->first;  // abscissas
6275 	    (*rv_c)[cntr] = it->second; // counts only (no ordinates)
6276 	  }
6277 	  // normalization occurs in Vchk_HistogramPtUnc going other direction
6278 	}
6279       }
6280 
6281       // uncertain correlation matrix
6282       if (dv->uncertainCorrelations.numRows())
6283 	flatten_rsm(&dv->uncertainCorrelations, &vi->ucm);
6284       // continuous interval uncertain vars
6285       if ((n = dv->numContinuousIntervalUncVars)) {
6286 	flatten_num_array(dv->continuousIntervalUncBasicProbs, &vi->nCI);
6287 	// unroll the array of maps in to separate variables (p, lb, ub)
6288 	flatten_real_intervals(dv->continuousIntervalUncBasicProbs,
6289 			       &vi->CIp, &vi->CIlb, &vi->CIub);
6290       }
6291       // discrete interval uncertain vars
6292       if ((n = dv->numDiscreteIntervalUncVars)) {
6293 	flatten_num_array(dv->discreteIntervalUncBasicProbs, &vi->nDI);
6294 	// unroll the array of maps in to separate variables (p, lb, ub)
6295 	flatten_int_intervals(dv->discreteIntervalUncBasicProbs,
6296 			      &vi->DIp, &vi->DIlb, &vi->DIub);
6297       }
6298       // discrete uncertain set int vars
6299       if ((n = dv->numDiscreteUncSetIntVars)) {
6300 	// Note: map consolidation and/or reordering cannot be undone
6301 	flatten_num_array(dv->discreteUncSetIntValuesProbs,    &vi->ndusi);
6302 	flatten_irma_keys(&dv->discreteUncSetIntValuesProbs,   &vi->dusi);
6303 	flatten_irma_values(&dv->discreteUncSetIntValuesProbs, &vi->DSIp);
6304       }
6305       // discrete uncertain set str vars
6306       if ((n = dv->numDiscreteUncSetStrVars)) {
6307 	// Note: map consolidation and/or reordering cannot be undone
6308 	flatten_num_array(dv->discreteUncSetStrValuesProbs,    &vi->nduss);
6309 	flatten_srma_keys(&dv->discreteUncSetStrValuesProbs,   &vi->duss);
6310 	flatten_srma_values(&dv->discreteUncSetStrValuesProbs, &vi->DSSp);
6311       }
6312       // discrete uncertain set real vars
6313       if ((n = dv->numDiscreteUncSetRealVars)) {
6314 	// Note: map consolidation and/or reordering cannot be undone
6315 	flatten_num_array(dv->discreteUncSetRealValuesProbs,    &vi->ndusr);
6316 	flatten_rrma_keys(&dv->discreteUncSetRealValuesProbs,   &vi->dusr);
6317 	flatten_rrma_values(&dv->discreteUncSetRealValuesProbs, &vi->DSRp);
6318       }
6319       // discrete state set int vars
6320       if ((n = dv->numDiscreteStateSetIntVars)) {
6321 	// Note: set consolidation and/or reordering cannot be undone
6322 	flatten_num_array(dv->discreteStateSetInt, &vi->ndssi);
6323 	flatten_isa(&dv->discreteStateSetInt,     &vi->dssi);
6324       }
6325       // discrete state set string vars
6326       if ((n = dv->numDiscreteStateSetStrVars)) {
6327 	// Note: set consolidation and/or reordering cannot be undone
6328 	flatten_num_array(dv->discreteStateSetStr, &vi->ndsss);
6329 	flatten_ssa(&dv->discreteStateSetStr,     &vi->dsss);
6330       }
6331       // discrete state set real vars
6332       if ((n = dv->numDiscreteStateSetRealVars)) {
6333 	// Note: set consolidation and/or reordering cannot be undone
6334 	flatten_num_array(dv->discreteStateSetReal, &vi->ndssr);
6335 	flatten_rsa(&dv->discreteStateSetReal,     &vi->dssr);
6336       }
6337 
6338       check_variables_node((void*)vi);
6339     }
6340   }
6341   // validate descriptors. The string arrays are empty unless the user
6342   // explicitly set descriptors.
6343   std::list<DataVariables>::iterator It = dvl->begin(), Ite = dvl->end();
6344   for(; It != Ite; ++It) {
6345     const DataVariablesRep& dvr = *It->data_rep();
6346     check_descriptor_format(dvr.continuousDesignLabels);
6347     check_descriptor_format(dvr.discreteDesignRangeLabels);
6348     check_descriptor_format(dvr.discreteDesignSetIntLabels);
6349     check_descriptor_format(dvr.discreteDesignSetStrLabels);
6350     check_descriptor_format(dvr.discreteDesignSetRealLabels);
6351     check_descriptor_format(dvr.continuousStateLabels);
6352     check_descriptor_format(dvr.discreteStateRangeLabels);
6353     check_descriptor_format(dvr.discreteStateSetIntLabels);
6354     check_descriptor_format(dvr.discreteStateSetStrLabels);
6355     check_descriptor_format(dvr.discreteStateSetRealLabels);
6356     check_descriptor_format(dvr.continuousAleatoryUncLabels);
6357     check_descriptor_format(dvr.discreteIntAleatoryUncLabels);
6358     check_descriptor_format(dvr.discreteStrAleatoryUncLabels);
6359     check_descriptor_format(dvr.discreteRealAleatoryUncLabels);
6360     check_descriptor_format(dvr.continuousEpistemicUncLabels);
6361     check_descriptor_format(dvr.discreteIntEpistemicUncLabels);
6362     check_descriptor_format(dvr.discreteStrEpistemicUncLabels);
6363     check_descriptor_format(dvr.discreteRealEpistemicUncLabels);
6364 
6365     check_descriptors_for_repeats(dvr.continuousDesignLabels,
6366                                   dvr.discreteDesignRangeLabels,
6367                                   dvr.discreteDesignSetIntLabels,
6368                                   dvr.discreteDesignSetStrLabels,
6369                                   dvr.discreteDesignSetRealLabels,
6370                                   dvr.continuousStateLabels,
6371                                   dvr.discreteStateRangeLabels,
6372                                   dvr.discreteStateSetIntLabels,
6373                                   dvr.discreteStateSetStrLabels,
6374                                   dvr.discreteStateSetRealLabels,
6375                                   dvr.continuousAleatoryUncLabels,
6376                                   dvr.discreteIntAleatoryUncLabels,
6377                                   dvr.discreteStrAleatoryUncLabels,
6378                                   dvr.discreteRealAleatoryUncLabels,
6379                                   dvr.continuousEpistemicUncLabels,
6380                                   dvr.discreteIntEpistemicUncLabels,
6381                                   dvr.discreteStrEpistemicUncLabels,
6382                                   dvr.discreteRealEpistemicUncLabels);
6383   }
6384 }
6385 
6386 void NIDRProblemDescDB::
var_str(const char * keyname,Values * val,void ** g,void * v)6387 var_str(const char *keyname, Values *val, void **g, void *v)
6388 {
6389   (*(Var_Info**)g)->dv->**(String DataVariablesRep::**)v = *val->s;
6390 }
6391 
6392 void NIDRProblemDescDB::
var_strL(const char * keyname,Values * val,void ** g,void * v)6393 var_strL(const char *keyname, Values *val, void **g, void *v)
6394 {
6395   StringArray *sa = &((*(Var_Info**)g)->dv->**(StringArray DataVariablesRep::**)v);
6396   const char **s = val->s;
6397   size_t i, n = val->n;
6398 
6399   sa->resize(n);
6400   for(i = 0; i < n; i++)
6401     (*sa)[i] = s[i];
6402 }
6403 
6404 static void
var_iulbl(const char * keyname,Values * val,VarLabel * vl)6405 var_iulbl(const char *keyname, Values *val, VarLabel *vl)
6406 {
6407   char *t;
6408   const char **s, **sl;
6409   size_t i, L, n;
6410 
6411   L = n = val->n;	// n for null
6412   s = val->s;
6413   for(i = 0; i < n; i++)
6414     L += strlen(s[i]);
6415   vl->s = sl = (const char **)malloc(n*sizeof(char*) + L);
6416   if (!sl)
6417     NIDRProblemDescDB::botch("malloc failure in var_ulbl");
6418   vl->n = n;
6419   t = (char*)(sl + n);
6420   for(i = 0; i < n; i++) {
6421     strcpy(t, s[i]);
6422     sl[i] = (const char*)t;
6423     t += strlen(t) + 1;
6424   }
6425 }
6426 
6427 void NIDRProblemDescDB::
var_caulbl(const char * keyname,Values * val,void ** g,void * v)6428 var_caulbl(const char *keyname, Values *val, void **g, void *v)
6429 {
6430   // IRIX disallows (int)v, so we use the circumlocution (char*)v - (char*)0.
6431   VarLabel *vl = &(*(Var_Info**)g)->CAUv[(char*)v - (char*)0];
6432   var_iulbl(keyname, val, vl);
6433 }
6434 
6435 void NIDRProblemDescDB::
var_ceulbl(const char * keyname,Values * val,void ** g,void * v)6436 var_ceulbl(const char *keyname, Values *val, void **g, void *v)
6437 {
6438   VarLabel *vl = &(*(Var_Info**)g)->CEUv[(char*)v - (char*)0];
6439   var_iulbl(keyname, val, vl);
6440 }
6441 
6442 void NIDRProblemDescDB::
var_dauilbl(const char * keyname,Values * val,void ** g,void * v)6443 var_dauilbl(const char *keyname, Values *val, void **g, void *v)
6444 {
6445   VarLabel *vl = &(*(Var_Info**)g)->DAUIv[(char*)v - (char*)0];
6446   var_iulbl(keyname, val, vl);
6447 }
6448 
6449 void NIDRProblemDescDB::
var_deuilbl(const char * keyname,Values * val,void ** g,void * v)6450 var_deuilbl(const char *keyname, Values *val, void **g, void *v)
6451 {
6452   VarLabel *vl = &(*(Var_Info**)g)->DEUIv[(char*)v - (char*)0];
6453   var_iulbl(keyname, val, vl);
6454 }
6455 
6456 void NIDRProblemDescDB::
var_dauslbl(const char * keyname,Values * val,void ** g,void * v)6457 var_dauslbl(const char *keyname, Values *val, void **g, void *v)
6458 {
6459   VarLabel *vl = &(*(Var_Info**)g)->DAUSv[(char*)v - (char*)0];
6460   var_iulbl(keyname, val, vl);
6461 }
6462 
6463 void NIDRProblemDescDB::
var_deuslbl(const char * keyname,Values * val,void ** g,void * v)6464 var_deuslbl(const char *keyname, Values *val, void **g, void *v)
6465 {
6466   VarLabel *vl = &(*(Var_Info**)g)->DEUSv[(char*)v - (char*)0];
6467   var_iulbl(keyname, val, vl);
6468 }
6469 
6470 void NIDRProblemDescDB::
var_daurlbl(const char * keyname,Values * val,void ** g,void * v)6471 var_daurlbl(const char *keyname, Values *val, void **g, void *v)
6472 {
6473   VarLabel *vl = &(*(Var_Info**)g)->DAURv[(char*)v - (char*)0];
6474   var_iulbl(keyname, val, vl);
6475 }
6476 
6477 void NIDRProblemDescDB::
var_deurlbl(const char * keyname,Values * val,void ** g,void * v)6478 var_deurlbl(const char *keyname, Values *val, void **g, void *v)
6479 {
6480   VarLabel *vl = &(*(Var_Info**)g)->DEURv[(char*)v - (char*)0];
6481   var_iulbl(keyname, val, vl);
6482 }
6483 
6484 #define MP_(x) DataInterfaceRep::* iface_mp_##x = &DataInterfaceRep::x
6485 #define MP2(x,y) iface_mp_##x##_##y = {&DataInterfaceRep::x,#y}
6486 #define MP2s(x,y) iface_mp_##x##_##y = {&DataInterfaceRep::x,y}
6487 #define MP3(x,y,z) iface_mp_TYPE_DATA_##x##_##z = {&DataInterfaceRep::x,&DataInterfaceRep::y,#z}
6488 
6489 static Iface_mp_Rlit
6490 	MP3(failAction,recoveryFnVals,recover);
6491 
6492 static Iface_mp_ilit
6493 	MP3(failAction,retryLimit,retry);
6494 
6495 static Iface_mp_lit
6496 	MP2(failAction,abort),
6497 	MP2(failAction,continuation);
6498 
6499 static Iface_mp_type
6500 	MP2s(analysisScheduling,MASTER_SCHEDULING),
6501 	MP2s(analysisScheduling,PEER_SCHEDULING),
6502       //MP2s(analysisScheduling,PEER_DYNAMIC_SCHEDULING),
6503       //MP2s(analysisScheduling,PEER_STATIC_SCHEDULING),
6504 	MP2s(evalScheduling,MASTER_SCHEDULING),
6505 	MP2s(evalScheduling,PEER_DYNAMIC_SCHEDULING),
6506 	MP2s(evalScheduling,PEER_STATIC_SCHEDULING),
6507 	MP2s(asynchLocalEvalScheduling,DYNAMIC_SCHEDULING),
6508         MP2s(asynchLocalEvalScheduling,STATIC_SCHEDULING);
6509 
6510 static Iface_mp_utype
6511 	MP2s(interfaceType,TEST_INTERFACE),
6512 	MP2s(interfaceType,FORK_INTERFACE),
6513 	MP2s(interfaceType,GRID_INTERFACE),
6514 	MP2s(interfaceType,MATLAB_INTERFACE),
6515 	MP2s(interfaceType,PYTHON_INTERFACE),
6516 	MP2s(interfaceType,SCILAB_INTERFACE),
6517 	MP2s(interfaceType,SYSTEM_INTERFACE),
6518 	//MP2s(resultsFileFormat,FLEXIBLE_RESULTS), // re-enable when more formats added?
6519 	MP2s(resultsFileFormat,LABELED_RESULTS);
6520 
6521 static String
6522 	MP_(algebraicMappings),
6523 	MP_(idInterface),
6524 	MP_(inputFilter),
6525 	MP_(outputFilter),
6526 	MP_(parametersFile),
6527 	MP_(resultsFile),
6528 	MP_(workDir);
6529 
6530 static String2DArray
6531 	MP_(analysisComponents);
6532 
6533 static StringArray
6534 	MP_(analysisDrivers),
6535         MP_(copyFiles),
6536 	MP_(linkFiles);
6537 
6538 static bool
6539 	MP_(activeSetVectorFlag),
6540 	MP_(allowExistingResultsFlag),
6541 	MP_(apreproFlag),
6542 	MP_(asynchFlag),
6543 	MP_(batchEvalFlag),
6544 	MP_(dirSave),
6545 	MP_(dirTag),
6546 	MP_(evalCacheFlag),
6547 	MP_(fileSaveFlag),
6548 	MP_(fileTagFlag),
6549 	MP_(nearbyEvalCacheFlag),
6550 	MP_(numpyFlag),
6551 	MP_(restartFileFlag),
6552 	MP_(templateReplace),
6553 	MP_(useWorkdir),
6554 	MP_(verbatimFlag);
6555 
6556 static int
6557 	MP_(analysisServers),
6558 	MP_(asynchLocalAnalysisConcurrency),
6559 	MP_(asynchLocalEvalConcurrency),
6560 	MP_(evalServers),
6561 	MP_(procsPerAnalysis),
6562 	MP_(procsPerEval);
6563 
6564 static Real
6565 	MP_(nearbyEvalCacheTol);
6566 
6567 #undef MP3
6568 #undef MP2s
6569 #undef MP2
6570 #undef MP_
6571 
6572 
6573 // Macros for Method
6574 
6575 #define MP_(x) DataMethodRep::* method_mp_##x = &DataMethodRep::x
6576 #define MP2(x,y) method_mp_##x##_##y = {&DataMethodRep::x,#y}
6577 #define MP2s(x,y) method_mp_##x##_##y = {&DataMethodRep::x,y}
6578 #if HAVE_OPTPP
6579 #  define MP2o(x,y) method_mp_##x##_##y = {&DataMethodRep::x,OPTPP::y}
6580 #else
6581 #  define MP2o(x,y) method_mp_##x##_##y = {NULL, NULL}
6582 #endif
6583 #define MP2p(x,y) method_mp_##x##_##y = {&DataMethodRep::x,Pecos::y}
6584 #define MP3(x,y,z) method_mp_TYPE_DATA_##x##_##z = {&DataMethodRep::x,&DataMethodRep::y,#z}
6585 #define MP3s(x,y,z) method_mp_TYPE_DATA_##x##_##z = {&DataMethodRep::x,&DataMethodRep::y,z}
6586 
6587 static IntVector
6588 	MP_(primeBase),
6589 	MP_(refineSamples),
6590 	MP_(sequenceLeap),
6591 	MP_(sequenceStart),
6592 	MP_(stepsPerVariable);
6593 
6594 static Method_mp_ilit2
6595 	MP3(replacementType,numberRetained,chc),
6596 	MP3(replacementType,numberRetained,elitist),
6597 	MP3(replacementType,numberRetained,random);
6598 
6599 static Method_mp_ilit2z
6600 	MP3(crossoverType,numCrossPoints,multi_point_binary),
6601 	MP3(crossoverType,numCrossPoints,multi_point_parameterized_binary),
6602 	MP3(crossoverType,numCrossPoints,multi_point_real);
6603 
6604 static Method_mp_lit
6605 	MP2(batchSelectionType,naive),
6606 	MP2(batchSelectionType,distance),
6607 	MP2(batchSelectionType,topology),
6608 	MP2(batchSelectionType,cl),
6609 	MP2(boxDivision,all_dimensions),
6610 	MP2(boxDivision,major_dimension),
6611 	MP2(convergenceType,average_fitness_tracker),
6612 	MP2(convergenceType,best_fitness_tracker),
6613 	MP2(convergenceType,metric_tracker),
6614 	MP2(crossoverType,blend),
6615 	MP2(crossoverType,two_point),
6616 	MP2(crossoverType,uniform),
6617         MP2(dataDistCovInputType,diagonal),
6618         MP2(dataDistCovInputType,matrix),
6619       //MP2(dataDistType,gaussian),
6620       //MP2(dataDistType,user),
6621 	MP2(evalSynchronize,blocking),
6622 	MP2(evalSynchronize,nonblocking),
6623       //MP2(expansionSampleType,incremental_lhs),
6624 	MP2(exploratoryMoves,adaptive),
6625 	MP2(exploratoryMoves,multi_step),
6626 	MP2(exploratoryMoves,simple),
6627 	MP2(fitnessType,domination_count),
6628 	MP2(fitnessType,layer_rank),
6629 	MP2(fitnessType,linear_rank),
6630 	MP2(fitnessType,merit_function),
6631 	MP2(fitnessType,proportional),
6632 	MP2(fitnessMetricType,predicted_variance),
6633 	MP2(fitnessMetricType,distance),
6634 	MP2(fitnessMetricType,gradient),
6635       //MP2(hybridCollabType,abo),
6636       //MP2(hybridCollabType,hops),
6637       //MP2(seqHybridType,adaptive),
6638 	MP2(initializationType,random),
6639 	MP2(initializationType,unique_random),
6640 	MP2(lipschitzType,global),
6641 	MP2(lipschitzType,local),
6642         MP2(meritFunction,merit_max),
6643         MP2(meritFunction,merit_max_smooth),
6644         MP2(meritFunction,merit1),
6645         MP2(meritFunction,merit1_smooth),
6646         MP2(meritFunction,merit2),
6647         MP2(meritFunction,merit2_smooth),
6648         MP2(meritFunction,merit2_squared),
6649 	MP2(mcmcType,adaptive_metropolis),
6650 	MP2(mcmcType,delayed_rejection),
6651 	MP2(mcmcType,dram),
6652 	MP2(mcmcType,metropolis_hastings),
6653 	MP2(mcmcType,multilevel),
6654 	MP2(modelDiscrepancyType,global_kriging),
6655 	MP2(modelDiscrepancyType,global_polynomial),
6656 	MP2(mutationType,bit_random),
6657 	MP2(mutationType,offset_cauchy),
6658 	MP2(mutationType,offset_normal),
6659 	MP2(mutationType,offset_uniform),
6660 	MP2(mutationType,replace_uniform),
6661 	MP2(patternBasis,coordinate),
6662 	MP2(patternBasis,simplex),
6663 	MP2(pointReuse,all),
6664         MP2(proposalCovInputType,diagonal),
6665         MP2(proposalCovInputType,matrix),
6666         MP2(proposalCovType,derivatives),
6667         MP2(proposalCovType,prior),
6668         MP2(proposalCovType,user),
6669 	MP2(reliabilityIntegration,first_order),
6670 	MP2(reliabilityIntegration,second_order),
6671 	MP2(replacementType,elitist),
6672 	MP2(replacementType,favor_feasible),
6673 	MP2(replacementType,roulette_wheel),
6674 	MP2(replacementType,unique_roulette_wheel),
6675 	MP2(rngName,mt19937),
6676 	MP2(rngName,rnum2),
6677 	MP2(searchMethod,gradient_based_line_search),
6678 	MP2(searchMethod,tr_pds),
6679 	MP2(searchMethod,trust_region),
6680 	MP2(searchMethod,value_based_line_search),
6681 	MP2(trialType,grid),
6682 	MP2(trialType,halton),
6683 	MP2(trialType,random),
6684         MP2(useSurrogate,inform_search),
6685         MP2(useSurrogate,optimize);
6686 
6687 static Method_mp_litc
6688 	MP3(crossoverType,crossoverRate,shuffle_random),
6689 	MP3(crossoverType,crossoverRate,null_crossover),
6690 	MP3(mutationType,mutationRate,null_mutation),
6691 	MP3(mutationType,mutationRate,offset_cauchy),
6692 	MP3(mutationType,mutationRate,offset_normal),
6693 	MP3(mutationType,mutationRate,offset_uniform),
6694 	MP3(replacementType,fitnessLimit,below_limit);
6695 
6696 static Method_mp_litrv
6697 	MP3(nichingType,nicheVector,distance),
6698 	MP3(nichingType,nicheVector,max_designs),
6699 	MP3(nichingType,nicheVector,radial),
6700 	MP3(postProcessorType,distanceVector,distance_postprocessor);
6701 
6702 static Method_mp_slit2
6703 	MP3(initializationType,flatFile,flat_file);
6704 
6705 static Method_mp_utype_lit
6706         MP3s(methodName,dlDetails,DL_SOLVER); // struct order: ip, sp, utype
6707 
6708 static Method_mp_ord
6709 	MP2s(approxCorrectionOrder,0),
6710 	MP2s(approxCorrectionOrder,1),
6711 	MP2s(approxCorrectionOrder,2);
6712 
6713 static Real
6714 	MP_(absConvTol),
6715 	MP_(centeringParam),
6716 	MP_(collocationRatio),
6717 	MP_(collocRatioTermsOrder),
6718 	MP_(constraintPenalty),
6719 	MP_(constrPenalty),
6720 	MP_(constraintTolerance),
6721 	MP_(contractFactor),
6722 	MP_(contractStepLength),
6723 	MP_(convergenceTolerance),
6724 	MP_(crossoverRate),
6725 	MP_(falseConvTol),
6726 	MP_(functionPrecision),
6727 	MP_(globalBalanceParam),
6728 	MP_(gradientTolerance),
6729       //MP_(hybridProgThresh),
6730 	MP_(hybridLSProb),
6731 	MP_(grThreshold),
6732 	MP_(initDelta),
6733 	MP_(initMeshSize),
6734 	MP_(initStepLength),
6735 	MP_(initTRRadius),
6736 	MP_(lineSearchTolerance),
6737 	MP_(localBalanceParam),
6738 	MP_(maxBoxSize),
6739 	MP_(maxStep),
6740 	MP_(minBoxSize),
6741 	MP_(minMeshSize),
6742 	MP_(multilevEstimatorRate),
6743 	MP_(mutationRate),
6744 	MP_(mutationScale),
6745 	MP_(percentVarianceExplained),
6746         MP_(priorPropCovMult),
6747 	MP_(refinementRate),
6748 	MP_(regressionL2Penalty),
6749 	MP_(shrinkagePercent),	// should be called shrinkageFraction
6750 	MP_(singConvTol),
6751 	MP_(singRadius),
6752         MP_(smoothFactor),
6753  	MP_(solnTarget),
6754 	MP_(solverRoundingTol),
6755 	MP_(solverTol),
6756 	MP_(statsRoundingTol),
6757 	MP_(stepLenToBoundary),
6758 	MP_(threshDelta),
6759 	MP_(threshStepLength),
6760 	MP_(trustRegionContract),
6761 	MP_(trustRegionContractTrigger),
6762 	MP_(trustRegionExpand),
6763 	MP_(trustRegionExpandTrigger),
6764 	MP_(trustRegionMinSize),
6765 	MP_(vbdDropTolerance),
6766 	MP_(volBoxSize),
6767 	MP_(vns),
6768 	MP_(wilksConfidenceLevel),
6769 	MP_(xConvTol);
6770 
6771 static RealVector
6772 	MP_(anisoDimPref),
6773 	MP_(concurrentParameterSets),
6774 	MP_(dataDistCovariance),
6775 	MP_(dataDistMeans),
6776 	MP_(finalPoint),
6777 	MP_(hyperPriorAlphas),
6778 	MP_(hyperPriorBetas),
6779 	MP_(listOfPoints),
6780 	MP_(predictionConfigList),
6781 	MP_(proposalCovData),
6782 	MP_(regressionNoiseTol),
6783         MP_(stepVector),
6784 	MP_(trustRegionInitSize);
6785 
6786 static RealVectorArray
6787 	MP_(genReliabilityLevels),
6788 	MP_(probabilityLevels),
6789 	MP_(reliabilityLevels),
6790 	MP_(responseLevels);
6791 
6792 static unsigned short
6793 	MP_(adaptedBasisAdvancements),
6794       //MP_(adaptedBasisInitLevel),
6795 	MP_(cubIntOrder),
6796         MP_(expansionOrder),
6797         MP_(kickOrder),
6798         MP_(maxOrder),
6799         MP_(quadratureOrder),
6800 	MP_(softConvLimit),
6801 	MP_(sparseGridLevel),
6802         MP_(startOrder),
6803 	MP_(vbdOrder),
6804 	MP_(wilksOrder);
6805 
6806 static SizetArray
6807 	MP_(collocationPointsSeq),
6808         MP_(expansionSamplesSeq),
6809 	MP_(pilotSamples),
6810 	MP_(randomSeedSeq),
6811 	MP_(startRankSeq);
6812 
6813 static UShortArray
6814         MP_(expansionOrderSeq),
6815         MP_(quadratureOrderSeq),
6816 	MP_(sparseGridLevelSeq),
6817 	MP_(startOrderSeq),
6818         MP_(tensorGridOrder),
6819 	MP_(varPartitions);
6820 
6821 static String
6822         MP_(advancedOptionsFilename),
6823         MP_(betaSolverName),
6824         MP_(dataDistFile),
6825         MP_(displayFormat),
6826 	MP_(exportApproxPtsFile),
6827 	MP_(exportCorrModelFile),
6828 	MP_(exportCorrVarFile),
6829 	MP_(exportDiscrepFile),
6830 	MP_(exportExpansionFile),
6831 	MP_(exportMCMCPtsFile),
6832 	MP_(historyFile),
6833 	MP_(hybridGlobalMethodName),
6834 	MP_(hybridGlobalMethodPointer),
6835 	MP_(hybridGlobalModelPointer),
6836 	MP_(hybridLocalMethodName),
6837 	MP_(hybridLocalMethodPointer),
6838 	MP_(hybridLocalModelPointer),
6839 	MP_(idMethod),
6840 	MP_(importApproxPtsFile),
6841 	MP_(importBuildPtsFile),
6842 	MP_(importCandPtsFile),
6843 	MP_(importExpansionFile),
6844 	MP_(importPredConfigs),
6845 	MP_(logFile),
6846 	MP_(lowFidModelPointer),
6847 	MP_(modelExportPrefix),
6848 	MP_(modelPointer),
6849         MP_(posteriorDensityExportFilename),
6850         MP_(posteriorSamplesExportFilename),
6851         MP_(posteriorSamplesImportFilename),
6852 	MP_(proposalCovFile),
6853 	MP_(pstudyFilename),
6854 	MP_(subMethodName),
6855         MP_(subMethodPointer),
6856         MP_(subModelPointer);
6857 
6858 static StringArray
6859 	MP_(hybridMethodNames),
6860 	MP_(hybridMethodPointers),
6861 	MP_(hybridModelPointers),
6862         MP_(miscOptions);
6863 
6864 static bool
6865 	MP_(adaptExpDesign),
6866         MP_(adaptOrder),
6867 	MP_(adaptPosteriorRefine),
6868         MP_(adaptRank),
6869 	MP_(backfillFlag),
6870 	MP_(calModelDiscrepancy),
6871 	MP_(chainDiagnostics),
6872 	MP_(chainDiagnosticsCI),
6873 	MP_(constantPenalty),
6874 	MP_(crossValidation),
6875 	MP_(crossValidNoiseOnly),
6876 	MP_(dOptimal),
6877         MP_(evaluatePosteriorDensity),
6878 	MP_(expansionFlag),
6879 	MP_(exportSampleSeqFlag),
6880 	MP_(exportSurrogate),
6881 	MP_(fixedSeedFlag),
6882 	MP_(fixedSequenceFlag),
6883         MP_(generatePosteriorSamples),
6884 	MP_(gpmsaNormalize),
6885 	MP_(importApproxActive),
6886 	MP_(importBuildActive),
6887 	MP_(latinizeFlag),
6888 	MP_(logitTransform),
6889 	MP_(mainEffectsFlag),
6890 	MP_(methodScaling),
6891 	MP_(methodUseDerivsFlag),
6892         MP_(modelEvidence),
6893         MP_(modelEvidLaplace),
6894         MP_(modelEvidMC),
6895 	MP_(mutualInfoKSG2),
6896 	MP_(mutationAdaptive),
6897 	MP_(normalizedCoeffs),
6898 	MP_(pcaFlag),
6899 	MP_(posteriorStatsKL),
6900 	MP_(posteriorStatsKDE),
6901 	MP_(posteriorStatsMutual),
6902 	MP_(printPopFlag),
6903 	MP_(pstudyFileActive),
6904 	MP_(randomizeOrderFlag),
6905 	MP_(regressDiag),
6906 	MP_(relativeConvMetric),
6907 	MP_(showAllEval),
6908 	MP_(showMiscOptions),
6909 	MP_(speculativeFlag),
6910 	MP_(standardizedSpace),
6911 	MP_(useTargetVarianceOptimizationFlag),
6912 	MP_(tensorGridFlag),
6913 	MP_(surrBasedGlobalReplacePts),
6914 	MP_(surrBasedLocalLayerBypass),
6915 	MP_(vbdFlag),
6916 	MP_(volQualityFlag),
6917 	MP_(wilksFlag);
6918 
6919 /* It seems these are redundant with Method_mp_type:
6920 static short
6921 	MP_(c3AdvanceType),
6922         MP_(expansionType),
6923         MP_(nestingOverride),
6924         MP_(refinementControl),
6925         MP_(refinementType),
6926 	MP_(wilksSidedInterval);
6927 */
6928 
6929 static int
6930 	MP_(batchSize),
6931 	MP_(batchSizeExplore),
6932 	MP_(buildSamples),
6933 	MP_(burnInSamples),
6934 	MP_(chainSamples),
6935 	MP_(concurrentRandomJobs),
6936 	MP_(contractAfterFail),
6937 	MP_(covarianceType),
6938         MP_(crossoverChainPairs),
6939         MP_(emulatorOrder),
6940 	MP_(expandAfterSuccess),
6941         MP_(evidenceSamples),
6942         MP_(iteratorServers),
6943 	MP_(jumpStep),
6944 	MP_(maxCrossIterations),
6945 	MP_(maxFunctionEvaluations),
6946 	MP_(maxHifiEvals),
6947 	MP_(maxIterations),
6948 	MP_(maxRefineIterations),
6949 	MP_(maxSolverIterations),
6950 	MP_(mutationRange),
6951         MP_(neighborOrder),
6952 	MP_(newSolnsGenerated),
6953 	MP_(numChains),
6954 	MP_(numCR),
6955 	MP_(numSamples),
6956 	MP_(numSteps),
6957 	MP_(numSymbols),
6958 	MP_(numTrials),
6959 	MP_(populationSize),
6960         MP_(procsPerIterator),
6961         MP_(proposalCovUpdatePeriod),
6962 	MP_(numPushforwardSamples),
6963 	MP_(randomSeed),
6964 	MP_(samplesOnEmulator),
6965 	MP_(searchSchemeSize),
6966 	MP_(subSamplingPeriod),
6967 	MP_(totalPatternSize),
6968 	MP_(verifyLevel);
6969 
6970 static size_t
6971 	MP_(collocationPoints),
6972         MP_(expansionSamples),
6973         MP_(kickRank),
6974         MP_(maxRank),
6975         MP_(numCandidateDesigns),
6976 	MP_(numCandidates),
6977 	MP_(numDesigns),
6978 	MP_(numFinalSolutions),
6979 	MP_(numGenerations),
6980 	MP_(numOffspring),
6981 	MP_(numParents),
6982 	MP_(numPredConfigs),
6983   //MP_(startOrder),
6984   MP_(startRank);
6985 
6986 static Method_mp_type
6987 	MP2s(allocationTarget,TARGET_MEAN),
6988 	MP2s(allocationTarget,TARGET_VARIANCE),
6989 	MP2s(c3AdvanceType,MAX_ORDER_ADVANCEMENT),
6990 	MP2s(c3AdvanceType,MAX_RANK_ADVANCEMENT),
6991 	MP2s(c3AdvanceType,MAX_RANK_ORDER_ADVANCEMENT),
6992 	MP2s(c3AdvanceType,START_ORDER_ADVANCEMENT),
6993 	MP2s(c3AdvanceType,START_RANK_ADVANCEMENT),
6994 	MP2s(covarianceControl,DIAGONAL_COVARIANCE),
6995 	MP2s(covarianceControl,FULL_COVARIANCE),
6996 	MP2s(distributionType,COMPLEMENTARY),
6997 	MP2s(distributionType,CUMULATIVE),
6998 	MP2s(emulatorType,EXPGP_EMULATOR),
6999 	MP2s(emulatorType,GP_EMULATOR),
7000 	MP2s(emulatorType,KRIGING_EMULATOR),
7001 	MP2s(emulatorType,MF_PCE_EMULATOR),
7002 	MP2s(emulatorType,MF_SC_EMULATOR),
7003 	MP2s(emulatorType,ML_PCE_EMULATOR),
7004 	MP2s(emulatorType,PCE_EMULATOR),
7005 	MP2s(emulatorType,SC_EMULATOR),
7006 	MP2s(emulatorType,VPS_EMULATOR),
7007 	MP2p(expansionBasisType,ADAPTED_BASIS_EXPANDING_FRONT),
7008 	MP2p(expansionBasisType,ADAPTED_BASIS_GENERALIZED),
7009 	MP2p(expansionBasisType,HIERARCHICAL_INTERPOLANT),
7010 	MP2p(expansionBasisType,NODAL_INTERPOLANT),
7011 	MP2p(expansionBasisType,TENSOR_PRODUCT_BASIS),
7012 	MP2p(expansionBasisType,TOTAL_ORDER_BASIS),
7013 	MP2s(expansionType,ASKEY_U),
7014 	MP2s(expansionType,STD_NORMAL_U),
7015 	MP2s(finalMomentsType,CENTRAL_MOMENTS),
7016 	MP2s(finalMomentsType,NO_MOMENTS),
7017 	MP2s(finalMomentsType,STANDARD_MOMENTS),
7018 	MP2p(growthOverride,RESTRICTED),                   // Pecos enumeration
7019 	MP2p(growthOverride,UNRESTRICTED),                 // Pecos enumeration
7020 	MP2s(iteratorScheduling,MASTER_SCHEDULING),
7021 	MP2s(iteratorScheduling,PEER_SCHEDULING),
7022       //MP2s(iteratorScheduling,PEER_DYNAMIC_SCHEDULING),
7023       //MP2s(iteratorScheduling,PEER_STATIC_SCHEDULING),
7024 	MP2s(lsRegressionType,EQ_CON_LS),
7025 	MP2s(lsRegressionType,SVD_LS),
7026 	MP2o(meritFn,ArgaezTapia),                         // OPTPP enumeration
7027 	MP2o(meritFn,NormFmu),                             // OPTPP enumeration
7028 	MP2o(meritFn,VanShanno),                           // OPTPP enumeration
7029 	MP2s(methodOutput,DEBUG_OUTPUT),
7030 	MP2s(methodOutput,NORMAL_OUTPUT),
7031 	MP2s(methodOutput,QUIET_OUTPUT),
7032 	MP2s(methodOutput,SILENT_OUTPUT),
7033 	MP2s(methodOutput,VERBOSE_OUTPUT),
7034 	MP2s(multilevAllocControl,ESTIMATOR_VARIANCE),
7035 	MP2s(multilevAllocControl,GREEDY_REFINEMENT),
7036 	MP2s(multilevAllocControl,RANK_SAMPLING),
7037 	MP2s(multilevAllocControl,RIP_SAMPLING),
7038 	MP2s(multilevDiscrepEmulation,DISTINCT_EMULATION),
7039 	MP2s(multilevDiscrepEmulation,RECURSIVE_EMULATION),
7040 	MP2p(nestingOverride,NESTED),                      // Pecos enumeration
7041 	MP2p(nestingOverride,NON_NESTED),                  // Pecos enumeration
7042 	MP2s(qoiAggregation,QOI_AGGREGATION_SUM),
7043 	MP2s(qoiAggregation,QOI_AGGREGATION_MAX),
7044 	MP2p(refinementControl,DIMENSION_ADAPTIVE_CONTROL_GENERALIZED),// Pecos
7045 	MP2p(refinementControl,DIMENSION_ADAPTIVE_CONTROL_DECAY),      // Pecos
7046 	MP2p(refinementControl,DIMENSION_ADAPTIVE_CONTROL_SOBOL),      // Pecos
7047 	MP2p(refinementControl,LOCAL_ADAPTIVE_CONTROL),                // Pecos
7048 	MP2p(refinementControl,UNIFORM_CONTROL),                       // Pecos
7049 	MP2p(refinementType,P_REFINEMENT),                 // Pecos enumeration
7050         MP2p(refinementType,H_REFINEMENT),                 // Pecos enumeration
7051 	MP2p(regressionType,BASIS_PURSUIT),                // Pecos enumeration
7052 	MP2p(regressionType,BASIS_PURSUIT_DENOISING),      // Pecos enumeration
7053 	MP2p(regressionType,DEFAULT_LEAST_SQ_REGRESSION),  // Pecos enumeration
7054 	MP2p(regressionType,LASSO_REGRESSION),             // Pecos enumeration
7055 	MP2p(regressionType,LEAST_ANGLE_REGRESSION),       // Pecos enumeration
7056 	MP2p(regressionType,ORTHOG_LEAST_INTERPOLATION),   // Pecos enumeration
7057 	MP2p(regressionType,ORTHOG_MATCH_PURSUIT),         // Pecos enumeration
7058 	MP2s(regressionType,FT_LS),
7059 	MP2s(regressionType,FT_RLS2),
7060 	MP2s(responseLevelTarget,GEN_RELIABILITIES),
7061 	MP2s(responseLevelTarget,PROBABILITIES),
7062 	MP2s(responseLevelTarget,RELIABILITIES),
7063 	MP2s(responseLevelTargetReduce,SYSTEM_PARALLEL),
7064 	MP2s(responseLevelTargetReduce,SYSTEM_SERIES),
7065         MP2p(statsMetricMode,ACTIVE_EXPANSION_STATS),   // Pecos
7066         MP2p(statsMetricMode,COMBINED_EXPANSION_STATS), // Pecos
7067 	MP2s(surrBasedLocalAcceptLogic,FILTER),
7068 	MP2s(surrBasedLocalAcceptLogic,TR_RATIO),
7069 	MP2s(surrBasedLocalConstrRelax,HOMOTOPY),
7070 	MP2s(surrBasedLocalMeritFn,ADAPTIVE_PENALTY_MERIT),
7071 	MP2s(surrBasedLocalMeritFn,AUGMENTED_LAGRANGIAN_MERIT),
7072 	MP2s(surrBasedLocalMeritFn,LAGRANGIAN_MERIT),
7073 	MP2s(surrBasedLocalMeritFn,PENALTY_MERIT),
7074 	MP2s(surrBasedLocalSubProbCon,LINEARIZED_CONSTRAINTS),
7075 	MP2s(surrBasedLocalSubProbCon,NO_CONSTRAINTS),
7076 	MP2s(surrBasedLocalSubProbCon,ORIGINAL_CONSTRAINTS),
7077 	MP2s(surrBasedLocalSubProbObj,AUGMENTED_LAGRANGIAN_OBJECTIVE),
7078 	MP2s(surrBasedLocalSubProbObj,LAGRANGIAN_OBJECTIVE),
7079 	MP2s(surrBasedLocalSubProbObj,ORIGINAL_PRIMARY),
7080 	MP2s(surrBasedLocalSubProbObj,SINGLE_OBJECTIVE),
7081 	MP2s(wilksSidedInterval,ONE_SIDED_LOWER),
7082 	MP2s(wilksSidedInterval,ONE_SIDED_UPPER),
7083 	MP2s(wilksSidedInterval,TWO_SIDED);
7084 
7085 static Method_mp_utype
7086 	MP2s(calibrateErrorMode,CALIBRATE_ONE),
7087 	MP2s(calibrateErrorMode,CALIBRATE_PER_EXPER),
7088 	MP2s(calibrateErrorMode,CALIBRATE_PER_RESP),
7089 	MP2s(calibrateErrorMode,CALIBRATE_BOTH),
7090         MP2s(exportApproxFormat,TABULAR_NONE),
7091         MP2s(exportApproxFormat,TABULAR_HEADER),
7092         MP2s(exportApproxFormat,TABULAR_EVAL_ID),
7093         MP2s(exportApproxFormat,TABULAR_IFACE_ID),
7094         MP2s(exportApproxFormat,TABULAR_ANNOTATED),
7095         MP2s(exportCorrModelFormat,TABULAR_NONE),
7096         MP2s(exportCorrModelFormat,TABULAR_HEADER),
7097         MP2s(exportCorrModelFormat,TABULAR_EVAL_ID),
7098         MP2s(exportCorrModelFormat,TABULAR_IFACE_ID),
7099         MP2s(exportCorrModelFormat,TABULAR_ANNOTATED),
7100         MP2s(exportCorrVarFormat,TABULAR_NONE),
7101         MP2s(exportCorrVarFormat,TABULAR_HEADER),
7102         MP2s(exportCorrVarFormat,TABULAR_EVAL_ID),
7103         MP2s(exportCorrVarFormat,TABULAR_IFACE_ID),
7104         MP2s(exportCorrVarFormat,TABULAR_ANNOTATED),
7105         MP2s(exportDiscrepFormat,TABULAR_NONE),
7106         MP2s(exportDiscrepFormat,TABULAR_HEADER),
7107         MP2s(exportDiscrepFormat,TABULAR_EVAL_ID),
7108         MP2s(exportDiscrepFormat,TABULAR_IFACE_ID),
7109         MP2s(exportDiscrepFormat,TABULAR_ANNOTATED),
7110         MP2s(exportSamplesFormat,TABULAR_NONE),
7111         MP2s(exportSamplesFormat,TABULAR_HEADER),
7112         MP2s(exportSamplesFormat,TABULAR_EVAL_ID),
7113         MP2s(exportSamplesFormat,TABULAR_IFACE_ID),
7114         MP2s(exportSamplesFormat,TABULAR_ANNOTATED),
7115         MP2s(importApproxFormat,TABULAR_NONE),
7116         MP2s(importApproxFormat,TABULAR_HEADER),
7117         MP2s(importApproxFormat,TABULAR_EVAL_ID),
7118         MP2s(importApproxFormat,TABULAR_IFACE_ID),
7119         MP2s(importApproxFormat,TABULAR_ANNOTATED),
7120         MP2s(importBuildFormat,TABULAR_NONE),
7121         MP2s(importBuildFormat,TABULAR_HEADER),
7122         MP2s(importBuildFormat,TABULAR_EVAL_ID),
7123         MP2s(importBuildFormat,TABULAR_IFACE_ID),
7124         MP2s(importBuildFormat,TABULAR_ANNOTATED),
7125         MP2s(importCandFormat,TABULAR_NONE),
7126         MP2s(importCandFormat,TABULAR_HEADER),
7127         MP2s(importCandFormat,TABULAR_EVAL_ID),
7128         MP2s(importCandFormat,TABULAR_IFACE_ID),
7129         MP2s(importCandFormat,TABULAR_ANNOTATED),
7130         MP2s(importPredConfigFormat,TABULAR_NONE),
7131         MP2s(importPredConfigFormat,TABULAR_HEADER),
7132         MP2s(importPredConfigFormat,TABULAR_EVAL_ID),
7133         MP2s(importPredConfigFormat,TABULAR_IFACE_ID),
7134         MP2s(importPredConfigFormat,TABULAR_ANNOTATED),
7135 	MP2s(integrationRefine,AIS),
7136 	MP2s(integrationRefine,IS),
7137 	MP2s(integrationRefine,MMAIS),
7138 	MP2s(methodName,ASYNCH_PATTERN_SEARCH),
7139 	MP2s(methodName,BRANCH_AND_BOUND),
7140 	MP2s(methodName,C3_FUNCTION_TRAIN),
7141 	MP2s(methodName,COLINY_BETA),
7142 	MP2s(methodName,COLINY_COBYLA),
7143 	MP2s(methodName,COLINY_DIRECT),
7144 	MP2s(methodName,COLINY_EA),
7145 	MP2s(methodName,COLINY_PATTERN_SEARCH),
7146 	MP2s(methodName,COLINY_SOLIS_WETS),
7147 	MP2s(methodName,CONMIN_FRCG),
7148 	MP2s(methodName,CONMIN_MFD),
7149 	MP2s(methodName,DACE),
7150 	MP2s(methodName,DATA_FIT_SURROGATE_BASED_LOCAL),
7151 	MP2s(methodName,DOT_BFGS),
7152 	MP2s(methodName,DOT_FRCG),
7153 	MP2s(methodName,DOT_MMFD),
7154 	MP2s(methodName,DOT_SLP),
7155 	MP2s(methodName,DOT_SQP),
7156 	MP2s(methodName,EFFICIENT_GLOBAL),
7157 	MP2s(methodName,FSU_CVT),
7158 	MP2s(methodName,FSU_HALTON),
7159 	MP2s(methodName,FSU_HAMMERSLEY),
7160 	MP2s(methodName,HIERARCH_SURROGATE_BASED_LOCAL),
7161 	MP2s(methodName,HYBRID),
7162 	MP2s(methodName,MESH_ADAPTIVE_SEARCH),
7163 	MP2s(methodName,MOGA),
7164 	MP2s(methodName,MULTI_START),
7165 	MP2s(methodName,NCSU_DIRECT),
7166 	MP2s(methodName,ROL),
7167 	MP2s(methodName,DEMO_TPL),
7168 	MP2s(methodName,NL2SOL),
7169 	MP2s(methodName,NLPQL_SQP),
7170 	MP2s(methodName,NLSSOL_SQP),
7171 	MP2s(methodName,MIT_NOWPAC),
7172 	MP2s(methodName,MIT_SNOWPAC),
7173         MP2s(methodName,ADAPTIVE_SAMPLING),
7174 	MP2s(methodName,BAYES_CALIBRATION),
7175 	MP2s(methodName,GENIE_DIRECT),
7176 	MP2s(methodName,GENIE_OPT_DARTS),
7177         MP2s(methodName,GPAIS),
7178 	MP2s(methodName,GLOBAL_EVIDENCE),
7179         MP2s(methodName,GLOBAL_INTERVAL_EST),
7180 	MP2s(methodName,GLOBAL_RELIABILITY),
7181         MP2s(methodName,IMPORTANCE_SAMPLING),
7182  	MP2s(methodName,LOCAL_EVIDENCE),
7183         MP2s(methodName,LOCAL_INTERVAL_EST),
7184 	MP2s(methodName,LOCAL_RELIABILITY),
7185 	MP2s(methodName,MULTIFIDELITY_FUNCTION_TRAIN),
7186 	MP2s(methodName,MULTIFIDELITY_POLYNOMIAL_CHAOS),
7187 	MP2s(methodName,MULTIFIDELITY_STOCH_COLLOCATION),
7188 	MP2s(methodName,MULTILEVEL_FUNCTION_TRAIN),
7189 	MP2s(methodName,MULTILEVEL_POLYNOMIAL_CHAOS),
7190 	MP2s(methodName,MULTILEVEL_SAMPLING),
7191         MP2s(methodName,POF_DARTS),
7192 	MP2s(methodName,RKD_DARTS),
7193 	MP2s(methodName,POLYNOMIAL_CHAOS),
7194 	MP2s(methodName,STOCH_COLLOCATION),
7195 	MP2s(methodName,SURROGATE_BASED_UQ),
7196 	MP2s(methodName,RANDOM_SAMPLING),
7197 	MP2s(methodName,NONLINEAR_CG),
7198 	MP2s(methodName,NPSOL_SQP),
7199 	MP2s(methodName,OPTPP_CG),
7200 	MP2s(methodName,OPTPP_FD_NEWTON),
7201 	MP2s(methodName,OPTPP_G_NEWTON),
7202 	MP2s(methodName,OPTPP_NEWTON),
7203 	MP2s(methodName,OPTPP_PDS),
7204 	MP2s(methodName,OPTPP_Q_NEWTON),
7205 	MP2s(methodName,PARETO_SET),
7206 	MP2s(methodName,PSUADE_MOAT),
7207 	MP2s(methodName,RICHARDSON_EXTRAP),
7208 	MP2s(methodName,SOGA),
7209 	MP2s(methodName,SURROGATE_BASED_GLOBAL),
7210 	MP2s(methodName,SURROGATE_BASED_LOCAL),
7211 	MP2s(methodName,VECTOR_PARAMETER_STUDY),
7212 	MP2s(methodName,LIST_PARAMETER_STUDY),
7213 	MP2s(methodName,CENTERED_PARAMETER_STUDY),
7214 	MP2s(methodName,MULTIDIM_PARAMETER_STUDY),
7215         MP2s(modelExportFormat,TEXT_ARCHIVE),
7216         MP2s(modelExportFormat,BINARY_ARCHIVE),
7217 	MP2s(preSolveMethod,SUBMETHOD_NIP),
7218 	MP2s(preSolveMethod,SUBMETHOD_NONE),
7219 	MP2s(preSolveMethod,SUBMETHOD_SQP),
7220 	MP2s(pstudyFileFormat,TABULAR_NONE),
7221         MP2s(pstudyFileFormat,TABULAR_HEADER),
7222         MP2s(pstudyFileFormat,TABULAR_EVAL_ID),
7223         MP2s(pstudyFileFormat,TABULAR_IFACE_ID),
7224         MP2s(pstudyFileFormat,TABULAR_ANNOTATED),
7225 	MP2s(reliabilitySearchType,AMV_PLUS_U),
7226 	MP2s(reliabilitySearchType,AMV_PLUS_X),
7227 	MP2s(reliabilitySearchType,AMV_U),
7228 	MP2s(reliabilitySearchType,AMV_X),
7229 	MP2s(reliabilitySearchType,EGRA_U),
7230 	MP2s(reliabilitySearchType,EGRA_X),
7231 	MP2s(reliabilitySearchType,NO_APPROX),
7232 	MP2s(reliabilitySearchType,QMEA_U),
7233 	MP2s(reliabilitySearchType,QMEA_X),
7234 	MP2s(reliabilitySearchType,TANA_U),
7235 	MP2s(reliabilitySearchType,TANA_X),
7236 	MP2s(sampleType,SUBMETHOD_LHS),
7237 	MP2s(sampleType,SUBMETHOD_RANDOM),
7238 	MP2s(subMethod,SUBMETHOD_COLLABORATIVE),
7239 	MP2s(subMethod,SUBMETHOD_EMBEDDED),
7240 	MP2s(subMethod,SUBMETHOD_SEQUENTIAL),
7241 	MP2s(subMethod,SUBMETHOD_MUQ),
7242 	MP2s(subMethod,SUBMETHOD_DREAM),
7243 	MP2s(subMethod,SUBMETHOD_WASABI),
7244 	MP2s(subMethod,SUBMETHOD_GPMSA),
7245 	MP2s(subMethod,SUBMETHOD_QUESO),
7246 	MP2s(subMethod,SUBMETHOD_NIP),
7247 	MP2s(subMethod,SUBMETHOD_SQP),
7248 	MP2s(subMethod,SUBMETHOD_EA),
7249 	MP2s(subMethod,SUBMETHOD_EGO),
7250 	MP2s(subMethod,SUBMETHOD_SBO),
7251 	MP2s(subMethod,SUBMETHOD_LHS),
7252 	MP2s(subMethod,SUBMETHOD_RANDOM),
7253 	MP2s(subMethod,SUBMETHOD_OA_LHS),
7254 	MP2s(subMethod,SUBMETHOD_OAS),
7255 	MP2s(subMethod,SUBMETHOD_BOX_BEHNKEN),
7256 	MP2s(subMethod,SUBMETHOD_CENTRAL_COMPOSITE),
7257 	MP2s(subMethod,SUBMETHOD_GRID),
7258 	MP2s(subMethod,SUBMETHOD_CONVERGE_ORDER),
7259 	MP2s(subMethod,SUBMETHOD_CONVERGE_QOI),
7260         MP2s(subMethod,SUBMETHOD_ESTIMATE_ORDER);
7261 
7262 #undef MP3s
7263 #undef MP3
7264 #undef MP2p
7265 #undef MP2o
7266 #undef MP2s
7267 #undef MP2
7268 #undef MP_
7269 
7270 
7271 // Macros for handling Model data
7272 
7273 #define MP_(x) DataModelRep::* model_mp_##x = &DataModelRep::x
7274 #define MP2(x,y) model_mp_##x##_##y = {&DataModelRep::x,#y}
7275 #define MP2s(x,y) model_mp_##x##_##y = {&DataModelRep::x,y}
7276 #define MP2p(x,y) model_mp_##x##_##y = {&DataModelRep::x,Pecos::y}
7277 
7278 static IntSet
7279 	MP_(surrogateFnIndices);
7280 
7281 static Model_mp_lit
7282 	MP2(approxPointReuse,all),
7283 	MP2(approxPointReuse,none),
7284 	MP2(approxPointReuse,region),
7285 	MP2(marsInterpolation,linear),
7286 	MP2(marsInterpolation,cubic),
7287 	MP2(modelType,active_subspace),
7288 	MP2(modelType,adapted_basis),
7289 	MP2(modelType,nested),
7290 	MP2(modelType,random_field),
7291 	MP2(modelType,simulation),
7292 	MP2(modelType,surrogate),
7293 	MP2(surrogateType,hierarchical),
7294 	MP2(surrogateType,global_exp_gauss_proc),
7295 	MP2(surrogateType,global_exp_poly),
7296 	MP2(surrogateType,global_function_train),
7297 	MP2(surrogateType,global_gaussian),
7298 	MP2(surrogateType,global_kriging),
7299 	MP2(surrogateType,global_mars),
7300 	MP2(surrogateType,global_moving_least_squares),
7301 	MP2(surrogateType,global_neural_network),
7302 	MP2(surrogateType,global_polynomial),
7303 	MP2(surrogateType,global_radial_basis),
7304 	MP2(surrogateType,global_voronoi_surrogate),
7305 	MP2(surrogateType,local_taylor),
7306         MP2(surrogateType,multipoint_qmea),
7307         MP2(surrogateType,multipoint_tana),
7308         MP2(trendOrder,none),
7309         MP2(trendOrder,constant),
7310         MP2(trendOrder,linear),
7311         MP2(trendOrder,reduced_quadratic),
7312         MP2(trendOrder,quadratic);
7313 
7314 static Model_mp_ord
7315 	MP2s(approxCorrectionOrder,0),
7316 	MP2s(approxCorrectionOrder,1),
7317 	MP2s(approxCorrectionOrder,2),
7318 	MP2s(polynomialOrder,1),
7319 	MP2s(polynomialOrder,2),
7320         MP2s(polynomialOrder,3);
7321 
7322 static Model_mp_type
7323 	MP2s(approxCorrectionType,ADDITIVE_CORRECTION),
7324 	MP2s(approxCorrectionType,COMBINED_CORRECTION),
7325 	MP2s(approxCorrectionType,MULTIPLICATIVE_CORRECTION),
7326 	MP2s(c3AdvanceType,MAX_ORDER_ADVANCEMENT),
7327 	MP2s(c3AdvanceType,MAX_RANK_ADVANCEMENT),
7328 	MP2s(c3AdvanceType,MAX_RANK_ORDER_ADVANCEMENT),
7329 	MP2s(c3AdvanceType,START_ORDER_ADVANCEMENT),
7330 	MP2s(c3AdvanceType,START_RANK_ADVANCEMENT),
7331 	MP2s(pointsManagement,MINIMUM_POINTS),
7332 	MP2s(pointsManagement,RECOMMENDED_POINTS),
7333       //MP2p(refinementControl,UNIFORM_CONTROL),  // Pecos
7334       //MP2p(refinementType,P_REFINEMENT),        // Pecos
7335 	MP2s(regressionType,FT_LS),
7336 	MP2s(regressionType,FT_RLS2),
7337 	MP2s(subMethodScheduling,MASTER_SCHEDULING),
7338 	MP2s(subMethodScheduling,PEER_SCHEDULING);
7339       //MP2s(subMethodScheduling,PEER_DYNAMIC_SCHEDULING),
7340       //MP2s(subMethodScheduling,PEER_STATIC_SCHEDULING),
7341 
7342 
7343 static Model_mp_utype
7344         MP2s(analyticCovIdForm,EXP_L2),
7345         MP2s(analyticCovIdForm,EXP_L1),
7346         MP2s(exportApproxFormat,TABULAR_NONE),
7347         MP2s(exportApproxFormat,TABULAR_HEADER),
7348         MP2s(exportApproxFormat,TABULAR_EVAL_ID),
7349         MP2s(exportApproxFormat,TABULAR_IFACE_ID),
7350         MP2s(exportApproxFormat,TABULAR_ANNOTATED),
7351         MP2s(exportApproxVarianceFormat,TABULAR_NONE),
7352         MP2s(exportApproxVarianceFormat,TABULAR_HEADER),
7353         MP2s(exportApproxVarianceFormat,TABULAR_EVAL_ID),
7354         MP2s(exportApproxVarianceFormat,TABULAR_IFACE_ID),
7355         MP2s(exportApproxVarianceFormat,TABULAR_ANNOTATED),
7356       //MP2s(importApproxFormat,TABULAR_NONE),
7357       //MP2s(importApproxFormat,TABULAR_HEADER),
7358       //MP2s(importApproxFormat,TABULAR_EVAL_ID),
7359       //MP2s(importApproxFormat,TABULAR_IFACE_ID),
7360       //MP2s(importApproxFormat,TABULAR_ANNOTATED),
7361         MP2s(importBuildFormat,TABULAR_NONE),
7362         MP2s(importBuildFormat,TABULAR_HEADER),
7363         MP2s(importBuildFormat,TABULAR_EVAL_ID),
7364         MP2s(importBuildFormat,TABULAR_IFACE_ID),
7365         MP2s(importBuildFormat,TABULAR_ANNOTATED),
7366         MP2s(importChallengeFormat,TABULAR_NONE),
7367         MP2s(importChallengeFormat,TABULAR_HEADER),
7368         MP2s(importChallengeFormat,TABULAR_EVAL_ID),
7369         MP2s(importChallengeFormat,TABULAR_IFACE_ID),
7370         MP2s(importChallengeFormat,TABULAR_ANNOTATED),
7371         MP2s(modelExportFormat,TEXT_ARCHIVE),
7372         MP2s(modelExportFormat,BINARY_ARCHIVE),
7373         MP2s(modelExportFormat,ALGEBRAIC_FILE),
7374         MP2s(modelExportFormat,ALGEBRAIC_CONSOLE),
7375         MP2s(randomFieldIdForm,RF_KARHUNEN_LOEVE),
7376         MP2s(randomFieldIdForm,RF_PCA_GP),
7377 	MP2s(subspaceNormalization,SUBSPACE_NORM_MEAN_VALUE),
7378 	MP2s(subspaceNormalization,SUBSPACE_NORM_MEAN_GRAD),
7379 	MP2s(subspaceNormalization,SUBSPACE_NORM_LOCAL_GRAD),
7380 	MP2s(subspaceSampleType,SUBMETHOD_LHS),
7381 	MP2s(subspaceSampleType,SUBMETHOD_RANDOM),
7382 	MP2s(subspaceIdCVMethod,MINIMUM_METRIC),
7383 	MP2s(subspaceIdCVMethod,RELATIVE_TOLERANCE),
7384 	MP2s(subspaceIdCVMethod,DECREASE_TOLERANCE);
7385 
7386 static Real
7387         MP_(adaptedBasisCollocRatio),
7388         MP_(annRange),
7389 	MP_(collocationRatio),
7390 	MP_(convergenceTolerance),
7391 	MP_(decreaseTolerance),
7392         MP_(discontGradThresh),
7393         MP_(discontJumpThresh),
7394 	MP_(krigingNugget),
7395 	MP_(percentFold),
7396 	MP_(regressionL2Penalty),
7397 	MP_(relTolerance),
7398 	MP_(solverRoundingTol),
7399 	MP_(solverTol),
7400 	MP_(statsRoundingTol),
7401 	MP_(truncationTolerance);
7402 
7403 static RealVector
7404 	MP_(krigingCorrelations),
7405       //MP_(krigingMaxCorrelations),
7406       //MP_(krigingMinCorrelations),
7407 	MP_(primaryRespCoeffs),
7408 	MP_(secondaryRespCoeffs),
7409   	MP_(solutionLevelCost);
7410 
7411 static IntVector
7412 	MP_(refineSamples);
7413 
7414 static String
7415 	MP_(actualModelPointer),
7416         MP_(advancedOptionsFilename),
7417 	MP_(decompCellType),
7418 	MP_(exportApproxPtsFile),
7419 	MP_(exportApproxVarianceFile),
7420 	MP_(idModel),
7421       //MP_(importApproxPtsFile),
7422 	MP_(importBuildPtsFile),
7423 	MP_(importChallengePtsFile),
7424 	MP_(interfacePointer),
7425 	MP_(krigingOptMethod),
7426 	MP_(modelExportPrefix),
7427 	MP_(optionalInterfRespPointer),
7428 	MP_(propagationModelPointer),
7429 	MP_(refineCVMetric),
7430 	MP_(responsesPointer),
7431 	MP_(rfDataFileName),
7432 	MP_(solutionLevelControl),
7433 	MP_(subMethodPointer),
7434 	MP_(variablesPointer);
7435 
7436 static StringArray
7437         MP_(diagMetrics),
7438 	MP_(orderedModelPointers),
7439 	MP_(primaryVarMaps),
7440         MP_(secondaryVarMaps);
7441 
7442 static bool
7443         MP_(adaptOrder),
7444         MP_(adaptRank),
7445 	MP_(autoRefine),
7446 	MP_(crossValidateFlag),
7447 	MP_(decompDiscontDetect),
7448 	MP_(exportSurrogate),
7449 	MP_(hierarchicalTags),
7450         MP_(identityRespMap),
7451       //MP_(importApproxActive),
7452 	MP_(importBuildActive),
7453 	MP_(importChallengeActive),
7454 	MP_(importChalUseVariableLabels),
7455 	MP_(importUseVariableLabels),
7456 	MP_(modelUseDerivsFlag),
7457         MP_(domainDecomp),
7458         MP_(pointSelection),
7459         MP_(pressFlag),
7460         MP_(subspaceIdBingLi),
7461         MP_(subspaceIdConstantine),
7462         MP_(subspaceIdEnergy),
7463         MP_(subspaceBuildSurrogate),
7464         MP_(subspaceIdCV),
7465         MP_(subspaceCVIncremental),
7466 	MP_(tensorGridFlag);
7467 
7468 static unsigned short
7469 	MP_(adaptedBasisSparseGridLev),
7470 	MP_(adaptedBasisExpOrder),
7471 	MP_(kickOrder),
7472 	MP_(maxOrder),
7473 	MP_(startOrder);
7474 
7475 static short
7476 	MP_(annNodes),
7477 	MP_(annRandomWeight),
7478 	MP_(c3AdvanceType),
7479 	MP_(krigingFindNugget),
7480 	MP_(krigingMaxTrials),
7481 	MP_(marsMaxBases),
7482 	MP_(mlsWeightFunction),
7483 	MP_(polynomialOrder),
7484 	MP_(rbfBases),
7485 	MP_(rbfMaxPts),
7486 	MP_(rbfMaxSubsets),
7487 	MP_(rbfMinPartition);
7488 
7489 static int
7490         MP_(decompSupportLayers),
7491         MP_(initialSamples),
7492         MP_(maxCrossIterations),
7493         MP_(maxFunctionEvals),
7494         MP_(maxIterations),
7495 	MP_(maxSolverIterations),
7496         MP_(numFolds),
7497         MP_(numReplicates),
7498         MP_(numRestarts),
7499         MP_(pointsTotal),
7500         MP_(refineCVFolds),
7501         MP_(softConvergenceLimit),
7502         MP_(subMethodProcs),
7503         MP_(subMethodServers),
7504         MP_(subspaceDimension),
7505         MP_(subspaceCVMaxRank);
7506 
7507 static size_t
7508 	MP_(collocationPoints),
7509 	MP_(kickRank),
7510 	MP_(maxRank),
7511 	MP_(startRank);
7512 //	MP_(verbosity);
7513 
7514 #undef MP2p
7515 #undef MP2s
7516 #undef MP2
7517 #undef MP_
7518 
7519 #define MP_(x) DataResponsesRep::* resp_mp_##x = &DataResponsesRep::x
7520 #define MP2(x,y) resp_mp_##x##_##y = {&DataResponsesRep::x,#y}
7521 #define MP2s(x,y) resp_mp_##x##_##y = {&DataResponsesRep::x,y}
7522 
7523 static IntSet
7524 	MP_(idAnalyticGrads),
7525 	MP_(idAnalyticHessians),
7526 	MP_(idNumericalGrads),
7527 	MP_(idNumericalHessians),
7528 	MP_(idQuasiHessians);
7529 
7530 static IntVector
7531 	MP_(fieldLengths),
7532 	MP_(numCoordsPerField);
7533 
7534 static RealVector
7535 	MP_(expConfigVars),
7536 	MP_(expObservations),
7537 	MP_(primaryRespFnWeights),
7538 	MP_(nonlinearEqTargets),
7539 	MP_(nonlinearIneqLowerBnds),
7540 	MP_(nonlinearIneqUpperBnds),
7541 	MP_(simVariance),
7542 	MP_(fdGradStepSize),
7543 	MP_(fdHessStepSize),
7544 	MP_(primaryRespFnScales),
7545 	MP_(nonlinearEqScales),
7546 	MP_(nonlinearIneqScales);
7547 
7548 static Resp_mp_lit
7549 	MP2(gradientType,analytic),
7550 	MP2(gradientType,mixed),
7551 	MP2(gradientType,none),
7552 	MP2(gradientType,numerical),
7553 	MP2(hessianType,analytic),
7554 	MP2(hessianType,mixed),
7555 	MP2(hessianType,none),
7556 	MP2(hessianType,numerical),
7557 	MP2(hessianType,quasi),
7558 	MP2(intervalType,central),
7559 	MP2(intervalType,forward),
7560 	MP2(methodSource,dakota),
7561 	MP2(methodSource,vendor),
7562         MP2(fdGradStepType,absolute),
7563         MP2(fdGradStepType,bounds),
7564         MP2(fdGradStepType,relative),
7565         MP2(fdHessStepType,absolute),
7566         MP2(fdHessStepType,bounds),
7567         MP2(fdHessStepType,relative),
7568 	MP2(quasiHessianType,bfgs),
7569 	MP2(quasiHessianType,damped_bfgs),
7570 	MP2(quasiHessianType,sr1);
7571 
7572 static String
7573 	MP_(scalarDataFileName),
7574         MP_(idResponses);
7575 
7576 static StringArray
7577 	MP_(nonlinearEqScaleTypes),
7578 	MP_(nonlinearIneqScaleTypes),
7579 	MP_(primaryRespFnScaleTypes),
7580 	MP_(primaryRespFnSense),
7581 	MP_(responseLabels),
7582 	MP_(varianceType);
7583 
7584 static bool
7585 	MP_(calibrationDataFlag),
7586 	MP_(centralHess),
7587 	MP_(interpolateFlag),
7588         MP_(ignoreBounds),
7589         MP_(readFieldCoords);
7590 
7591 static size_t
7592 	MP_(numExpConfigVars),
7593         MP_(numExperiments),
7594 	MP_(numFieldLeastSqTerms),
7595 	MP_(numFieldObjectiveFunctions),
7596 	MP_(numFieldResponseFunctions),
7597 	MP_(numLeastSqTerms),
7598 	MP_(numNonlinearEqConstraints),
7599 	MP_(numNonlinearIneqConstraints),
7600 	MP_(numObjectiveFunctions),
7601 	MP_(numResponseFunctions),
7602 	MP_(numScalarLeastSqTerms),
7603 	MP_(numScalarObjectiveFunctions),
7604 	MP_(numScalarResponseFunctions);
7605 
7606 static Resp_mp_utype
7607         MP2s(scalarDataFormat,TABULAR_NONE),
7608         MP2s(scalarDataFormat,TABULAR_HEADER),
7609         MP2s(scalarDataFormat,TABULAR_EVAL_ID),
7610         MP2s(scalarDataFormat,TABULAR_EXPER_ANNOT);
7611 
7612 #undef MP2s
7613 #undef MP2
7614 #undef MP_
7615 
7616 // Macros for Environment
7617 
7618 #define MP_(x) DataEnvironmentRep::* env_mp_##x = &DataEnvironmentRep::x
7619 #define MP2s(x,y) env_mp_##x##_##y = {&DataEnvironmentRep::x,y}
7620 
7621 //static Env_mp_lit
7622 //      MP2(,);
7623 
7624 static Env_mp_utype
7625         MP2s(postRunInputFormat,TABULAR_NONE),
7626         MP2s(postRunInputFormat,TABULAR_HEADER),
7627         MP2s(postRunInputFormat,TABULAR_EVAL_ID),
7628         MP2s(postRunInputFormat,TABULAR_IFACE_ID),
7629         MP2s(postRunInputFormat,TABULAR_ANNOTATED),
7630         MP2s(preRunOutputFormat,TABULAR_NONE),
7631         MP2s(preRunOutputFormat,TABULAR_HEADER),
7632         MP2s(preRunOutputFormat,TABULAR_EVAL_ID),
7633         MP2s(preRunOutputFormat,TABULAR_IFACE_ID),
7634         MP2s(preRunOutputFormat,TABULAR_ANNOTATED),
7635         MP2s(tabularFormat,TABULAR_NONE),
7636         MP2s(tabularFormat,TABULAR_HEADER),
7637         MP2s(tabularFormat,TABULAR_EVAL_ID),
7638         MP2s(tabularFormat,TABULAR_IFACE_ID),
7639         MP2s(tabularFormat,TABULAR_ANNOTATED),
7640         MP2s(resultsOutputFormat,RESULTS_OUTPUT_TEXT),
7641         MP2s(resultsOutputFormat,RESULTS_OUTPUT_HDF5),
7642         MP2s(modelEvalsSelection,MODEL_EVAL_STORE_TOP_METHOD),
7643         MP2s(modelEvalsSelection,MODEL_EVAL_STORE_NONE),
7644         MP2s(modelEvalsSelection,MODEL_EVAL_STORE_ALL),
7645         MP2s(modelEvalsSelection,MODEL_EVAL_STORE_ALL_METHODS),
7646         MP2s(interfEvalsSelection,INTERF_EVAL_STORE_SIMULATION),
7647         MP2s(interfEvalsSelection,INTERF_EVAL_STORE_NONE),
7648         MP2s(interfEvalsSelection,INTERF_EVAL_STORE_ALL);
7649 
7650 static String
7651         MP_(errorFile),
7652         MP_(outputFile),
7653         MP_(postRunInput),
7654         MP_(postRunOutput),
7655         MP_(preRunInput),
7656         MP_(preRunOutput),
7657         MP_(readRestart),
7658         MP_(resultsOutputFile),
7659         MP_(runInput),
7660         MP_(runOutput),
7661 	MP_(tabularDataFile),
7662         MP_(topMethodPointer),
7663         MP_(writeRestart);
7664 
7665 static bool
7666 	MP_(checkFlag),
7667 	MP_(graphicsFlag),
7668 	MP_(postRunFlag),
7669 	MP_(preRunFlag),
7670         MP_(resultsOutputFlag),
7671 	MP_(runFlag),
7672 	MP_(tabularDataFlag);
7673 
7674 static int
7675         MP_(outputPrecision),
7676         MP_(stopRestart);
7677 
7678 //#undef MP2
7679 #undef MP2s
7680 #undef MP_
7681 
7682 #define MP_(x) DataVariablesRep::* var_mp_##x = &DataVariablesRep::x
7683 #define MP2s(x,y) var_mp_##x = {&DataVariablesRep::x,y}
7684 #define VP_(x) *Var_Info::* var_mp_Var_Info_##x = &Var_Info::x
7685 #define Vtype(x,y) var_mp_##x##_##y = {&DataVariablesRep::x,y}
7686 
7687 static size_t
7688 	MP_(numBetaUncVars),
7689 	MP_(numBinomialUncVars),
7690 	MP_(numContinuousDesVars),
7691 	MP_(numContinuousIntervalUncVars),
7692 	MP_(numContinuousStateVars),
7693 	MP_(numDiscreteDesRangeVars),
7694 	MP_(numDiscreteDesSetIntVars),
7695 	MP_(numDiscreteDesSetStrVars),
7696 	MP_(numDiscreteDesSetRealVars),
7697 	MP_(numDiscreteIntervalUncVars),
7698 	MP_(numDiscreteStateRangeVars),
7699 	MP_(numDiscreteStateSetIntVars),
7700 	MP_(numDiscreteStateSetStrVars),
7701 	MP_(numDiscreteStateSetRealVars),
7702 	MP_(numDiscreteUncSetIntVars),
7703 	MP_(numDiscreteUncSetStrVars),
7704 	MP_(numDiscreteUncSetRealVars),
7705 	MP_(numExponentialUncVars),
7706 	MP_(numFrechetUncVars),
7707 	MP_(numGammaUncVars),
7708 	MP_(numGeometricUncVars),
7709 	MP_(numGumbelUncVars),
7710 	MP_(numHistogramBinUncVars),
7711 	MP_(numHistogramPtIntUncVars),
7712 	MP_(numHistogramPtStrUncVars),
7713 	MP_(numHistogramPtRealUncVars),
7714 	MP_(numHyperGeomUncVars),
7715 	MP_(numLognormalUncVars),
7716 	MP_(numLoguniformUncVars),
7717 	MP_(numNegBinomialUncVars),
7718 	MP_(numNormalUncVars),
7719 	MP_(numPoissonUncVars),
7720 	MP_(numTriangularUncVars),
7721 	MP_(numUniformUncVars),
7722 	MP_(numWeibullUncVars);
7723 
7724 static IntVector
7725 	VP_(ddsi),
7726 	VP_(DIlb),
7727 	MP_(discreteDesignRangeLowerBnds),
7728 	MP_(discreteDesignRangeUpperBnds),
7729 	MP_(discreteDesignRangeVars),
7730 	MP_(discreteDesignSetIntVars),
7731 	MP_(discreteIntervalUncVars),
7732         MP_(discreteStateRangeLowerBnds),
7733 	MP_(discreteStateRangeUpperBnds),
7734 	MP_(discreteStateRangeVars),
7735 	MP_(discreteStateSetIntVars),
7736 	MP_(discreteUncSetIntVars),
7737 	VP_(DIub),
7738         MP_(histogramPointIntUncVars),
7739         VP_(hpia),
7740         VP_(dssi),
7741         VP_(ddsia),
7742         VP_(ddssa),
7743         VP_(ddsra),
7744         VP_(dusi);
7745 
7746 static IntArray
7747 	VP_(nddsi),
7748 	VP_(nddss),
7749 	VP_(nddsr),
7750 	VP_(ndssi),
7751 	VP_(ndsss),
7752 	VP_(ndssr),
7753 	VP_(ndusi),
7754 	VP_(nduss),
7755 	VP_(ndusr),
7756 	VP_(nhbp),
7757 	VP_(nhpip),
7758 	VP_(nhpsp),
7759 	VP_(nhprp),
7760 	VP_(nCI),
7761 	VP_(nDI);
7762 
7763 static RealVector
7764 	MP_(betaUncLowerBnds),
7765 	MP_(betaUncUpperBnds),
7766 	MP_(betaUncVars),
7767         MP_(binomialUncProbPerTrial),
7768         MP_(continuousDesignLowerBnds),
7769 	MP_(continuousDesignUpperBnds),
7770 	MP_(continuousDesignVars),
7771 	MP_(continuousDesignScales),
7772 	MP_(continuousIntervalUncVars),
7773 	MP_(continuousStateLowerBnds),
7774 	MP_(continuousStateUpperBnds),
7775 	MP_(continuousStateVars),
7776 	MP_(discreteDesignSetRealVars),
7777 	MP_(discreteStateSetRealVars),
7778 	MP_(discreteUncSetRealVars),
7779 	MP_(frechetUncBetas),
7780 	MP_(frechetUncVars),
7781         MP_(geometricUncProbPerTrial),
7782 	MP_(gumbelUncBetas),
7783 	MP_(gumbelUncVars),
7784 	MP_(histogramBinUncVars),
7785         MP_(histogramPointRealUncVars),
7786 	MP_(linearEqConstraintCoeffs),
7787 	MP_(linearEqScales),
7788 	MP_(linearEqTargets),
7789 	MP_(linearIneqConstraintCoeffs),
7790 	MP_(linearIneqLowerBnds),
7791 	MP_(linearIneqUpperBnds),
7792 	MP_(linearIneqScales),
7793         MP_(negBinomialUncProbPerTrial),
7794 	MP_(normalUncLowerBnds),
7795 	MP_(normalUncMeans),
7796 	MP_(normalUncUpperBnds),
7797 	MP_(normalUncVars),
7798 	MP_(triangularUncModes),
7799 	MP_(triangularUncVars),
7800 	MP_(uniformUncVars),
7801 	MP_(weibullUncVars),
7802 	VP_(ddsr),
7803 	VP_(dssr),
7804 	VP_(dusr),
7805 	VP_(CIlb),
7806 	VP_(CIub),
7807 	VP_(CIp),
7808 	VP_(DIp),
7809 	VP_(DSIp),
7810 	VP_(DSSp),
7811 	VP_(DSRp),
7812 	VP_(hba),
7813 	VP_(hbo),
7814 	VP_(hbc),
7815 	VP_(hpic),
7816 	VP_(hpsc),
7817 	VP_(hpra),
7818 	VP_(hprc),
7819 	VP_(ucm);
7820 
7821 static String
7822 	MP_(idVariables);
7823 
7824 static StringArray
7825 	MP_(continuousDesignLabels),
7826 	MP_(continuousDesignScaleTypes),
7827 	MP_(continuousStateLabels),
7828 	MP_(discreteDesignRangeLabels),
7829 	MP_(discreteDesignSetIntLabels),
7830 	MP_(discreteDesignSetStrLabels),
7831 	MP_(discreteDesignSetRealLabels),
7832 	MP_(discreteStateRangeLabels),
7833 	MP_(discreteStateSetIntLabels),
7834 	MP_(discreteStateSetStrLabels),
7835         MP_(discreteStateSetRealLabels),
7836 	MP_(discreteDesignSetStrVars),
7837 	MP_(discreteUncSetStrVars),
7838 	MP_(discreteStateSetStrVars),
7839         MP_(histogramPointStrUncVars),
7840 	MP_(linearEqScaleTypes),
7841 	MP_(linearIneqScaleTypes),
7842         VP_(hpsa),
7843         VP_(ddss),
7844         VP_(duss),
7845         VP_(dsss);
7846 
7847 static BitArray
7848         MP_(discreteDesignSetIntCat),
7849         MP_(discreteDesignSetRealCat),
7850         MP_(discreteStateSetIntCat),
7851         MP_(discreteStateSetRealCat),
7852         MP_(discreteUncSetIntCat),
7853         MP_(discreteUncSetRealCat);
7854 
7855 static Var_brv
7856 	MP2s(betaUncAlphas,0.),
7857 	MP2s(betaUncBetas,0.),
7858 	MP2s(exponentialUncBetas,0.),
7859 	MP2s(exponentialUncVars,0.),
7860 	MP2s(frechetUncAlphas,2.),
7861 	MP2s(gammaUncAlphas,0.),
7862 	MP2s(gammaUncBetas,0.),
7863 	MP2s(gammaUncVars,0.),
7864 	MP2s(gumbelUncAlphas,0.),
7865 	MP2s(lognormalUncErrFacts,1.),
7866 	MP2s(lognormalUncLambdas,0.),
7867 	MP2s(lognormalUncLowerBnds,0.),
7868 	MP2s(lognormalUncMeans,0.),
7869 	MP2s(lognormalUncStdDevs,0.),
7870 	MP2s(lognormalUncUpperBnds,std::numeric_limits<Real>::infinity()),
7871 	MP2s(lognormalUncVars,0.),
7872 	MP2s(lognormalUncZetas,0.),
7873 	MP2s(loguniformUncLowerBnds,0.),
7874 	MP2s(loguniformUncUpperBnds,std::numeric_limits<Real>::infinity()),
7875 	MP2s(loguniformUncVars,0.),
7876 	MP2s(normalUncStdDevs,0.),
7877 	MP2s(poissonUncLambdas,0.),
7878 	MP2s(triangularUncLowerBnds,-std::numeric_limits<Real>::infinity()),
7879 	MP2s(triangularUncUpperBnds, std::numeric_limits<Real>::infinity()),
7880 	MP2s(uniformUncLowerBnds,-std::numeric_limits<Real>::infinity()),
7881 	MP2s(uniformUncUpperBnds, std::numeric_limits<Real>::infinity()),
7882 	MP2s(weibullUncAlphas,0.),
7883 	MP2s(weibullUncBetas,0.);
7884 
7885 static Var_biv
7886 	MP2s(binomialUncNumTrials,0),
7887 	MP2s(binomialUncVars,0),
7888 	MP2s(geometricUncVars,0),
7889 	MP2s(hyperGeomUncNumDrawn,0),
7890 	MP2s(hyperGeomUncSelectedPop,0),
7891 	MP2s(hyperGeomUncTotalPop,0),
7892 	MP2s(hyperGeomUncVars,0),
7893 	MP2s(negBinomialUncNumTrials,0),
7894 	MP2s(negBinomialUncVars,0),
7895 	MP2s(poissonUncVars,0);
7896 
7897 static Var_mp_type
7898 	Vtype(varsDomain,MIXED_DOMAIN),
7899 	Vtype(varsDomain,RELAXED_DOMAIN),
7900 	Vtype(varsView,ALL_VIEW),
7901 	Vtype(varsView,DESIGN_VIEW),
7902 	Vtype(varsView,UNCERTAIN_VIEW),
7903 	Vtype(varsView,ALEATORY_UNCERTAIN_VIEW),
7904 	Vtype(varsView,EPISTEMIC_UNCERTAIN_VIEW),
7905         Vtype(varsView,STATE_VIEW);
7906 
7907 #undef Vtype
7908 #undef VP_
7909 #undef MP2s
7910 #undef MP_
7911 
7912 } // namespace Dakota
7913 
7914 #undef Warn
7915 #undef Squawk
7916 
7917 #define N_ifm(x,y)	NIDRProblemDescDB::iface_##x,&iface_mp_##y
7918 #define N_ifm3(x,y,z)	NIDRProblemDescDB::iface_##x,y,NIDRProblemDescDB::iface_##z
7919 #define N_mdm(x,y)	NIDRProblemDescDB::method_##x,&method_mp_##y
7920 //#define N_mdf(x,y)	N_mdm(x,y),NIDRProblemDescDB::method_##x##_final
7921 #define N_mdm3(x,y,z)	NIDRProblemDescDB::method_##x,y,NIDRProblemDescDB::method_##z
7922 #define N_mom(x,y)	NIDRProblemDescDB::model_##x,&model_mp_##y
7923 #define N_mof(x,y)	N_mom(x,y),NIDRProblemDescDB::model_##x##_final
7924 #define N_mom3(x,y,z)	NIDRProblemDescDB::model_##x,y,NIDRProblemDescDB::model_##z
7925 #define N_rem(x,y)	NIDRProblemDescDB::resp_##x,&resp_mp_##y
7926 #define N_rem3(x,y,z)	NIDRProblemDescDB::resp_##x,y,NIDRProblemDescDB::resp_##z
7927 #define N_stm(x,y)	NIDRProblemDescDB::env_##x,&env_mp_##y
7928 #define N_vae(x,y)	NIDRProblemDescDB::var_##x,(void*)y
7929 #define N_vam(x,y)	NIDRProblemDescDB::var_##x,&var_mp_##y
7930 #define N_vam3(x,y,z)	NIDRProblemDescDB::var_##x,y,NIDRProblemDescDB::var_##z
7931 
7932 #include "NIDR_keywds.hpp"
7933