NEC2Logo
Using NEC-4 with cocoaNEC

Kok Chen, W7AY [w7ay (at) arrl.net]
Last updated: August 7, 2009




cocoaNEC 2.0 comes with an embedded NEC-2 engine. NEC-2 is sufficiently accurate for many antenna modeling purposes, but does not work for buried wires or wires that are close to ground.

NEC-3, and the current NEC-4 program which superseded NEC-3, allows buried wires and wires close to ground to be modeled correctly. NEC-4 further allows electrically small antennas to be modeled with better accuracy.

The NEC-4 program is licensed by the Lawrence Livermore National Laboratory and is under United States export control.

Please refer to the LLNL web page here for information on how to obtain NEC-4.

Because of the export control, there is no other way for an individual to obtain NEC-4 other than going through the process described in the aforementioned web page. To use NEC-4 with cocoaNEC, you will have to be personally licensed by the Lawrence Livermore Lab.

In addition, for the case of MacOS X, you will also have to create your own Unix executable binary from the material that they send to you.

Currently, the NEC-4 package comes as a set of prepackaged programs for the Windows operating system only. For Unix in general and Mac OS X in particular, you will need to compile and build your own Unix executable from the sources that are in a tar file on the distribution CD-ROM (see Building the NEC-4 Unix Executable below).

cocoaNEC assumes that the Unix executable is saved in the /Applications folder as a file named nec4d. As in the case of the built-in NEC-2 engine (nec2c), cocoaNEC first generates a card deck file from either a spreadsheet or an NC description. When NEC-4 is selected as the compute engine, cocoaNEC runs nec4d as a MacOS X task, sending NEC-4 the names of the card deck file and the file that NEC-4 writes its output to. cocoaNEC waits for the task to finish execution, it then proceeds to parse the NEC-4 output into the familiar graphical views in the cocoaNEC output window.

To select NEC-4 as the compute engine, open the Preferences panel (Preferences menu item in the cocoaNEC menu in the main menu bar) and choose "NEC-4 (external)" as shown below:

choice


Once selected, the NEC Engine setting will be saved to your cocoaNEC plist (preferences) file when you exit the application.The next time you launch cocoaNEC, you will be using the same engine that you had previously selected.

One difference that you will notice when using NEC-4 is that the MacOS X Activity Monitor will show an extra process which runs for the duration that NEC engine is running (e.g., while the circular progress indicator that is next to the NC window's Run button is active).

You can dynamically select between using the nec2c engine and the NEC-4 engine. The same model that is run under nec2c and under NEC-4 will appear as separate contexts in the output window. This allows you to use the output of one engine as a reference to compare the output of the second engine.

The output of the model that is produced using the NEC-4 engine will appear as a context whose name has "(NEC-4)" appended to it, as shown below:

context

The summary view of the output window will also indicate the engine you have selected for the run:

summary


Building the NEC-4 Unix Executable

The CD-ROM from the Lawrence Livermore National Laboratory has a folder containing the NEC-4 Manuals, a folder containing NEC-4 programs that run on the Windows operating system, and a tar file name NEC4_TAR.

1. Create a folder on your Desktop called nec4, and drag copy the NEC4_TAR archive file from the CD-ROM to this new folder.


2. Launch Terminal.app and issue the Unix shell command to connect to the directory which you have created (remember that you need to hit at the end of each command), i.e.,


cd ~/Desktop/nec4


3. Now extract the files from the archive file which you have copied from the CD-ROM:

tar xvf NEC4_TAR

In addition to the original file that you have copied from the CD-ROM, you should now see three folders, diglib, nec4dst and plotdst.


4. Optional changes to NECPAR.INC

Within the nec4dst folder, there is a file called NECPAR.INC. This is where you can change the memory allocation that NEC-4 uses. If you feel that you will need more segments (defaulted to 1210), then edit the MAXSEG and MAXMAT values.

You can edit the files from the Unix terminal (e.g., with emacs) or with a Mac OS X text editing application (e.g., with BBEdit).


5. Mandatory changes to nec4d.f

Within the nec4dst folder, there is a file called nec4d.f. This is the NEC-4 program in FORTRAN. The function that NEC4 uses to obtain elapsed time is not compatible with the gfortran in Mac OS X (SECOND is an intrinsic function in gfortran).

Change all references to "CALL SECOND" in the file to "CALL CPU_TIME" (17 instances).


6. Build NEC-4

NOTE: If you do not already have the GNU FORTRAN compiler on your computer, please follow the instructions in the Installing gfortran section below.

Select nec4dst as your working directory in Terminal.app and run the FORTRAN compiler.

cd nec4dst
gfortran -o nec4d -O1 nec4d.f

gfortran will emit some warning messages regarding assigned goto statements, e.g.,

ASSIGN 2 TO NEXT
GO TO NEXT, (2,3,6,11,7)

These are used by the Sommerfeld approximation code. In spite of the warnings, I have not detected anything detrimental.

Note: the assigned goto statement has been deprecated and deleted from the language since Fortran'95, but the statement has remained in most FORTRAN compilers.

I have experimented with manually replacing these assigned goto statements with equivalent FORTRAN computed goto statements and I have found no difference in the results when the Sommerfeld ground model is selected. I believe that you are moderately safe to ignore the gfortran warnings.

However, if you really want to fix the code to avoid using deprecated code, you can perform the following steps:

(a) change each of the ASSIGN N TO NEXT (where N is some number) statement to a simple NEXT = N statement (where N is the same number), and

(b) replace each of the GO TO NEXT, (x, y, z, ...) statement with the following computed goto statement

GO TO (1,2,3,4,5,6,7,8,9,10,11) NEXT

(Note that with the computed goto statement, if the value of NEXT is 4, the code will branch to the 4th label inside the computed goto list, which in the code above is the label "4". This is not the cleanest approach to getting rid of the assigned goto statements, but desperate times call for desperate measures.)


7. Move the Unix executable to /Applications.

When gfortran finishes, the resultant Unix executable is called nec4d. This can take a while; it takes about 40 seconds on a 2.3 GHz G5 PowerPC processor.

Move the file nec4d from the nec4dst folder to the /Applications folder, and you are done. You can move the nec4 folder to the Trash.

Please note that the Unix executable is not cross platform between Macintoshes running on PowerPC and on Intel processors.


Installing gfortran

1. Download the zip package containing the gfortran tools from here.

As of this writing, the most current version at the above web site is the "Jan 08 gfortran build" (about 90 MB) which you can down directly from here:

http://www.macresearch.org/files/gfortran/gfortran-4.3-Jan08.mpkg.zip

2. Once downloaded, unzip the file by double clicking on the zip file, and you should end up with a meta installer (mpkg) package.

3. Double click on the mpkg file, and follow the instructions. You will at some point be presented with a window that looks like this:

gfortran installer

Select the gfortran package that fits your processor and operating system (the PowerPC/Leoprad version is selected in the example above). There is no need to install the Xcode Plug-ins if you are only going to follow the instructions given above for building NEC-4.

Extra information can be obtained from here.


Acknowlegements

Many thanks to Bill K1GQ for collaborating with me on making NEC-4 functional with cocoaNEC.