netlistsvg icon indicating copy to clipboard operation
netlistsvg copied to clipboard

WIP: Auto orient analog components

Open kasbah opened this issue 6 years ago • 15 comments

Just experimenting a bit with auto-orientation. So far only with resistors in the analog examples. It's a brute force approach, going through all the different combinations and I am trying to find a good metric. Everything just seems to favor complete vertical orientation so far.

image

Minimizing bendPoints seems like it could be a good metric and would allow for some horizontal components if it wasn't so awkward at placing them. E.g. for the common_emitter example this could be a total of 3 bendPoints (1 less than when both resistors are vertical) if the vin chain to the base of the transistor was straight:

image

kasbah avatar Jun 04 '18 15:06 kasbah

I just found out about postCompaction and set it to 4 which I believe is LEFT_RIGHT_CONNECTION_LOCKING. All the strategies seem to improve all graph layouts in a similar way but I found some edges crossing a node in one case when using EDGE_LENGTH. Here is the common_emitter with the base resistor horizontal again. It doesn't actually impact the bendPoints count though. I guess those are not joined/removed.

image

kasbah avatar Jun 04 '18 18:06 kasbah

I've implemented my own way of counting the bendPoints (ignoring any bendPoints that are on straight edges) and that seems to select good graphs. It's favoring having as many vertical resistors as possible while minimizing bendPoints. I also made it discount bendPoints that are going to output labels as that seems to help with the examples I have.

image

kasbah avatar Jun 04 '18 20:06 kasbah

That invokes netlistsvg 2^(two-port) times. As I said I'm not sure all users prefer that to specifying the direction manually.

Is there a reason this can't be performed in replicad?

dvc94ch avatar Jun 04 '18 21:06 dvc94ch

Yes, I am making no claims of great efficiency. It seems to at least be bearable for the examples. Nitpick: if added here it would invoke elkjs.layout 2^(n two-port components) times, and elkjs does seem to be pretty darn speedy.

Anyway, for the time being I am just playing around. If anything is added here it should be behind an option as to not impact other use cases. If it's better to put it into replicad then I'll put it there but there is nothing replicad specific at all to what I am doing.

postCompaction should definitely be the default as it improves all example layouts, I'll do a separate PR for that.

Just trying the mcu example.

before:

after: image

kasbah avatar Jun 04 '18 21:06 kasbah

@dvc94ch how did you get the pycircuit common_emitter to look like that in the readme?

When I run the example with current netlistsvg I get:

kasbah avatar Jun 05 '18 19:06 kasbah

Not sure, not too many commits have been made, see which one caused the change... It still can be flackey sometimes even with the correct ports and orientations...

dvc94ch avatar Jun 05 '18 20:06 dvc94ch

Ah thanks, yes, looks like it was the addition of lateral tags with f72ac93bf48f8427cb0b4b7c7c0c39e1d95a4e67. What do you mean by flakey? Just a bit odd looking schematics sometimes?

kasbah avatar Jun 05 '18 21:06 kasbah

Whoa. Post compaction looks awesome! I need to try that. That might solve a lot of the issues with the ELK layouts that I was running into.

nturley avatar Jun 05 '18 21:06 nturley

This capacitor is backward.

image

Reverse it and it should look much better.

nturley avatar Jun 05 '18 21:06 nturley

Not much better unfortunately.

@@ -19,16 +19,16 @@
         },
         "C2": {
           "connections": {
-            "A": [
+            "B": [
               70
             ],
-            "B": [
+            "A": [
               10
             ]
           },
           "port_directions": {
-            "A": "input",
-            "B": "output"
+            "B": "input",
+            "A": "output"
           },
           "type": "c_h"
         },

I would be interested to try and force input labels to be all the way to the left.

kasbah avatar Jun 05 '18 21:06 kasbah

Rebased this onto #42 and it makes the mcu example almost perfect:

image

Full common emitter is better too, though you can see some of that weirdness mentioned in #42 going on in some variations.

image

kasbah avatar Jun 06 '18 00:06 kasbah

Today I tried to feed it this Charlieplexing netlist.

image

It doesn't do so well with any orientation unfortunately.

image

kasbah avatar Jun 13 '18 19:06 kasbah

For my circuits I found that the vertical capacitors works best. Most capacitors were decoupling, therefore one or both of the ends were going to a GND or a VCC. Horizontal capacitors were rare, the only places I can think of is:

  • series capacitors in filters
  • those AC coupling caps in the class A amplifier example

Similarly with resistors: pull-ups / pull-downs, if one or more pins on that resistor goes to GND or VCC, make it vertical. Though there are more places where I use horizontal resistors and regret them being so derpy due to lack of port swapping:

  • 0 ohm resistors connecting 2 signals
  • current measuring shunts
  • series termination

I'm not saying that auto orienting wouldn't be cool, but it's not the end of the world to not have it so far.

PS: Re https://github.com/nturley/netlistsvg/pull/40#issuecomment-397060391 The ideal would be for the diodes to be vertical and resistors to be horizontal. So ignoring auto orienting logic, does netlistsvg yield something compact? If not then your bend minimizing logic won't really detect this ideal orientation as optimal (so it shouldn't really be blamed).

amstan avatar Apr 13 '19 07:04 amstan

Yep, the charlieplexing example looks pretty horrific even with directions hardcoded:

bad

After some port swapping (R3 was backwards, hooking up even numbered leds backwards since we don't support 180 rotation yet):

port_swapping

Looks a little better to me as a human, but there are a few bends in there, probably because of other elk layout choices, so I'm still not sure your bend metric would have picked this one above the first picture.

Regardless, I think the input to your combination trials shouldn't just be a matter of is it "_v" or "_h", you also need to consider the 180 deg rotation, that will essentially solve port swapping too.

amstan avatar Apr 13 '19 07:04 amstan