Coverage for pyrc\utils\performance.py: 26%
54 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-06-29 15:57 +0200
« prev ^ index » next coverage.py v7.14.1, created at 2026-06-29 15:57 +0200
1# -------------------------------------------------------------------------------
2# Copyright (C) 2026 Joel Kimmich, Tim Jourdan
3# ------------------------------------------------------------------------------
4# License
5# This file is part of PyRC, distributed under GPL-3.0-or-later.
6# ------------------------------------------------------------------------------
8import numpy as np
9from sympy import symbols
11from pyrc.core.components.capacitor import Capacitor
12from pyrc.core.components.resistor import Resistor
13from pyrc.tools.science import celsius_to_kelvin
14from pyrc.tools.timing import Timing
15from pyrc.core.network import RCNetwork
16from pyrc.core.inputs import BoundaryCondition
19class PerformanceCheck:
21 def __init__(self):
22 self.network = RCNetwork()
23 # Initialize rng with seed to always get the same random numbers
24 self.rng = np.random.default_rng(1)
26 def get_random_temperature(self):
27 return celsius_to_kelvin(self.rng.normal(loc=15, scale=25))
29 def build_big_network(self, number_of_nodes=500, resistors_between_nodes=1):
30 """
31 Creates a huge RC-Network and saves it in self.__network.
33 """
34 boundaries = [BoundaryCondition(celsius_to_kelvin(-10)),
35 BoundaryCondition(celsius_to_kelvin(40))]
37 nodes = []
38 for i in range(number_of_nodes):
39 nodes.append(
40 Capacitor(
41 capacity=abs(self.rng.normal(loc=10, scale=3)),
42 temperature=self.get_random_temperature(),
43 )
44 )
46 resistors = []
47 for i in range((number_of_nodes + 1) * resistors_between_nodes):
48 if i == 1:
49 resistance = symbols("my_variable")
50 elif i == 2:
51 resistance = symbols("my_variable2")
52 else:
53 resistance = self.rng.normal(loc=10, scale=20)
54 resistors.append(
55 Resistor(resistance=resistance)
56 )
58 # Connect all parts to one network
59 boundaries[0].connect(resistors[0])
61 # resistor_index = 0
62 last_part = resistors[0]
63 for i in range(1, len(resistors)):
64 if i % resistors_between_nodes == 0:
65 # add node
66 index = int(i / resistors_between_nodes - 1)
67 if index < len(nodes):
68 last_part.connect(nodes[index])
69 last_part = nodes[index]
70 last_part.connect(resistors[i])
71 last_part = resistors[i]
73 last_part.connect(boundaries[1])
75 # Network created. Add all objects to rc_objects list.
76 self.network.rc_objects.set_lists(capacitors=nodes, resistors=resistors, boundaries=boundaries)
78 def test_performance(self, *args, **kwargs):
79 time_dependent_function = lambda t, temp, *args, **kwargs: (np.sin(t) + 3,np.sin(t*2) + 9)
81 time = Timing("Performance test")
83 self.build_big_network(*args, **kwargs)
84 time.catch_time("Network build up.")
86 self.network.make_system_matrices()
87 time.catch_time("Symbol matrices created.")
89 def start_solve():
90 time.catch_time("Start solving...")
92 _ = self.network.solve_network((0, 10), hook_function=start_solve,
93 time_dependent_tuple=((symbols("my_variable2"), symbols("my_variable")),
94 time_dependent_function))
95 time.catch_time("10 seconds simulated.")
96 time.end_timing()
99if __name__ == "__main__":
100 network_test = PerformanceCheck()
101 network_test.test_performance()