Keyboard Shortcuts
Likes
Search
How to include component values in LTSpice trace formulas
Just joined the group and don't know how to search whether this has already been addressed - guidance on that would be appreciated! But here's my question: I would like to be able to define a trace for the energy stored in a capacitor using the formula E= 1/2 * V^2 * C. But I can't find how to include component values such as capacitance, C, in the trace formulas. Is there a way to do this? Thanks - Doug |
Doug asked: ? ? "I would like to be able to define a trace for the energy stored in a capacitor using the formula E= 1/2 * V^2 * C. But I can't find how to include component values such as capacitance, C, in the trace formulas. Is there a way to do this?" There is. First, use .PARAMs to define parameters using those formulas.? This step is not really essential, but it helps to keep things organized. Then, when you have your (parameter) value calculated, put it within {curly braces}, where the component's value should go.? For example, for a capacitor, right-click on the text where the value would go (originally "C" when you put it on the schematic), and change it to something like {MyCapValue} where MyCapValue is a user-defined parameter that was defined in a .param statement.? Or, I believe you could use {E/(0.5*V(something)**2)}. Be careful NOT to use "^" to mean exponentiation.? In most places in LTspice, "^" is the operator for XOR.? "**" properly raises one number to an exponent.? See the tables on the Help page for the .PARAM statement. A pair of curly braces is the way to turn a parameter into a number.? Where SPICE/LTspice expects there to be a number but instead you have a parameter, use curly braces to turn it into a number. Regards, Andy |
¿ªÔÆÌåÓýYou should first of all, read the Help. It is very information-intensive, so it needs reading carefully and reading several time. There is a special way of treating capacitors that do unusual things or are charged in unusual ways, which is at the end of the Help page on Capacitors: There is also a general nonlinear capacitor available. Instead of specifying the capacitance, one writes an expression for the charge. LTspice will compile this expression and symbolically differentiate it with respect to all the variables, finding the partial derivative's that correspond to capacitances. Syntax: Cnnn n1 n2 Q= [ic=] [m=] There is a special variable, x, that means the voltage across the device. Therefore, a 100pF constant capacitance can be written as Cnnn n1 n2 Q=100p*x A capacitance with an abrupt change from 100p to 300p at zero volts can be written as Cnnn n1 n2 Q=x*if(x<0,100p,300p) I have assumed from your message that your capacitor value is not
constant. If it is constant, just put its value in the expression.
You can do this even just with Waveform Arithmetic (see the
Help!). Click on the title of the plot of the voltage (V(n005) for
example) across your capacitor and change? the expression? to
V(n005)^2*10^(-7)/2, if your capacitor is 100 nF. The y-axis is in
terms of volt-squared, because LTspice doesn't know that the
'10^(-7)' is a capacitor.? It ought to be plotted in joules if you
multiply it by 1F/1J (1 farad/1joule), but it doesn't work because
LTspice recognizes '1F' as '1 femto', i.e. 1-^(-15). John Woodgate OOO-Own Opinions Only J M Woodgate and Associates Rayleigh, Essex UK On 2018-11-17 19:31,
douglas.fay@... [LTspice] wrote:
? |
¿ªÔÆÌåÓýCorrection. Replace ^ by **, and the last
number should be 10**(-15).? It may be possible to work round
the '1F' problem by changing it to '1C/1V', which is '1 coulomb
per volt'. John Woodgate OOO-Own Opinions Only J M Woodgate and Associates Rayleigh, Essex UK On 2018-11-17 21:38, John Woodgate
jmw@... [LTspice] wrote:
Click on the title of the plot of the voltage (V(n005) for example) across your capacitor and change? the expression? to V(n005)^2*10^(-7)/2, if your capacitor is 100 nF. The y-axis is in terms of volt-squared, because LTspice doesn't know that the '10^(-7)' is a capacitor.? It ought to be plotted in joules if you multiply it by 1F/1J (1 farad/1joule), but it doesn't work because LTspice recognizes '1F' as '1 femto', i.e. 1-^(-15). |
Thanks so much Andy, and the other folks who responded. That's the approach I'll use. To answer a question in one of the responses about what I'm trying to do, it's to try various values of C, L and other parameters for an LC tank circuit for a Tesla coil to see which values result in the greatest energy being stored.
... and thanks for the other info, like using ** instead of ^ - that would explain the weird results I got when I used ^ :-) |
Oooops - just found a problem with using .PARAM statements to do what I wanted. To briefly summarize my objective, it is to plot a trace of the energy stored in a capacitor over time. I want to use the formula E = 1/2 * C * V**2.?
So here's the problem I ran into: the .PARAM statement works fine on the schematic for all sorts of purposes, but I am unable to access .param values in trace formulas. For example, I can easily define a trace like "v(nodeA)*I(resistorB)*100". But LTSpice does not let me access .params like r(resistor1) as part of the trace formula - or, at least, I can't find anything in the program or documentation that lets me do that. > If anyone knows a clean, direct way to get param values into the formula for a trace, I'd love to know what it is < But in the meantime, here's an outline of the hack that I think should work, in case anyone is interested, and in case there isn't a way to get the .param value directly into the trace formula: 1. Enter a directive, .param MyCapValue=<desired_cap_value> on the schematic. 2. Set the value of the capacitor in question to {MyCapValue} 3. If necessary, also use MyCapValue to manipulate other values in the circuit. In my case, I will set MyInductanceValue to <desired_product_of_L*C> / {MyCapValue}, in order to make sure the resonant frequency? of the LC tank circuit stays the same regardless of the chosen capacitance value. 4. Set up a grounded DummyDCVoltageSource separate from the main circuit and set it's output voltage to {MyCapValue}. 5. Stick a label on the dummy source output, say "D". 6. Stick labels on both sides of the capacitor, say C1 and C2 7. Then the trace formula for energy stored in the capacitor would be (v(C1)-v(C2))**2*0.5*v(D) I haven't actually tried this yet, so I don't know whether LTSpice will give me grief about misusing units (e.g., using Volts to represent Farads), but I figure that can be worked. |
... I forgot to clarify that the capacitance value does not change over time - I just want to try different values to figure out what works best. But I don't want to have to remember to put the changed value in the trace formula as well as changing it in the schematic - I'd like to be able to change it only one place on the schematic and then have that value automatically used in the trace formula ...
|
0cab7709201904469fc4e9d8929c0f93? wrote:"To answer a question in one of the responses about what I'm trying to
do, it's to try various values of C, L and other parameters for an LC
tank circuit for a Tesla coil to see which values result in the greatest
energy being stored." The cleanest way to do achieve this is by means of .measurements. .Params are not available in the waveform viewer as you have discovered, but they are to the .measurement sub-system. Since you have already defined your "figure of merit", i.e. the energy stored in the capacitor, you can create a .measurement that calculates just that. The results of .measurements are written to the logfile after the simulations runs. If you have .stepped any parameters, you then plot the defined .measurements versus your swept parameter, which directly achieves your objective. To plot these .measurements, firstly you must open the logfile after the simulation runs (Ctrl-L), and then right-click within the logfile window and select "Plot .step'ed .meas data". Then, another waveform window pops up and you can select which .measurements to plot. I've uploaded an example that plots the energy in a tuned circuit versus capacitance with real a inductor and capacitor with finite and defined Qs. This might not be exactly what you want, but it'll get you started, Files > Temp > Energy.zip Regards, Tony |
> If anyone knows a clean, direct way to get param values into the formula for a trace, I'd love to know what it is < Just create a voltage source and assign its value the parameter name.? You can now use the voltage of that source in your formula to access the parameter value.? If you desire, you can also divide the expression by 1V to make the parameter dimensionless. |
Doug, you probably realized this already, but I had completely misunderstood your original question.? When you asked about "traces", I thought you meant PCB traces, A.K.A. "wires" or nets.? Silly me, I didn't realize that you meant traces in the waveform window. Probably the easiest way to get a parameter (.PARAM) value from a schematic into the waveform section of LTspice, is to use a voltage source with value equal to the parameter, as you outlined, and as "hevans" described.? Voltage sources are cheap in SPICE. Just be aware that .PARAMeters are evaluated only before simulation begins, so not only do their values not change over time, but they can't depend on any voltages or currents elsewhere in your circuit. But Tony's approach of using .MEAS statements, then plotting their results from the .LOG file, is very powerful. Most everywhere in LTspice, numbers are just numbers.? When you combine things into formulas, LTspice doesn't care that Volts really mean Farads.? I think it never complains about combining or mixing up units.? However, LTspice's plotting window is moderately intelligent about interpreting units, in order to attach helpful units to your displayed axes, but otherwise it also doesn't care.? So, for example, if your formula uses Volts as the analog of capacitance, you can try multiplying it by (1000mF/1V) and LTspice might recognize that you wanted Farads.? I'm not entirely sure that it knows Farads, Joules, or whatever, but it might.? (You'd have to do it this way instead of "1F/1V" because "1F" likely would be interpreted as 1 femto and that would scale things rather incorrectly.) It should also be noted that there are a few situations where LTspice's waveform viewer treats units and multipliers differently than the rest of LTspice (and SPICE).? For example, I BELIEVE (not certain) that the waveform viewer lets you use MHz to mean megahertz, whereas in SPICE and in the simulation engine in LTspice, MHz is always interpreted as milli (or milliHertz but without the Hertz).? I haven't looked to see if the waveform viewer knows anything special about "Farads" when spelled out; or for that matter, if it even knows that Farads exist.? I'm sure it has a limited set of units that it understands.? This part of LTspice is not well documented. Regards, Andy |
Doug, One way is found here: Note that this method does not rely on the value of the capacitor because it integrates the VI product v(t)*i(t) which is energy by definition.? Be sure that the voltage on the capacitor is 0 at time=0.? Use the UIC option of transient analysis if necessary.? Use small time step to improve accuracy if necessary. Good luck, Dan |
I have a follow-up question: I'm trying to model a transistor equivalent circuit and would like to use a user defined function for the nonlinear capacitance Q=... definition. Either I'm really missing something or this is not possible at all!?? Would be great if someone could confirm if its just not possible to use .func functions inside the Q=... definition for a C. Thanks! |
hagaaar587plus7 wrote: ? ? "I have a follow-up question" I'm not sure why this is a follow-up question.? It doesn't follow-up on a question you already asked, and it seems to be unrelated to the original question.??When you send a message, please use a Subject line that fits the question.? What you did is called "hijacking" another message thread.? (I'm leaving the subject line the same for now, so you can find this reply near your question.) ? ? "I'm trying to model a transistor equivalent circuit and would like to use a user defined function for the nonlinear capacitance Q=... definition." Which capacitance would that be? Looking at bipolar transistors, there are half a dozen capacitance parameters.? Looking at MOSFETs, the number is a little greater. ? ? "Either I'm really missing something or this is not possible at all!?" I don't know.? What did you try?? What was the result?? Are you trying to introduce a function into one of the parameters of a .MODEL statement?? Are you trying to add a nonlinear capacitor to a transistor's .SUBCKT, or just connected to the transistor on your schematic? I would guess that trying to make a .MODEL parameter voltage-dependent is probably futile.? I'm pretty sure that .MODEL statement parameters must be constants during a simulation (even though the underlying elements may vary according to their formulas).? I'm pretty sure that nonlinear capacitor elements, either within or not within subcircuits, ought to work fine. Consider uploading an example of what you tried that didn't work. Regards, Andy |
First, sorry for "hijacking". In Message 3 (which led me here), John cited the help and the "general nonlinear capacitor" function of the C part in the schematic editor.
So again, my question is, if it's not possible to use .func statements inside the Q=... definition of a C part in the schematic editor. ("general nonlinear capacitance" according to help) I've uploaded pictures of a minimal example including the resulting error message ("No such function", although it is listed in the netlist) here: |
On 12/24/18 8:03 AM, hagaaar587plus7@... [LTspice] wrote:
?It is best to upload files to the temp directory of this group rather than externally. (and the code example could have just be placed as in-line text in the message. Have you read the description of the .func statement here: which says: To invoke parameter substitution and expression evaluation with these user-defined functions, enclose the expression in curly braces. The enclosed expression will be replaced with the floating-point value. Now, I am not sure that this will work for you case. -- Richard Damon |
Thanks a lot that seems to do the trick! (needs further testing, but probably works)
I've read the Func dot command page in the wiki several times, but didn't understand it correctly apparently... Just if anyone cares, I implemented the same functions (without curly braces) in b sources to evaluate the functions I was writing. So I was confused as why the same expression worked for I=... in b-sources but not for the Q=.... statement in C. Anyway thanks again! |