Package empro :: Package toolkit :: Package results :: Module excitation_based
[frames] | no frames]

Source Code for Module empro.toolkit.results.excitation_based

1 -class _ConstantWeighting(object):
2 - def __init__(self, weighting):
3 self._weighting = weighting
4
5 - def weight(self, excitation, frequency):
6 return self._weighting[excitation]
7
8 - def weights(self, frequency):
9 return self._weighting
10
11 - def __add__(self, other):
12 assert(self._weighting==other._weighting or self._weighting.keys()==other._weighting.keys()) 13 for name, value in self._weighting.iteritems(): 14 self._weighting[name] = value + other._weighting[name] 15 return self
16
17 - def __sub__(self, other):
18 assert(self._weighting==other._weighting or self._weighting.keys()==other._weighting.keys()) 19 for name, value in self._weighting.iteritems(): 20 self._weighting[name] = value - other._weighting[name] 21 return self
22
23 - def __mul__(self, scalar):
24 for name, value in self._weighting.iteritems(): 25 self._weighting[name] = scalar * other._weighting[name] 26 return self
27
28 -class _ExcitationWeighting(object):
29 - def __init__(self, weighting, results):
30 self._weighting = weighting 31 self._results = results 32 import empro, empro.enparams 33 import os 34 import math, cmath 35 36 baseDir = os.path.split(empro.activeProject.simulations()[-1].simulationPath())[0] 37 cbInput = os.path.join(baseDir,"%06d" % int(self._results._sim),"Run0001", "project.input") 38 if not os.path.exists(cbInput): 39 raise RuntimeError("Need %(cbInput)s to extract as-simulated ports" % vars()) 40 assert(os.path.exists(cbInput)) 41 import empro._calcbridge as calcbridge 42 cb = calcbridge.v6ProjectFile() 43 cb.load(cbInput) 44 45 sioFile = os.path.join(baseDir,"%06d" % int(self._results._sim), "data.000.sio") 46 ctiFile = os.path.join(baseDir,"%06d" % int(self._results._sim), "data.000.cti") 47 sioFile2 = os.path.join(baseDir,"%06d" % int(self._results._sim), "emds_dsn","design","design.sio") 48 ctiFile2 = os.path.join(baseDir,"%06d" % int(self._results._sim), "emds_dsn","design","design.cti") 49 50 simExcitations = empro.enparams.MultiComponentEvaluator() 51 for i in range(cb.numberOfComponents()): 52 # set the input as simulated 53 cc = cb.getComponent(i) 54 a,p = cc.getAmplitude(), cc.getPhase() 55 portName = cc.name() 56 simExcitations.add(portName, complex(a*math.cos(p),a*math.sin(p)), "Voltage", cc.getResistance(),cc.getInductance(),cc.getCapacitance(),"Series") 57 58 if os.path.exists(sioFile): 59 # fixes the input transformation 60 self._trafo = empro.enparams.SimulatedCircuitTransformer(sioFile,simExcitations) 61 elif os.path.exists(sioFile2): 62 self._trafo = empro.enparams.SimulatedCircuitTransformer(sioFile2,simExcitations) 63 elif os.path.exists(ctiFile): 64 self._trafo = empro.enparams.SimulatedCircuitTransformer(ctiFile,simExcitations) 65 elif os.path.exists(ctiFile2): 66 self._trafo = empro.enparams.SimulatedCircuitTransformer(ctiFile2,simExcitations) 67 else: 68 raise RuntimeError("Need sio or cti file to compute weights based on port excitations") 69 70 # fills in the output transformation 71 self._requestedTransform = empro.enparams.MultiComponentEvaluator() 72 for portName, spec in sorted(self._weighting.iteritems()): 73 self._requestedTransform.add(portName, spec.source, spec.feedType, spec.R, spec.L, spec.C, spec.arrangement)
74
75 - def weight(self, excitation, frequency):
76 weights = self._trafo.calculateWeights(frequency, self._requestedTransform) 77 return weights[excitation]
78
79 - def weights(self, frequency):
80 weights = self._trafo.calculateWeights(frequency, self._requestedTransform) 81 return weights
82 83
84 -class ExcitationBased(object):
85 - def __init__(self, results, excitation, frequencies=None, coherent=True):
86 self._excitation = excitation 87 self._frequencies = frequencies 88 self._coherent = coherent 89 self._results = results 90 self._availableExcitationsToRun = None 91 self._excitationWeights = self._transformExcitation(self._excitation)
92
93 - def _transformExcitation(self, excitationSpec):
94 # If all specs are based on complex numbers use plain weighting 95 # If an excitation based weighting is expected, transform first 96 import numbers 97 allNumbers = True 98 for value in excitationSpec.values(): 99 if not isinstance(value, numbers.Number): 100 allNumbers = False 101 break 102 if allNumbers: 103 return _ConstantWeighting(excitationSpec) 104 105 allExcitation = True 106 import excitation 107 for value in excitationSpec.values(): 108 if not isinstance(value, excitation.Excitation): 109 allExcitation = False 110 break 111 if allExcitation: 112 return _ExcitationWeighting(weighting=excitationSpec, results=self._results) 113 raise RuntimeError("Excitation specification is not supported")
114
115 - def __add__(self, other):
116 assert(type(other) == type(self)) 117 assert(self._frequencies==other._frequencies) 118 assert(self._coherent==other._coherent) 119 self._excitationWeights += other._excitationWeights 120 return self
121
122 - def __radd__(self, other):
123 if other is 0: 124 return self 125 return other.__add__(self)
126
127 - def __sub__(self, other):
128 assert(type(other) == type(self)) 129 assert(self._frequencies==other._frequencies) 130 assert(self._excitationWeights==other._excitationWeights or self._excitationWeights.keys()==other._excitationWeights.keys()) 131 self._excitationWeights -= other._excitationWeights 132 return self
133
134 - def __mul__(self, scalar):
135 self._excitationWeights *= scalar 136 return self
137
138 - def __rmul__(self, scalar):
139 self._excitationWeights *= scalar 140 return self
141
142 - def weights(self, frequency):
143 return self._excitationWeights.weights(frequency)
144