From e7adc990fc0fef0725e31e1ecac4f0203a904510 Mon Sep 17 00:00:00 2001 From: Steven Masfaraud Date: Thu, 1 Mar 2018 21:15:34 +0100 Subject: [PATCH] Adding inductor --- bms/__init__.py | 1 - bms/core.py | 25 ++++++++++-------- bms/physical/electrical.py | 52 ++++++++++++++++++++++++++++++++++++-- scripts/physical/RLC.py | 41 ++++++++++++++++++++++++++++++ setup.py | 13 +++++++--- 5 files changed, 115 insertions(+), 17 deletions(-) create mode 100644 scripts/physical/RLC.py diff --git a/bms/__init__.py b/bms/__init__.py index a6743df..d5f5ff9 100644 --- a/bms/__init__.py +++ b/bms/__init__.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- -__version__ = "0.1.0" from .core import * diff --git a/bms/core.py b/bms/core.py index 4770e1e..3218e8d 100644 --- a/bms/core.py +++ b/bms/core.py @@ -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 @@ -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={} @@ -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=[] diff --git a/bms/physical/electrical.py b/bms/physical/electrical.py index ef5efe5..263da72 100644 --- a/bms/physical/electrical.py +++ b/bms/physical/electrical.py @@ -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)] \ No newline at end of file diff --git a/scripts/physical/RLC.py b/scripts/physical/RLC.py new file mode 100644 index 0000000..01c6031 --- /dev/null +++ b/scripts/physical/RLC.py @@ -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 diff --git a/setup.py b/setup.py index 45bfde2..e323e3a 100644 --- a/setup.py +++ b/setup.py @@ -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', @@ -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'])