WRspice contains a separate expression handling system for expressions found in device lines. Voltage and current source lines may contain expressions, as can resistor and capacitor device lines. These use the same syntax as is used in vector expressions in WRspice shell commands, and in single-quoted expressions. The expression can contain vectors from the current plot or the constants plot, and circuit parameters accessed through the @device[param] construct. In addition, the variable ``x'', which can appear explicitly in the expression, is defined to be the controlling variable in dependent sources, or is set to the the scale variable in the analysis (e.g., time for transient analysis). During a ``pure'' dc sweep, i.e., not part of a chained analysis, the x variable is set to the voltage or current of the swept source, or the first swept source if there are two.
In a chained analysis, the x variable will be that of core analysis. Thus, for a chained transient analysis, x is time, as in the unchained case. Since the functional dependence is inoperable in any kind of ac small-signal analysis (ac, noise, transfer function, pz, distortion, ac sensitivity) x is not set and never used. In ``op'' analysis, x is always numerically zero. The same is true in dc sensitivity analysis.
Although the syntax and most of the function names are equivalent to vector expressions used in post-processing, the mathematics subsystems are completely different. There are two main differences from ordinary vector expressions:
Although the available functions lists are similar, the internal evaluation routines are different. The shell math functions must operate on vectors of complex values, whereas the functions called in device expressions take scalar real values only. Furthermore, the device expressions must be differentiable with respect to included node voltages and branch currents, as the derivative of the expression is computed as part of the iterative process of solving the circuit matrix equations. This means that logical and conditional operators are not available in source expressions, nor is there a modulus operator or a value returned from assignment. The sgn function grossly violates the differentiability requirement, and many of the functions and/or their derivatives have restricted ranges or singularities. These can easily lead to convergence problems unless some care is exercised.
As for all expressions, if an expression is enclosed in single quotes, it will be evaluated when the file is read, reducing to a constant. However, if the expression contains references to circuit variables such as node voltages or branch currents, it will be left as an expression, to be evaluated during the simulation.
The following math functions are available in device expressions on most systems:
abs absolute value acos arc cosine acosh arc hyperbolic cosine asin arc sine asinh arc hyperbolic sine atan arc tangent atanh arc hyperbolic tangent cbrt cube root cos cosine cosh hyperbolic cosine deriv derivative erf error function erfc error function complement exp exponential (e raised to power) j0 Bessel order 0 j1 Bessel order 1 jn Bessel order n ln natural log log natural log log10 log base 10 pow x to power y pwr x to power y sgn sign (+1,0,-1) sin sine sinh hyperbolic sine sqrt square root tan tangent tanh hyperbolic tangent y0 Neumann order 0 y1 Neumann order 1 yn Neumann order n
Most functions take a single argument. Exceptions are jn and yn, which require two arguments. The first argument is an integer value for the order, and the second argument is the function input. The pow and functionally identical pwr functions also require two arguments, the first argument being the base, and the second being the exponent. The deriv function will differentiate the parse tree of the argument with respect to the ``x'' variable (whether implicit or explicit). This is completely unlike the deriv function for vectors, which performs a numerical differentiation with respect to some scale. Differentiating the parse tree gives an analytic result which is generally more accurate.
In addition, there are special ``tran functions'' described below which produce specified output in transient analysis. WRspice recognizes by context functions and tran functions with the same name (exp, sin, gauss). An unrecognized function is assumed to be a table reference (specified with a .table line).
During a ``pure'' dc sweep analysis, for ``independent'' sources (keyed by v, i, or a and not e, f, g, or h) other than the swept ones, if an expression is given, the output of the source will be the result of the expression where the input x is the swept voltage, rather than time as when in transient anslysis. However, if the source line has a ``dc'' keyword and optional following constant value, during pure dc analysis the source will output the fixed value, or zero, if the value is omitted. However, in pure dc analysis the tran functions generally return zero. The exceptions are pwl, table and table references, and interp. These functions return values, but with the swept voltage (x) as the input (in the case of table the input may be explicit anyway). For ``dependent'' sources (keyed by e, f, g, or h) the x is the controlling voltage or current as in transient analysis. Again, if a ``dc'' keyword appears, the output will be fixed at the given value, ignoring the controlling variable.
In addition to the built-in functions, expressions used in devices can include user defined functions, which must have been defined previously with the define command, or with a .param line, or in a parameter definition list in a subcircuit call or definition.
The following tokens are recognized in the function string:
+,*,/ binary: add, multiply, divide - unary or binary: negate or subtract ^ binary: exponentiation () association , argument separator x independent variable number a floating point number string a library function, table, or circuit vector
The independent variable x is context specific, and usually represents a global input variable. It is the running variable in the current analysis (time in transient analysis, for example), or the input variable in dependent source specifications (see 2.15.4).
Example expressions:
sqrt(v(1)*v(1) + v(3)*v(3))
ln(exp(v(3)^1.5)/3)
2*x - x*x*x
In expressions, if a string starts with a backslash (' \') character, it will not be replaced with a value, should the name happen to match one of the named constants, or other potential substitutions. This will be needed, for example, if a node name matches one of the predefined constant names, and you need to reference that node in a source.
For example, suppose there is a node named ``c'', which is also the name of the speed-of-light constant. A source specification like
vcon 1 2 5*v(c)will not be accepted. The correct form is
vcon 1 2 5*v(" \c")