-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #596 from jhdark/materials_class
Materials class and temperature processing
- Loading branch information
Showing
12 changed files
with
325 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from dolfinx import fem | ||
|
||
|
||
def as_fenics_constant(value, mesh): | ||
"""Converts a value to a dolfinx.Constant | ||
Args: | ||
value (float, int or dolfinx.Constant): the value to convert | ||
mesh (dolfinx.mesh.Mesh): the mesh of the domiain | ||
Returns: | ||
dolfinx.Constant: the converted value | ||
Raises: | ||
TypeError: if the value is not a float, an int or a dolfinx.Constant | ||
""" | ||
if isinstance(value, (float, int)): | ||
return fem.Constant(mesh, float(value)) | ||
elif isinstance(value, fem.Constant): | ||
return value | ||
else: | ||
raise TypeError( | ||
f"Value must be a float, an int or a dolfinx.Constant, not {type(value)}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import ufl | ||
import festim as F | ||
|
||
|
||
class Material: | ||
""" | ||
Material class | ||
Args: | ||
D_0 (float or fem.Constant): the pre-exponential factor of the | ||
diffusion coefficient (m2/s) | ||
E_D (float or fem.Constant): the activation energy of the diffusion | ||
coeficient (eV) | ||
name (str): the name of the material | ||
Attributes: | ||
D_0 (float or fem.Constant): the pre-exponential factor of the | ||
diffusion coefficient (m2/s) | ||
E_D (float or fem.Constant): the activation energy of the diffusion | ||
coeficient (eV) | ||
name (str): the name of the material | ||
""" | ||
|
||
def __init__(self, D_0, E_D, name=None) -> None: | ||
self.D_0 = D_0 | ||
self.E_D = E_D | ||
self.name = name | ||
|
||
def get_diffusion_coefficient(self, mesh, temperature): | ||
"""Defines the diffusion coefficient | ||
Args: | ||
mesh (dolfinx.mesh.Mesh): the domain mesh | ||
temperature (dolfinx.fem.Constant): the temperature | ||
Returns: | ||
ufl.algebra.Product: the diffusion coefficient | ||
""" | ||
|
||
# check type of values | ||
D_0 = F.as_fenics_constant(self.D_0, mesh) | ||
E_D = F.as_fenics_constant(self.E_D, mesh) | ||
|
||
return D_0 * ufl.exp(-E_D / F.k_B / temperature) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import festim as F | ||
from dolfinx import fem | ||
import numpy as np | ||
import pytest | ||
import ufl | ||
|
||
test_mesh = F.Mesh1D(vertices=np.array([0.0, 1.0, 2.0, 3.0, 4.0])) | ||
x = ufl.SpatialCoordinate(test_mesh.mesh) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"value", [1, fem.Constant(test_mesh.mesh, 1.0), 1.0, "coucou", 2 * x[0]] | ||
) | ||
def test_temperature_type_and_processing(value): | ||
"""Test that the temperature type is correctly set""" | ||
|
||
if not isinstance(value, (fem.Constant, int, float)): | ||
with pytest.raises(TypeError): | ||
F.as_fenics_constant(value, test_mesh.mesh) | ||
else: | ||
assert isinstance(F.as_fenics_constant(value, test_mesh.mesh), fem.Constant) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import festim as F | ||
import numpy as np | ||
|
||
test_mesh = F.Mesh1D(vertices=np.array([0.0, 1.0, 2.0, 3.0, 4.0])) | ||
|
||
|
||
def test_define_diffusion_coefficient(): | ||
"""Test that the diffusion coefficient is correctly defined""" | ||
T, D_0, E_D = 10, 1.2, 0.5 | ||
|
||
my_mat = F.Material(D_0=D_0, E_D=E_D) | ||
D = my_mat.get_diffusion_coefficient(test_mesh.mesh, T) | ||
|
||
D_analytical = D_0 * np.exp(-E_D / F.k_B / T) | ||
|
||
assert np.isclose(float(D), D_analytical) |
Oops, something went wrong.