Skip to content
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

Open
wants to merge 28 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2.1
jobs:
build:
docker:
- image: cimg/base:current-22.04
- image: cimg/base:2022.08-22.04
parallelism: 8
steps:
- checkout
Expand Down
2 changes: 1 addition & 1 deletion benchmark/tst.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, tag="tm", C=1000, T0=0):
def eval(self, scope):
# print(scope.C, scope.P, scope.T)

scope.T_dot = scope.P / scope.C
scope.T_dot = scope.P / scope.var3


class Thermal_Conductor(Subsystem):
Expand Down
22 changes: 11 additions & 11 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@
# needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# extensions coming with Sphinx (named 'sphinx.declarative.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.doctest',
'sphinx.ext.intersphinx',
'sphinx.ext.todo',
'sphinx.ext.coverage',
'sphinx.ext.mathjax',
'sphinx.ext.ifconfig',
'sphinx.ext.viewcode',
'sphinx.ext.githubpages',
'sphinx.ext.napoleon',
'sphinx.declarative.autodoc',
'sphinx.declarative.doctest',
'sphinx.declarative.intersphinx',
'sphinx.declarative.todo',
'sphinx.declarative.coverage',
'sphinx.declarative.mathjax',
'sphinx.declarative.ifconfig',
'sphinx.declarative.viewcode',
'sphinx.declarative.githubpages',
'sphinx.declarative.napoleon',
"sphinx_rtd_theme",
]

Expand Down
130 changes: 130 additions & 0 deletions examples/dampened_oscillator/dampened_oscillator_declarative.py
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
Copy link
Collaborator

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?

Copy link
Collaborator Author

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.

Copy link
Collaborator

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?


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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need @EquationSpec here and not @equation ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this >> operator?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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:
items.oscillator1.coupling.connect(items.coupling.side1)
connect(items.oscillator1.coupling, items.coupling.side1)

The intent is that when the user has familiarized with this syntax it is faster to read and produced a cleaner looking code

Copy link
Collaborator

Choose a reason for hiding this comment

The 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__":

Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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)
Empty file.
Loading