|
|
@@ -488,11 +488,16 @@ class AddOneBlock(CBD):
|
|
|
|
|
|
class DerivatorBlock(CBD):
|
|
|
"""
|
|
|
- The derivator block is a CBD that calculates the derivative.
|
|
|
+ The derivator block is a CBD that calculates the derivative,
|
|
|
+ using the backwards formula.
|
|
|
+
|
|
|
+ .. versionchanged:: 1.4
|
|
|
+ Removed :code:`delta_t` input port.
|
|
|
"""
|
|
|
def __init__(self, block_name):
|
|
|
- CBD.__init__(self, block_name, ["IN1", "delta_t", "IC"], ["OUT1"])
|
|
|
+ CBD.__init__(self, block_name, ["IN1", "IC"], ["OUT1"])
|
|
|
# TO IMPLEMENT
|
|
|
+ self.addBlock(DeltaTBlock(block_name="delta_t"))
|
|
|
self.addBlock(ProductBlock(block_name="multIc"))
|
|
|
self.addBlock(NegatorBlock(block_name="neg1"))
|
|
|
self.addBlock(AdderBlock(block_name="sum1"))
|
|
|
@@ -522,61 +527,14 @@ class IntegratorBlock(CBD):
|
|
|
"""
|
|
|
The integrator block is a CBD that calculates the integration.
|
|
|
The block is implemented according to the backwards Euler rule.
|
|
|
+
|
|
|
+ .. versionchanged:: 1.4
|
|
|
+ Removed :code:`delta_t` input port.
|
|
|
"""
|
|
|
def __init__(self, block_name):
|
|
|
- CBD.__init__(self, block_name, ["IN1", "delta_t", "IC"], ["OUT1"])
|
|
|
- # TO IMPLEMENT
|
|
|
- # TRAPEZOID RULE // TODO: fix the circularity issue => other solver?
|
|
|
- # # self.addBlock(ConstantBlock(block_name="zero", value=0.0))
|
|
|
- # self.addBlock(ConstantBlock(block_name="half", value=0.5))
|
|
|
- # self.addBlock(DelayBlock(block_name="delayIn"))
|
|
|
- # self.addBlock(ProductBlock(block_name="multDelta"))
|
|
|
- # self.addBlock(ProductBlock(block_name="multX"))
|
|
|
- # self.addBlock(DelayBlock(block_name="delayState"))
|
|
|
- # self.addBlock(AdderBlock(block_name="sumX"))
|
|
|
- # self.addBlock(AdderBlock(block_name="sumState"))
|
|
|
- # self.addBlock(NegatorBlock(block_name="neg"))
|
|
|
- # # self.addBlock(TimedGateBlock(block_name="gate", time=0.0))
|
|
|
- #
|
|
|
- # self.addConnection("IN1", "delayIn")
|
|
|
- # self.addConnection("IN1", "neg")
|
|
|
- # # self.addConnection("zero", "delayIn", input_port_name="IC")
|
|
|
- # self.addConnection("neg", "delayIn", input_port_name="IC")
|
|
|
- # self.addConnection("IN1", "sumX")
|
|
|
- # self.addConnection("delayIn", "sumX")
|
|
|
- # self.addConnection("sumX", "multX")
|
|
|
- # self.addConnection("delta_t", "multDelta")
|
|
|
- # self.addConnection("half", "multDelta")
|
|
|
- # self.addConnection("multDelta", "multX")
|
|
|
- # self.addConnection("sumState", "delayState")
|
|
|
- # # self.addConnection("zero", "gate", input_port_name="IN1")
|
|
|
- # # self.addConnection("multX", "gate", input_port_name="IN2")
|
|
|
- # # self.addConnection("gate", "sumState")
|
|
|
- # self.addConnection("multX", "sumState")
|
|
|
- # self.addConnection("IC", "delayState", input_port_name="IC")
|
|
|
- # self.addConnection("delayState", "sumState")
|
|
|
- # self.addConnection("sumState", "OUT1")
|
|
|
-
|
|
|
- # FORWARD EULER:
|
|
|
- # self.addBlock(ProductBlock("mul"))
|
|
|
- # self.addBlock(AdderBlock("sum"))
|
|
|
- # self.addBlock(AdderBlock("sum_ic"))
|
|
|
- # self.addBlock(DelayBlock("delay"))
|
|
|
- # self.addBlock(NegatorBlock("neg"))
|
|
|
- #
|
|
|
- # self.addConnection("IN1", "mul")
|
|
|
- # self.addConnection("delta_t", "mul")
|
|
|
- # self.addConnection("mul", "sum")
|
|
|
- # self.addConnection("delay", "sum")
|
|
|
- # self.addConnection("sum", "delay")
|
|
|
- # self.addConnection("IC", "neg")
|
|
|
- # self.addConnection("neg", "sum_ic")
|
|
|
- # self.addConnection("sum_ic", "delay", input_port_name="IC")
|
|
|
- # self.addConnection("mul", "sum_ic")
|
|
|
- # self.addConnection("sum", "OUT1")
|
|
|
-
|
|
|
- # BACKWARD EULER:
|
|
|
+ CBD.__init__(self, block_name, ["IN1", "IC"], ["OUT1"])
|
|
|
self.addBlock(ConstantBlock(block_name="zero", value=0))
|
|
|
+ self.addBlock(DeltaTBlock(block_name="delta_t"))
|
|
|
self.addBlock(DelayBlock(block_name="delayIn"))
|
|
|
self.addBlock(ProductBlock(block_name="multDelta"))
|
|
|
self.addBlock(DelayBlock(block_name="delayState"))
|
|
|
@@ -593,6 +551,18 @@ class IntegratorBlock(CBD):
|
|
|
self.addConnection("sumState", "OUT1")
|
|
|
|
|
|
|
|
|
+class DeltaTBlock(ConstantBlock):
|
|
|
+ """
|
|
|
+ Helperblock that will always output the current time delta,
|
|
|
+ even when using adaptive stepsize.
|
|
|
+ """
|
|
|
+ def __init__(self, block_name):
|
|
|
+ ConstantBlock.__init__(self, block_name)
|
|
|
+
|
|
|
+ def getValue(self):
|
|
|
+ return self.getClock().getDeltaT()
|
|
|
+
|
|
|
+
|
|
|
class Clock(CBD):
|
|
|
"""
|
|
|
System clock. **Must be present in a simulation model.**
|
|
|
@@ -657,6 +627,12 @@ class Clock(CBD):
|
|
|
self.__start_time = start_time
|
|
|
self.getBlockByName("IC").setValue(start_time)
|
|
|
|
|
|
+ def getDeltaT(self):
|
|
|
+ dSig = self.getSignal("delta")
|
|
|
+ if len(dSig) == 0:
|
|
|
+ return 0.0
|
|
|
+ return dSig[-1]
|
|
|
+
|
|
|
def _rewind(self):
|
|
|
CBD._rewind(self)
|
|
|
time = self.getInputSignal(-1, "h").value
|
|
|
@@ -710,6 +686,9 @@ class DummyClock(BaseBlock):
|
|
|
"""
|
|
|
return self.getTime(curIt) - self.__start_time
|
|
|
|
|
|
+ def getDeltaT(self):
|
|
|
+ return self.__delta_t
|
|
|
+
|
|
|
def _rewind(self):
|
|
|
self.__start_time -= self.__delta_t
|
|
|
|