Post Go back to editing

Unable to create a standalone diode model in the LTspice

Category: Software
Product Number: LTspice
Software Version: 26.0.2

Hello to the community.

Please comment on the following use case. I have a design to simulate. And this design has a BAS70 diode in it. The standard library doesn't have it, so I decided to import a SPICE model found on the Net. It is not a problem. All I have to do is to put it in the "standard.dio" usually found in the "C:\Users\$USER_NAME$\Local\LTspice\lib\cmp" directory" (Windows 10).

The problem is I plan to share this design with other members of the team. So asking them to update their libraries is not that convenient. Instead I decided to put all the design-specific models in the same directory the schematic resides, like this:

  • $DESIGN_DIR$\design_to_simulate.asc — schematic to simulate;
  • $DESIGN_DIR$\llib\sym — symbols for the design-related parts;
  • $DESIGN_DIR$\llib\sub — subcircuits (models) for the design-related parts;

After that, it is enough to specify these "sym" and "sub" directories in the "Search Paths" tab of the settings, like presented in the figure below. Quite convenient. Per my taste, of course.

==========

This works fine for the relatively complex components, like "subcircuits": there is a "component.asy" in the "sym" directory and its model "component.sub" in the "sub". Connection is established using the "ModelFile" attribute.

==========

But here is a quirk. A diode is a pretty simple device, covered by a single .model SPICE directive. There is no need to create a subcircuit for it. So I decided to stick to the following approach:

  1. create a schematic symbol for the diode;
  2. name it BAS70;
  3. specify the model in the "SpiceModel" attribute;

It seemed to me the best and most straightforward approach: a self-contained symbol with no external dependencies.

==========

Unfortunately, it didn't work. Attempt to simulate a very basic schematic yields the following error:

Check on the netlist gives the following:

*                          \PowerOld\root.asc
* Generated by LTspice 26.0.2 for Windows.
V1 N001 0 SINE(0 12 50)
R1 N002 0 10K
D1 N001 N002 .model BAS70 D(IS=99.5p RS=0.600 BV=70.0 IBV=10.0u CJO=2.00p M=0.333 N=1.70 TT=7.20n) BAS70
.model D D
.lib C:\Users\    \AppData\Local\LTspice\lib\cmp\standard.dio
.tran 0.2
.backanno
.end

==========

One may see that the value of the attribute "SpiceMode" is attached immediately after the list of the nodes of the "D1" component. It is illegal for SPICE. The ".model" must be found somewhere in the code on a distinct line. I tried different combinations of the symbol's attributes, but neither gave an expected result.

It is possible, though, to overcome this limitation by placing the SPICE directive directly on the schematic, like this:

This way the netlist is correct and the simulation concludes.

* \PowerOld\root.asc
* Generated by LTspice 26.0.2 for Windows.
V1 N001 0 SINE(0 12 50)
R1 N002 0 10K
D1 N001 N002 BAS70
.model D D
.lib C:\Users\\AppData\Local\LTspice\lib\cmp\standard.dio
.tran 0.2
.model BAS70 D(IS=99.5p RS=0.600 BV=70.0 IBV=10.0u CJO=2.00p  M=0.333 N=1.70 TT=7.20n)
.backanno
.end

==========

So is this behavior expected? Or is it a bug? Or maybe I misunderstood the purpose of the "SpiceModel" field? Yes, it is possible to workaround this problem either by creating a subcircuit or placing the .model directly on the schematic. But both are less convenient than what I was expecting.

Thanks for your attention.

  • Hi  ,

    Or maybe I misunderstood the purpose of the "SpiceModel" field?

    You did (misunderstand), but that's not your fault. "spicemodel" is looking for a .subckt, not a .model. A .model statement, or any "dot" statement or directive must start on its own line, so below will fail:

    D1 N001 N002 .model BAS70 D(IS=99.5p RS=0.600 BV=70.0 IBV=10.0u CJO=2.00p M=0.333 N=1.70 TT=7.20n) BAS70

    The most portable method is to put the model right on the schematic, as you have done.

    This is what that looks like if you look at the attributes for the symbol:

    If you want to instead share the .model (not subcircuit) library, you can, using user.* files, which must be placed in the "User Files" directory. In this case, you need to create a text file called user.dio, and put it in your User Files as defined here:

    In that text file, place the .model statement for the BAS70:

    .model BAS70 D(IS=99.5p RS=0.600 BV=70.0 IBV=10.0u CJO=2.00p M=0.333 N=1.70 TT=7.20n)

    You may need to close and reopen your schematic at this point. Comment out the model statement on your schematic.

    Right-click the diode and click Pick New Diode. Your BAS70 should appear at the bottom of the list. Choose it and click OK.

    If you want to share the library, send it along with your schematic. 

    mike

  • Cool, that works! Thanks for the advice.

    Putting the ".model" directives might be the "most portable" option for sure. But it messes up the schematic itself. There is a plain netlist for this type of information. So the solution you proposed suits me better.

    =====

    And I have to say that this topic is poorly covered in the existing help/documentation. In particular, as you have already noticed, the "SpiceModel" and "ModelFile" attributes are not distinct enough, if you ask me. You said, that :

    >> "spicemodel" is looking for a .subckt, not a .model.

    But the "ModelFile" actually does the same.

    As an example. If I want to import the LM2903 comparator, I do the following:

    1. create the "$SOME_PATH/sym/LM2903.asy" symbol;
    2. create the "$SOME_PATH/sub/LM2903.sub", which contains the implementation of the LM2903 in the form of the ".subckt";
    3. update the "Symbol Search Paths" and "Simulation Library Search Paths" so they point to the "$SOME_PATH/sym/" and "$SOME_PATH/sub/" respectively;

    Some clarification or a few real-world examples would be welcomed to clear outline how to use the "SpiceModel" and "ModelFile" attributes correctly. And of course, the reference to the "Symbol and Library Search Paths" chapter so a newcomer would be able to quickly untangle this knot.

    =====

    Consider this message as more of feedback but complain. :-)

  • Hi  ,

    Some clarification or a few real-world examples would be welcomed to clear outline how to use the "SpiceModel" and "ModelFile" attributes correctly. And of course, the reference to the "Symbol and Library Search Paths" chapter so a newcomer would be able to quickly untangle this knot.

    No argument there. It could use some work. Check out the Help > FAQs > Third Party Models. 

    But the "ModelFile" actually does the same.

    Well, yes, and no. ModelFile is specifically for a .sub or .lib file name. This attribute produces a .lib statement in the netlist. This is editable in the symbol, but not on a schematic. Some libraries containing a number of subcircuits, the actual subckt line is called by SpiceModel or Value. Either works, but not both. A symbol's attributes (other than prefix, description, and ModelFile) are simply used to build the spiceline for the component. Not too fancy.

    As an example. If I want to import the LM2903 comparator, I do the following:

    1. create the "$SOME_PATH/sym/LM2903.asy" symbol;
    2. create the "$SOME_PATH/sub/LM2903.sub", which contains the implementation of the LM2903 in the form of the ".subckt";
    3. update the "Symbol Search Paths" and "Simulation Library Search Paths" so they point to the "$SOME_PATH/sym/" and "$SOME_PATH/sub/" respectively;

    Yes, that is one way. Note that for symbols, you simply need to define $SOME_PATH/  in Search Paths, as search is recursive (when you open a schematic or click Refresh in the Place Component dialog). In contrast, subcircuits do require the full path, as you state.

    If your intent is portability, I might suggest a slightly different path, using new features of 26.0.2. Below would be the complete method of creating the symbol for an LM2903.

    1. Download the LM2903B PSPICE library zip file and place it in the same directory as your schematic
    2. In LTspice, open the LM2903B.lib file that comes in the zip. (You will have to change options in the browse dialog to Show All)
    3. Open a Schematic or create a new one.
    4. Press P to Place Component
    5. Fnd the opamp2 symbol or any component that has s symbol you like
    6. Click Place
    7. Ctrl + Right-click the op amp or comparator symbol you have placed
    8. Click Open Symbol. The opamp2 symbol opens
    9. Right-click anywhere and choose View > Pin Table. You'll see that the pin order matches to what you want for your symbol, but the names could use a few changes. Let's save your new symbol first.
    10. Close the Pin Table and Choose File > Save As.
    11. Name the symbol lm2903b.sym and save in the same directory as the schematic and the LM2903  library
    12. Fix the pin names. This is actually not necessary, as pin order is all SPICE cares about, but it is helpful to us humans for debugging circuits later. Right-click the V- pin. Change the name to "GND". Right-click the V+ pin and change to "VCC".
    13. Right-click anywhere and Choose Attributes > Edit Attributes

    14. Value = LM2903B, Description = whatever you want, ModelFile = lm2903b.lib.
    15. Save the symbol.
    16. With your schematic open, press "P" for Place Component. The Place Component dialog appears.
    17. Click Refresh.
    18. Choose Schematic Directory from the drop down at the top.
    19. Choose your new symbol.
    20. Click Place.

    That's it. :-)

    Now, for portability, always send the symbol and library along with the schematic in the same directory. 

    That said, you could set up your local libraries as you wish, but remember to package up any files you would need. You can quickly check what symbols and libraries are used by simply Choosing View > Update and View SPICE Netlist.

    mike

  • No argument there. It could use some work. Check out the Help > FAQs > Third Party Models. 

    Yep, I've got it. Thanks for the clue. The concept of "user.*" libraries for the basic components is indeed mentioned there. Hope that someone will find this thread and stick to this approach.

    This attribute produces a .lib statement in the netlist.
    And that's a better explanation. I eventually dove into the "raw" SPICE syntax, so it is nice to know which LTspice's primitive/text fields correspond to which SPICE's directive. By the way, original SPICE3 3f3 documentation does not mention the ".lib" directive, only ".include". And LTspice's help index for some reasons doesn't list it as well, though it is described in the "LTspice Simulator/Dot Commands" chapter.
    Playing with the symbol attributes eventually yields this or that line of text in the netlistl, but they don't match well with the SPICE's concept. For instance, aside from already confusing "SpiceModel" and "ModelFile" attributes, there are also "SpiceLine" and "SpiceLine2and a single glance is not enough to figure out their purpose.
    I see that all these text are like positional arguments in the model/subcircuit instantiation. But again, not clear how exactly. 
    Of course, I'm not pretending to be an expert in all this stuff, so again, just a feedback. :-)
    If your intent is portability, I might suggest a slightly different path, using new features of 26.0.2.
    Ok, I will check on your suggestion a little later. Need first collect or created simplified models for the all components I need. Silicon manufacturers are not spoiling customers with them, you know. :-(
    ==========
    By the way, in regard to the ".lib" directive. Could you, probably, explain what does "section" syntax means?

    The path to the library is obvious. But what is the "entryname". The name of the component? Like "Take this only this component out of the library and put it into the netlist"? But what for?
  • Hi  ,

    And that's a better explanation. I eventually dove into the "raw" SPICE syntax, so it is nice to know which LTspice's primitive/text fields correspond to which SPICE's directive. By the way, original SPICE3 3f3 documentation does not mention the ".lib" directive, only ".include". And LTspice's help index for some reasons doesn't list it as well, though it is described in the "LTspice Simulator/Dot Commands" chapter.

    I believe LTspice has had the library directive for quite some time. .lib and .include are functionally equivalent, except for the section feature (below).

    And LTspice's help index for some reasons doesn't list it as well

    I should fix the index…

    I eventually dove into the "raw" SPICE syntax, so it is nice to know which LTspice's primitive/text fields correspond to which SPICE's directive.

    Yes, once you understand that the attributes are just building out a spiceline, it makes much more sense.

    ModelFile becomes .lib "Filename"

    Description is not used

    All other attributes are simply placed in line on the spice netlist

    Prefix§Instance [Nodes…] SpiceModel Value Spiceline1… 

    Typically, the actual model is put in the Value field, since that usually defaults to visible.

    Agreed the attribute names are non-ideal.

    By the way, in regard to the ".lib" directive. Could you, probably, explain what does "section" syntax means?

    The path to the library is obvious. But what is the "entryname". The name of the component? Like "Take this only this component out of the library and put it into the netlist"? But what for?

    You can have a library (section) within the library and only load that section. So, for instance, in the file called MyLibrary.lib you might have:

    ****** Sections in MyLibrary.lib *****

    **** section_1 ****

    .lib section_1

    subckt foo 1 2 3
    ....netlist
    ends

    subckt bar 1 2 3
    ....netlist
    ends

    .endl
    **** section_2 ****
    .lib section_2

    subckt foo 1 2 3
    ....other netlist
    ends

    subckt bar 1 2 3
    ....other netlist
    ends

    .endl

    In your schematic you would have:

    .lib "MyLibrary.lib" section_2

    This would only load the second library.

    Note that you can also use this syntax in a symbol's ModelFile attribute. Be sure to use quotes for the filename.

    mike

  • You can have a library (section) within the library and only load that section. So, for instance, in the file called MyLibrary.lib you might have:

    Good, I've got it.

    Probably this is a feature to reduce a netlist size? It would be nice to have this mentioned in the help. I mean a few words about the "section" syntax.

    In fact LtSpice's help is already quite good, don't get me wrong. I've got attached to the SPICE syntax exactly using it, though I never became a "true" hardware designer. :-) It just feels a little neglected and outdated. Somewhere there are the screenshots mentioning the LTspice XIV, or something like that.