Network

Struct Network 

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

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

Source

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.

Source

pub fn from_node_edges(edges: &[NodeEdge]) -> Result<Self, BuildError>

Creates a new instance of Self from a slice of NodeEdge. See the docstring of NodeEdge for an example.

Source

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.

Source

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 petgraphs 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());
Source

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);
Source

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§

Source§

impl Clone for Network

Source§

fn clone(&self) -> Network

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Network

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.