Electronic – LTSpice Syntax Error: function if(): requires exactly three arguments

ltspicesimulation

This is a LT-spice Nestlist subcircuit code was found on a research paper. However, when I run a simulation on this subcircuit code, I keep running into error "Fatal Error: Syntax Error: function if(): requires exactly three arguments". I spent a long time in figuring out the cause of the error but failed. The only think I find is that it seems to be caused by the line defining F(V1,V2). The subcircuit code is the following. Can somebody help me to find out what's the problem with the subcircuit nestlist code and is there a solution to rewrite the code so that the error doesn't occur?

The code is following: I can't identify nor sovle the if statment error in this code.

.subckt mem_dev TE BE

.params a1=0.17 a2=0.17 b=0.05 Vp=0.16 Vn=0.15 Ap=4000 An=4000 xp=0.3 xn=0.5 alphap=1 alphan=5 xo=0.11 eta=1

.func wp(V) = (xp-V)/(1-xp)+1
.func wn(V) = V/(1-xn)

.func G(V) = if(V <= Vp, IF(V >= -Vn, 0, -An*(exp(-V)-exp(Vn))), Ap*(exp(V)-exp(Vp)))
.func F(V1,V2) = IF(eta*V1 >= 0, IF(V2 >= xp, exp(-alphap*(V2-xp))*wp(V2) ,1), IF(V2 <= (1-xn), exp(alphan*(V2+xn-1))*wn(V2) ,1))

.func IVRel(V1,V2) = IF(V1 >= 0, a1*V2*sinh(b*V1), a2*V2*sinh(b*V1) )

Cx XSV 0 {1}
.ic V(XSV) = xo
Gx 0 XSV value={eta*F(V(TE,BE),V(XSV,0))*G(V(TE,BE))}

Gm TE BE value = {IVRel(V(TE,BE),V(XSV,0))}
.ends mem_dev

To test the subcircuit, the asc file for the simulation testing circuit has the following code:

Version 4
SHEET 1 880 680
WIRE 256 304 -48 304
WIRE 256 320 256 304
WIRE 224 384 -48 384
WIRE 256 384 224 384
WIRE 224 432 224 384
WIRE 0 0 0 0
FLAG 224 432 0
SYMBOL diode 240 320 R0
SYMATTR InstName D1
SYMATTR Value mem_dev
SYMATTR Prefix X
SYMBOL voltage -48 288 R0
WINDOW 123 0 0 Left 0
WINDOW 39 0 0 Left 0
SYMATTR InstName V1
SYMATTR Value SINE(0 0.5 100)
TEXT -64 24 Left 2 !.include Memristor.cir
TEXT 16 -16 Left 2 !.param initialY=0
TEXT -80 416 Left 2 !.tran 100m

Best Answer

In the past, during the LTspice IV version, there was a time when ambiguous definitions for .funcs were allowed. When I say "ambiguous" I mean that the manual says:

Syntax: .func ([args]) {}

but the equal sign and no curly braces were allowed, i.e. .func f(x)=x**2 instead of .func f(x) {x**2}. But probably due to the difficuly of parsing these lines with more complicated functions, Mike Engelhardt (the creator of the program) said that unless the user writes the functios "by the book", all bets are off. It is still possible to have = definitions in LTspice XVII, but in the same unsupported manner.

This is what happens here. All the functions are defined with = and without {}, so the parser has problems deciphering the context. Once you search and replace all the = with { and all \n with }, you'll see it works. You will also need to modify the initial conditions to .ic V(XSV) = {xo}, because xo is not a numeric literal and needs evaluation (curly braces perform evaluation). When in doubt, best use the book.