NC Language

(For the NC Syntax see here.)

An NC Program starts with an optional list of global variable declarations. It is then followed by a mandatory function called 'model', and followed optionally by a function called 'control.'

The antenna model is contained in the body of a function called "model." The following shows probably the shortest possible NC program that, when run will generate output to the Output window (the Output window is described here).


model ( "dipole" )
{

    voltageFeed( wire( 0, -5, 12, 0, 5, 12, 0.01, 21 ), 1, 0 ) ;

}



The function starts with a keyword model, followed by a name string enclosed within parentheses. An NC string consists of text that is enclosed between double quotes. In this case, the string "dipole" is an arbitrary name that will be passed to NEC-2 as a comment "card."

The function header is then followed by the body of the model in between braces ("curly brackets"). In the case above, there is a single statement in the function body. All NC statements end with a semicolon. Each statement tells NC to do something. In the case above, the single statement is a nesting of two different system functions. One function creates a wire and the other function attaches a voltage feed point to this wire.

To explain what these two functions do, it is probably clearer to rewrite the above as


// simple dipole antenna

model ( "dipole" )
{

    element fedWire ;

    fedWire = wire( 0, -5, 12, 0, 5, 12, 0.01, 21 ) ;
    voltageFeed( fedWire, 1, 0 ) ;

}



Space characters, tab characters and carriage returns are ignored by NC. You can insert them wherever you like to make the program more readable.

Anything past a double slash until the end of a line is treated as comments and also ignored by NC.

The body of a function can start with variable declaration statements. A variable declaration statement starts with a type name and followed by one or more identifiers, separated by commas. The type keyword can be either "int", "real", "element","coaxtype", "vector" or "transform".

(Please refer to the Physical Coax and Twin-lead Transmission Line section for additional information on coaxtype, and the Vectors and Transforms section for additional information on vectors and transforms.)

In the above, we have declared the identifier fedWire to be a variable of type element. An element represents a piece of straight wire. An int variable represents an integer and a real variable represents a real number.

The wire function creates the actual piece of straight wire. Equating the variable fedWire to this wire allows you to refer to this piece of wire by this name later.

The first three arguments of the function wire are the x,y and z coordinates, respectively of one end of the wire. The next three arguments are the coordinates of the second end of the wire. These are real numbers. If you have entered the arguments into the program as integers with no fractional part, NC will automatically change them to real numbers for you. The seventh argument is the radius of the straight wire and the final argument is an integer which tells NEC-2 how many segments you wish to partition the wire into when it calculate currents. If you enter a real number in the last argument, NC will first truncate it to an integer before passing it to the function wire.

The related line function and taperedWire function can be used in place of the wire function. For their descriptions, read the section on NC Functions.

In addition to system functions such as wire and line, you can also define your own functions. NC functions can be recursive.

The function voltageFeed lets you place a voltage source (feed point) at the center of a wire. The first argument is the wire reference, the second argument is the real part of the voltage source and the last argument is the imaginary part of the voltage source.

The wire, voltageFeed and other functions in NC are described here. Functions that specify ground parameters and radials are described here.

The following is a further improvement of the same program.


// simple dipole antenna

model ( "dipole" )
{

    element fedWire ;
    real length, height ;
    int segments ;

    segments = 21 ;
    height = 12 ;
    length = 5 ;

    fedWire = wire( 0, -length, height, 0, length, height, 0.01, segments ) ;
    voltageFeed( fedWire, 1, 0 ) ;

}



Notice that we have replaced some constants by variables. The arguments in the wire function are now clearer, and furthermore, if you wish to change the length of the dipole, you can now do it at a single place. It also allows you to run an optimizing loop that changes the length of the dipole.

Integer constants in NC are just the natural numbers. Real constants are numbers that can have fractional values.

Values that have dimensions, such as the x coordinate of one end of a wire, are expressed in meters. NC however has some modifier symbols to convert dimensions for you. A single double quote immediately after (with no spaces in between) an integer or a real constant will cause an inch-to-meter conversion. I.e., a constant that is immediately followed by a double quote is considered to be a number whose unit is in inches. E.g., 1" = one inch = 0.0254 meters. 1.35" = 1.35 inches.

A foot-to-meters conversion is done to numbers that have a single quote after them. E.g., 1' = 1 foot = 0.3048 meters.

A # immediately before an integer number will create a real number whose value is equivalent to the radius of a wire with the corresponding American Wire Gauge. I.e., #12 is the radius of a 12 AWG wire.

The character u immediately following a number applies a factor of 1e-6, the character n immediately following a number applies a factor of 1e-9 and the character p immediately following a number applies a factor of 1e-12.

NC has the usual arithmetic expressions. E.g., -5 is the negative of 5, 1+4 adds 1 to 4, 1+4*5 adds 1 to the product of 4 and 5.

You can nest expressions by placing them in parentheses, e.g., (1+4)*5.

In addition, NC has relational expressions. The relation ( a < b ) returns true if a is less than b. Other relations are > (greater than), <= (less than or equal) and >= (greater than or equal).


Boolean values are expressed in NC as integers. A boolean "true" is the same as an integer 1 and a boolean "false" is the same as an integer 0.

You can compare expressions for equality. ( a == b ) returns true (integer 1) if expression a has the same value as expression b. ( a != b ) returns true if the value of expression a is not equal to the value of expression b.

The relational expressions have higher precedence than the equality precedence. The expression ( a < b == c > d ) is equivalent to ( ( a < b ) == ( c > d ) ).

You can also use & for the boolean AND and | (vertical bar) for the boolean OR. The AND operator has precedence over the OR operator. ( a & b | c ) will evaluate as ( ( a & b ) | c ).

When in doubt, nest your expressions with additional parentheses.

The lowest precedence operator is the assignment operator. In the expression a = b+c & d, the right hand side of the assignment is first evaluated and the result is deposited into the variable on the left of the assignment.

Expressions are terminated by a semicolon. The semicolon makes an expression into a statement, and statements can be inserted into the body of a function.

A compound statement is a statement that contains other statements. You can create a compound statement by placing a list of statements in between open and closed braces:


{

    a = 1.0 ;
    b = 2.5 ;
    c = ( d + e ) ;
}



There is a conditional if-then-else statement, e.g.


if ( a < b ) c = 1.0 ; else c = 2.54 ;


The else part of an if-then-else is optional, e.g.,


if ( a == b ) c = 1.0 ;



There are two looping constructs. One is the while statement:


while ( a < 10 ) a = a + 1.5 ;


A
while statement continues to execute as long as the while clause (the "a < 10" above) is true.

The second looping statement is the
repeat statement:


repeat ( 12 ) a = a + 1.5 ;


The
repeat clause should be an integer constant or expression. The repeat statement executes for the number of times that is equal to this integer value. In the above example, the statement a = a+1.5 will be executed twelve times.

If the
while or repeat statement is made up of a compound statement, you can "break" out of a loop prematurely by using a break statement:


repeat ( 12 ) {

    a = a + 1.5 ;
    if ( a > 5.0 ) break ;
    b = b*1.01 ;
}