2 Replies - 1611 Views - Last Post: 13 March 2013 - 06:29 AM Rate Topic: -----

#1 ngspice_python  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-March 13

NameError: global name 'stringNode' is not defined

Posted 13 March 2013 - 05:47 AM

Good day everyone, I am working on a script to write input files to a NGSPICE input file. but get some errors. Can I maybe get some help? working with python is total new to me, and I want to learn more about it.

Code

import os
import csv
import sys
import re
import string
import datetime
import glob
import logging


# Standard initialization
pythonstartupFilename=os.environ.get('PYTHonstartUP')
if pythonstartupFilename and os.path.isfile(pythonstartupFilename):
    execfile(pythonstartupFilename)

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(levelname)s %(message)s',
                    filename='/python_scripts/myapp.log',
                    filemode='w')

class ModuleShadeNgspice():    
    spicePath = r'C:\spice_c\bin\ngspice.exe'
    inputFile=r'myInputFile.cir'
                                             
    def makeCell(self,spiceFileWriter,stringNode,startCellNode,endCellNode,photonCurrent): 
        """Define the solar cell""" 
        logging.debug("makeCell")
        startNode="in"+str(stringNode)+str(startCellNode)
        endNode="in"+str(stringNode)+str(endCellNode)
        nodeInBetween="p"+str(stringNode)+str(startCellNode)
        solarcellName="solarcell"+str(stringNode)+str(startCellNode)
        diodeName="D1N4376"
        spiceFileWriter.writerow([".subckt",solarcellName ,startNode ,endNode, "current=-2.8"])
        spiceFileWriter.writerow([".model", diodeName,"d(IS=8.57e-10")"])
        spiceFileWriter.writerow(["Isolarcell" ,startNode, nodeInBetween, str(photonCurrent)])
        spiceFileWriter.writerow(["Djunc" ,startNode, nodeInBetween, diodeName])
        spiceFileWriter.writerow(["Rshunt" ,startNode, nodeInBetween, "1k"])
        spiceFileWriter.writerow(["Rseries" ,nodeInBetween, endNode, "0.1"])
        spiceFileWriter.writerow([".ends",solarcellName])
                      
    def addByPassDiode(self,spiceFileWriter):
        """Add comments here""" 
        logging.debug("addByPassDiode")
                           
    def makeSolarCellString(self,spiceFileWriter,stringId,nrOfCells):   
        """Add comments here""" 
        logging.debug("makeString")
        cells=range(0,nrOfCells)
        startCellNode=0
        photonCurrent=9
        for cell in cells:
            endCellNode=startCellNode+1
            self.makeCell(spiceFileWriter,stringId, startCellNode,endCellNode,photonCurrent);
            startCellNode=startCellNode+1
        self.addByPassDiode(spiceFileWriter)
        
        
    def makeModule(self,spiceFileWriter):
        """Add comments here""" 
        nrOfStrings=1
        stringsIds=range(0,nrOfStrings)
        nrOfCellsPerString=2
        for stringId in stringsIds:
            self.makeSolarCellString(spiceFileWriter,stringId,nrOfCellsPerString) 
    
    
    def defineSimulation(spiceFileWriter,stringNode,startCellNode,endCellNode):
        startNode="in"+str(stringNode)+str(startCellNode)
        endNode="in"+str(stringNode)+str(endCellNode)
        spiceFileWriter.writerow(["Vcc" ,startNode, endNode, "5V"])
        
    def makeSpiceFile(self):
        """Add comments here""" 
        spiceFile=open(self.inputFile, 'w')
        spiceFileWriter=csv.writer(spiceFile, delimiter=' ', lineterminator='\n')          
        self.makeModule(spiceFileWriter);       
        self.defineSimulation(spiceFileWriter, stringNode, startCellNode, endCellNode)  
        spiceFile.close() 
                      
    def __init__(self):
        logging.debug("instantiate...")    
        if not os.path.exists(ModuleShadeNgspice.spicePath):
             raise Exception("ngspice.exe could not be found")
     
    def runMe(self):
        self.makeSpiceFile() 
        runCommand=self.spicePath+" --batch "+self.inputFile
        print(runCommand)   
        os.system(runCommand)
           
myShadowRun=ModuleShadeNgspice()
myShadowRun.runMe()
    



Error
File "C:\python_scripts\Module_Shade_Ngspice.py", line 113, in makeSpiceFile
self.defineSimulation(spiceFileWriter, stringNode, startCellNode, endCellNode)
NameError: global name 'stringNode' is not defined

I am getting confused when program gets bigger.

maybe someone can have a look?

regards

willem

Is This A Good Question/Topic? 0
  • +

Replies To: NameError: global name 'stringNode' is not defined

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: NameError: global name 'stringNode' is not defined

Posted 13 March 2013 - 06:13 AM

None of these variables you are passing in makeSpiceFile exist!

stringNode, startCellNode, endCellNode


Not knowing anything about this NGSPICE, I don't know what to tell you beyond that.
Was This Post Helpful? 0
  • +
  • -

#3 ngspice_python  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-March 13

Re: NameError: global name 'stringNode' is not defined

Posted 13 March 2013 - 06:29 AM

I see what you mean. But if I put "stringNode, startCellNode, endCellNode" at:
def makeSpiceFile(self,stringNode, startCellNode, endCellNode):


i am getting the error TypeError: makeSpiceFile() takes exactly 4 arguments (1 given) in:

  def runMe(self):
        self.makeSpiceFile() 
        runCommand=self.spicePath+" --batch "+self.inputFile
        print(runCommand)
        #os.spawnl(os.P_WAIT, "dir")     
        os.system(runCommand)
           
myShadowRun=ModuleShadeNgspice()
myShadowRun.runMe()


By changing
  def runMe(self):
        self.makeSpiceFile() 
        runCommand=self.spicePath+" --batch "+self.inputFile
        print(runCommand)
        #os.spawnl(os.P_WAIT, "dir")     
        os.system(runCommand) 


to
  def runMe(self,stringNode, startCellNode, endCellNode):
        self.makeSpiceFile(stringNode, startCellNode, endCellNode) 
        runCommand=self.spicePath+" --batch "+self.inputFile
        print(runCommand)
        #os.spawnl(os.P_WAIT, "dir")     
        os.system(runCommand) 

i get the error File "C:\python_scripts\Module_Shade_Ngspice.py", line 130, in <module>
myShadowRun.runMe()
TypeError: runMe() takes exactly 4 arguments (1 given)
in lines

myShadowRun=ModuleShadeNgspice()
myShadowRun.runMe()


when changing it to:
myShadowRun=ModuleShadeNgspice(stringNode, startCellNode, endCellNode)
myShadowRun.runMe(stringNode, startCellNode, endCellNode)


I am again getting the error File "C:\python_scripts\Module_Shade_Ngspice.py", line 129, in <module>
myShadowRun=ModuleShadeNgspice(stringNode, startCellNode, endCellNode)
NameError: name 'stringNode' is not defined


The way I am trying to solve this, is not the right way i guess. But i really don't know how to do it better? Maybe there is something simple i am doing wrong that causes all the problems?

willem
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1