# Scale vector function by scalar function

**URL:** <https://fenicsproject.discourse.group/t/scale-vector-function-by-scalar-function/10638>\
**Category:** General\
**Created:** [March 9, 2023, 3:02pm UTC](https://fenicsproject.discourse.group/t/scale-vector-function-by-scalar-function/10638 "2023-03-09T15:02:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ekrem\_Ekici](https://avatars.discourse-cdn.com/v4/letter/e/977dab/32.png) [@Ekrem\_Ekici](https://fenicsproject.discourse.group/u/Ekrem_Ekici)\
**Post date:** [March 9, 2023, 3:02pm UTC](https://fenicsproject.discourse.group/t/scale-vector-function-by-scalar-function/10638/1 "2023-03-09T15:02:45Z")

</div>

Hi,

I want to scale vector-valued function with a scalar function. Since function spaces of those functions are different, I confused how can I implement that. Here is the MWE;

```auto
from dolfinx.fem import Function, FunctionSpace, Expression
from ufl import dx, VectorElement
from mpi4py import MPI
from dolfinx.mesh import create_unit_square

n_elem = 4
mesh= create_unit_square(MPI.COMM_WORLD, n_elem, n_elem)

V = FunctionSpace(mesh, ('CG',1))

f = Function(V)
f.interpolate(lambda x: x[0]**2)

v_cg = VectorElement("CG", mesh.ufl_cell(), 2)
Q = FunctionSpace(mesh, v_cg)

# I tried to interpolate f onto the vector function space;
f_vector = Function(Q)
f_expr = Expression(f, Q.element.interpolation_points())
f_vector.interpolate(f_expr) 

v = Function(Q)
v.interpolate(lambda x: (-x[1], x[0]))

result_vector = Function(Q)
result_vector.x.array[:] = v.x.array[:]/f_vector.x.array[:]

```

I get this error below;

```auto
File "/home/ee331/Dev/Venvs/v060/lib/python3.8/dist-packages/dolfinx/fem/function.py", line 361, in _
    self._cpp_object.interpolate(expr._cpp_object, cells)
RuntimeError: Function value size not equal to Expression value size

```

Any help would be much appreciated!

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [March 9, 2023, 3:11pm UTC](https://fenicsproject.discourse.group/t/scale-vector-function-by-scalar-function/10638/2 "2023-03-09T15:11:40Z")

</div>

Consider the following:

```python
from dolfinx.fem import Function, FunctionSpace, Expression
from ufl import dx, VectorElement
from mpi4py import MPI
from dolfinx.mesh import create_unit_square

n_elem = 4
mesh = create_unit_square(MPI.COMM_WORLD, n_elem, n_elem)

V = FunctionSpace(mesh, ('CG', 1))

f = Function(V)
f.interpolate(lambda x: x[0]**2)

v_cg = VectorElement("CG", mesh.ufl_cell(), 2)
Q = FunctionSpace(mesh, v_cg)

v = Function(Q)
v.interpolate(lambda x: (-x[1], x[0]))

vs = Function(Q)
vs.interpolate(Expression(v*f, Q.element.interpolation_points()))
print(vs.x.array)

```

or similarly:

```python
vs.interpolate(Expression(v/f, Q.element.interpolation_points()))

```

---

<div class="post-metadata">

**Author:** ![Ekrem\_Ekici](https://avatars.discourse-cdn.com/v4/letter/e/977dab/32.png) [@Ekrem\_Ekici](https://fenicsproject.discourse.group/u/Ekrem_Ekici)\
**Post date:** [March 10, 2023, 12:37pm UTC](https://fenicsproject.discourse.group/t/scale-vector-function-by-scalar-function/10638/3 "2023-03-10T12:37:11Z")

</div>

If I want to normalize the magnitude of the vector-valued function, how should I scale that; I tried the following;

```auto
from dolfinx.fem import Function, FunctionSpace, Expression
from ufl import dx, VectorElement
from mpi4py import MPI
from dolfinx.mesh import create_unit_square
import numpy as np

n_elem = 4
mesh = create_unit_square(MPI.COMM_WORLD, n_elem, n_elem)

V = FunctionSpace(mesh, ('CG', 1))

f = Function(V)
f.interpolate(lambda x: x[0]**2)

v_cg = VectorElement("CG", mesh.ufl_cell(), 2)
Q = FunctionSpace(mesh, v_cg)

v = Function(Q)
v.interpolate(lambda x: (-x[1], x[0]))

vs = Function(Q)
vs.interpolate(Expression(v*(f), Q.element.interpolation_points()))

def normalize(mesh, vs):
    array = vs.x.array
    max_temp = mesh.comm.allreduce(np.amax(array), op=MPI.MAX)
    vs.x.array[:] = array/max_temp
    return vs

vs = normalize(mesh,vs)

# Visualize
from dolfinx.io import XDMFFile
xdmf = XDMFFile(mesh.comm, "vs.xdmf", "w")
xdmf.write_mesh(mesh)
xdmf.write_function(vs)

```

But it didn’t work. Where am I doing wrong?

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [March 10, 2023, 1:55pm UTC](https://fenicsproject.discourse.group/t/scale-vector-function-by-scalar-function/10638/4 "2023-03-10T13:55:11Z")

</div>

> [@Ekrem\_Ekici](#):
>
> ```auto
> array = vs.x.array
> max_temp = mesh.comm.allreduce(np.amax(array), op=MPI.MAX)
> 
> ```

This is not the magnitude of the vector valued function.  
The magnitude is

```python
mag = ufl.sqrt(vs[0]**2 + vs[1]**2)

```

and can be incorporated into the the `dolfinx.fem.Expression` above.  
If you want the max magnitude of all entries, I would interpolate the magnitude into a suitable space, take it’s max value (with allreduce), and divide by it.

---

<div class="post-metadata">

**Author:** ![Ekrem\_Ekici](https://avatars.discourse-cdn.com/v4/letter/e/977dab/32.png) [@Ekrem\_Ekici](https://fenicsproject.discourse.group/u/Ekrem_Ekici)\
**Post date:** [March 10, 2023, 3:15pm UTC](https://fenicsproject.discourse.group/t/scale-vector-function-by-scalar-function/10638/5 "2023-03-10T15:15:10Z")

</div>

Many thanks, here is the code in case anyone needs it;

```auto
from dolfinx.fem import Function, FunctionSpace, Expression
from ufl import dx, VectorElement, sqrt
from mpi4py import MPI
from dolfinx.mesh import create_unit_square
import numpy as np

n_elem = 4
mesh = create_unit_square(MPI.COMM_WORLD, n_elem, n_elem)

V = FunctionSpace(mesh, ('CG', 1))

f = Function(V)
f.interpolate(lambda x: x[0]**2)

v_cg = VectorElement("CG", mesh.ufl_cell(), 2)
Q = FunctionSpace(mesh, v_cg)

v = Function(Q)
v.interpolate(lambda x: (-x[1], x[0]))

vs = Function(Q)
vs.interpolate(Expression(v*(f), Q.element.interpolation_points()))

def normalize(mesh, vs):
    array = vs.x.array
    mag = sqrt(vs[0]**2 + vs[1]**2)
    V = FunctionSpace(mesh,("CG", 2))
    vs_mag = Function(V)
    vs_mag.interpolate(Expression(mag, V.element.interpolation_points()))
    max_mag = mesh.comm.allreduce(np.amax(vs_mag.x.array), op=MPI.MAX)

    vs.x.array[:] = array/max_mag
    return vs

vs = normalize(mesh,vs)

# Visualize
from dolfinx.io import XDMFFile
xdmf = XDMFFile(mesh.comm, "vs.xdmf", "w")
xdmf.write_mesh(mesh)
xdmf.write_function(vs)

```
