Coverage for pyrc \ validation \ coupling \ model.py: 0%

44 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 

8# Script for validation 

9# 

10# Calculation of the dynamic solution of two coupled volumes with different temperature 

11# 

12# Model solution 

13# 

14import numpy as np 

15 

16from pyrc.core.nodes import Node 

17from pyrc.visualization.plot import LinePlot 

18from pyrc.validation.coupling import setup 

19from pyrc.validation.network import NetworkTemplate, Block 

20from pyrc.core.resistors import CombinedResistor 

21 

22 

23class CouplingNetwork(NetworkTemplate): 

24 def __init__(self, blocks: list[Block], save_and_load: bool = True, **kwargs): 

25 super().__init__(**kwargs) 

26 self.settings.save_all_x_seconds = 0.1 

27 self.settings.solve_settings.atol = 1e-8 

28 self.blocks = blocks 

29 

30 if save_and_load: 

31 self.save_to_pickle = True 

32 self.load_from_pickle = True 

33 self.load_solution = True 

34 

35 self.x_major_points = np.cumsum([0, *[b.width for b in self.blocks]]) 

36 

37 def _create_network(self): 

38 block_nodes = [] 

39 block: Block 

40 for i, block in enumerate(self.blocks): 

41 x_start = sum([b.width for b in self.blocks[:i]]) 

42 start_vector = np.array([x_start, 0, 0]) 

43 for increment in range(block.increments): 

44 block_nodes.append( 

45 Node( 

46 material=block.material, 

47 temperature=block.initial_temperature, 

48 position=block.increment_positions[increment] + start_vector, 

49 delta=(block.width / block.increments, 1, 1), 

50 rc_objects=self.rc_objects, 

51 rc_solution=self.rc_solution, 

52 ) 

53 ) 

54 

55 # add resistors between the nodes 

56 all_resistors = [] 

57 for i, node in enumerate(block_nodes[:-1]): 

58 all_resistors.append(CombinedResistor()) 

59 all_resistors[-1].double_connect(block_nodes[i], block_nodes[i + 1]) 

60 

61 # no BoundaryConditions must be added (adiabatic sides) 

62 self.rc_objects.set_lists(capacitors=block_nodes, resistors=all_resistors) 

63 

64 

65def run_coupling_model(plot_data: str = None, save_and_load=True): 

66 blocks, time_indexes, times, name_add_on = setup() 

67 network = CouplingNetwork( 

68 blocks, 

69 save_and_load, 

70 working_dir=r"C:\Simulations\PyRC\evaluation\coupling", 

71 ) 

72 network.num_cores_jacobian = 4 

73 # network.settings.save_folder_path = r"C:\Simulations\PyRC\debug" 

74 network.create_network() 

75 network.solve_network( 

76 # t_span=(0, 3600*24*1), 

77 t_span=(0, 2000), 

78 print_progress=True, 

79 check_courant=False, 

80 ) 

81 

82 if plot_data: 

83 plot = LinePlot( 

84 x=[n.position[0] for n in network.nodes], 

85 ys=network.rc_solution.temperature_vectors[time_indexes], 

86 labels=[f"{float(t):.1f} s" for t in times], 

87 y_title="Temperature / °C (model)", 

88 x_title="Length / m", 

89 ) 

90 plot.plot() 

91 plot.ax.set_xlim(left=0, right=network.x_major_points[-1]) 

92 plot.fig.legend(loc="outside upper right", ncols=5) 

93 plot.save(plot_data) 

94 return ( 

95 [n.position[0] for n in network.nodes], 

96 network.rc_solution.temperature_vectors[time_indexes], 

97 times, 

98 name_add_on, 

99 network, 

100 ) 

101 

102 

103if __name__ == '__main__': 

104 run_coupling_model(True)