I'm trying to create a graph like this automatically with edges given and need some help. The graph doesn't need to be exactly same but I would like to make some constraints for the generated graph.
This is an example of edges of an electrical graph. And all the graphs have at most two voltage sources V1, V2 and some components may exist or not.
edges = {V1n <-> V2n, V1p <-> R1p, R1n <-> R3p, R1n <-> R2p,
R3n <-> R4p, R3n <-> V2p, R4n <-> V2n, R2n <-> V1n};
The graph above is usually drawn like below as an electrical circuit but the style is complicated so it's not necessary to make it as below.
Here are some constraints:
- If voltage source
V1
orV2
exists thenV1
andV2
should be drawn vertically on the left and right as in the figure. - Other vertices other than
V1p, V1n, V2p, V2n
should be drawn in the region betweenV1
andV2
as an example image. - Vertices associated the same components
{V1p, V1n}, {V2p,V2n}, {R1p, R1n}, {R2p, R2n}, {R3p, R3n}, {R4p, R4n}
should be placed next to each other. It would be nice if the edge length between vertices in each pair above are same as well (just to make the symbols same size and look nicer- so it's not required if it's complicated).
Here are some examples of expected graph (or something similar, no need to be exactly same) from given edges:
edges = {V1n <-> V2n, V1p <-> R1p, R1n <-> R3p, R1n <-> R2p,
R3n <-> R4p, R3n <-> V2p, R4n <-> V2n, R2n <-> V2n};
Graph with V1
removed:
edges1 = {R1n <-> R3p, R1n <-> R2p, R3n <-> R4p, R3n <-> V2p, R4n <-> V2n};
Graph with V2
removed:
edges2= {V1p <-> R1p, R1n <-> R3p, R1n <-> R2p, R3n <-> R4p};
Graph with both V1
and V2
removed:
edges3= {R1n <-> R3p, R1n <-> R2p, R3n <-> R4p};
I have tried this (with help from my previous post) but got stuck on how to make graph with constraints above.
resistor[{a1_, a2_}] :=
Block[{d, l, res, s, t}, d = a2 - a1; l = Norm[d];
s = a1*(1 - 2/5) + a2*(2/5); t = a1*(1 - 3/5) + a2*(3/5);
res =
Line[Table[{Norm[t - s]*(i/16), (1/(6*l))*Sin[i*(Pi/2)]}, {i, 0,
16}]];
res =
GeometricTransformation[res,
TranslationTransform[s] @* RotationTransform[ArcTan @@ d]];
{Thick, Darker[Green], Line[{{a1, s}, {t, a2}}], res}]
dc[{a1_, a2_}] :=
Block[{d, l, res, s, t, r}, d = a2 - a1; l = Norm[d];
s = a1*(1 - 2/5) + a2*(2/5); t = a1*(1 - 3/5) + a2*(3/5);
r = Norm[s - t]/2;
res = {Circle[{r, 0}, r], Line[{{2*(r/4), r/4}, {2*(r/4), -r/4}}],
Line[{{{r/3 + r, r/4}, {r/3 + r, -r/4}}, {{r/3 + r - r/4,
0}, {r/3 + r + r/4, 0}}}]};
res =
GeometricTransformation[res,
TranslationTransform[s] @* RotationTransform[ArcTan @@ d]];
{Thick, Darker[Green], Line[{{a1, s}, {t, a2}}], res}]
edges = {V1n <-> V2n, V1p <-> R1p, R1n <-> R3p, R1n <-> R2p,
R3n <-> R4p, R3n <-> V2p, R4n <-> V2n, R2n <-> V1n};
coords = GraphEmbedding[edges];
vertexToCoords[{v1_,
v2_}] := {Position[VertexList[edges], v1][[1, 1]],
Position[VertexList[edges], v2][[1, 1]]};
Graph[edges,
Prolog -> {resistor[coords[[vertexToCoords[{R1p, R1n}]]]],
resistor[coords[[vertexToCoords[{R2p, R2n}]]]],
resistor[coords[[vertexToCoords[{R3p, R3n}]]]],
resistor[coords[[vertexToCoords[{R4p, R4n}]]]],
dc[coords[[vertexToCoords[{V1p, V1n}]]]],
dc[coords[[vertexToCoords[{V1p, V1n}]]]],
dc[coords[[vertexToCoords[{V2p, V2n}]]]]}, VertexLabels -> "Name"]
There are some problems with the graph:
V1
andV2
are not placed vertically on the left and right.- Vertices of the same components are not placed near each other. For example
{R2p, R2n}
or{R4p, R4n}
.