summaryrefslogtreecommitdiff
path: root/src/ngspicetoModelica
diff options
context:
space:
mode:
Diffstat (limited to 'src/ngspicetoModelica')
-rw-r--r--src/ngspicetoModelica/Mapping.json281
-rw-r--r--src/ngspicetoModelica/ModelicaUI.py202
-rw-r--r--src/ngspicetoModelica/NgspicetoModelica.py189
3 files changed, 176 insertions, 496 deletions
diff --git a/src/ngspicetoModelica/Mapping.json b/src/ngspicetoModelica/Mapping.json
deleted file mode 100644
index e254d66a..00000000
--- a/src/ngspicetoModelica/Mapping.json
+++ /dev/null
@@ -1,281 +0,0 @@
-{
- "Components":{
- "R" : "Analog.Basic.Resistor",
- "C" : "Analog.Basic.Capacitor",
- "L" : "Analog.Basic.Inductor",
- "e" : "Analog.Basic.VCV",
- "g" : "Analog.Basic.VCC",
- "f" : "Analog.Basic.CCC",
- "h" : "Analog.Basic.CCV",
- "0" : "Analog.Basic.Ground",
- "gnd" : "Analog.Basic.Ground"
-
- },
- "Sources":{
- "v":{
- "pulse":"Analog.Sources.TrapezoidVoltage",
- "sine":"Analog.Sources.SineVoltage",
- "pwl" : "Analog.Sources.TableVoltage",
- "dc" : "Analog.Sources.ConstantVoltage"
- },
-
- "i":{
- "dc":"Analog.Sources.ConstantCurrent"
- }
-
- },
- "Devices":{
- "d":{
- "import":"Analog.Semiconductors.Diode",
- "mapping":{
-
- "is":"Ids"
- },
- "default":{
- "Ids":"880.5e-18",
- "Vt":"0.025",
- "R":"1e12"
- }
-
- },
-
- "m":{
- "import":"BondLib.Electrical.Analog.Spice",
- "mapping":{
- "tnom":"Tnom",
- "vto":"VT0",
- "gamma":"GAMMA",
- "phi":"PHI",
- "ld":"LD",
- "uo":"U0",
- "lambda":"LAMBDA",
- "tox":"TOX",
- "pb":"PB",
- "cj":"CJ",
- "cjsw":"CJSW",
- "mj":"MJ",
- "mjsw":"MJSW",
- "cgdo":"CGD0",
- "js":"JS",
- "cgbo":"CGB0",
- "cgso":"CGS0"
-
-
- },
- "default":{
- "Tnom":"300",
- "VT0":"0",
- "GAMMA":"0",
- "PHI":"0",
- "LD":"0",
- "U0":"0",
- "LAMBDA":"0",
- "TOX":"3e-9",
- "PB":"0.8",
- "CJ":"0",
- "CJSW":"1e-9",
- "MJ":"0.33",
- "MJSW":"0.33",
- "CGD0":"0",
- "JS":"0",
- "CGB0":"0",
- "CGS0":"0"
-
-
- }
-
- },
- "q":{
- "import":"Analog.Semiconductors",
- "mapping":{
- "bf":"Bf",
- "br":"Br",
- "is":"Is",
- "vak":"Vak",
- "tf":"Tauf",
- "tr":"Taur",
- "cjs":"Ccs",
- "cje":"Cje",
- "cjc":"Cjc",
- "vje":"Phie",
- "mje":"Me",
- "vjc":"Phic",
- "mjc":"Mc"
- },
- "default":{
- "Bf":"50",
- "Br":"0.1",
- "Is":"1e-16",
- "Tauf":"1.2e-10",
- "Taur":"5e-9",
- "Vak":"0.02",
- "Ccs":"1e-12",
- "Cje":"4e-12",
- "Cjc":"5e-13",
- "Phie":"0.8",
- "Me":"0.4",
- "Phic":"0.8",
- "Mc":"0.333"
-
- }
-
- },
-
- "j":{
- "import":"Spice3.Internal.JFET",
- "mapping":{
- "kf":"KF",
- "rs":"RS",
- "is":"IS",
- "cgd":"CGD",
- "vto":"VTO",
- "rd":"RD",
- "pb":"PB",
- "beta":"BETA",
- "fc":"FC",
- "af":"AF",
- "cgs":"CGS",
- "lambda":"LAMBDA",
- "b" : "B"
-
- },
-
- "default":{
- "KF":"0",
- "RS":"0",
- "IS":"1e-14",
- "CGD":"0",
- "VTO":"-2",
- "RD":"0",
- "PB":"1",
- "BETA":"1e-4",
- "FC":"0.5",
- "AF":"1",
- "CGS":"0",
- "LAMBDA":"0",
- "B":"1"
-
-
- }
- }
-
- },
-
-
- "Models":{
- "zener":{
- "import":"Analog.Semiconductors.ZDiode",
- "mapping":{
- "v_breakdown":"Bv",
- "i_breakdown":"Ibv",
- "i_sat":"Ids",
- "n_forward":"Nbv"
-
- },
- "default":{
- "Ids":"880.5e-18",
- "Vt":"0.025",
- "R":"1e12",
- "Bv":"8.1",
- "Ibv":"0.020245",
- "Nbv":"1.6989"
-
- }
-
- }
- },
-
- "Units":{
- "k":"e3",
- "u":"e-6",
- "p":"e-12",
- "t":"e12",
- "f":"e-15",
- "g":"e9",
- "m":"e-3",
- "meg":"e6",
- "n":"e-9",
-
- "v":"",
- "a":"",
- "s":"",
- "hz":"",
- "ohm":"",
- "mho":"",
- "h":"",
-
-
- "kv":"e3",
- "ka":"e3",
- "ks":"e3",
- "khz":"e3",
- "kohm":"e3",
- "kmho":"e3",
- "kh":"e3",
-
-
- "uv":"e-06",
- "ua":"e-06",
- "us":"e-06",
- "uhz":"e-06",
- "uohm":"e-06",
- "umho":"e-06",
- "uh":"e-06",
-
- "pv":"e-12",
- "pa":"e-12",
- "ps":"e-12",
- "phz":"e-12",
- "pohm":"e-12",
- "pmho":"e-12",
- "ph":"e-12",
-
-
- "tv":"e12",
- "ta":"e12",
- "ts":"e12",
- "thz":"e12",
- "tohm":"e12",
- "tmho":"e12",
- "th":"e12",
-
-
- "gv":"e9",
- "ga":"e9",
- "gs":"e9",
- "ghz":"e9",
- "gohm":"e9",
- "gmho":"e9",
- "gh":"e9",
-
-
- "mv":"e-03",
- "ma":"e-03",
- "ms":"e-03",
- "mhz":"e-03",
- "mohm":"e-03",
- "mmho":"e-03",
- "mh":"e-03",
-
-
- "megv":"e06",
- "mega":"e06",
- "megs":"e06",
- "meghz":"e06",
- "megohm":"e06",
- "megmho":"e06",
- "megh":"e06",
-
-
-
- "nv":"e-09",
- "na":"e-09",
- "ns":"e-09",
- "nhz":"e-09",
- "nohm":"e-09",
- "nmho":"e-09",
- "nh":"e-09"
-
- }
-
-}
diff --git a/src/ngspicetoModelica/ModelicaUI.py b/src/ngspicetoModelica/ModelicaUI.py
index eca03534..aa7cb330 100644
--- a/src/ngspicetoModelica/ModelicaUI.py
+++ b/src/ngspicetoModelica/ModelicaUI.py
@@ -1,9 +1,9 @@
import os
-from subprocess import Popen, PIPE, STDOUT
from PyQt4 import QtGui, QtCore
from configuration.Appconfig import Appconfig
from projManagement import Worker
from projManagement.Validation import Validation
+from .NgspicetoModelica import NgMoConverter
BROWSE_LOCATION = '/home'
@@ -44,52 +44,182 @@ class OpenModelicaEditor(QtGui.QWidget):
self.show()
def browseFile(self):
-
self.ngspiceNetlist = QtGui.QFileDialog.getOpenFileName(
self, 'Open Ngspice file', BROWSE_LOCATION)
self.FileEdit.setText(self.ngspiceNetlist)
def callConverter(self):
+ dir_name = os.path.dirname(os.path.realpath(self.ngspiceNetlist))
+ # file_basename = os.path.basename(self.ngspiceNetlist)
+
+ cwd = os.getcwd()
+ os.chdir(dir_name)
+
+ obj_NgMoConverter = NgMoConverter(self.map_json)
+
try:
- self.cmd1 = (
- "python3 ../ngspicetoModelica/NgspicetoModelica.py " +
- self.ngspiceNetlist + ' ' +
- self.map_json
- )
- # self.obj_workThread1 = Worker.WorkerThread(self.cmd1)
- # self.obj_workThread1.start()
- convert_process = Popen(
- self.cmd1,
- shell=True,
- stdin=PIPE,
- stdout=PIPE,
- stderr=STDOUT,
- close_fds=True)
- error_code = convert_process.stdout.read()
- if not error_code:
- self.msg = QtGui.QMessageBox()
- self.msg.setText(
- "Ngspice netlist successfully converted to OpenModelica" +
- "netlist")
- self.obj_appconfig.print_info(
- "Ngspice netlist successfully converted to OpenModelica" +
- "netlist"
+ # Getting all the require information
+ lines = obj_NgMoConverter.readNetlist(self.ngspiceNetlist)
+ # print("Complete Lines of Ngspice netlist : " +
+ # "lines ---------------->", lines)
+ optionInfo, schematicInfo = \
+ obj_NgMoConverter.separateNetlistInfo(lines)
+ # print("All option details like analysis,subckt,.ic,.model :" +
+ # "OptionInfo------------------->", optionInfo)
+ # print("Schematic connection info :schematicInfo", schematicInfo)
+ modelName, modelInfo, subcktName, paramInfo, transInfo,\
+ inbuiltModelDict = (
+ obj_NgMoConverter.addModel(optionInfo)
+ )
+ # print("Name of Model : " +
+ # "modelName-------------------->", modelName)
+ # print("Model Information : " +
+ # "modelInfo--------------------->", modelInfo)
+ # print("Subcircuit Name : " +
+ # "subcktName------------------------>", subcktName)
+ # print("Parameter Information : " +
+ # "paramInfo---------------------->", paramInfo)
+ # print("InBuilt Model ---------------------->", inbuiltModelDict)
+
+ modelicaParamInit = obj_NgMoConverter.processParam(paramInfo)
+ # print("Make modelicaParamInit from paramInfo : " +
+ # "processParamInit------------->", modelicaParamInit)
+ compInfo, plotInfo = obj_NgMoConverter.separatePlot(schematicInfo)
+ # print("Plot info like plot,print etc :plotInfo",plotInfo)
+ IfMOS = '0'
+
+ for eachline in compInfo:
+ # words = eachline.split()
+ if eachline[0] == 'm':
+ IfMOS = '1'
+ break
+
+ subOptionInfo = []
+ subSchemInfo = []
+ if len(subcktName) > 0:
+ # subOptionInfo = []
+ # subSchemInfo = []
+ for eachsub in subcktName:
+ filename_temp = eachsub + '.sub'
+ data = obj_NgMoConverter.readNetlist(filename_temp)
+ # print "Data---------->",data
+ subOptionInfo, subSchemInfo = (
+ obj_NgMoConverter.separateNetlistInfo(data)
+ )
+ for eachline in subSchemInfo:
+ # words = eachline.split()
+ if eachline[0] == 'm':
+ IfMOS = '1'
+ break
+ # print("Subcircuit OptionInfo :" +
+ # "subOptionInfo------------------->", subOptionInfo)
+ # print("Subcircuit Schematic Info :" +
+ # "subSchemInfo-------------------->", subSchemInfo)
+
+ node, nodeDic, pinInit, pinProtectedInit = \
+ obj_NgMoConverter.nodeSeparate(
+ compInfo, '0', [], subcktName, []
)
- self.msg.exec_()
-
- else:
- self.err_msg = QtGui.QErrorMessage()
- self.err_msg.showMessage(
- 'Unable to convert NgSpice netlist to Modelica netlist.' +
- 'Check the netlist :' +
- error_code)
- self.err_msg.setWindowTitle(
- "Ngspice to Modelica conversion error")
- self.obj_appconfig.print_error(error_code)
+ # print("All nodes in the netlist :node---------------->", node)
+ # print("NodeDic which will be used for modelica :" +
+ # "nodeDic------------->", nodeDic)
+ # print("PinInit-------------->", pinInit)
+ # print("pinProtectedInit----------->", pinProtectedInit)
+
+ modelicaCompInit, numNodesSub = obj_NgMoConverter.compInit(
+ compInfo,
+ node,
+ modelInfo,
+ subcktName,
+ dir_name,
+ transInfo,
+ inbuiltModelDict
+ )
+ # print("ModelicaComponents :" +
+ # "modelicaCompInit----------->", modelicaCompInit)
+ # print("SubcktNumNodes :" +
+ # "numNodesSub---------------->", numNodesSub)
+
+ connInfo = obj_NgMoConverter.connectInfo(
+ compInfo, node, nodeDic, numNodesSub, subcktName)
+
+ # print("ConnInfo------------------>", connInfo)
+
+ # After Sub Ckt Func
+ if len(subcktName) > 0:
+ data, subOptionInfo, subSchemInfo, subModel, subModelInfo,\
+ subsubName, subParamInfo, modelicaSubCompInit,\
+ modelicaSubParam, nodeSubInterface, nodeSub, nodeDicSub,\
+ pinInitSub, connSubInfo = (
+ obj_NgMoConverter.procesSubckt(
+ subcktName, numNodesSub, dir_name
+ )
+ ) # Adding 'numNodesSub' by Fahim
+
+ # Creating Final Output file
+ newfile = self.ngspiceNetlist.split('.')
+ newfilename = newfile[0]
+ outfile = newfilename + ".mo"
+ out = open(outfile, "w")
+ out.writelines('model ' + os.path.basename(newfilename))
+ out.writelines('\n')
+ if IfMOS == '0':
+ out.writelines('import Modelica.Electrical.*;')
+ elif IfMOS == '1':
+ out.writelines('import BondLib.Electrical.*;')
+ # out.writelines('import Modelica.Electrical.*;')
+ out.writelines('\n')
+
+ for eachline in modelicaParamInit:
+ if len(paramInfo) == 0:
+ continue
+ else:
+ out.writelines(eachline)
+ out.writelines('\n')
+ for eachline in modelicaCompInit:
+ if len(compInfo) == 0:
+ continue
+ else:
+ out.writelines(eachline)
+ out.writelines('\n')
+
+ out.writelines('protected')
+ out.writelines('\n')
+ out.writelines(pinInit)
+ out.writelines('\n')
+ out.writelines('equation')
+ out.writelines('\n')
+
+ for eachline in connInfo:
+ if len(connInfo) == 0:
+ continue
+ else:
+ out.writelines(eachline)
+ out.writelines('\n')
+
+ out.writelines('end ' + os.path.basename(newfilename) + ';')
+ out.writelines('\n')
+
+ out.close()
+
+ os.chdir(cwd)
+
+ self.msg = QtGui.QMessageBox()
+ self.msg.setText(
+ "Ngspice netlist successfully converted to OpenModelica" +
+ "netlist"
+ )
+ self.obj_appconfig.print_info(
+ "Ngspice netlist successfully converted to OpenModelica" +
+ "netlist"
+ )
+ self.msg.exec_()
except Exception as e:
self.msg = QtGui.QErrorMessage()
+ self.msg.setModal(True)
+ self.msg.setWindowTitle("Ngspice to Modelica conversion error")
self.msg.showMessage(
'Unable to convert NgSpice netlist to Modelica netlist.' +
'Check the netlist :' +
diff --git a/src/ngspicetoModelica/NgspicetoModelica.py b/src/ngspicetoModelica/NgspicetoModelica.py
index bdbd6ac2..a78bad1b 100644
--- a/src/ngspicetoModelica/NgspicetoModelica.py
+++ b/src/ngspicetoModelica/NgspicetoModelica.py
@@ -1,8 +1,6 @@
-import sys
import os
import re
import json
-from string import maketrans
class NgMoConverter:
@@ -31,10 +29,9 @@ class NgMoConverter:
except Exception as e:
print("Error in opening file")
print(str(e))
- sys.exit()
+ raise
else:
- print(filename + " does not exist")
- sys.exit()
+ raise FileNotFoundError(filename + " does not exist")
data = f.read()
data = data.splitlines()
@@ -150,10 +147,10 @@ class NgMoConverter:
f = open(filename)
except BaseException:
print("Error in opening file")
- sys.exit()
+ raise
else:
- print(filename + " does not exist")
- sys.exit()
+ raise FileNotFoundError(filename + " does not exist")
+
data = f.read()
data = data.replace(
'+',
@@ -206,7 +203,7 @@ class NgMoConverter:
line = line.split()
final_line = ','.join(line)
stat = 'parameter Real ' + final_line + ';'
- stat = stat.translate(maketrans('{}', ' '))
+ stat = stat.translate(str.maketrans('{}', ' '))
modelicaParam.append(stat)
return modelicaParam
@@ -481,8 +478,7 @@ class NgMoConverter:
["import"]
) + ".PNP"
else:
- print("Transistor " + str(trans) + " Not found")
- sys.exit(1)
+ raise NameError("Transistor " + str(trans) + " Not found")
stat = start + " " + words[0] + "("
tempstatList = []
@@ -576,8 +572,7 @@ class NgMoConverter:
["import"]
) + ".Mp"
else:
- print("MOSFET " + str(trans) + " not found")
- sys.exit(1)
+ raise NameError("MOSFET " + str(trans) + " not found")
stat = start + " " + words[0] + "("
tempstatList = []
@@ -684,8 +679,7 @@ class NgMoConverter:
["import"]
)+".J_PJFJFET"
else:
- print "JFET "+str(trans)+" Not found"
- sys.exit(1)
+ raise NameError("JFET " + str(trans) + " Not found")
"""
start = self.mappingData["Devices"][deviceName]["import"]
@@ -894,7 +888,7 @@ class NgMoConverter:
appen_line = intLine[newindex:len(intLine)]
appen_param = ','.join(appen_line)
paramLine = 'parameter Real ' + appen_param + ';'
- paramLine = paramLine.translate(maketrans('{}', ' '))
+ paramLine = paramLine.translate(str.maketrans('{}', ' '))
subParamInfo.append(paramLine)
return subParamInfo
@@ -1257,166 +1251,3 @@ class NgMoConverter:
pinInitSub,
connSubInfo
)
-
-
-def main(args):
- """
- It is main function of module Ngspice to Modelica converter
- """
- if len(sys.argv) == 3:
- filename = sys.argv[1]
- map_json = sys.argv[2]
- else:
- print("USAGE:")
- print("python3 NgspicetoModelica.py <filename>")
- sys.exit()
-
- dir_name = os.path.dirname(os.path.realpath(filename))
- # file_basename = os.path.basename(filename)
-
- cwd = os.getcwd()
- os.chdir(dir_name)
-
- obj_NgMoConverter = NgMoConverter(map_json)
-
- # Getting all the require information
- lines = obj_NgMoConverter.readNetlist(filename)
- # print "Complete Lines of Ngspice netlist :lines ---------------->",lines
- optionInfo, schematicInfo = obj_NgMoConverter.separateNetlistInfo(lines)
- # print "All option details like analysis,subckt,.ic,.model :
- # OptionInfo------------------->",optionInfo
- # print "Schematic connection info :schematicInfo",schematicInfo
- modelName, modelInfo, subcktName, paramInfo, transInfo,\
- inbuiltModelDict = (
- obj_NgMoConverter.addModel(optionInfo)
- )
- # print "Name of Model : modelName-------------------->",modelName
- # print "Model Information :modelInfo--------------------->",modelInfo
- # print "Subcircuit Name :subcktName------------------------>",subcktName
- # print "Parameter Information :paramInfo---------------------->",paramInfo
- # print "InBuilt Model ---------------------->",inbuiltModelDict
-
- modelicaParamInit = obj_NgMoConverter.processParam(paramInfo)
- # print "Make modelicaParamInit from paramInfo
- # : processParamInit------------->",modelicaParamInit
- compInfo, plotInfo = obj_NgMoConverter.separatePlot(schematicInfo)
- # print "Plot info like plot,print etc :plotInfo",plotInfo
- IfMOS = '0'
-
- for eachline in compInfo:
- # words = eachline.split()
- if eachline[0] == 'm':
- IfMOS = '1'
- break
-
- subOptionInfo = []
- subSchemInfo = []
- if len(subcktName) > 0:
- # subOptionInfo = []
- # subSchemInfo = []
- for eachsub in subcktName:
- filename_temp = eachsub + '.sub'
- data = obj_NgMoConverter.readNetlist(filename_temp)
- # print "Data---------->",data
- subOptionInfo, subSchemInfo = (
- obj_NgMoConverter.separateNetlistInfo(data)
- )
- for eachline in subSchemInfo:
- # words = eachline.split()
- if eachline[0] == 'm':
- IfMOS = '1'
- break
- # print "Subcircuit OptionInfo
- # : subOptionInfo------------------->",subOptionInfo
- # print "Subcircuit Schematic Info
- # : subSchemInfo-------------------->",subSchemInfo
-
- node, nodeDic, pinInit, pinProtectedInit = obj_NgMoConverter.nodeSeparate(
- compInfo, '0', [], subcktName, [])
- # print "All nodes in the netlist :node---------------->",node
- # print "NodeDic which will be used for modelica
- # : nodeDic------------->",nodeDic
- # print "PinInit-------------->",pinInit
- # print "pinProtectedInit----------->",pinProtectedInit
-
- modelicaCompInit, numNodesSub = obj_NgMoConverter.compInit(
- compInfo,
- node,
- modelInfo,
- subcktName,
- dir_name,
- transInfo,
- inbuiltModelDict
- )
- # print "ModelicaComponents
- # : modelicaCompInit----------->",modelicaCompInit
- # print "SubcktNumNodes
- # : numNodesSub---------------->",numNodesSub
-
- connInfo = obj_NgMoConverter.connectInfo(
- compInfo, node, nodeDic, numNodesSub, subcktName)
-
- # print "ConnInfo------------------>",connInfo
-
- # After Sub Ckt Func
- if len(subcktName) > 0:
- data, subOptionInfo, subSchemInfo, subModel, subModelInfo,\
- subsubName, subParamInfo, modelicaSubCompInit, modelicaSubParam,\
- nodeSubInterface, nodeSub, nodeDicSub, pinInitSub, connSubInfo = (
- obj_NgMoConverter.procesSubckt(
- subcktName, numNodesSub, dir_name
- )
- ) # Adding 'numNodesSub' by Fahim
-
- # Creating Final Output file
- newfile = filename.split('.')
- newfilename = newfile[0]
- outfile = newfilename + ".mo"
- out = open(outfile, "w")
- out.writelines('model ' + os.path.basename(newfilename))
- out.writelines('\n')
- if IfMOS == '0':
- out.writelines('import Modelica.Electrical.*;')
- elif IfMOS == '1':
- out.writelines('import BondLib.Electrical.*;')
- # out.writelines('import Modelica.Electrical.*;')
- out.writelines('\n')
-
- for eachline in modelicaParamInit:
- if len(paramInfo) == 0:
- continue
- else:
- out.writelines(eachline)
- out.writelines('\n')
- for eachline in modelicaCompInit:
- if len(compInfo) == 0:
- continue
- else:
- out.writelines(eachline)
- out.writelines('\n')
-
- out.writelines('protected')
- out.writelines('\n')
- out.writelines(pinInit)
- out.writelines('\n')
- out.writelines('equation')
- out.writelines('\n')
-
- for eachline in connInfo:
- if len(connInfo) == 0:
- continue
- else:
- out.writelines(eachline)
- out.writelines('\n')
-
- out.writelines('end ' + os.path.basename(newfilename) + ';')
- out.writelines('\n')
-
- out.close()
-
- os.chdir(cwd)
-
-
-# Call main function
-if __name__ == '__main__':
- main(sys.argv)