Field normalisation#
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
.
[1]:
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, nvdim=3, value=value)
Asking for a norm of the field returns a scalar field with norm values.
[2]:
field.norm
[2]:
- Mesh
- Region
- pmin = [-50, -50, -50]
- pmax = [50, 50, 50]
- dims = ['x', 'y', 'z']
- units = ['m', 'm', 'm']
- n = [20, 20, 20]
- Region
- nvdim = 1
[3]:
field.nvdim
[3]:
3
[4]:
field.norm(field.mesh.region.random_point())
/tmp/ipykernel_24263/4270432668.py:1: DeprecationWarning: This method will be removed and should not be used anymore.
field.norm(field.mesh.region.random_point())
[4]:
array([1.73205081])
If we want to normalise the field with a new value, we write
[5]:
field.norm = 1
The new value of the field is
[6]:
field(field.mesh.region.random_point())
/tmp/ipykernel_24263/1582582780.py:1: DeprecationWarning: This method will be removed and should not be used anymore.
field(field.mesh.region.random_point())
[6]:
array([0.57735027, 0.57735027, 0.57735027])
and the norm is
[7]:
field.norm(field.mesh.region.random_point())
/tmp/ipykernel_24263/4270432668.py:1: DeprecationWarning: This method will be removed and should not be used anymore.
field.norm(field.mesh.region.random_point())
[7]:
array([1.])
If we now change the value of the field, the norm is not valid anymore
[8]:
field.update_field_values((-1, -1, 10))
field.norm(field.mesh.region.random_point())
/tmp/ipykernel_24263/4282347518.py:2: DeprecationWarning: This method will be removed and should not be used anymore.
field.norm(field.mesh.region.random_point())
[8]:
array([10.09950494])
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.
[9]:
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, nvdim=3, value=(10, 0, 0), norm=norm_function)
If we sample the value of the field outside the sphere we get
[10]:
sphere_field((-50, -50, -50))
[10]:
array([0., 0., 0.])
[11]:
sphere_field((0, 0, 0))
[11]:
array([1., 0., 0.])