Skip to content

Commit

Permalink
tests for plotting
Browse files Browse the repository at this point in the history
  • Loading branch information
Ury committed Jan 17, 2024
1 parent 2cb62dc commit cba3839
Show file tree
Hide file tree
Showing 16 changed files with 195 additions and 80 deletions.
2 changes: 1 addition & 1 deletion examples/01_Binary_Precipitation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
"\tbeta\t0.000e+00\t\t0.0000\t\t0.0000e+00\t5.7737e+03\n",
"\n",
"N\tTime (s)\tSim Time (s)\tTemperature (K)\tMatrix Comp\n",
"3675\t1.8e+06\t\t21.4\t\t723\t\t0.0126\n",
"3675\t1.8e+06\t\t48.5\t\t723\t\t0.0126\n",
"\n",
"\tPhase\tPrec Density (#/m3)\tVolume Frac\tAvg Radius (m)\tDriving Force (J/mol)\n",
"\tbeta\t1.374e+22\t\t1.5504\t\t6.1126e-09\t3.2902e+02\n",
Expand Down
8 changes: 4 additions & 4 deletions examples/02_Multicomponent_Precipitation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@
"\tbeta\t0.000e+00\t\t0.0000\t\t0.0000e+00\t2.4397e+02\n",
"\n",
"N\tTime (s)\tSim Time (s)\tTemperature (K)\tAl\tCr\t\n",
"5000\t1.3e+04\t\t13.4\t\t1073\t\t8.8266\t8.5647\t\n",
"5000\t1.3e+04\t\t34.9\t\t1073\t\t8.8266\t8.5647\t\n",
"\n",
"\tPhase\tPrec Density (#/m3)\tVolume Frac\tAvg Radius (m)\tDriving Force (J/mol)\n",
"\tbeta\t6.346e+20\t\t11.5153\t\t3.2934e-08\t9.0621e+00\n",
"\n",
"N\tTime (s)\tSim Time (s)\tTemperature (K)\tAl\tCr\t\n",
"7694\t1.0e+06\t\t23.0\t\t1073\t\t8.7978\t8.5718\t\n",
"7694\t1.0e+06\t\t53.7\t\t1073\t\t8.7978\t8.5718\t\n",
"\n",
"\tPhase\tPrec Density (#/m3)\tVolume Frac\tAvg Radius (m)\tDriving Force (J/mol)\n",
"\tbeta\t8.751e+18\t\t11.8685\t\t1.3883e-07\t2.1499e+00\n",
Expand Down Expand Up @@ -228,13 +228,13 @@
"\tbeta\t0.000e+00\t\t0.0000\t\t0.0000e+00\t2.4397e+02\n",
"\n",
"N\tTime (s)\tSim Time (s)\tTemperature (K)\tAl\tCr\t\n",
"5000\t1.3e+04\t\t18.4\t\t1073\t\t8.8416\t8.5239\t\n",
"5000\t1.3e+04\t\t21.3\t\t1073\t\t8.8416\t8.5239\t\n",
"\n",
"\tPhase\tPrec Density (#/m3)\tVolume Frac\tAvg Radius (m)\tDriving Force (J/mol)\n",
"\tbeta\t6.389e+20\t\t11.6912\t\t3.3030e-08\t9.0377e+00\n",
"\n",
"N\tTime (s)\tSim Time (s)\tTemperature (K)\tAl\tCr\t\n",
"7690\t1.0e+06\t\t28.2\t\t1073\t\t8.8139\t8.5284\t\n",
"7690\t1.0e+06\t\t32.1\t\t1073\t\t8.8139\t8.5284\t\n",
"\n",
"\tPhase\tPrec Density (#/m3)\tVolume Frac\tAvg Radius (m)\tDriving Force (J/mol)\n",
"\tbeta\t8.851e+18\t\t12.0534\t\t1.3903e-07\t2.1473e+00\n",
Expand Down
22 changes: 11 additions & 11 deletions examples/03_Multiphase_Precipitation.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/04_Precipitation_with_Elastic_Energy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
"\tCU4TI\t0.000e+00\t\t0.0000\t\t0.0000e+00\t1.9660e+03\n",
"\n",
"N\tTime (s)\tSim Time (s)\tTemperature (K)\tMatrix Comp\n",
"4571\t1.0e+05\t\t52.9\t\t623\t\t0.1757\n",
"4571\t1.0e+05\t\t108.0\t\t623\t\t0.1757\n",
"\n",
"\tPhase\tPrec Density (#/m3)\tVolume Frac\tAvg Radius (m)\tDriving Force (J/mol)\n",
"\tCU4TI\t1.455e+23\t\t9.3695\t\t5.1198e-09\t1.2258e+02\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/05_Strength_Modeling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
"\tbeta\t0.000e+00\t\t0.0000\t\t0.0000e+00\t3.6624e+03\n",
"\n",
"N\tTime (s)\tSim Time (s)\tTemperature (K)\tMatrix Comp\n",
"3768\t9.0e+05\t\t38.4\t\t673\t\t0.0165\n",
"3768\t9.0e+05\t\t71.9\t\t673\t\t0.0165\n",
"\n",
"\tPhase\tPrec Density (#/m3)\tVolume Frac\tAvg Radius (m)\tDriving Force (J/mol)\n",
"\tbeta\t7.216e+19\t\t0.7344\t\t2.8289e-08\t8.2115e+01\n",
Expand Down
8 changes: 4 additions & 4 deletions examples/06_Single_Phase_Diffusion.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@
"text": [
"Iteration\tSim Time (h)\tRun time (s)\n",
"0\t\t0.0e+00\t\t0.0\n",
"100\t\t2.9e+01\t\t3.6\n",
"200\t\t5.7e+01\t\t7.1\n",
"300\t\t8.6e+01\t\t9.6\n",
"349\t\t1.0e+02\t\t10.5\n"
"100\t\t2.9e+01\t\t5.7\n",
"200\t\t5.7e+01\t\t17.2\n",
"300\t\t8.6e+01\t\t22.7\n",
"349\t\t1.0e+02\t\t24.2\n"
]
}
],
Expand Down
21 changes: 10 additions & 11 deletions examples/07_Homogenization_Model.ipynb

Large diffs are not rendered by default.

25 changes: 13 additions & 12 deletions examples/08_Model_Coupling.ipynb

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions examples/09_Thermodynamics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
{
"data": {
"text/plain": [
"<matplotlib.legend.Legend at 0x26d84a96ac0>"
"<matplotlib.legend.Legend at 0x1f6bf6b6a60>"
]
},
"execution_count": 5,
Expand Down Expand Up @@ -226,7 +226,7 @@
{
"data": {
"text/plain": [
"<matplotlib.legend.Legend at 0x26d84bff910>"
"<matplotlib.legend.Legend at 0x1f6bf821730>"
]
},
"execution_count": 6,
Expand Down Expand Up @@ -312,7 +312,7 @@
{
"data": {
"text/plain": [
"<matplotlib.legend.Legend at 0x26d84fc5430>"
"<matplotlib.legend.Legend at 0x1f6bfddccd0>"
]
},
"execution_count": 7,
Expand Down Expand Up @@ -403,7 +403,7 @@
{
"data": {
"text/plain": [
"<matplotlib.legend.Legend at 0x26d86265a30>"
"<matplotlib.legend.Legend at 0x1f6c13a4a90>"
]
},
"execution_count": 8,
Expand Down
10 changes: 5 additions & 5 deletions examples/10_Surrogates.ipynb

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions examples/11_Extra_Factors.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -73,7 +73,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -106,7 +106,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -163,7 +163,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 9,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -232,7 +232,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 10,
"metadata": {},
"outputs": [
{
Expand Down
6 changes: 3 additions & 3 deletions examples/12_Custom_Iterators.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"\tbeta\t0.000e+00\t\t0.0000\t\t0.0000e+00\t5.7737e+03\n",
"\n",
"N\tTime (s)\tSim Time (s)\tTemperature (K)\tMatrix Comp\n",
"3831\t1.8e+06\t\t30.8\t\t723\t\t0.0126\n",
"3831\t1.8e+06\t\t36.8\t\t723\t\t0.0126\n",
"\n",
"\tPhase\tPrec Density (#/m3)\tVolume Frac\tAvg Radius (m)\tDriving Force (J/mol)\n",
"\tbeta\t1.395e+22\t\t1.5504\t\t6.0887e-09\t3.2954e+02\n",
Expand Down Expand Up @@ -159,7 +159,7 @@
"\tbeta\t0.000e+00\t\t0.0000\t\t0.0000e+00\t5.7737e+03\n",
"\n",
"N\tTime (s)\tSim Time (s)\tTemperature (K)\tMatrix Comp\n",
"3751\t1.8e+06\t\t24.6\t\t723\t\t0.0126\n",
"3751\t1.8e+06\t\t26.6\t\t723\t\t0.0126\n",
"\n",
"\tPhase\tPrec Density (#/m3)\tVolume Frac\tAvg Radius (m)\tDriving Force (J/mol)\n",
"\tbeta\t1.386e+22\t\t1.5505\t\t6.0962e-09\t3.2700e+02\n",
Expand Down Expand Up @@ -188,7 +188,7 @@
{
"data": {
"text/plain": [
"<matplotlib.legend.Legend at 0x248faf989d0>"
"<matplotlib.legend.Legend at 0x17798e58ac0>"
]
},
"execution_count": 5,
Expand Down
4 changes: 3 additions & 1 deletion kawin/diffusion/Plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ def plotPhases(diffModel, ax = None, plotPhase = None, zScale = 1, *args, **kwar
ax.set_ylim([0, 1])
ax.set_xlabel('Distance (m)')
ax.set_ylabel('Phase Fraction')
ax.legend()

if plotPhase is None:
ax.legend()

return ax
21 changes: 7 additions & 14 deletions kawin/precipitation/Plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def plotBase(precModel, axes, variable, bounds = None, timeUnits = 's', radius='
}

totalVariables = ['Total Volume Fraction', 'Total Average Radius', 'Total Aspect Ratio', \
'Total Nucleation Rate', 'Total Precipitate Density']
'Total Nucleation Rate', 'Total Precipitate Density', 'Total Volume Average Radius']
singleVariables = ['Volume Fraction', 'Critical Radius', 'Average Radius', 'Aspect Ratio', \
'Driving Force', 'Nucleation Rate', 'Precipitate Density', 'Volume Average Radius']
eqCompositions = ['Eq Composition Alpha', 'Eq Composition Beta']
Expand Down Expand Up @@ -169,7 +169,7 @@ def plotEuler(precModel, axes, variable, bounds = None, timeUnits = 's', radius=
plotBase(precModel, axes, variable, bounds, timeUnits, radius, *args, **kwargs)

def plotTemperature(precModel, timeScale, labels, variable, axes, *args, **kwargs):
axes.semilogx(timeScale * precModel.time, precModel.T, *args, **kwargs)
axes.semilogx(timeScale * precModel.time, precModel.temperature, *args, **kwargs)
axes.set_ylabel(labels[variable])

def plotCompositions(precModel, timeScale, labels, variable, axes, *args, **kwargs):
Expand Down Expand Up @@ -242,23 +242,16 @@ def plotSaurations(precModel, timeScale, labels, variable, axes, *args, **kwargs
#Thus only a single element is needed
plotVariable = np.zeros(precModel.betaFrac.shape)
for p in range(len(precModel.phases)):
if precModel.numberOfElements == 1:
if variable == 'Eq Volume Fraction':
num = precModel.xComp[0] - precModel.xEqAlpha[p]
else:
num = precModel.xComp - precModel.xEqAlpha[p]
den = precModel.xEqBeta[p] - precModel.xEqAlpha[p]
if variable == 'Eq Volume Fraction':
num = precModel.xComp[0,0] - precModel.xEqAlpha[:,p,0]
else:
if variable == 'Eq Volume Fraction':
num = precModel.xComp[0,0] - precModel.xEqAlpha[:,p,0]
else:
num = precModel.xComp[:,0] - precModel.xEqAlpha[:,p,0]
den = precModel.xEqBeta[:,p,0] - precModel.xEqAlpha[:,p,0]
num = precModel.xComp[:,0] - precModel.xEqAlpha[:,p,0]
den = precModel.xEqBeta[:,p,0] - precModel.xEqAlpha[:,p,0]
#If precipitate is unstable, both xEqAlpha and xEqBeta are set to 0
#For these cases, change the values of numerator and denominator so that supersaturation is 0 instead of undefined
num[den == 0] = 0
den[den == 0] = 1
plotVariable[p] = num / den
plotVariable[:,p] = num / den

if len(precModel.phases) == 1:
axes.semilogx(timeScale * precModel.time, plotVariable[:,0], *args, **kwargs)
Expand Down
9 changes: 6 additions & 3 deletions kawin/precipitation/PopulationBalance.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,9 +893,12 @@ def PlotKDE(self, axes, bw_method = None, fill = False, logX = False, logY = Fal
determined by precipitate curvature
*args, **kwargs - extra arguments for plotting
'''
kernel = sts.gaussian_kde(self.PSDsize, bw_method = bw_method, weights = self.PSD)
x = np.linspace(self.min, self.max, 1000)
y = kernel(x) * self.ZeroMoment() * (self.PSDbounds[1] - self.PSDbounds[0])
x = np.linspace(self.min, self.max, 1000)
if np.all(self.PSD == 0):
y = np.zeros(x.shape)
else:
kernel = sts.gaussian_kde(self.PSDsize, bw_method = bw_method, weights = self.PSD)
y = kernel(x) * self.ZeroMoment() * (self.PSDbounds[1] - self.PSDbounds[0])

if hasattr(scale, '__len__'):
scale = np.interp(x, self.PSDbounds, scale)
Expand Down
117 changes: 117 additions & 0 deletions kawin/tests/test_plotting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
from kawin.precipitation import PrecipitateModel
from kawin.diffusion.Diffusion import DiffusionModel
import matplotlib.pyplot as plt
import numpy as np

def test_precipitate_plotting():
binary_single = PrecipitateModel(phases=['beta'], elements=['A'])
binary_multi = PrecipitateModel(phases=['beta', 'gamma', 'zeta'], elements=['A'])
ternary_single = PrecipitateModel(phases=['beta'], elements=['A', 'B'])
ternary_multi = PrecipitateModel(phases=['beta', 'gamma', 'zeta'], elements=['A', 'B'])

models = [
(binary_single, 1, 1),
(binary_multi, 1, 3),
(ternary_single, 2, 1),
(ternary_multi, 2, 3),
]

varTypes = [
('Volume Fraction', [2]),
('Total Volume Fraction', None),
('Critical Radius', [2]),
('Average Radius', [2]),
('Volume Average Radius', [2]),
('Total Average Radius', None),
('Total Volume Average Radius', None),
('Aspect Ratio', [2]),
('Total Aspect Ratio', None),
('Driving Force', [2]),
('Nucleation Rate', [2]),
('Total Nucleation Rate', None),
('Precipitate Density', [2]),
('Total Precipitate Density', None),
('Temperature', None),
('Composition', [1]),
('Eq Composition Alpha', [1,2]),
('Eq Composition Beta', [1,2]),
('Supersaturation', [2]),
('Eq Volume Fraction', [2]),
('Size Distribution', [2]),
('Size Distribution Curve', [2]),
('Size Distribution KDE', [2]),
('Size Distribution Density', [2]),
]

for m in models:
for v in varTypes:
fig, ax = plt.subplots(1,1)
m[0].plot(ax, v[0])
numLines = len(ax.lines)
plt.close(fig)

#Check that the number of lines on the plot correspond to the right amount
# Number of lines should either be 1, elements, phases or elements*phases depending on variable
desiredNumber = 1
if v[1] is not None:
desiredNumber = np.prod([m[vi] for vi in v[1]], dtype=np.int32)
assert numLines == desiredNumber

def test_diffusion_plotting():
#Single phase and Homogenizaton model goes through the same path for plotting
binary_single = DiffusionModel(zlim=[-1,1], N=100, elements=['A', 'B'], phases=['alpha'])
binary_multi = DiffusionModel(zlim=[-1,1], N=100, elements=['A', 'B'], phases=['alpha', 'beta', 'gamma'])
ternary_single = DiffusionModel(zlim=[-1,1], N=100, elements=['A', 'B', 'C'], phases=['alpha'])
ternary_multi = DiffusionModel(zlim=[-1,1], N=100, elements=['A', 'B', 'C'], phases=['alpha', 'beta', 'gamma'])

models = [
(binary_single, 2, 1),
(binary_multi, 2, 3),
(ternary_single, 3, 1),
(ternary_multi, 3, 3),
]

for m in models:
m[0].setTemperature(900)

#For each plot, check that the number of lines correspond to number of elements or phases
#For 'plot', number of lines should be elements (with or without reference) or a single element
#For 'plotTwoAxis', number of lines for each axis should be length of input array
#For 'plotPhases', number of lines is number of phases or single phase
fig, ax = plt.subplots(1,1)
m[0].plot(ax, plotReference = False)
assert len(ax.lines) == m[1]-1
plt.close(fig)

fig, ax = plt.subplots(1,1)
m[0].plot(ax, plotReference = True)
assert len(ax.lines) == m[1]
plt.close(fig)

fig, ax = plt.subplots(1,1)
m[0].plot(ax, plotElement = m[0].allElements[0])
assert len(ax.lines) == 1
plt.close(fig)

fig, ax = plt.subplots(1,1)
m[0].plot(ax, plotElement = m[0].allElements[1])
assert len(ax.lines) == 1
plt.close(fig)


fig, axL = plt.subplots(1,1)
axR = ax.twinx()
m[0].plotTwoAxis(Lelements=[m[0].allElements[0]], Relements = m[0].allElements[1:], axL=axL, axR=axR)
assert len(axL.lines) == 1
assert len(axR.lines) == len(m[0].allElements)-1
plt.close(fig)

fig, ax = plt.subplots(1,1)
m[0].plotPhases(ax)
assert len(ax.lines) == m[2]
plt.close(fig)

fig, ax = plt.subplots(1,1)
m[0].plotPhases(ax, plotPhase=m[0].phases[0])
assert len(ax.lines) == 1
plt.close(fig)

0 comments on commit cba3839

Please sign in to comment.