Coverage for pyrc \ utils \ performance.py: 27%

52 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-13 16:59 +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# ------------------------------------------------------------------------------ 

7 

8import numpy as np 

9from sympy import symbols 

10 

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 

17 

18 

19class PerformanceTest: 

20 

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) 

25 

26 def get_random_temperature(self): 

27 return celsius_to_kelvin(self.rng.normal(loc=15, scale=25)) 

28 

29 def build_big_network(self, number_of_nodes=10000, resistors_between_nodes=1): 

30 """ 

31 Creates a huge RC-Network and saves it in self.__network. 

32 

33 """ 

34 boundaries = [BoundaryCondition(celsius_to_kelvin(-10)), 

35 BoundaryCondition(celsius_to_kelvin(40))] 

36 

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 ) 

45 

46 resistors = [] 

47 for i in range((number_of_nodes + 1) * resistors_between_nodes): 

48 if i == 1: 

49 resistance = symbols("my_variable") 

50 else: 

51 resistance = self.rng.normal(loc=10, scale=20) 

52 resistors.append( 

53 Resistor(resistance=resistance) 

54 ) 

55 

56 # Connect all parts to one network 

57 boundaries[0].connect(resistors[0]) 

58 

59 # resistor_index = 0 

60 last_part = resistors[0] 

61 for i in range(1, len(resistors)): 

62 if i % resistors_between_nodes == 0: 

63 # add node 

64 index = int(i / resistors_between_nodes - 1) 

65 if index < len(nodes): 

66 last_part.connect(nodes[index]) 

67 last_part = nodes[index] 

68 last_part.connect(resistors[i]) 

69 last_part = resistors[i] 

70 

71 last_part.connect(boundaries[1]) 

72 

73 # Network created. Add all objects to rc_objects list. 

74 self.network.rc_objects.set_lists(capacitors=nodes, resistors=resistors, boundaries=boundaries) 

75 

76 def test_performance(self, *args, **kwargs): 

77 time_dependent_function = lambda t, temp, **kwargs: np.sin(t) + 3 

78 

79 time = Timing("Performance test") 

80 

81 self.build_big_network(*args, **kwargs) 

82 time.catch_time("Network build up.") 

83 

84 self.network.make_system_matrices() 

85 time.catch_time("Symbol matrices created.") 

86 

87 def start_solve(): 

88 time.catch_time("Start solving...") 

89 

90 _ = self.network.solve_network((0, 10), hook_function=start_solve, 

91 time_dependent_function=time_dependent_function) 

92 time.catch_time("10 seconds simulated.") 

93 time.end_timing() 

94 

95 

96if __name__ == "__main__": 

97 network_test = PerformanceTest() 

98 network_test.test_performance()