B4J Question Attempt to create a simulation tool for electronic components,,,

JordiCP

Expert
Licensed User
Longtime User
That would be really nice!

The lib seems quite large. Haven't seen if it includes graphic editor or it is just the engine.

Many many years ago, my final thesis was the study of mixed time-frequency domain methods to get the steady behavior of non-linear circuits, as an alternative approach to transient integration and Fourier analysis, which was not suitable due to non-linearities. Used Matlab for it. I don't remember nearly anything now, but will be happy to beta-test or try to assist where I can help, taking into account that my time is very limited.

Perhaps a good approach would be to start prototyping it using JavaObject / inline Java to access the class methods and build a lib when ready?
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
I have an old BASIC code for AC analysis but cant get the heads or tails out of it. I thought it would be easy to convert to B4J.
Here is the code:
 

Attachments

  • NETWORK1.BAS
    7.6 KB · Views: 234
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Download the jar and put in the additional libraries folder: https://repo1.maven.org/maven2/org/knowm/jspice/0.0.1/jspice-0.0.1.jar
License: https://github.com/knowm/jspice/blob/master/LICENSE

2. Link to source: https://github.com/knowm/jspice/tree/master/src/main/java/org/knowm/jspice

3. See the attached example and the two classes. I added very few methods, but I hope that it will allow you to add more methods in the same way.

B4X:
Sub AppStart (Args() As String)
    Dim spice As JSpice
    spice.Initialize
    Dim builder As NetlistBuilder
    builder.Initialize
    builder.AddNetlistDCCurrent("a", 1, Array As String("0", "1")).AddNetlistResistor("R1", 10, Array As String("1", "0"))
    builder.AddNetlistResistor("R2", 1000, Array As String("1", "2")).AddNetlistResistor("R3", 1000, Array As String("2", "0"))
    Dim Netlist As JavaObject = builder.Build
    spice.Simulate(Netlist)
End Sub

----nodes----
V(1) = 9.950248756218906
V(2) = 4.975124378109452
----components----
I(R1) = 0.9950248756218907
I(R2) = 0.004975124378109454
I(R3) = 0.004975124378109453
I(a) = 1.0
-------------
 

Attachments

  • JSpice.zip
    1.7 KB · Views: 243
Upvote 0

derez

Expert
Licensed User
Longtime User
Added this property
B4X:
Public Sub addNetlistDCVoltage (Id As String, voltage As Double, Nodes() As String) As NetlistBuilder
    builder.RunMethod("addNetlistDCVoltage", Array(Id, voltage, Nodes))
    Return Me
End Sub

and created a simple UI to insert the elements, like this:
B4X:
Private Sub AddVlbl_MouseClicked (EventData As MouseEvent)
    Dim sf As Object = xui.Msgbox2Async("Add V src " & VidTA.text & " of value " & Vta.Text & " Volt from " &  FromnodeTA.text & " to " & ToNodeTa.text, "Add V source", "Yes","Cancel",Null,Null)
    Wait For (sf) Msgbox_Result (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        builder.addNetlistDCVoltage(VidTA.text,Vta.text , Array As String (FromnodeTA.text, ToNodeTa.text))
    End If
End Sub

Private Sub AddR_MouseClicked (EventData As MouseEvent)
    Dim sf As Object = xui.Msgbox2Async("Add Resistor " & RidTA.text & " of value " & Rta.Text & " Ohm from " &  Rfromnodeta.text & " to " & Rtonodeta.text, "Add Resistor","Yes","Cancel",Null,Null)
    Wait For (sf) Msgbox_Result (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        builder.addNetlistResistor(RidTA.text, Rta.text,Array As String( Rfromnodeta.text, Rtonodeta.text))
    End If    
End Sub

How do I get the results log to be displayed in the form , not just in the log area ?
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
I have no checked in detail but I guess the log is probably from a "System.out.println(... .)" somewhere in the jar.....
I have not found any file name in the class and also no file that holds the logs.
This is annoying since I have completed the application but it can't work without the IDE, to see the results.


1672840748745.png
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
I have not found any file name in the class and also no file that holds the logs.
This is annoying since I have completed the application but it can't work without the IDE, to see the results.


View attachment 137695
The call to simulate should return a SimulationResult:

JSpice.simulate(netlist)

Check the Jar for what class SR looks like and the use JavaObject to parse the returned object....I guess..

 
Last edited:
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
I'm not in that class yet...
Try with the attached - have done with inline Java code by adding the original java code of the JSpice class to the B4J class (had to make a new UI app else the event will not raise in the B4J Main class.

Just beware - you might have a chart popping up depending on what you do prior to calling simulate - it is hard coded in the jar and therefore the reason why it is actually supposed to be a non-UI app / command line app.
 

Attachments

  • JSpice.zip
    4.6 KB · Views: 152
Last edited:
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Another "simulation" added.....change it to your liking

1673018666736.png

The second simulation is for this circuit:
1673019797207.png

Class for the above circuit simulation code (also see B4J Main)

B4J Class:
Sub Class_Globals
    Private netlist, netlistDCCurrent, netlistDCVoltage, NetlistResistor As JavaObject
End Sub

Public Sub Initialize
    netlist.InitializeNewInstance("org.knowm.jspice.netlist.Netlist", Null)
End Sub

Public Sub RunI1V1R6() As JavaObject
    
    
    Dim val As Double = 0.02
    netlistDCCurrent.InitializeNewInstance("org.knowm.jspice.netlist.NetlistDCCurrent", Array("a", val, Array As String("0", "4")))
    netlist.RunMethod("addNetListComponent", Array(netlistDCCurrent))
    val = 10.0
    netlistDCVoltage.InitializeNewInstance("org.knowm.jspice.netlist.NetlistDCVoltage", Array("x", val, Array As String("2", "5")))
    netlist.RunMethod("addNetListComponent", Array(netlistDCVoltage))
    val = 100
    NetlistResistor.InitializeNewInstance("org.knowm.jspice.netlist.NetlistResistor", Array("R1", val, Array As String("5", "0")))
    netlist.RunMethod("addNetListComponent", Array(NetlistResistor))
    val = 1000
    NetlistResistor.InitializeNewInstance("org.knowm.jspice.netlist.NetlistResistor", Array("R2", val, Array As String("0", "3")))
    netlist.RunMethod("addNetListComponent", Array(NetlistResistor))
    NetlistResistor.InitializeNewInstance("org.knowm.jspice.netlist.NetlistResistor", Array("R3", val, Array As String("2", "3")))
    netlist.RunMethod("addNetListComponent", Array(NetlistResistor))
    val = 100
    NetlistResistor.InitializeNewInstance("org.knowm.jspice.netlist.NetlistResistor", Array("R4", val, Array As String("1", "2")))
    netlist.RunMethod("addNetListComponent", Array(NetlistResistor))
    val = 1000
    NetlistResistor.InitializeNewInstance("org.knowm.jspice.netlist.NetlistResistor", Array("R5", val, Array As String("3", "0")))
    netlist.RunMethod("addNetListComponent", Array(NetlistResistor))
    val = 10000
    NetlistResistor.InitializeNewInstance("org.knowm.jspice.netlist.NetlistResistor", Array("R6", val, Array As String("1", "4")))
    netlist.RunMethod("addNetListComponent", Array(NetlistResistor))

    Return netlist
End Sub
 

Attachments

  • JSpice1.zip
    5.2 KB · Views: 189
Last edited:
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Have modified the class slightly and have added an example of DC Sweep Analysis - the chart comes from within the Jar...

DC SWEEP.png


Code for the DC Sweep Analysis:

B4J:
    'THIS IS AN EXAMPLE OF DC SWEEP ANALYSIS - IT WILL SHOW A GRAPH
    spice.Initialize
    Dim netlist3 As IVR
    netlist3.Initialize
    Dim dcVoltage As Double = 10
    netlist3.addNetlistDCVoltage("x", dcVoltage, Array As String("1", "0"))        'set the voltage source
    Dim R4 As Double = 100
    netlist3.addNetlistResistor("R4", R4, Array As String("1", "2"))               'set the resistors
    Dim R3 As Double = 1000
    netlist3.addNetlistResistor("R3", R3, Array As String("2", "3"))               'set the resistors
    Dim R2 As Double = 1000
    netlist3.addNetlistResistor("R2", R2, Array As String("3", "0"))               'set the resistors
    Dim R1 As Double = 100
    netlist3.addNetlistResistor("R1", R1, Array As String("2", "0"))               'set the resistors
   
    Dim startval As Double = 100
    Dim endval As Double = 10000
    Dim stepval As Double = 100
   
    Dim dcSweepConfig As JavaObject
    dcSweepConfig.InitializeNewInstance("org.knowm.jspice.simulate.dcsweep.DCSweepConfig", Array("R1", "V(3)", startval, endval, stepval))
   
    Dim nl As JavaObject = netlist3.build
    nl.RunMethod("setSimulationConfig", Array(dcSweepConfig))
   
    spice.simulate(nl)

This is the circuit used of the DC Sweep Analysis

1673080218523.png
 

Attachments

  • JSpice1.zip
    6.1 KB · Views: 185
Last edited:
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Go here - embededd in B4J

 
Upvote 0

derez

Expert
Licensed User
Longtime User
Have modified the class slightly and have added an example of DC Sweep Analysis - the chart comes from within the Jar...

View attachment 137820

Code for the DC Sweep Analysis:

B4J:
    'THIS IS AN EXAMPLE OF DC SWEEP ANALYSIS - IT WILL SHOW A GRAPH
    spice.Initialize
    Dim netlist3 As IVR
    netlist3.Initialize
    Dim dcVoltage As Double = 10
    netlist3.addNetlistDCVoltage("x", dcVoltage, Array As String("1", "0"))        'set the voltage source
    Dim R4 As Double = 100
    netlist3.addNetlistResistor("R4", R4, Array As String("1", "2"))               'set the resistors
    Dim R3 As Double = 1000
    netlist3.addNetlistResistor("R3", R3, Array As String("2", "3"))               'set the resistors
    Dim R2 As Double = 1000
    netlist3.addNetlistResistor("R2", R2, Array As String("3", "0"))               'set the resistors
    Dim R1 As Double = 100
    netlist3.addNetlistResistor("R1", R1, Array As String("2", "0"))               'set the resistors
 
    Dim startval As Double = 100
    Dim endval As Double = 10000
    Dim stepval As Double = 100
 
    Dim dcSweepConfig As JavaObject
    dcSweepConfig.InitializeNewInstance("org.knowm.jspice.simulate.dcsweep.DCSweepConfig", Array("R1", "V(3)", startval, endval, stepval))
 
    Dim nl As JavaObject = netlist3.build
    nl.RunMethod("setSimulationConfig", Array(dcSweepConfig))
 
    spice.simulate(nl)

This is the circuit used of the DC Sweep Analysis

View attachment 137821

Thank you, It runs also in B4A ! (except the sweep, but I don't think it will be of use to me))
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
Any idea why the required order of nodes for DC voltage source is from + to - , which defines the direction of current in the opposite to sense ?
I had to manipulte the inputs and outputs to put it in order to suit my understanding.
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Any idea why the required order of nodes for DC voltage source is from + to - , which defines the direction of current in the opposite to sense ?
I had to manipulte the inputs and outputs to put it in order to suit my understanding.
It is the conventional current flow direction.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
The convention is for the current outside of the battery. Inside it flows from the - to +. It should be like in the current source, or resistor.
The calculations use voltage differnces on the resistors due to their current and sum of the currents that go into a node and out of it. If the definition of current of a battery is like it is now, the summation in the node is wrong.
In the app that I wrote, I change the order before and after the simulation. Just wondered...
 
Last edited:
Upvote 0

derez

Expert
Licensed User
Longtime User
I have managed to write the code that does the first part (not the sweep) by solving the set of linear equasions derived from the network:
For each resistor I * R = V2-V1 (where the current goes from 1 to 2)
For each node - the sum of in currents is equal the sum of out currents.
The unknown of the matrix are the resistors currents, the nodes voltages and the V sources and I sources which get their values directly.
To solve the Ax=B set you can use my MatrixOP library or any other solution.

1673687363105.png

For this example the code solves Ax=B where A is the matrix, X is the unknowns vector and B the results vector.
I get the following matrix and the results of the computation, like with jSpice.

1 0 0 0 0 0 0 0 0 0 0 0 0 0.02
0 100 0 0 0 0 0 0 0 0 0 0 1 0
0 0 1000 0 0 0 0 0 0 0 1 0 0 0
0 0 0 1000 0 0 0 0 0 -1 1 0 0 0
0 0 0 0 100 0 0 0 -1 1 0 0 0 0
0 0 0 0 0 1000 0 0 0 0 -1 0 0 0
0 0 0 0 0 0 10000 0 1 0 0 -1 0 0
0 0 0 0 0 0 0 0 0 1 0 0 -1 10
0 0 0 0 -1 0 1 0 0 0 0 0 0 0
0 0 0 -1 1 0 0 1 0 0 0 0 0 0
0 0 1 1 0 -1 0 0 0 0 0 0 0 0
1 0 0 0 0 0 -1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 -1 0 0 0 0 0 0

I(Ix1) 20 mA
I(R1) -12.5 mA
I(R2) -3.75 mA
I(R3) 7.5 mA
I(R4) 20 mA
I(R5) 3.75 mA
I(R6) 20 mA
I(Vx1) -12.5 mA
V(1) 13.25 Volt
V(2) 11.25 Volt
V(3) 3.75 Volt
V(4) 213.25 Volt
V(5) 1.25 Volt

Note: The right approach when writing SW is to re-use what has already been developed and proved working, but since I do it as a hobby I like to understand the algorithms and when I succeed I have a much bigger satisfaction ! (and I still use the Spice to check my work...)
 

Attachments

  • 1673687324866.png
    1673687324866.png
    29.5 KB · Views: 135
Last edited:
Upvote 0
Top