forked from open-atmos/PySDM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspectrum_plotter.py
More file actions
165 lines (143 loc) · 5.49 KB
/
Copy pathspectrum_plotter.py
File metadata and controls
165 lines (143 loc) · 5.49 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import matplotlib
import numpy as np
from matplotlib import pyplot
from open_atmos_jupyter_utils import show_plot
from packaging import version
from PySDM_examples.Shima_et_al_2009.error_measure import error_measure
from PySDM.physics.constants import si
_matplotlib_version_3_3_3 = version.parse("3.3.0")
_matplotlib_version_actual = version.parse(matplotlib.__version__)
class SpectrumColors:
def __init__(self, begining="#2cbdfe", end="#b317b1"):
self.b = begining
self.e = end
def __call__(self, value: float):
bR, bG, bB = int(self.b[1:3], 16), int(self.b[3:5], 16), int(self.b[5:7], 16)
eR, eG, eB = int(self.e[1:3], 16), int(self.e[3:5], 16), int(self.e[5:7], 16)
R = bR + int((eR - bR) * value)
G = bG + int((eG - bG) * value)
B = bB + int((eB - bB) * value)
result = f"#{hex(R)[2:4]}{hex(G)[2:4]}{hex(B)[2:4]}"
return result
class SpectrumPlotter:
def __init__(self, settings, title=None, grid=True, legend=True, log_base=10):
self.settings = settings
self.format = "pdf"
self.colors = SpectrumColors()
self.smooth = False
self.smooth_scope = 2
self.legend = legend
self.grid = grid
self.title = title
self.xlabel = "particle radius [µm]"
self.ylabel = "dm/dlnr [g/m^3/(unit dr/r)]"
self.log_base = log_base
self.ax = pyplot
self.fig = pyplot
self.finished = False
def finish(self):
if self.finished:
return
self.finished = True
if self.grid:
self.ax.grid()
base_arg = {
"base"
+ (
"x" if _matplotlib_version_actual < _matplotlib_version_3_3_3 else ""
): self.log_base
}
if self.title is not None:
try:
self.ax.title(self.title)
except TypeError:
self.ax.set_title(self.title)
try:
self.ax.xscale("log", **base_arg)
self.ax.xlabel(self.xlabel)
self.ax.ylabel(self.ylabel)
except AttributeError:
self.ax.set_xscale("log", **base_arg)
self.ax.set_xlabel(self.xlabel)
self.ax.set_ylabel(self.ylabel)
if self.legend:
self.ax.legend()
def show(self):
self.finish()
pyplot.tight_layout()
show_plot()
def save(self, file):
self.finish()
pyplot.savefig(file, format=self.format)
def plot(
self, spectrum, t, label=None, color=None, title=None, add_error_to_label=False
):
error = self.plot_analytic_solution(self.settings, t, spectrum, title)
if label is not None and add_error_to_label:
label += f" error={error:.4g}"
self.plot_data(self.settings, t, spectrum, label, color)
return error
def plot_analytic_solution(self, settings, t, spectrum, title):
if t == 0:
analytic_solution = settings.spectrum.size_distribution
else:
def analytic_solution(x):
return settings.norm_factor * settings.kernel.analytic_solution(
x=x, t=t, x_0=settings.X0, N_0=settings.n_part
)
volume_bins_edges = self.settings.formulae.trivia.volume(
settings.radius_bins_edges
)
dm = np.diff(volume_bins_edges)
dr = np.diff(settings.radius_bins_edges)
pdf_m_x = volume_bins_edges[:-1] + dm / 2
pdf_m_y = analytic_solution(pdf_m_x)
pdf_r_x = settings.radius_bins_edges[:-1] + dr / 2
pdf_r_y = pdf_m_y * dm / dr * pdf_r_x
x = pdf_r_x * si.metres / si.micrometres
y_true = (
pdf_r_y
* self.settings.formulae.trivia.volume(radius=pdf_r_x)
* settings.rho
/ settings.dv
* si.kilograms
/ si.grams
)
self.ax.plot(x, y_true, color="black")
if spectrum is not None:
y = spectrum * si.kilograms / si.grams
error = error_measure(y, y_true, x)
self.title = (
title or f"error measure: {error:.2f}"
) # TODO #327 relative error
return error
return None
def plot_data(self, settings, t, spectrum, label, color):
if self.smooth:
scope = self.smooth_scope
if t != 0:
new = np.copy(spectrum)
for _ in range(2):
for i in range(scope, len(spectrum) - scope):
new[i] = np.mean(spectrum[i - scope : i + scope + 1])
scope = 1
for i in range(scope, len(spectrum) - scope):
spectrum[i] = np.mean(new[i - scope : i + scope + 1])
x = settings.radius_bins_edges[:-scope]
dx = np.diff(x)
self.ax.plot(
(x[:-1] + dx / 2) * si.metres / si.micrometres,
spectrum[:-scope] * si.kilograms / si.grams,
label=label or f"t = {t}s",
color=color
or self.colors(t / (self.settings.output_steps[-1] * self.settings.dt)),
)
else:
self.ax.step(
settings.radius_bins_edges[:-1] * si.metres / si.micrometres,
spectrum * si.kilograms / si.grams,
where="post",
label=label or f"t = {t}s",
color=color
or self.colors(t / (self.settings.output_steps[-1] * self.settings.dt)),
)