This page was generated from unit-10.1-ngspice/NGSpiceNGSolve.ipynb.

10.1 Coupling NGSolve and NGSpiceΒΆ

[1]:
from netgen.occ import *
outer = MoveTo(0,0).RectangleC(0.1,0.1).Face()
el1 = MoveTo(0,0.002).RectangleC(0.03,0.001).Face()
el2 = MoveTo(0,-0.002).RectangleC(0.03,0.001).Face()
el1.edges.name="el1"
el2.edges.name="el2"

dom = outer-el1-el2
geom = OCCGeometry(dom, dim=2)
[2]:
from ngsolve import *
from ngsolve.webgui import Draw
mesh = Mesh(geom.GenerateMesh(maxh=0.005))
# Draw (mesh);
[3]:
fes = H1(mesh, order=3, dirichlet="el1|el2")
u,v = fes.TnT()
gfu = GridFunction(fes)
gfu.Set (mesh.BoundaryCF({"el1":1, "el2":0}), BND)

mata = BilinearForm(10*8.854e-12*grad(u)*grad(v)*dx).Assemble().mat
inv = mata.Inverse(inverse="sparsecholesky", freedofs=fes.FreeDofs())
gfu.vec.data -= inv@mata * gfu.vec

capacity = InnerProduct(mata*gfu.vec, gfu.vec)
print ("capacity=", capacity)
Draw (gfu);
capacity= 1.0519578703831773e-09
[4]:
try:
    from PySpice.Spice.Netlist import Circuit
    from PySpice.Unit import *
except:
    pass
[5]:
try:
    circuit = Circuit('test1')

    source = circuit.SinusoidalVoltageSource(1, 'input', circuit.gnd, amplitude=1@u_V, frequency=1@u_kHz)
    # source = circuit.PulseVoltageSource(1, 'input', circuit.gnd, 1, 2, 0, 0, 0, 0.001, 0.01)

    circuit.R(1, 'input', 'output', 100@u_kOhm)
    # circuit.C(1, 'output', 0, 1@u_nF)
    circuit.C(1, 'output', circuit.gnd, capacity)

    print (circuit)
except:
    pass
.title test1
V1 input 0 DC 0V AC 1V SIN(0V 1V 1kHz 0s 0Hz)
R1 input output 100kOhm
C1 output 0 1.0519578703831773e-09

[6]:
try:
    simulator = circuit.simulator()
    analysis = simulator.transient(step_time=source.period/50, end_time=source.period*4)
    # analysis = simulator.transient(step_time=1e-4, end_time=1e-2)

    import matplotlib.pyplot as plt
    plt.plot (analysis.input)
    plt.plot (analysis.output)
except:
    pass
spinit was not found
Note: can't find init file.
Unsupported Ngspice version 36
../../_images/i-tutorials_unit-10.1-ngspice_NGSpiceNGSolve_6_1.png
[ ]: