sharpie7 / circuitjs1

Electronic Circuit Simulator in the Browser
GNU General Public License v2.0
2.29k stars 633 forks source link

Params in Subcircuit #623

Open Fvi589 opened 3 years ago

Fvi589 commented 3 years ago

Hi,

Would it be possible to be able to modify the values ​​of the passive elements contained in a subcircuit ?

Best regard Franck

pfalstad commented 3 years ago

Hmm, maybe the sliders could be converted to parameters when you create the subcircuit?

Fvi589 commented 3 years ago

yes it would be great, if by sliders, we could change the params

Fvi589 commented 3 years ago

If we put switches in the subcircuit, could we activate them by a slider or other, in the right bar? Example: circuit breaker ON / OFF

Best regard

pfalstad commented 3 years ago

I was thinking that sliders would just mark the parameters. If you use the subcircuit, there wouldn't actually be sliders. You would change the parameters using the component's edit menu. Of course if you wanted sliders, you could add some using the Sliders... dialog, but normally you wouldn't.

I also wasn't thinking that there would be any on/off choices. But actually, that is another idea. Maybe we could use switches instead (or in addition) to mark the parameters. Any switch in the subcircuit, can be turned on and off using the edit menu. Not with a slider in the right bar, though.

nulldg commented 3 years ago

I like this idea, but I wouldn't limit it to passive elements. Plus, the behavior should be documented, otherwise very few will know this feature exists, let alone will know how to use it effectively.

Fvi589 commented 3 years ago

I made a very simple example just to illustrate. My idea is from the subcircuit, which I will use in another assembly, to be able to change the value of R by slider, but also to be able to have access to the switch through the subcircuit, without having to edit the subcircuit. I do not know if I'm clear, do not hesitate to tell me.

$ 1 0.000005 10.20027730826997 50 5 43 5e-11 r 128 160 240 160 0 1000 s 240 160 368 160 0 1 false 207 368 160 464 160 4 B2 207 128 160 48 160 4 B1 . Essai 0 2 1 2 B1 1 0 2 B2 3 0 3 ResistorElm\s1\s2\rSwitchElm\s2\s3 0\s1000\s0\s1\sfalse 410 192 272 320 272 1 Essai 0\s1000 0\s1\sfalse

ormaaj commented 3 years ago

I thought about maybe reusing the slider code for this but actually I don't think that's good enough to handle all of the requirements for subcircuits.

I am considering adding something like the spice bracket expansion syntax in such a way that one can evaluate a controlled source expression and have the result transcluded into any input field of any component. For each pin / label of a subcircuit there would be a corresponding field accepting the same optionally eval-able expression syntax.

There are a few prerequisites for this:

  1. A way to reference an arbitrary node by name.
  2. A user-defined .param equivalent for use in an expression which accepts an expression in its definition. Spice doesn't allow recursive parameters but instead has .func for that (but .func isn't actually very useful). I'll have to think about how to deal with that.
  3. Since subcircuits include components with .model-like definitions, the expression passed to a subcircuit argument / "pin" needs to be referencable from a component's model definition.
  4. Extend the function of the "labels" so that they can be referred to as variables with a v(label) workalike rather than using [VC]C[VC]S pin names as variables. v(var [, var]) is also useful for direct "casting" of arbitrary values or currents. Right now you have to use the VCCS and CCVS to do various conversions, potentially coupling several of each type to mutually reference each other's pins.
  5. i(vsource). Being able to refer to the current going into any pin might be more useful than the spice way using voltage sources. I might try to think of a way to use labels for that too, you just need a way to define the polarity. Though a nice thing about i(vsource) is it sort of enforces consistent psc use and makes "unconventional" sign conventions conspicuous.
pfalstad commented 3 years ago

People want the slider features to be improved as well (see #504). If the slider code isn't good enough we could make it better.

Right now we can take any parameter and link it directly to a slider. We could instead have (1) a list of sliders with labels, variable names, and min/max values; and (2) allow any parameter to be replaced by an expression that references the sliders (using their variable names). (2) would be done using a UI similar to what we have now, for sliders.

The same could be done for subcircuits.

It's a separate issue, but it also might be a good idea to be able to create voltage/current sensor elements which could be a linked to a variable that could be referenced in any [VC]C[VC]S expression. People complain about the [VC]C[VC]S's as well, because they don't look anything like standard symbols used for controlled sources (#479), and that would fix that problem.

ormaaj commented 3 years ago

True making the sliders programmable might well accomplish the same, I like that idea. Also easy since they already have the necessary hooks into the right places.

Also obvious feature - a way to toggle continuous evaluation and "eval once" fixed constants.