-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Declarative interface #195
base: develop
Are you sure you want to change the base?
Changes from all commits
c26d2fc
c0b0a9b
ab5c9c7
f898a5a
f70c9cb
61965f1
5d4610c
13fe249
b972138
6fcd5c7
935922a
fb4a509
4e000f8
3c26e73
1e7653e
ea780d9
f34d266
4bf0d4a
7fe4ace
f50d9c5
b587cf8
b6e41a5
8c9d475
ce29ce8
0d0bb53
8cdc547
be6ae2c
a116e0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
from numerous.declarative.specification import ScopeSpec, ItemsSpec, Module, EquationSpec | ||
from numerous.declarative.variables import Parameter, Constant, State | ||
from numerous.declarative.bus import Connector, create_connections, get_value_for, set_value_from | ||
|
||
import numpy as np | ||
|
||
|
||
class DampenedOscillator(Module): | ||
""" | ||
Equation and item modelling a spring and dampener | ||
""" | ||
tag = "dampened_oscillator" | ||
|
||
class Mechanics(ScopeSpec): | ||
# Define variables | ||
k = Constant(1) # spring constant | ||
c = Constant(1) # coefficient of friction | ||
a = Parameter(0) # acceleration | ||
x, x_dot = State(0) # distance | ||
v, v_dot = State(0) # velocity | ||
|
||
mechanics = Mechanics() | ||
|
||
coupling = Connector( | ||
x = set_value_from(mechanics.x), | ||
F = get_value_for(mechanics.v_dot) | ||
) | ||
|
||
@EquationSpec(mechanics) | ||
def eval(self, scope: Mechanics): | ||
scope.a = -scope.k * scope.x - scope.c * scope.v | ||
scope.v_dot = scope.a | ||
scope.x_dot = scope.v | ||
|
||
|
||
class SpringCoupling(Module): | ||
|
||
tag = "springcoup" | ||
|
||
class Mechanics(ScopeSpec): | ||
k = Parameter(1) | ||
c = Parameter(1) | ||
F1 = Parameter(0) | ||
F2 = Parameter(0) | ||
x1 = Parameter(0) | ||
x2 = Parameter(0) | ||
|
||
mechanics = Mechanics() | ||
|
||
class Items(ItemsSpec): | ||
side1: DampenedOscillator | ||
side2: DampenedOscillator | ||
|
||
side1 = Connector( | ||
F = set_value_from(mechanics.F1), | ||
x = get_value_for(mechanics.x1) | ||
) | ||
|
||
side2 = Connector( | ||
F=set_value_from(mechanics.F2), | ||
x=get_value_for(mechanics.x2) | ||
) | ||
|
||
items = Items() | ||
|
||
def __init__(self, tag="springcoup", k=1.0): | ||
super().__init__(tag) | ||
|
||
self.mechanics.set_values(k=k) | ||
|
||
@EquationSpec(mechanics) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need @EquationSpec here and not @equation ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The equation spec allows passing of the instance of the ScopeSpec which internally adds the equation to the namespace mechanics. In this way the previous code of add_to_namespace can be omitted, while it is still explicit that the equation is connected to the namespace. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you add a parameter, 'equation_spec', to the @equation decorator instead of creating a new equation decorator? |
||
def eval(self, scope: Mechanics): | ||
scope.c = scope.k | ||
|
||
dx = scope.x1 - scope.x2 | ||
F = np.abs(dx) * scope.c | ||
|
||
scope.F1 = -F if scope.x1 > scope.x2 else F # [kg/s] | ||
|
||
scope.F2 = -scope.F1 | ||
|
||
|
||
class OscillatorSystem(Module): | ||
|
||
class Items(ItemsSpec): | ||
# The oscillator system will have two oscillators interacting via the coupling | ||
oscillator1: DampenedOscillator | ||
oscillator2: DampenedOscillator | ||
coupling: SpringCoupling | ||
|
||
items = Items() | ||
mrdobalina2k marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
with create_connections() as connections: | ||
# connect oscillators via the coupling | ||
items.oscillator1.coupling >> items.coupling.side1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this >> operator? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The >> or << operators are used as a short hand for connecting items.oscillator1.couplin with items.coupling.side1. Alternatives could be: The intent is that when the user has familiarized with this syntax it is faster to read and produced a cleaner looking code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think normally in programming languages >> or << refers to bit-shifts. |
||
items.oscillator2.coupling >> items.coupling.side2 | ||
|
||
|
||
def __init__(self, k=0.01, c=0.001, x0=(1, 2), tag=None): | ||
super(OscillatorSystem, self).__init__(tag=tag) | ||
|
||
# Initialized the modules in the oscillator system | ||
|
||
self.items.oscillator1 = DampenedOscillator(tag='osc1') | ||
self.items.oscillator1.mechanics.set_values(k=k, c=c, x=x0[0]) | ||
|
||
self.items.oscillator2 = DampenedOscillator(tag='osc2') | ||
self.items.oscillator2.mechanics.set_values(k=k, c=c, x=x0[1]) | ||
|
||
self.items.coupling = SpringCoupling(k=k) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think some description is needed, rather than only the code. Like what are we trying to solve here? And why do you chose this method over the "standard" numerous syntax with Subsystem etc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think a dedicated documentation file introducing the concept should be added before the next release. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also there is a short bullet list in the description of this pull request. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest adding r""" strings at the top of the example, which can later be parsed by sphinx to create an example gallery. I think there's no time as the present, and best that we add this before the final approval. This is a major release after all. |
||
from numerous.engine import simulation | ||
from numerous.engine import model | ||
from matplotlib import pyplot as plt | ||
|
||
subsystem = OscillatorSystem(tag='system', k=0.01, c=0.001, x0=[1.0, 2.0]) | ||
# Define simulation | ||
s = simulation.Simulation(model.Model(subsystem, use_llvm=False), t_start=0, t_stop=500.0, num=1000, num_inner=100, | ||
max_step=1) | ||
# Solve and plot | ||
|
||
s.solve() | ||
|
||
s.model.historian_df[['system.osc1.mechanics.x', 'system.osc2.mechanics.x']].plot() | ||
# print() | ||
plt.show() | ||
plt.interactive(False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do these new methods/classes have documentation already?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They have a very basic docstring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this docstring be expanded beyond the basic one?