|
1 | 1 | from festim import SurfaceQuantity |
2 | 2 | import fenics as f |
| 3 | +import numpy as np |
3 | 4 |
|
4 | 5 |
|
5 | 6 | class TotalSurface(SurfaceQuantity): |
@@ -58,3 +59,166 @@ def title(self): |
58 | 59 |
|
59 | 60 | def compute(self): |
60 | 61 | return f.assemble(self.function * self.ds(self.surface)) |
| 62 | + |
| 63 | + |
| 64 | +class TotalSurfaceCylindrical(TotalSurface): |
| 65 | + """ |
| 66 | + Computes the total value of a field on a given surface |
| 67 | + int(f ds) |
| 68 | + ds is the surface measure in cylindrical coordinates. |
| 69 | + ds = r dr dtheta |
| 70 | +
|
| 71 | + Args: |
| 72 | + field (str, int): the field ("solute", 0, 1, "T", "retention") |
| 73 | + surface (int): the surface id |
| 74 | + azimuth_range (tuple, optional): Range of the azimuthal angle |
| 75 | + (theta) needs to be between 0 and 2 pi. Defaults to (0, 2 * np.pi) |
| 76 | +
|
| 77 | + Attributes: |
| 78 | + field (str, int): the field ("solute", 0, 1, "T", "retention") |
| 79 | + surface (int): the surface id |
| 80 | + title (str): the title of the derived quantity |
| 81 | + show_units (bool): show the units in the title in the derived quantities |
| 82 | + file |
| 83 | + function (dolfin.function.function.Function): the solution function of |
| 84 | + the field |
| 85 | + r (ufl.indexed.Indexed): the radius of the cylinder |
| 86 | +
|
| 87 | + .. note:: |
| 88 | + Units are in H/m in 1D, H in 2D domains for hydrogen concentration |
| 89 | + and K m in 1D, K m2 in 2D domains for temperature |
| 90 | + """ |
| 91 | + |
| 92 | + def __init__(self, field, surface, azimuth_range=(0, 2 * np.pi)) -> None: |
| 93 | + super().__init__(field=field, surface=surface) |
| 94 | + self.r = None |
| 95 | + self.azimuth_range = azimuth_range |
| 96 | + |
| 97 | + @property |
| 98 | + def export_unit(self): |
| 99 | + # obtain domain dimension |
| 100 | + try: |
| 101 | + dim = self.function.function_space().mesh().topology().dim() |
| 102 | + except AttributeError: |
| 103 | + dim = self.dx._domain._topological_dimension |
| 104 | + # TODO we could simply do that all the time |
| 105 | + # return unit depending on field and dimension of domain |
| 106 | + if self.field == "T": |
| 107 | + return f"K m{dim}".replace(" m1", " m") |
| 108 | + else: |
| 109 | + return f"H m{dim-2}".replace(" m0", "") |
| 110 | + |
| 111 | + @property |
| 112 | + def azimuth_range(self): |
| 113 | + return self._azimuth_range |
| 114 | + |
| 115 | + @azimuth_range.setter |
| 116 | + def azimuth_range(self, value): |
| 117 | + if value[0] < 0 or value[1] > 2 * np.pi: |
| 118 | + raise ValueError("Azimuthal range must be between 0 and pi") |
| 119 | + self._azimuth_range = value |
| 120 | + |
| 121 | + @property |
| 122 | + def allowed_meshes(self): |
| 123 | + return ["cylindrical"] |
| 124 | + |
| 125 | + def compute(self): |
| 126 | + |
| 127 | + if self.r is None: |
| 128 | + mesh = ( |
| 129 | + self.function.function_space().mesh() |
| 130 | + ) # get the mesh from the function |
| 131 | + rthetaz = f.SpatialCoordinate(mesh) # get the coordinates from the mesh |
| 132 | + self.r = rthetaz[0] # only care about r here |
| 133 | + |
| 134 | + tot_surf = f.assemble(self.function * self.r * self.ds(self.surface)) |
| 135 | + tot_surf *= self.azimuth_range[1] - self.azimuth_range[0] |
| 136 | + |
| 137 | + return tot_surf |
| 138 | + |
| 139 | + |
| 140 | +class TotalSurfaceSpherical(TotalSurface): |
| 141 | + """ |
| 142 | + Computes the total value of a field on a given surface |
| 143 | + int(f ds) |
| 144 | + ds is the surface measure in spherical coordinates. |
| 145 | + ds = r**2 sin(theta) dtheta dphi |
| 146 | +
|
| 147 | + Args: |
| 148 | + field (str, int): the field ("solute", 0, 1, "T", "retention") |
| 149 | + surface (int): the surface id |
| 150 | + azimuth_range (tuple, optional): Range of the azimuthal angle |
| 151 | + (phi) needs to be between 0 and 2 pi. Defaults to (0, 2 * np.pi) |
| 152 | + polar_range (tuple, optional): Range of the polar angle |
| 153 | + (theta) needs to be between 0 and pi. Defaults to (0, np.pi). |
| 154 | +
|
| 155 | + Attributes: |
| 156 | + field (str, int): the field ("solute", 0, 1, "T", "retention") |
| 157 | + surface (int): the surface id |
| 158 | + title (str): the title of the derived quantity |
| 159 | + show_units (bool): show the units in the title in the derived quantities |
| 160 | + file |
| 161 | + function (dolfin.function.function.Function): the solution function of |
| 162 | + the field |
| 163 | + r (ufl.indexed.Indexed): the radius of the cylinder |
| 164 | +
|
| 165 | + .. note:: |
| 166 | + Units are in H for hydrogen concentration |
| 167 | + and K in 1D, K m in 2D domains for temperature |
| 168 | + """ |
| 169 | + |
| 170 | + def __init__( |
| 171 | + self, field, surface, azimuth_range=(0, 2 * np.pi), polar_range=(0, np.pi) |
| 172 | + ) -> None: |
| 173 | + super().__init__(field=field, surface=surface) |
| 174 | + self.r = None |
| 175 | + self.azimuth_range = azimuth_range |
| 176 | + self.polar_range = polar_range |
| 177 | + |
| 178 | + @property |
| 179 | + def export_unit(self): |
| 180 | + if self.field == "T": |
| 181 | + return f"K m2" |
| 182 | + else: |
| 183 | + return "H" |
| 184 | + |
| 185 | + @property |
| 186 | + def azimuth_range(self): |
| 187 | + return self._azimuth_range |
| 188 | + |
| 189 | + @azimuth_range.setter |
| 190 | + def azimuth_range(self, value): |
| 191 | + if value[0] < 0 or value[1] > 2 * np.pi: |
| 192 | + raise ValueError("Azimuthal range must be between 0 and 2 pi") |
| 193 | + self._azimuth_range = value |
| 194 | + |
| 195 | + @property |
| 196 | + def polar_range(self): |
| 197 | + return self._polar_range |
| 198 | + |
| 199 | + @polar_range.setter |
| 200 | + def polar_range(self, value): |
| 201 | + if value[0] < 0 or value[1] > np.pi: |
| 202 | + raise ValueError("Polar range must be between 0 and pi") |
| 203 | + self._polar_range = value |
| 204 | + |
| 205 | + @property |
| 206 | + def allowed_meshes(self): |
| 207 | + return ["spherical"] |
| 208 | + |
| 209 | + def compute(self): |
| 210 | + |
| 211 | + if self.r is None: |
| 212 | + mesh = ( |
| 213 | + self.function.function_space().mesh() |
| 214 | + ) # get the mesh from the function |
| 215 | + rthetaphi = f.SpatialCoordinate(mesh) # get the coordinates from the mesh |
| 216 | + self.r = rthetaphi[0] # only care about r here |
| 217 | + |
| 218 | + tot_surf = f.assemble(self.function * self.r**2 * self.ds(self.surface)) |
| 219 | + |
| 220 | + tot_surf *= (self.azimuth_range[1] - self.azimuth_range[0]) * ( |
| 221 | + np.cos(self.polar_range[0]) - np.cos(self.polar_range[1]) |
| 222 | + ) |
| 223 | + |
| 224 | + return tot_surf |
0 commit comments