-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomfi.py
More file actions
98 lines (81 loc) · 2.93 KB
/
comfi.py
File metadata and controls
98 lines (81 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""Functions and stuff for ease of use on MHD results"""
import numpy as np
import h5py
#import scipy.constants
def nuin(dens, temp):
"""Calculate collision frequency"""
return 7.4e-11 * dens * 1e20 * 1e-6 * 9.62771 * np.sqrt(temp)
class Results:
"""Class to get and plot results from a CoMFi simulation
Usage:
Results()
"""
def __init__(self, params, filename='mhdsim.h5'):
self.h5file = h5py.File(filename, 'r')
self.params = params
def var(self, varindex, time_step, dataset="unknowns"):
""" Returns a matrix of the unknown.
Paramters:
varindex: Index of the unknown.
time_step: Time step.
Returns: np.array of size (nx, nz)
"""
unknowns = np.array(self.h5file['/'+str(time_step)+'/'+dataset])
var = unknowns[:, varindex]
return var.reshape(self.params["nx"], self.params["nz"])
def unknowns(self, time_step):
""" Returns a dictionary of the unknowns """
unknowns = {"Bx": self.var(0, time_step),
"Bz": self.var(1, time_step),
"Bperp": self.var(2, time_step),
"n_n": self.var(3, time_step),
"Ux": self.var(4, time_step),
"Uz": self.var(5, time_step),
"Uperp": self.var(6, time_step),
"n_p": self.var(7, time_step),
"Vx": self.var(8, time_step),
"Vz": self.var(9, time_step),
"Vperp": self.var(10, time_step),
"E_n": self.var(11, time_step),
"E_p": self.var(12, time_step),
"GLM": self.var(13, time_step)
}
return unknowns
def raw_matrix(self, time_step, dataset="unknowns"):
"""Returns raw matrix from hdf5 file"""
return np.array(self.h5file['/'+str(time_step)+'/'+dataset])
def ip1_neumann(unknown):
result = np.zeros_like(unknown)
result[:, :-1] = unknown[:, 1:]
result[:, -1] = unknown[:, -1]
return result
def ip1_mirror(unknown):
result = np.zeros_like(unknown)
result[:, :-1] = unknown[:, 1:]
result[:, -1] = -unknown[:, -1]
return result
def im1_neumann(unknown):
result = np.zeros_like(unknown)
result[:, 1:] = unknown[:, :-1]
result[:, 0] = unknown[:, 0]
return result
def im1_mirror(unknown):
result = np.zeros_like(unknown)
result[:, 1:] = unknown[:, :-1]
result[:, 0] = -unknown[:, 0]
return result
def jp1_neumann(unknown):
result = np.zeros_like(unknown)
result[1:, :] = unknown[:-1, :]
result[0, :] = unknown[0, :]
return result
def jm1_neumann(unknown):
result = np.zeros_like(unknown)
result[:-1, :] = unknown[1:, :]
result[-1, :] = unknown[-1, :]
return result
def jm1_dirichlet(unknown, bc):
result = np.zeros_like(unknown)
result[:-1, :] = unknown[1:, :]
result[-1, :] = bc
return result