pub struct Network(/* private fields */);
Expand description
A network which is valid for performing mesh and nodal analysis.
This struct represents a network which can be solved via MeshAnalysis
and
NodalAnalysis
. It is essentially a
newtype
wrapper around a
UnGraph<usize, Excitation>
which forms a valid electrical circuit. Please see BuildError
for
all the possible ways a graph can fail to represent a valid electrical circuit.
§Sign conventions
The sign of an edge is oriented from source to target. If the following network has a current source at edge 0 and resistances everywhere else and all edges are oriented clockwise (e.g source of edge 0 is at edge 3 and target of edge 0 is at edge 1), an input of -5 at the current source results in an edge current of -5 over all edges.
┌──[1]──┐
| │ │
5| [0] [2]
V │ │
└──[3]──┘
Likewise, if edge 2 is “flipped” (source at edge 3, target at edge 1), its edge current would be +5.
If edge 0 is a voltage source with value +3 instead (i.e high potential at the source, low potential at the target) and all resistances have the value 1 (resistances must always be positive), the current going through all edges would be -1 (since the voltage drop must be -1 over all resistances so the entire loop adds up to a voltage of 0).
┌──[1]──┐
^ │ │
3| [0] [2]
| │ │
└──[3]──┘
§Constructing a Network
The following constructor methods are available:
Network::new
(using a graph)Network::from_node_edges
(using a slice ofNodeEdge
)Network::from_edge_list_edges
(using a slice ofEdgeListEdge
)
Please see the docstrings of the methods for examples.
§Serialization and deserialization
This struct serializes into a Vec<NodeEdge>
and can be (fallible) deserialized from the following types:
Vec<NodeEdge>
Vec<EdgeListEdge>
UnGraph<usize, Type>
Available when the feature serde is enabled.
Implementations§
Source§impl Network
impl Network
Sourcepub fn new(graph: UnGraph<usize, Type>) -> Result<Self, BuildError>
pub fn new(graph: UnGraph<usize, Type>) -> Result<Self, BuildError>
A graph is a valid network if it fulfills the conditions outlined in the docstring of Network
.
The edge weights define whether the edge is a resistance, a current source or a voltage source.
Sourcepub fn from_node_edges(edges: &[NodeEdge]) -> Result<Self, BuildError>
pub fn from_node_edges(edges: &[NodeEdge]) -> Result<Self, BuildError>
Sourcepub fn from_edge_list_edges(
edge_list_edges: &[EdgeListEdge],
) -> Result<Self, BuildError>
pub fn from_edge_list_edges( edge_list_edges: &[EdgeListEdge], ) -> Result<Self, BuildError>
Creates a new instance of Self
from a slice of EdgeListEdge
.
See the docstring of EdgeListEdge
for an example.
Sourcepub fn graph(&self) -> &UnGraph<usize, Type>
pub fn graph(&self) -> &UnGraph<usize, Type>
Accesses the [petgraph::stable_graph::UnGraph
] representation of the network.
The network analysis structs MeshAnalysis
and
NodalAnalysis
use petgraph
s
UnGraph
when they are created from a Network
instance. This method allows accessing
the graph directly. Please be aware that Network
holds more information than just the graph
(i.e. the edge source type information) and can therefore not be losslessly represented by a UnGraph
(otherwise, the need for this type would not exist in the first place).
§Examples
use network_analysis::{EdgeListEdge, Network, Type};
use petgraph::algo::dijkstra;
use petgraph::visit::NodeIndexable;
let network = Network::from_edge_list_edges(
&[
EdgeListEdge::new(vec![3], vec![1], Type::Voltage),
EdgeListEdge::new(vec![0], vec![2], Type::Resistance),
EdgeListEdge::new(vec![1], vec![3], Type::Resistance),
EdgeListEdge::new(vec![2], vec![0], Type::Resistance),
]
).expect("valid network");
let g = network.graph();
// Now use some of the functionality provided by petgraph, e.g. the "dijkstra" path finding algorithm
let node_map = dijkstra(&g, 0.into(), Some(2.into()), |_| 1);
assert_eq!(&2i32, node_map.get(&g.from_index(2)).unwrap());
Sourcepub fn voltage_source_count(&self) -> usize
pub fn voltage_source_count(&self) -> usize
Returns the number of voltage sources by counting all edges where edge.edge_type == Type::Voltage
.
§Examples
use network_analysis::{EdgeListEdge, Network, Type};
let network = Network::from_edge_list_edges(
&[
EdgeListEdge::new(vec![3], vec![1], Type::Voltage),
EdgeListEdge::new(vec![0], vec![2], Type::Resistance),
EdgeListEdge::new(vec![1], vec![3], Type::Current),
EdgeListEdge::new(vec![2], vec![0], Type::Voltage),
]
).expect("valid network");
assert_eq!(network.voltage_source_count(), 2);
Sourcepub fn current_source_count(&self) -> usize
pub fn current_source_count(&self) -> usize
Returns the number of current sources by counting all edges where edge.edge_type == Type::Current
.
§Examples
use network_analysis::{EdgeListEdge, Network, Type};
let network = Network::from_edge_list_edges(
&[
EdgeListEdge::new(vec![3], vec![1], Type::Voltage),
EdgeListEdge::new(vec![0], vec![2], Type::Resistance),
EdgeListEdge::new(vec![1], vec![3], Type::Current),
EdgeListEdge::new(vec![2], vec![0], Type::Voltage),
]
).expect("valid network");
assert_eq!(network.current_source_count(), 1);
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Network
impl RefUnwindSafe for Network
impl Send for Network
impl Sync for Network
impl Unpin for Network
impl UnwindSafe for Network
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.