Skip to content

Commit 4d80214

Browse files
committed
Add function to find all nodal currents
1 parent e388fca commit 4d80214

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

randomnwn/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
create_matrix,
3232
solve_network,
3333
solve_drain_current,
34+
solve_nodal_current,
3435
scale_sol,
3536
)
3637

randomnwn/calculations.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,76 @@ def solve_drain_current(
320320
return current_array.squeeze()
321321

322322

323+
def solve_nodal_current(
324+
NWN: nx.Graph,
325+
source_node: Union[Tuple, List[Tuple]],
326+
drain_node: Union[Tuple, List[Tuple]],
327+
voltage: float,
328+
scaled: bool = False,
329+
solver: str = "spsolve",
330+
**kwargs
331+
) -> np.ndarray:
332+
"""
333+
Solve for the current through each node of a NWN. It will appear that
334+
no current is flowing through source (drain) nodes for positive (negative)
335+
voltages.
336+
337+
Parameters
338+
----------
339+
NWN : Graph
340+
Nanowire network.
341+
342+
source_node : tuple, or list of tuples
343+
Voltage source nodes.
344+
345+
drain_node : tuple, or list of tuples
346+
Grounded output nodes.
347+
348+
voltage : float
349+
Voltage of the source nodes.
350+
351+
scaled : bool, optional
352+
Whether or not to scaled the output by i0. Default: False.
353+
354+
solver: str, optional
355+
Name of sparse matrix solving algorithm to use. Default: "spsolve".
356+
357+
Returns
358+
-------
359+
current_array : ndarray
360+
Array containing the current flow through each drain node in the order
361+
passed.
362+
363+
"""
364+
# Get nodal voltages
365+
V_out = solve_network(
366+
NWN, source_node, drain_node, voltage, "voltage", solver, **kwargs
367+
)
368+
369+
# Preallocate output
370+
current_array = np.zeros(len(NWN.nodes))
371+
372+
# Calculate input current for each node
373+
for node in NWN.nodes:
374+
node_ind = NWN.graph["node_indices"][node]
375+
for edge in NWN.edges(node):
376+
edge0_ind = NWN.graph["node_indices"][edge[0]]
377+
edge1_ind = NWN.graph["node_indices"][edge[1]]
378+
V_delta = V_out[edge1_ind] - V_out[edge0_ind]
379+
380+
# Only add current entering a node so we can see how much
381+
# current passes through. Else, we just get zero due to KCL.
382+
if V_delta > 0:
383+
current_array[node_ind] += (V_delta *
384+
NWN.edges[edge]["conductance"] * np.sign(voltage))
385+
386+
# Scale the output if desired
387+
if scaled:
388+
current_array *= NWN.graph["units"]["i0"]
389+
390+
return current_array
391+
392+
323393
def scale_sol(NWN: nx.Graph, sol: np.ndarray):
324394
"""
325395
Scale the voltage and current solutions by their characteristic values.

randomnwn/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.2.4"
1+
__version__ = "0.2.5"

0 commit comments

Comments
 (0)