Apart from setting the value of a field, it is sometimes necessary to normalise it to a certain value. Also sometimes in the case of vector fields, it is required to set the value of certain points in the mesh to be zero. This can be achieved using discretisedfield.Field.norm
.
import discretisedfield as df
p1 = (-50, -50, -50)
p2 = (50, 50, 50)
cell = (5, 5, 5)
mesh = df.Mesh(p1=p1, p2=p2, cell=cell)
value = (1, 1, 1)
field = df.Field(mesh, dim=3, value=value)
Asking for a norm of the field returns a scalar field with norm values.
field.norm
field.dim
3
field.norm(field.mesh.region.random_point())
1.7320508075688772
If we want to normalise the field with a new value, we write
field.norm = 1
The new value of the field is
field(field.mesh.region.random_point())
(0.5773502691896258, 0.5773502691896258, 0.5773502691896258)
and the norm is
field.norm(field.mesh.region.random_point())
1.0
If we now change the value of the field, the norm is not valid anymore
field.value = (-1, -1, 10)
field.norm(field.mesh.region.random_point())
10.099504938362077
Instead of a constant, a Pyhton function or another field object can be used to set up the norm. Let us say we want to define a sphere inside the mesh of radius 50. This means that the norm of the field outside the sphere is zero and inside it is 1.
def norm_function(pos):
x, y, z = pos
if x**2 + y**2 + z**2 <= 50**2:
return 1
else:
return 0
sphere_field = df.Field(mesh, dim=3, value=(10, 0, 0), norm=norm_function)
If we sample the value of the field outside the sphere we get
sphere_field((-50, -50, -50))
(0.0, 0.0, 0.0)
sphere_field((0, 0, 0))
(1.0, 0.0, 0.0)