import discretisedfield as df
p1 = (-50, -50, -50)
p2 = (50, 50, 50)
n = (2, 2, 2)
mesh = df.Mesh(p1=p1, p2=p2, n=n)
discretisedfield.Field
asigns a value to every discretisation cell of the mesh. There are two main parameters of the field that define its value: dim
and value
. dim
is a positive integer which defines the dimension of the value. For instance, if dim=1
, one number is assigned to each discretisation cell and that field can be considered as a scalar field. On the other hand, if dim=3
, the value is a three dimensional vector. Let us start with a scalar field.
dim = 1
After all these parameters are defined, we can instantiate a field.
field = df.Field(mesh, dim=1)
We only defined the dimension of the value, but we did not provide any information about what that value actually is. By default, if value
is not provided, a field is initialised as a "zero-field". By calling the field with a point coordinates, we can sample the value at that point.
point = (2, 3, 5)
field(point)
0.0
Sampling the value of the field always returns a numpy array, whose length equals the dimension of the field. Let us now provide the same value for the field at all its points. This can be achieved by simply passing a number.
value = 1.23
field.value = value
If we sample the field now, we can see that we do not have a "zero-field" anymore.
field(point)
1.23
The underlying attribute that keeps the values of all points in the field is discretisedfield.Field.array
.
field.array
array([[[[1.23], [1.23]], [[1.23], [1.23]]], [[[1.23], [1.23]], [[1.23], [1.23]]]])
(field.array == 1.23).all()
True
Its shape is
field.array.shape
(2, 2, 2, 1)
The first three numbers are the number of discretisation cells in all three dimensions
field.mesh.n
(2, 2, 2)
and the last number is the dimension of the value in the field
field.dim
1
So far, we investigated a basic definition of a scalar field. Now, we are going to define a vector field with dim=3
, so that the vector at each point is pointing in the positive $y$ direction (value=(0, 1, 0)
). In addition to those values, we are going to provide another parameter which is optional and could serve to distinguish different fields - name
.
field = df.Field(mesh, dim=3, value=(0, 1, 0))
If we check the shape of the underlying array
, we can see that the last number is now 3.
field.array.shape
(2, 2, 2, 3)
The values are now three-dimensional
field.array
array([[[[0., 1., 0.], [0., 1., 0.]], [[0., 1., 0.], [0., 1., 0.]]], [[[0., 1., 0.], [0., 1., 0.]], [[0., 1., 0.], [0., 1., 0.]]]])
field(point)
(0.0, 1.0, 0.0)
If we want to change the value of the field, we can do it via discretisedfield.Field.value
.
field.value = [1, 0, -3]
The values are now changed
field.array
array([[[[ 1., 0., -3.], [ 1., 0., -3.]], [[ 1., 0., -3.], [ 1., 0., -3.]]], [[[ 1., 0., -3.], [ 1., 0., -3.]], [[ 1., 0., -3.], [ 1., 0., -3.]]]])
Note that we have used a list instead of a tuple to set a new value. Both give the same result and it is true for any iterable of length 3.