Menu

Automatic increasing numbers in symbol properties

George
2024-01-20
2024-04-12
1 2 > >> (Page 1 of 2)
  • George

    George - 2024-01-20

    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!

     
  • stefan

    stefan - 2024-01-20

    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
    • George

      George - 2024-01-20

      Wow, great, that is what I needed, thank you a lot!

       
    • George

      George - 2024-01-20

      I tried to test the feature, but after netlisting I get:

      A2 net1 net2 strans@name
      .model stransA2 s_xfer(gain=1.0 int_ic=[0.0 0.0 0.0] num_coeff=[0.1148] den_coeff=[1 0.01 39.4366])
      

      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):

      K {type=xspice_dig format=\"@name $pinStr  @@out @model \" 
      template=\"name=A1 model=strans@name device_model=\\\\\".model strans@name s_xfer(gain=1.0 int_ic=\[$ic\] num_coeff=\[$num1\] den_coeff=\[$den1\])\\\\\" \"}
      
       
      • stefan

        stefan - 2024-01-20

        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.

         
        • George

          George - 2024-01-20

          I found a workaround:

          K {type=xspice_dig format=\"@name $pinStr  @@out strans@name \" 
          template=\"name=A1 model=strans@name device_model=\\\\\".model strans@name s_xfer(gain=1.0 int_ic=\[$ic\] num_coeff=\[$num1\] den_coeff=\[$den1\])\\\\\" \"}
          

          Put strans@name directly instead of the model, it works, thank you!

           
      • stefan

        stefan - 2024-01-20

        George I think it should now work, Can you please test?

         
        • George

          George - 2024-01-20

          Yes with the latest commit it works, thank you very much!

           
          • stefan

            stefan - 2024-01-20

            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
  • dick freebird

    dick freebird - 2024-01-20

    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?

     
    • stefan

      stefan - 2024-01-20

      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

       
    • stefan

      stefan - 2024-01-21

      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:

      ...
      ...
      .model somethingM1 ...
      ...
      

      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.
      proc model_list {{attr {}} {filename {}}} {
        if {$attr eq {}} { return {}}
        set attrlist {}
        ## i = instname, s = symbol, t = symbol type
        foreach {i s t} [xschem instance_list] {
          # get 'attr' attribute from instance
          set attrval [xschem getprop instance $i $attr]
          # replace @var with var=xxx given in instance
          set translated [xschem translate $i $attrval]
          if {$translated ne {}} {
            append attrlist $translated \n
          }
        }
        # if a file is given write result into that file
        if {$filename ne {}} {
          set fd [open $filename w]
          puts $fd $attrlist
          close $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
  • stefan

    stefan - 2024-01-20

    TL; DR:

    To get a per-instance model use this syntax in an instance attribute list:

    name=M1
    model=nfet@name
    device_model=".model nfet@name nmos ... ... ...  "
    

    in the netlist you will get:

    ...
    M1 drain gate source body nfetM1 W=... L=...
    ...
    ...
    .model nfetM1 nmos ... ... ...
    
     
  • dick freebird

    dick freebird - 2024-01-27

    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.

     
    • stefan

      stefan - 2024-01-27

      I agree I should commit these scripts that can be used by power users or modified to their needs. I will do that soon.

       
  • George

    George - 2024-02-10

    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

     
  • stefan

    stefan - 2024-02-10

    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:

    * sch_path: /home/schippes/.xschem/xschem_library/auto_name_model/top.sch
    **.subckt top
    x1 XBB XAA XCC inertia
    N1 XAA XBB XCC inertiaN1
    .model inertiaN1 inertia_omega_tau J=1.2
    **.ends
    
    * expanding   symbol:  inertia.sym # of pins=3
    ** sym_path: /home/schippes/.xschem/xschem_library/auto_name_model/inertia.sym
    ** sch_path: /home/schippes/.xschem/xschem_library/auto_name_model/inertia.sch
    .subckt inertia BB AA CC
    *.iopin AA
    *.iopin BB
    *.iopin CC
    x1 BB AA CC visible_model_params
    .ends
    
    
    * expanding   symbol:  visible_model_params.sym # of pins=3
    ** sym_path: /home/schippes/.xschem/xschem_library/auto_name_model/visible_model_params.sym
    ** sch_path: /home/schippes/.xschem/xschem_library/auto_name_model/visible_model_params.sch
    .subckt visible_model_params B A C
    *.iopin A
    *.iopin B
    *.iopin C
    N1 A B C inertiaN1
    .model inertiaN1 inertia_omega_tau J=1.4
    .ends
    .end
    

    Symbol attributes are given as follows:

    type=mechanical_rotational
    format="@name @pinlist inertia@name
    .model inertia@name inertia_omega_tau J=@J"
    template="name=N1 J=1.1"
    
     

    Last edit: stefan 2024-02-10
    • George

      George - 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

       
      • stefan

        stefan - 2024-02-11

        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):

        Q1 c  b  e  BC846 Is=1e-17 BF=100
        Q2 c1 b1 e1 BC846 Is=5e-17 BF=80
        
        .model BC846 NPN IS=1e-16 BF=200  VAF=73.4 IKF=0.0962 ...
        

        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
        • Anonymous

          Anonymous - 2024-02-11

          thank you for clarification, I will use subcurcuit for encapsulation of devices.

           
        • George

          George - 2024-02-11

          thanks you for clarification, I will subcircuit for model encapsulation.

           
  • George

    George - 2024-02-11

    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
    • stefan

      stefan - 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

       
      • George

        George - 2024-02-11

        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

         
        • stefan

          stefan - 2024-02-11

          Try to double quote args with brackets:
          Gain="[...]"
          But just guessing...

           
1 2 > >> (Page 1 of 2)

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.