Hello!
Ref number in "name" of each symbol is automatically increased when placed onto schematic.
So the question is - is it possible to attach such capability to others symbol properties?
The idea is to use it with "device_model" property to create unique model with each placed symbol.
Thank you in advance!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi, George,
I have pushed a simple xschem commit that allows to 'uniquify' device models per instance.
If you have a symbol instance named 'M1' (or X1) and add to the instance the following attribute: device_model=".model nfet@name NMOS ... "
or: device_model=".subckt ampli@name ..."
in the netlist you get: .model nfetM1 ...
or .subckt ampliX1 ...
Since @nameattribute is unique in the design so will be the spice models.
This commit makes the @ character somewhat special (it is used for @var substitution). If you have spice models that use the @ character you must use \\@ to escape it. (two backslashes are needed)
Last edit: stefan 2024-01-20
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
So it replace @name in device_model attribute, but doesn't do it for instance.
Maybe I did something wrong in symbol definition?
Definition is (from my tcl script generation):
You are right I need to do the translation also on the @model. This is tricky since when netlisting the format attribute of the symbol contains @model, then model is retrieved from the instance but model contains in turn @name so a nested translation must be done. Will work on it.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you. The workaround is ok since it removes one level of indirection (put strans@name directly inside the format string instead of @model).
You can now use the original way (model=strans@name in instance and use @model in symbol format string).
👍
1
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This is a neat way to get things like mismatch without a full
fancy Monte Carlo infrastructure.
I would like to ask for a companion feature, which is for each
"uniquified" line to be echoed to a "side-file" so that all of the
auto-generated models are known / listed, for subsequent uses
(like editing that list down to a set of model params to be
exercised in a simulation, which might be a small subset).
I'd also like to point out that some of the things that a
"uniquified" model card would want to mess with, might end up
inside curly braces or other forms which might be weird or need
escape characters to pass cleanly into SPICE format?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
No escapes are needed for characters other than @, backslash and quote (") .
quote characters must be single escaped, because the device_model attribute itself is enclosed into quotes : device_model="this is a random string with a quoted \"word\" present"
Consider thsi example:
device_model="this is a string with escaped \\@ \"at\" character, the following unescaped @name will cause name to be substituted"
when the above attribute is retrieved by xschem (extracted from the surrounding quotes) it becomes:
this is a string with escaped \@ "at" character, the following unescaped @name will cause name to be substituted
When substitution is done (assuming name=M1) it becomes:
this is a string with escaped @ "at" character, the following unescaped M1 will cause name to be substituted
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I would like to ask for a companion feature, which is for each
"uniquified" line to be echoed to a "side-file" so that all of the
auto-generated models are known / listed, for subsequent uses
(like editing that list down to a set of model params to be
exercised in a simulation, which might be a small subset).
In this case I think it is best to write the models directly into an external file, and add a .include models.cir
somewhere in the simulation deck.
So instances will only specify model=something@name and the corresponding definition is in models.cir, like:
.......modelsomethingM1......
if on the other hand you want to have everything in the schematic you can run a small script to dump in a file all the used models:
# get given attribute from all instances in current schematic.# if a filename is given like:# model_list device_model outfile# all results are written in 'outfile' otherwise the result # is simply returned.procmodel_list{{attr{}}{filename{}}}{if{$attreq{}}{return{}}setattrlist{}## i = instname, s = symbol, t = symbol typeforeach{ist}[xscheminstance_list]{# get 'attr' attribute from instancesetattrval[xschemgetpropinstance$i$attr]# replace @var with var=xxx given in instancesettranslated[xschemtranslate$i$attrval]if{$translatedne{}}{appendattrlist$translated\n}}# if a file is given write result into that fileif{$filenamene{}}{setfd[open$filenamew]puts$fd$attrlistclose$fd}return$attrlist}
so calling from xschem:
model_list device_model models.cir
will write all found device_model attributes into models.cir
Last edit: stefan 2024-01-21
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
That script looks useful to me. A question - I've seen multiple times some scripts offered as solutions, are these all saved in a library somewhere, with info, to go browse? Like. I might be looking for this exact thing a couple months from now, but forget it even existed let alone which thread.
Seems like good work that should be kept around where people can find it.
A few words on how users would best arrange local (or access ones from installation "script lib") script files, maybe also on adding such to a menu button or pulldown list, I think would be good too.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello!
I found an issue - the behaviour of device_model property if such device existed in subcircuit hierarchy. In that case it put corresponding .model statement at top level and it could create conflicts if somewhere the symbol with the same name exists.
is it possible to substitute full hierarchy name of symbol in @name when @name appears in model or device_mode attribute, or, the second way is to put .model statements from device_model attribute in corresponding .subckt?
Hi, George,
Unzip the attached file, it creates a auto_name_model directory.
run xschem from there: xschem top.sch
The approach here is to create one model per placed instance, immediately after the instance, so not at the top of hierarchy:
The model is embedded in the symbol, so once the symbol is done no more work is required. Another designer placing the symbol will get the correct model.
The model travels with the symbol, so there is no risk of linking the wrong model definition.
the device_model attribute placed in the symbol works a bit differently and puts the model definition at global scope, but as you said there is the risk of name collisions even if using @name (you can have the same @name at different hierarchies).
A device_model avoids dumping the same model multiple times, it prints a given model name only once. You can thus have a symbol with a model attached to it , use this symbol multiple times in different hierarchies and get only one .model definition in the netlist. This is fine if you don't need to differentiate these model defs.
Using a hierarchy path path (like for example @path@name) to get a unique and fully qualified name does not work.
A hierarchical circuit netlist is not created by traversing the hierarchy. Instead each sub block is expanded only once,
Important fact, when xschem is netlisting a sub block it does not know from where this block was instantiated. It just expand the sub block.
Xschem netlists each element of the top level first. For any 'X' call (subcircuit) the corresponding sub circuit is added to a global list. If the subcircuit is already in the list nothing is added
After the top level is done xschem picks the first subcircuit in the list. .
When expanding the sub block it works exactly as with the top level. Print all instance lines and for any X subckt calls add the symbol to the list if not already there. So the list grows while netlisting.
At the end all .subckts are done (once) and netlist is complete.
Some SPICE devices have model parameters that can be specified at instance level. In this case you don't need multiple in-subckt models, you override parameters at instance level.
Example (this does not work because BJT devices can not override model parameters, but just to give the idea):
Also, could I attach subcircuit to symbol?
The issue with subckt approach is that I have tcl scripts that produces symbols with arbitary number of pins, and in this case I have to generate subcircuit with corresponding number of pins.
UPD yes I see it works the same as with .model, so the issue with referencing to the same .subckt multiple devices with the same name in different hierarchies still persist, ... or?
UPD2 but we can provide different parameters to subcircuit call for the same subcircuit definition! then problem solved
Last edit: George 2024-02-11
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yeah, but I faced another issue - ngspice doesn't allow to send vector parameters to subcircuit that is used in xspice:
gain=[0 1 1 0]
something like this
there is workaround like this:
gain=[{gain1} {gain2} {gain3} {gain4}] but it is super awkward
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello!
Ref number in "name" of each symbol is automatically increased when placed onto schematic.
So the question is - is it possible to attach such capability to others symbol properties?
The idea is to use it with "device_model" property to create unique model with each placed symbol.
Thank you in advance!
Hi, George,
I have pushed a simple xschem commit that allows to 'uniquify' device models per instance.
If you have a symbol instance named 'M1' (or X1) and add to the instance the following attribute:
device_model=".model nfet@name NMOS ... "
or:
device_model=".subckt ampli@name ..."
in the netlist you get:
.model nfetM1 ...
or
.subckt ampliX1 ...
Since
@name
attribute is unique in the design so will be the spice models.This commit makes the @ character somewhat special (it is used for @var substitution). If you have spice models that use the @ character you must use
\\@
to escape it. (two backslashes are needed)Last edit: stefan 2024-01-20
Wow, great, that is what I needed, thank you a lot!
I tried to test the feature, but after netlisting I get:
So it replace @name in device_model attribute, but doesn't do it for instance.
Maybe I did something wrong in symbol definition?
Definition is (from my tcl script generation):
You are right I need to do the translation also on the @model. This is tricky since when netlisting the
format
attribute of the symbol contains@model
, then model is retrieved from the instance but model contains in turn@name
so a nested translation must be done. Will work on it.I found a workaround:
Put strans@name directly instead of the model, it works, thank you!
George I think it should now work, Can you please test?
Yes with the latest commit it works, thank you very much!
Thank you. The workaround is ok since it removes one level of indirection (put strans@name directly inside the format string instead of @model).
You can now use the original way (model=strans@name in instance and use @model in symbol format string).
This is a neat way to get things like mismatch without a full
fancy Monte Carlo infrastructure.
I would like to ask for a companion feature, which is for each
"uniquified" line to be echoed to a "side-file" so that all of the
auto-generated models are known / listed, for subsequent uses
(like editing that list down to a set of model params to be
exercised in a simulation, which might be a small subset).
I'd also like to point out that some of the things that a
"uniquified" model card would want to mess with, might end up
inside curly braces or other forms which might be weird or need
escape characters to pass cleanly into SPICE format?
No escapes are needed for characters other than @, backslash and quote (") .
quote characters must be single escaped, because the
device_model
attribute itself is enclosed into quotes :device_model="this is a random string with a quoted \"word\" present"
Consider thsi example:
device_model="this is a string with escaped \\@ \"at\" character, the following unescaped @name will cause name to be substituted"
when the above attribute is retrieved by xschem (extracted from the surrounding quotes) it becomes:
this is a string with escaped \@ "at" character, the following unescaped @name will cause name to be substituted
When substitution is done (assuming name=M1) it becomes:
this is a string with escaped @ "at" character, the following unescaped M1 will cause name to be substituted
In this case I think it is best to write the models directly into an external file, and add a
.include models.cir
somewhere in the simulation deck.
So instances will only specify
model=something@name
and the corresponding definition is inmodels.cir
, like:if on the other hand you want to have everything in the schematic you can run a small script to dump in a file all the used models:
so calling from xschem:
will write all found device_model attributes into models.cir
Last edit: stefan 2024-01-21
TL; DR:
To get a per-instance model use this syntax in an instance attribute list:
in the netlist you will get:
That script looks useful to me. A question - I've seen multiple times some scripts offered as solutions, are these all saved in a library somewhere, with info, to go browse? Like. I might be looking for this exact thing a couple months from now, but forget it even existed let alone which thread.
Seems like good work that should be kept around where people can find it.
A few words on how users would best arrange local (or access ones from installation "script lib") script files, maybe also on adding such to a menu button or pulldown list, I think would be good too.
I agree I should commit these scripts that can be used by power users or modified to their needs. I will do that soon.
Hello!
I found an issue - the behaviour of device_model property if such device existed in subcircuit hierarchy. In that case it put corresponding .model statement at top level and it could create conflicts if somewhere the symbol with the same name exists.
is it possible to substitute full hierarchy name of symbol in @name when @name appears in model or device_mode attribute, or, the second way is to put .model statements from device_model attribute in corresponding .subckt?
Thank you in advance
Hi, George,
Unzip the attached file, it creates a
auto_name_model
directory.run xschem from there:
xschem top.sch
The approach here is to create one model per placed instance, immediately after the instance, so not at the top of hierarchy:
Symbol attributes are given as follows:
Last edit: stefan 2024-02-10
So, the only way for now is place each model inside the subcircuit?
It is quite tedious but that's ok, thank you
The model is embedded in the symbol, so once the symbol is done no more work is required. Another designer placing the symbol will get the correct model.
The model travels with the symbol, so there is no risk of linking the wrong model definition.
the
device_model
attribute placed in the symbol works a bit differently and puts the model definition at global scope, but as you said there is the risk of name collisions even if using @name (you can have the same @name at different hierarchies).A
device_model
avoids dumping the same model multiple times, it prints a given model name only once. You can thus have a symbol with a model attached to it , use this symbol multiple times in different hierarchies and get only one .model definition in the netlist. This is fine if you don't need to differentiate these model defs.Using a hierarchy path path (like for example @path@name) to get a unique and fully qualified name does not work.
A hierarchical circuit netlist is not created by traversing the hierarchy. Instead each sub block is expanded only once,
Important fact, when xschem is netlisting a sub block it does not know from where this block was instantiated. It just expand the sub block.
Xschem netlists each element of the top level first. For any 'X' call (subcircuit) the corresponding sub circuit is added to a global list. If the subcircuit is already in the list nothing is added
After the top level is done xschem picks the first subcircuit in the list. .
When expanding the sub block it works exactly as with the top level. Print all instance lines and for any X subckt calls add the symbol to the list if not already there. So the list grows while netlisting.
At the end all .subckts are done (once) and netlist is complete.
Some SPICE devices have model parameters that can be specified at instance level. In this case you don't need multiple in-subckt models, you override parameters at instance level.
Example (this does not work because BJT devices can not override model parameters, but just to give the idea):
Note: I am not sure if other simulators (like Xyce) allow
.model
lines to be placed inside.subckt
blocks.Ngspice allows that gracefully.
[EDIT] I have verified .model inside subcircuits works for Xyce too.
Last edit: stefan 2024-02-11
thank you for clarification, I will use subcurcuit for encapsulation of devices.
thanks you for clarification, I will subcircuit for model encapsulation.
Also, could I attach subcircuit to symbol?
The issue with subckt approach is that I have tcl scripts that produces symbols with arbitary number of pins, and in this case I have to generate subcircuit with corresponding number of pins.
UPD yes I see it works the same as with .model, so the issue with referencing to the same .subckt multiple devices with the same name in different hierarchies still persist, ... or?
UPD2 but we can provide different parameters to subcircuit call for the same subcircuit definition! then problem solved
Last edit: George 2024-02-11
Yes you can attach subcircuits. Just replace .model with .subckt (add
.ends at the end)
If you need a different subcircuit for every instance add a .subckt
(using @name to avoid collisions) in the format string.
Otherwise if you want one .subckt for all use device_model as you do
with .model.
In general subcircuits allow parameters passing so it is advisable to
use just one subckt for all instances present in a design.
If a symbol has a device_model attribute its type should not be subcircuit
Yeah, but I faced another issue - ngspice doesn't allow to send vector parameters to subcircuit that is used in xspice:
gain=[0 1 1 0]
something like this
there is workaround like this:
gain=[{gain1} {gain2} {gain3} {gain4}] but it is super awkward
Try to double quote args with brackets:
Gain="[...]"
But just guessing...