Coverage for pyrc \ validation \ conduction \ comparison.py: 0%

33 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 

8from pyrc.validation.conduction.analytic import run_conduction_analytic 

9from pyrc.validation.conduction.model import run_conduction_model 

10import numpy as np 

11from pyrc.visualization.plot import LinePlot 

12 

13 

14def interpolate_extrapolate_vectors(first_x, second_x, second_y): 

15 """ 

16 Interpolates and extrapolates y-values from the second dataset onto 

17 the x-values of the first dataset. 

18 

19 For extrapolation, uses linear extension through the two nearest points. 

20 

21 Parameters: 

22 ----------- 

23 first_x : array-like 

24 X values of the first dataset (e.g., 1500 points) 

25 second_x : array-like 

26 X values of the second dataset (e.g., 7 points) 

27 second_y : array-like 

28 Y values of the second dataset (to be interpolated/extrapolated) 

29 

30 Returns: 

31 -------- 

32 new_x : numpy.ndarray 

33 Same as first_x 

34 new_y : numpy.ndarray 

35 Interpolated/extrapolated y-values at first_x positions 

36 """ 

37 

38 # Convert inputs to numpy arrays 

39 first_x = np.array(first_x) 

40 second_x = np.array(second_x) 

41 second_y = np.array(second_y) 

42 

43 # Initialize result array 

44 new_y = np.zeros_like(first_x, dtype=float) 

45 

46 # Get the x-range of the second dataset 

47 x_min = second_x[0] 

48 x_max = second_x[-1] 

49 

50 for i, x_target in enumerate(first_x): 

51 if x_min <= x_target <= x_max: 

52 # Interpolation case: x_target is within the range 

53 new_y[i] = np.interp(x_target, second_x, second_y) 

54 elif x_target < x_min: 

55 # Left extrapolation: use the two leftmost points 

56 x1, x2 = second_x[0], second_x[1] 

57 y1, y2 = second_y[0], second_y[1] 

58 # Linear extrapolation: y = y1 + (y2-y1)/(x2-x1) * (x_target-x1) 

59 slope = (y2 - y1) / (x2 - x1) 

60 new_y[i] = y1 + slope * (x_target - x1) 

61 else: # x_target > x_max 

62 # Right extrapolation: use the two rightmost points 

63 x1, x2 = second_x[-2], second_x[-1] 

64 y1, y2 = second_y[-2], second_y[-1] 

65 # Linear extrapolation: y = y2 + (y2-y1)/(x2-x1) * (x_target-x2) 

66 slope = (y2 - y1) / (x2 - x1) 

67 new_y[i] = y2 + slope * (x_target - x2) 

68 

69 return new_y 

70 

71 

72if __name__ == "__main__": 

73 calculation_time = 1100 

74 x_model, y_model = run_conduction_model(False, calculation_time=calculation_time) 

75 x_analytic, y_analytic = run_conduction_analytic(False) 

76 

77 # recalculate the values from analytic solution to x values of model solution to print in same plot 

78 y_analytic_polation = interpolate_extrapolate_vectors(x_model, x_analytic, y_analytic) 

79 

80 plot = LinePlot(x=x_model, ys=y_model - y_analytic_polation, y_title="Temperature-Delta / K", x_title="Length / m") 

81 plot.plot() 

82 plot.ax.set_xlim(left=0, right=x_model[-1]) 

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

84 plot.save( 

85 r"C:\Simulations\PyRC\debug\conduction_comparison" 

86 f"_{calculation_time}.png" 

87 )