Building NEC-4.1

Kok Chen, W7AY [w7ay (at) arrl (dot) net]
Last updated: May 25, 2012

Building the NEC-4.1 Unix Executable

Please note that the following instructions are for the original NEC-4.1 CD-ROM. The procedure of building a NEC-4.1 from the currently issued combined NEC-4.1 and NEC-4.2 CR-ROM may be different from what is described below.

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 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 on a following page.

Select nec4dst as your working directory in 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.,

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.


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