# Affine transformation of function in Parallel

**URL:** <https://fenicsproject.discourse.group/t/affine-transformation-of-function-in-parallel/4782>\
**Category:** Uncategorized\
**Created:** [December 22, 2020, 6:50pm UTC](https://fenicsproject.discourse.group/t/affine-transformation-of-function-in-parallel/4782 "2020-12-22T18:50:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkhandag](https://avatars.discourse-cdn.com/v4/letter/p/d26b3c/32.png) [@pkhandag](https://fenicsproject.discourse.group/u/pkhandag)\
**Post date:** [December 22, 2020, 6:50pm UTC](https://fenicsproject.discourse.group/t/affine-transformation-of-function-in-parallel/4782/1 "2020-12-22T18:50:49Z")

</div>

Hi, I want to do affine transformation of a function in parallel. Below is the sample code I am using from : [Affine transformation: Translation X-Axis of Function](https://fenicsproject.discourse.group/t/affine-transformation-translation-x-axis-of-function/3438/9)  
This works fine in serial but gives incorrect result in parallel.

```auto
from dolfin import * 
from dolfin_adjoint import *

import matplotlib.pyplot as plt
import numpy as np

lx = 1.0
xmin=0.0
xmax=1.0

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

u_0 = interpolate(Expression("x[0]",degree=3), V)

# Shift spatially along x the dofs
b = lx/2.0 #shift along x-axis

class ShiftedExpr(UserExpression):
    def __init__ (self,func,**kwargs):
        super(). __init__ (**kwargs)
        self.func = func
    def eval(self,values,x):
        x0_shift = x[0] - b
        if(x0_shift < xmin):
            x0_shift += lx
        x_shift = np.array([x0_shift,x[1]])
        values[0] = self.func(x_shift)
    def value_shape(self):
        return ()
    
u_1 = project(ShiftedExpr(u_0),V)

```

The problem in parallel seems to be in the line:  
`u_1 = project(ShiftedExpr(u_0),V)`

[How to interpolate between non-matching meshes in parallel?](https://fenicsproject.discourse.group/t/how-to-interpolate-between-non-matching-meshes-in-parallel/859/4) : I also tried to use transfer\_function given in this link instead of project(), but it fails too.  
Could this be resolved for parallel?

---

<div class="post-metadata">

**Author:** ![pkhandag](https://avatars.discourse-cdn.com/v4/letter/p/d26b3c/32.png) [@pkhandag](https://fenicsproject.discourse.group/u/pkhandag)\
**Post date:** [January 14, 2021, 12:26am UTC](https://fenicsproject.discourse.group/t/affine-transformation-of-function-in-parallel/4782/2 "2021-01-14T00:26:48Z")

</div>

I found that `LagrangeInterpolator` works in parallel as an alternative to `interpolate` or `project` for Lagrange functions as per this link: [Function interpolation in parallel? - FEniCS Q&A](https://fenicsproject.org/qa/7758/function-interpolation-in-parallel/)  
But, with `LagrangeInterpolator` used in parallel, my code is not completing.

Am I missing anything here?  
Any help will be highly appreciated! Thanks!

After running in parallel, the code is stuck with the similar message shown below:  
"Process 0: Computed bounding box tree with 249 nodes for 125 points.  
Process 3: Building point search tree to accelerate distance queries.  
Process 3: Computed bounding box tree with 263 nodes for 132 points. "

Here is the minimal test code that I am running in parallel.

```auto
from dolfin import * 
from dolfin_adjoint import *

import matplotlib.pyplot as plt
import numpy as np

lx = 1.0
xmin=0.0
xmax=1.0

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

# u_0 = interpolate(Expression("x[0]",degree=3), V)

# Shift spatially along x the dofs
b = lx/2.0 #shift along x-axis

class ShiftedExpr(UserExpression):
    def __init__ (self,func,**kwargs):
        super(). __init__ (**kwargs)
        self.func = func
    def eval(self,values,x):
        x0_shift = x[0] - b
        if(x0_shift < xmin):
            x0_shift += lx
        x_shift = np.array([x0_shift,x[1]])
        values[0] = self.func(x_shift)
    def value_shape(self):
        return ()
    
# u_1 = project(ShiftedExpr(u_0),V)

u_0= Function(V)
u_0.set_allow_extrapolation(True) 

u_1=Function(V)
LagrangeInterpolator.interpolate(u_1, ShiftedExpr(u_0))

```
