With this lib you can easily create NEC2 file and automatically run NEC2 simulation with python.
Geometry is defined with help of powerful 3D vectors. You can cut, rotate, copy, add vectors.
Parsing NEC2 output result file is yet possible but in beta stage.
The final goal is to call and score nec2 simulation with optimisation algorithms..
If you are ham radio and find this useful please send QSL to HB9UQL.
nec2py dependencies are :
import math import os import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D
to use nec2pylib :
from nec2pylib import *
What can you do with vectors :
A=Vector(x,y,z) #create a new vector B=A+A #add vectors. C=A⁻B #sub vectors. C=-C #reverse vector. D=A.rotateX(45) #rotation around X axis. E=A.cutA(2.00) #cut absolute, so vector overall length is 2 meters. F=A.cutR(3.00) #cut relative, so vector is 3 meters longer. G=A.syXZ() #symmetry : mirror trough XZ plan. H=A.shiftX() #shift vector along X axisow a.show() #print vector to stdout
An example is worth a thousand words.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | #!/usr/bin/env python from nec2pylib import * import os import sys import datetime # ======================================================================================================= # Antenna nec2 analysis function # ======================================================================================================= m = nec2(plot=0) #create new nec model (plot=1 for watch 3d geometry with matplotlib) m.wireRadius = 1.5e-3 # overide end set the default wire raduis # Comments m.cm(' -------------------------------------------------------------------') m.cm(' NEC2 model for simulatingi side antenna') m.cm(' ') m.cm(' ') m.cm(' Date : '+datetime.datetime.now().strftime("%B %d, %Y")) m.cm(' ') m.cm(' -------------------------------------------------------------------') m.ce() #parmeters labda=300e6/145e6 #labda for 435 mhz angle=15 #angle for radiator wire height=6.5 armlengh=1.00 #one meter #feed point feedA = Vector(0,0,height+0.1) feedB = Vector(0,0,height-0.1) m.addWire(3,feedB,feedA).feedAtMiddle() #add a small wire wire for feed point #top wire A=Vector(0.00,0.00,5.00).cutA(labda/4).rotateX(angle) #creat a vector of 1/4 lambda with defined angle from vertical A.show("A :") m.addWire(5,feedA,feedA+A) #add this wire from feed A #bottom wire B=A.symXY() #upside down A.show("B :") m.addWire(5,feedB,feedB+B) #add this second wire from feed B #arm arm=Vector(0.00,armlengh,height) #horizontal negation for opposite side m.addWire(4,Vector(0,0.10,height), arm) #add arm, shiftX because isolated #mast m.addWire(5,arm, arm.shiftZ(1.50)) #add 1,5 meter from top m.addWire(5,arm, arm.shiftZ(-6.50)) #add mast 6,5 meter from ground # Geometry End m.ge(1) #m.graph.showStruct() # plot antenna with matplotlib #ground m.gn() # Frequencies : HF ham band stepCount=3 m.gn() m.fr(144,146 ,stepCount) #start/stop frequency + nbr of iteration m.xq() m.fr(430,440 ,stepCount) #start/stop frequency + nbr of iteration m.xq() # end of input m.en() # Nec simulation fileName="/run/shm/test" #file name (/run/shm = creat in ram file system to be more fast) m.fileWrite(fileName+".nec") #create and write nec file to disk os.system("cat "+fileName+".nec") #show nec2 input file to stdout os.system("nec2++ -i "+fileName+".nec") #run nec2++ or nec2 of your choice os.system("cat "+fileName+".out") #show nec2 result file to stdout r=NecResult(fileName+".out") # load result from output file in r #scoring for fr, data in r.result.iteritems(): #calc mean value #swr=r.swr(r.result[fr]["Fr"],50.0) sys.stdout.write("fr : "+str(r.result[fr]["Fr"])) sys.stdout.write("\tz : "+str(r.result[fr]["Zi"])) #sys.stdout.write("\tswr : "+str(swr)) sys.stdout.write(chr(10)) #new line sys.stdout.flush() |
Next : we have to play with line 26 to 29 to lower the SWR and improve radiation pattern
73