Skip to content

Commit

Permalink
Adding inductor
Browse files Browse the repository at this point in the history
  • Loading branch information
masfaraud committed Mar 1, 2018
1 parent ddc4ce9 commit e7adc99
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 17 deletions.
1 change: 0 additions & 1 deletion bms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-

__version__ = "0.1.0"

from .core import *
25 changes: 14 additions & 11 deletions bms/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def _ResolutionOrder(self,variables_to_solve):
if ancetre not in ancestors_vars:
ancestors_vars.append(ancetre)

order_sc=[sc for sc in nx.topological_sort(C,reverse=False) if sc in ancestors_vars]
order_sc=[sc for sc in nx.topological_sort(C) if sc in ancestors_vars]
order_ev=[]
for isc in order_sc:
evs=list(scc[isc])# liste d'équations et de variables triées pour être séparées
Expand Down Expand Up @@ -661,6 +661,19 @@ def GenerateDynamicSystem(self):
G.add_edge(node,block.variables[block.nodes_with_fluxes.index(inb)])
# print(node_block,block.variables[inb].name)

# # Draw graph for debug
# pos=nx.spring_layout(G)
# nx.draw(G,pos)
# names={}
# for node in G.nodes():
# if type(node)==tuple:
# names[node]=(node[0].name,node[1])
# else:
# names[node]=node.name
# nx.draw_networkx_labels(G,pos,names)
# plt.show()


G2=nx.DiGraph()
G2.add_nodes_from(G)
eq_out_var={}
Expand All @@ -681,16 +694,6 @@ def GenerateDynamicSystem(self):
G2.add_edge(e[1],e[0])
# print('@@@@@@@@@@@@@@@@@@@@@@@@')

## Draw graph for debug
# pos=nx.spring_layout(G)
# nx.draw(G,pos)
# names={}
# for node in G.nodes():
# if type(node)==tuple:
# names[node]=(node[0].name,node[1])
# else:
# names[node]=node.name
# nx.draw_networkx_labels(G,pos,names)

sinks=[]
sources=[]
Expand Down
52 changes: 50 additions & 2 deletions bms/physical/electrical.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,59 @@ def PartialDynamicSystem(self,ieq,variable):
# i1=-i2
if variable==self.variables[0]:
#i1 as output
print('Bat1#0')
# print('Bat1#0')
return [Gain(self.variables[1],self.variables[0],-1)]
elif variable==self.variables[1]:
#i2 as output
# print('Bat1#1')
return [Gain(self.variables[0],self.variables[1],-1)]



class Inductor(PhysicalBlock):
def __init__(self,node1,node2,L,name='Inductor'):
occurence_matrix=np.array([[1,1,1,0],[0,1,0,1]])#1st eq: (U1-U2)=Ldi1/dt 2nd: i1=-i2
PhysicalBlock.__init__(self,[node1,node2],[0,1],occurence_matrix,[],name)
self.L=L

def PartialDynamicSystem(self,ieq,variable):
"""
returns dynamical system blocks associated to output variable
"""

if ieq==0:

# if variable==self.physical_nodes[0].variable:
## print('1')
# # U1 is output
# # U1=i1/pC+U2
# Uc=Variable(hidden=True)
# block1=ODE(self.variables[0],Uc,[1],[0,self.C])
# sub1=Sum([self.physical_nodes[1].variable,Uc],variable)
# return [block1,sub1]
# elif variable==self.physical_nodes[1].variable:
# print('2')
# # U2 is output
# # U2=U1-i1/pC
# Uc=Variable(hidden=True)
# block1=ODE(self.variables[0],Uc,[-1],[0,self.C])
# sum1=Sum([self.physical_nodes[0].variable,Uc],variable)
# return [block1,sum1]
if variable==self.variables[0]: # i1=(u1-u2)/Lp
print('3')
# i1 is output
# i1=pC(U1-U2)
Uc=Variable(hidden=True)
subs1=Subtraction(self.physical_nodes[0].variable,self.physical_nodes[1].variable,Uc)
block1=ODE(Uc,variable,[1],[0,self.L])
return [block1,subs1]
elif ieq==1:
# i1=-i2
if variable==self.variables[0]:
#i1 as output
return [Gain(self.variables[1],self.variables[0],-1)]
elif variable==self.variables[1]:
#i2 as output
print('Bat1#1')
return [Gain(self.variables[0],self.variables[1],-1)]


41 changes: 41 additions & 0 deletions scripts/physical/RLC.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
"""
RLC circuit with physical modelling
_______
___1_____| R |______ 2
+_|_ |______| |
| | /
| G | L \
|___| C /
- |___________| |_________|
4 | | 3
"""

import bms
from bms.physical.electrical import Generator,Resistor,ElectricalNode,Capacitor,Ground,Inductor
from bms.signals.functions import Sinus

U=Sinus('Generator',2,5)# Voltage of generator
R=10# Resistance in Ohm
C=0.01# Capacitance in Fahrads
L=0.025# Inductance in Henry

n1=ElectricalNode('1')
n2=ElectricalNode('2')
n3=ElectricalNode('3')
n4=ElectricalNode('4')

gen=Generator(n4,n1,U)
res=Resistor(n1,n2,R)
cap=Capacitor(n2,n3,C)
ind=Inductor(n3,n4,L)
gnd=Ground(n4)

ps=bms.PhysicalSystem(4,300,[gen,res,cap,gnd,ind],[])
ds=ps.dynamic_system

#ds._ResolutionOrder3()
d=ds.Simulate()
ds.PlotVariables([[U,n1.variable,n2.variable,n3.variable],[res.variables[0],cap.variables[0]]])

# Validation: analytical solutions
13 changes: 10 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ def readme():
with open('README.rst') as f:
return f.read()

import bms
def version_scheme(version):
return '.'.join([str(i) for i in version.tag._key[1]])

def local_scheme(version):
return ''


setup(name='bms',
version=bms.__version__,# in bms __init__
use_scm_version={'version_scheme':version_scheme,'local_scheme':local_scheme},
setup_requires=['setuptools_scm'],
description='Block-Model Simulator for python',
long_description=readme(),
keywords='block model simulation simulator time',
Expand All @@ -22,5 +29,5 @@ def readme():
license='Creative Commons Attribution-Share Alike license',
packages=['bms','bms.blocks','bms.signals'],
package_dir={'bms':'bms'},
install_requires=['numpy','matplotlib','networkx','dill'],
install_requires=['numpy','matplotlib>=2.0','networkx','dill'],
classifiers=['Topic :: Scientific/Engineering','Development Status :: 3 - Alpha'])

0 comments on commit e7adc99

Please sign in to comment.