Pythonic Virtual Machine
Compisite Circuit tutorial

Building a composite circuit

Lets first start by creating the composite circuit so we as per usual start the mandatory imports;

1 from vafmbase import ChannelType
2 from vafmcircuits import Machine
3 import vafmcircuits

So to start building the compisite circuit we first must create a function with the following arguments, the name of the function will be the name of your compisite circuit so you can make that what you desire, but for now lets call it ADC.

1 def ADC(compo,**keys):

A composite circuit has input and output channels similar to regular circuits, so we must define these. You can name them whatever you want but each one must be unique, below we have added two input channels named "signal1" and "signal2" and one output channel named "out";

1 compo.AddInput("signal1")
2 compo.AddInput("signal2")
3 compo.AddOutput("out")

Now we have created the signal channels next is to add the addition circuit that we require.

1 compo.AddCircuit(type='opAdd',name='adder',factors=2, pushed=True)

Just like in a regular input file we still must connect the channels but this time we have channels that are associated with the composite circuit so in order to access these channels we must use the global affix followed by .channel name;

1 compo.Connect("global.signal1","adder.in1")
2 compo.Connect("global.signal2","adder.in2")
3 compo.Connect("adder.out","global.out")

Finally its a nice idea to add a print statement to let you know when the circuit has been added;

1 print "ADC assembled!"

So the final compisite file should look like this (notice the indentation if you aren't familiar with python) ;

1 from vafmbase import ChannelType
2 from vafmcircuits import Machine
3 import vafmcircuits
4 
5 
6 def ADC(compo,**keys):
7 
8  compo.AddInput("signal1")
9  compo.AddInput("signal2")
10  compo.AddOutput("out")
11  compo.AddCircuit(type='opAdd',name='adder')
12  compo.Connect("global.signal1","adder.in1")
13  compo.Connect("global.signal2","adder.in2")
14  compo.Connect("adder.out","global.out")
15 
16  print "ADC assembled!"

Now the main input file will be created, first as always we include the modules but this time we must also include the composite circuit;

1 from vafmbase import ChannelType
2 from vafmcircuits import Machine
3 from tut3comp import *
4 import vafmcircuits

Lets now initialise our machine and add a waver circuit just like we did in tutorial 6.1

1 machine = Machine(name='machine', dt=0.01);
2 osc = machine.AddCircuit(type='waver',name='osc', amp=1, freq=1)

Now we must add our composite circuit, its done in more or less the same way as regular circuit except we must use the assembly parameter, this parameter must be made equal to the function name of that composite circuit so for example in this case its ADC. This parameter is not a string so do not use inverted commas as shown below;

1 machine.AddCircuit(type='Machine', name='compo1', assembly=ADC)

Now lets add our output circuit and register the channels we need;

1 out1 = machine.AddCircuit(type='output',name='output',file='tut3.dat', dump=1)
2 out1.Register('global.time', 'osc.sin', 'osc.cos', 'compo1.out')

Notice that the circuit compo1 is using the output channel name that you set earlier in the other file.

Next we must connect the channels as we have done in previous input files and again we will use the composite circuits input channels that we defined earlier;

1 machine.Connect("osc.sin","compo1.signal1")
2 machine.Connect("osc.sin","compo1.signal2")

Finally lets ask the machine to wait for 5 seconds;

1 machine.Wait(5)

So our final input file should look like this;

1 from vafmbase import ChannelType
2 from vafmcircuits import Machine
3 from tut3comp import *
4 import vafmcircuits
5 
6 
7 
8 machine = Machine(name='machine', dt=0.01);
9 
10 machine.AddCircuit(type='waver',name='osc', amp=1, freq=1)
11 
12 machine.AddCircuit(type='Machine', name='compo1', assembly=ADC)
13 
14 out1 = machine.AddCircuit(type='output',name='output',file='tut3.dat', dump=1)
15 out1.Register('global.time', 'osc.sin', 'osc.cos', 'compo1.out')
16 
17 machine.Connect("osc.sin","compo1.signal1")
18 machine.Connect("osc.sin","compo1.signal2")
19 
20 machine.Wait(5)

When you run and plot the output you should get something looking like figure 4 which resembles what we got in tutorial 1.


Documentation for pyVAFM
Generated on Wed Feb 8 2017 10:13:49 for pyVAFM by doxygen 1.8.9.1