| 1 | from MatlabFuncs import *
|
|---|
| 2 | from uniform_uncertain import *
|
|---|
| 3 | from normal_uncertain import *
|
|---|
| 4 | from copy import deepcopy
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 | def QmuSetupVariables(md, dvar, variables):
|
|---|
| 8 |
|
|---|
| 9 | #get descriptor
|
|---|
| 10 | descriptor = variables.descriptor
|
|---|
| 11 |
|
|---|
| 12 | #decide whether this is a distributed variable, which will drive whether we expand it into npart values,
|
|---|
| 13 | #or if we just carry it forward as is.
|
|---|
| 14 |
|
|---|
| 15 | #ok, key off according to type of descriptor:
|
|---|
| 16 | if strncmp(descriptor, 'scaled_', 7):
|
|---|
| 17 | #we have a scaled variable, expand it over the partition.
|
|---|
| 18 | if isinstance(variables, uniform_uncertain):
|
|---|
| 19 | if ((type(variables.lower) in [list, np.ndarray] and len(variables.lower) > md.qmu.numberofpartitions) or (type(variables.upper) in [list, np.ndarray] and len(variables.upper) > md.qmu.numberofpartitions)):
|
|---|
| 20 | raise RuntimeError('QmuSetupDesign error message: upper and lower should be either a scalar or a "npart" length vector')
|
|---|
| 21 |
|
|---|
| 22 | elif isinstance(variables, normal_uncertain):
|
|---|
| 23 | if type(variables.stddev) in [list, np.ndarray] and len(variables.stddev) > md.qmu.numberofpartitions:
|
|---|
| 24 | raise RuntimeError('QmuSetupDesign error message: stddev should be either a scalar or a "npart" length vector')
|
|---|
| 25 |
|
|---|
| 26 | #ok, dealing with semi - discrete distributed variable. Distribute according to how many
|
|---|
| 27 | #partitions we want
|
|---|
| 28 | for j in range(md.qmu.numberofpartitions):
|
|---|
| 29 | dvar.append(deepcopy(variables))
|
|---|
| 30 | # "'" is because qmu.in files need for strings to be in actual ''
|
|---|
| 31 | # must also account for whether we are given 1 instance or an array of instances
|
|---|
| 32 | # handle descriptors for everything
|
|---|
| 33 | if type(dvar[-1].descriptor) in [list, np.ndarray] and len(variables.descriptor) > 1 and len(variables.upper) != md.qmu.numberofpartitions:
|
|---|
| 34 | if type(variables.descriptor) == np.ndarray:
|
|---|
| 35 | dvar[-1].descriptor = np.append(dvar[-1].descriptor, "'" + str(variables.descriptor) + '_' + str(j + 1) + "'")
|
|---|
| 36 | else:
|
|---|
| 37 | dvar[-1].descriptor.append("'" + str(variables.descriptor) + '_' + str(j + 1) + "'")
|
|---|
| 38 | else:
|
|---|
| 39 | dvar[-1].descriptor = "'" + str(variables.descriptor) + '_' + str(j + 1) + "'"
|
|---|
| 40 |
|
|---|
| 41 | # handle uniform_uncertain
|
|---|
| 42 | if isinstance(variables, uniform_uncertain):
|
|---|
| 43 | if type(variables.lower) in [list, np.ndarray] and len(variables.lower) > 1 and len(variables.upper) != md.qmu.numberofpartitions:
|
|---|
| 44 | if type(variables.lower) == np.ndarray:
|
|---|
| 45 | dvar[-1].lower = np.append(dvar[-1].lower, variables.lower[j])
|
|---|
| 46 | else:
|
|---|
| 47 | dvar[-1].lower.append(variables.lower[j])
|
|---|
| 48 | else:
|
|---|
| 49 | dvar[-1].lower = variables.lower
|
|---|
| 50 |
|
|---|
| 51 | if type(variables.upper) in [list, np.ndarray] and len(variables.upper) > 1 and len(variables.upper) != md.qmu.numberofpartitions:
|
|---|
| 52 | if type(variables.upper) == np.ndarray:
|
|---|
| 53 | dvar[-1].upper = np.append(dvar[-1].upper, variables.upper[j])
|
|---|
| 54 | else:
|
|---|
| 55 | dvar[-1].upper.append(variables.upper[j])
|
|---|
| 56 | else:
|
|---|
| 57 | dvar[-1].upper = variables.upper
|
|---|
| 58 |
|
|---|
| 59 | # handle normal_uncertain
|
|---|
| 60 | elif isinstance(variables, normal_uncertain):
|
|---|
| 61 | if type(variables.stddev) in [list, np.ndarray] and len(variables.stddev) > 1 and len(variables.upper) != md.qmu.numberofpartitions:
|
|---|
| 62 | if type(variables.stddev) == np.ndarray:
|
|---|
| 63 | dvar[-1].stddev = np.append(dvar[-1].stddev, variables.stddev[j])
|
|---|
| 64 | else:
|
|---|
| 65 | dvar[-1].stddev.append(variables.stddev[j])
|
|---|
| 66 | else:
|
|---|
| 67 | dvar[-1].stddev = variables.stddev
|
|---|
| 68 |
|
|---|
| 69 | # running with a single instance, and therefore length 1 arrays of qmu classes
|
|---|
| 70 | else:
|
|---|
| 71 | dvar.append(variables)
|
|---|
| 72 | # text parsing in dakota requires literal "'identifier'" not just "identifier"
|
|---|
| 73 | for v in dvar:
|
|---|
| 74 | v.descriptor = "'" + str(v.descriptor) + "'"
|
|---|
| 75 |
|
|---|
| 76 | return dvar
|
|---|