1
+ import sympy as sym
2
+ import argparse
3
+ import numpy as np
4
+
5
+ parser = argparse .ArgumentParser (description = 'Arguments for calculation of the dissipation rate' )
6
+ parser .add_argument ("-f" , "--folder" , type = str , help = "Folder path" , required = True )
7
+ parser .add_argument ("-i" , "--input" , type = str , help = "Name of the input file" , required = True )
8
+ parser .add_argument ("-o" , "--output" , type = str , default = "ke_rate.dat" , help = "Name of the output file" , required = False )
9
+ args , leftovers = parser .parse_known_args ()
10
+
11
+ folder = args .folder
12
+ filename = args .input
13
+ outname = args .output
14
+
15
+ data = np .loadtxt (folder + filename ,skiprows = 1 )
16
+
17
+ # Sybmolic position x
18
+ x = sym .Symbol ('x' )
19
+
20
+ # Data in x: time step
21
+ positions = data [:,0 ]
22
+
23
+ # Data in y: kinetic energy
24
+ y = data [:,1 ]
25
+
26
+ size = len (positions )
27
+
28
+ # Function that calculates derivative at node i using a Lagrange polynomial
29
+ # of degree deg and a stencil from left_index to right_index. It works for
30
+ # equidistant (fixed time step) and non-equidistant nodes (adaptive time
31
+ # step).
32
+ def calculate_derivative (deg , i , left_index , right_index ):
33
+ v = [1 ]* (deg + 1 )
34
+ v_derivative = [0 ]* (deg + 1 )
35
+ result = 0
36
+ derivative = 0
37
+ count = 0
38
+ for k in range (left_index , right_index + 1 ):
39
+ for j in range (left_index , right_index + 1 ):
40
+ if k != j :
41
+ # l_k(x)
42
+ v [count ] *= (x - positions [j ])/ (positions [k ]- positions [j ])
43
+
44
+ #Calculate derivative of l_k(x)
45
+ v_derivative [count ] = sym .diff (v [count ],x )
46
+
47
+ #Calculate L(x) as a linear combination of y_i*l_k(x)
48
+ result += y [k ]* v [count ]
49
+ derivative += y [k ]* v_derivative [count ]
50
+ count += 1
51
+
52
+ # Evaluate the Lagrange polynomial at the desired x position
53
+ return derivative .evalf (subs = {x :positions [i ]})
54
+
55
+ final_ke_rate = []
56
+ for i in range (0 ,size ):
57
+ kerate = 0.0
58
+ if i == 0 :
59
+ kerate = calculate_derivative (1 , i , i , i + 1 )
60
+ elif i == size - 1 :
61
+ kerate = calculate_derivative (1 , i , i - 1 , i )
62
+ elif (i == 1 ) or (i == (size - 2 )):
63
+ kerate = calculate_derivative (2 , i , i - 1 , i + 1 )
64
+ else :
65
+ kerate = calculate_derivative (4 , i , i - 2 , i + 2 )
66
+
67
+ final_ke_rate .append (kerate * - 1 )
68
+
69
+ # Save data with the time step and the kinetic energy rate in two columns
70
+ DataOut = np .column_stack ((positions ,final_ke_rate ))
71
+ np .savetxt (folder + outname ,DataOut )
0 commit comments