1
\$\begingroup\$

Has anybody used ltspiceIV in a optimization process using Matlab? I need feedback because I am running my circuit in transient analysis, and when I found the optimal point of my optimization, I ran it again but alone (not in the optimization loop) and I don't get the same values. Also, in a for loop, I obtain NaN (Not a number) value in my results. Is Ltspice suitable for optimization?

I work on electrical machine modeling. It is modelling the machine using equivalent magnetic circuit (which looks like electric circuit with resistor and source). I am then using ltspice coupled with matlab to solve this circuit. I create my netlist using matlab, I solve using ltspice, I collect the results from .raw files using a matlab program and I process the results using also matlab. It works until now. (the end of the question after that)

I am doing optimization so I need to run over and over again ltspice until the algorithm find the optimal point. I want to know if this process of running of multiple time (through optimisation or "for loop") is suitable for ltspice? I found that it can give me false results or doesn't compute the results at certains time (I obtain NaN (Not an Number) values).

In my circuit, there are a lot of controlled source, subcircuit, inductance and resistors.

\$\endgroup\$
  • \$\begingroup\$ You are going to need to provide much more information. 1) What parameters and circuit are you optimising? 2) How are you integrating MATLAB and LTspice. Aside from that, NaN can be handled in MATLAB tends to indicate a larger issue, and LTspice can be very slow for optimisation. \$\endgroup\$ – loudnoises Feb 4 '18 at 14:15
1
\$\begingroup\$

Here is some code to run a sim from matlab. One option would be to generate a netlist file from the .asc and run that from the command prompt, you could even change the netlist file from matlab and add, delete components or in the least change values.

Another idea I've had is you could also do this with netlists and write the netlists from matlab and even do genetic optimization with circuits by having matlab write the circuit for you, then run the circuit, then check the result in matlab.

Anyway here is the code, I changed some names and you don't have the .asc files or the write nets, so the code will need some retooling but here is an example of how to get the code out.

You'll also need the function Ltspice2Matlab within the scope of this code. An advantage is I don't think the function generates Nan's and has a good timebase that it returns. If it does you can simply replace the Nan's with the previous value or and average of the previous value and the next value using isnan

status1 = system('C:\...\LTspiceIV\scad3.exe -Run -b blah.asc')

comparerun = LTspice2Matlab('blah.raw');

status2 = system('C:\...\LTspiceIV\scad3.exe -Run -b blah2.asc')

comparerun2 = LTspice2Matlab('blah2.raw');

%get some data from netlists out of the raw file
cVres = comparerun.variable_mat(find(strcmp(comparerun.variable_name_list,'V(one)')),:); 
cVzerostage = comparerun.variable_mat(find(strcmp(comparerun.variable_name_list,'V(two)')),:);
cVthree= comparerun.variable_mat(find(strcmp(comparerun.variable_name_list,'V(three)')),:);


% figure;plot(baserun.time_vect,Vthree)


ylabt = 'Voltage';
tittx = 'title goes here';
h = figure;subplot(20,1,1:12),plot(Vres,cVthree),hold on,plot(cVres,cVthree,'r')
legend('Origonal Run','Compare Run');
title(tittx);
xlabel('xlabel')
ylabel(ylabt);

% if you want to compare the two runs with a residual
interpV4thstage = interp1(comparerun.time_vect,cVthree,baserun.time_vect);
subplot(20,1,17:20),plot(Vres,Vthree-interpV4thstage,'m')
title('Residual');
xlabel('xlabel')
ylabel(ylabt);
\$\endgroup\$
  • \$\begingroup\$ I use matlab to generate the netlist and after Ltspice2matlab helps to collect the results. In the results, there are points which are not defined at some time but I solve that by doing an interpolation with a fixed time step before doing the meaning. It's worked. \$\endgroup\$ – A. S-S Feb 8 '18 at 9:57
  • \$\begingroup\$ My other concern is convergence problem. It is not obvious to change all the time the convergence options for each case especially when I need to do an optimization. I am looking for options that can work for all the parameters that I will simulate but I can't find those. My circuit is a little bit difficult. I simulate a switching circuit (inverter) coupled with another circuit (equivalent magnetic circuit) consisted of time variable resistor (subcircuits), controlled sources defined by table. If you have any ideas or tips on how I can generalize convergence options, please share \$\endgroup\$ – A. S-S Feb 8 '18 at 10:05
  • \$\begingroup\$ Yep, you'll need to change the solver options. Here is some information about the solver If you like the answer upvote. Be sure to mark the best answer also. \$\endgroup\$ – laptop2d Feb 8 '18 at 15:19
  • \$\begingroup\$ @ laptop2d Do you have some experience in using ltspice for optimization? I begin to have serious doubt about the ability of spice to solve a optimization problem because although I change the options, ltspice still got convergence problem \$\endgroup\$ – A. S-S Feb 8 '18 at 17:56
  • \$\begingroup\$ I have used LT spice for designing: DC to DC converters and an MPPT tracker. It works great. One of the solvers was built specifically for switchers, and a good portion of Linears switchers are probably designed using LT spice. If your getting convergence problems they are probably due to the circuit, and can be solved with clever use of parasitic's. Please participate in the community and use the voting system. If your having a hard time getting your circuit to converge, then post this as a question and I'll look at it. I won't help people that don't participate in the voting system. \$\endgroup\$ – laptop2d Feb 8 '18 at 18:06
0
\$\begingroup\$

1) Why do I get NaN results from LTspice

LTspice will give NaN results when the circuit provided cannot be solved, which can be caused by supplying parameters that prevent the model from being computed.

To solve this you need to place limits on what circuit parameter values you supply to LTspice, which can be performed with constrained optimisation like fmincon. If the parameter space regions which cause the model to fail are not obvious, you can simply choose to ignore parameter value sets that cause a NaN result, though for many optimisation algorithms this can cause a failure.

EDIT 1.5) How to deal with averaging NaN values in MATLAB

There are two ways of doing this: nanmean which is a part of the statistics toolbox, or you can use mean(vector(~isnan(vector))) where this will only average the non NaN values of the vector.

2) Why when I find an optimal parameter set in MATLAB does it not translate to LTspice in isolation?

This means that somewhere in your function that builds the LTspice model there is an error that causes an incorrect circuit model to be built. Either that or when you build your circuit with the optimal results you make a human error in the design.

3) Is LTspice suitable for optimisation?

In my experience LTspice is unbearably slow, and if you can build your circuit model in C code instead you can work much faster. However this has it's own problems that you need to understand modelling at a much deeper level and takes much longer to design than an LTspice schematic.

If you run on macOS you may consider using MacSpice as it contains a built in optimisation algorithm which would prevent you from having to use two programmes to optimise a circuit.

\$\endgroup\$
  • \$\begingroup\$ The problem is that I can't isolate the parameters corrsponding to the NaN values because they move all the time. I run a first time for example in a for loop, I obtained NaN values at some places. I run the same program again, the NaN values will be somewhere else. It is very random. if I simulate the values individually (not in a for loop), I will always have my results, never NaN. I don't know if it is always the programm that we write which is the problem or the problem in ltspice itsef. Also, Did you heard about Spice Opus? \$\endgroup\$ – A. S-S Feb 4 '18 at 14:50
  • \$\begingroup\$ You should capture the netlists that cause your NaN values and check them individually for errors as it is almost certainly in the automated netlist generation if you say you can build LTspice circuits that operate correctly. \$\endgroup\$ – loudnoises Feb 4 '18 at 16:10
  • \$\begingroup\$ Thank you for the solution that you suggest. But as I said early, the appearance of NaN is very random. I will give some context. I am optimizing the mean value of the torque by varying parameters let's say x. the torque is derived from the results of my circuits. I observed that when I am in "for" loop for example, the torque vs time that I obtained is at some time points not defined (NaN values) and consequently the mean value isn't computed. Also, I don't known if this is the cause but I change my simulation options too, default method didn't work and I choose .options method = gear \$\endgroup\$ – A. S-S Feb 4 '18 at 16:46
  • \$\begingroup\$ See edits for averaging with nan values. \$\endgroup\$ – loudnoises Feb 4 '18 at 17:10
  • \$\begingroup\$ I don't understand. Which edits with nan values? \$\endgroup\$ – A. S-S Feb 4 '18 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.