Green's function
==============

Fundamental solution
-------------------------------
$E$ here is the fundamental solution for the Laplacian


```python
from sympy import *
from sympy.physics.vector.functions import dot
init_printing()
```


```python
x1, x2, xi1, xi2 = symbols('x_1 x_2 xi_1 xi_2')
E = -1/(2*pi) * log(sqrt((x1-xi1)**2 + (x2-xi2)**2))
E
```

**Task**: Check that $\nabla^2_\xi E = 0$ for $x \neq \xi$.

*Hint*: https://docs.sympy.org/latest/tutorial/calculus.html#derivatives


```python
A = diff(E,x1,2)+diff(E,x2,2)
A
```


```python
simplify(A)
```

Directional derivative
------------------------------


```python
n1, n2 = symbols('n_1 n_2')
n = Matrix([n1,n2])
gradE = Matrix([diff(E,x1),diff(E,x2)])

```

**Task**: Compute the directional derivative $\frac{\partial E}{\partial n}$.


```python
DirDerE = (n1/sqrt(n1**2+n2**2))*diff(E,x1) + (n2/sqrt(n1**2+n2**2))*diff(E,x2)
DirDerE
```

**Task** (optional): Write a function which returns the directional derivative of an expression.


```python
def ddn(expr):
    NewExpr = (n1/sqrt(n1**2+n2**2))*diff(expr,x1) + (n2/sqrt(n1**2+n2**2))*diff(expr,x2)
    #NewExpr = diff(expr,x1) + diff(expr,x2)
    
    return NewExpr
    #pass
```


```python
ddn(E)
```

Reflection principle
----------------------------

For simple geometries Green's function can sometimes be found by reflecting the fundamental solution at the boundary and linearly combining the fundamental solution with its reflection. 



**Task**: Based on $E$, find the solution for the half-space problem
\begin{align*}
    \nabla^2G(x,\xi) &= -\delta(x-\xi), & \xi\in\Omega \\
    G(x,\xi) &= 0, & \xi\in\partial\Omega \\
    \Omega &= \{\xi\in\mathbb{R}^2 : \xi_2 > 0\}
\end{align*}

*Hint*: https://docs.sympy.org/latest/tutorial/basic_operations.html#substitution


```python
G = ddn(E)

Reflection = G.subs(x2, -x2)
TotalGreen = Reflection + G

SubsGreen = TotalGreen.subs(n1,0).subs(n2,-1)

SubsGreen
```

**Check condition**


```python
SubsGreen.subs(xi2,0)
```

**Task**: Based on $E$, find the solution for the half-space problem
\begin{align*}
    \nabla^2G(x,\xi) &= -\delta(x-\xi), & \xi\in\Omega \\
    \frac{\partial G(x,\xi)}{\partial n} &= 0, & \xi\in\partial\Omega \\
    \Omega &= \{\xi\in\mathbb{R}^2 : \xi_2 > 0\}
\end{align*}


```python
Reflection = E.subs(x2, -x2)
TotalGreen = Reflection + E
TotalGreen
```

**Check condition**

\begin{align*}
    \frac{\partial G(x,\xi)}{\partial n} &= 0, & \xi\in\partial\Omega
\end{align*}


```python
ddn(TotalGreen).subs(n1,0).subs(n2,-1).subs(x2,0)
```


```python

```

4ff15ce4cc88e3ec5b1d4cd80516e358c54bcb9d
ipynb
Jupyter Notebook
PythonCodes/Exercises/Class-SEAS/green/green.ipynb
Nicolucas/TEAR
bbeb599cf2bab70fd7a82041336a1a918e8727f2
MIT
2020-02-25T08:05:13.000Z
2020-02-25T08:05:13.000Z
PythonCodes/Exercises/Class-SEAS/green/green.ipynb
Nicolucas/TEAR
bbeb599cf2bab70fd7a82041336a1a918e8727f2
MIT
PythonCodes/Exercises/Class-SEAS/green/green.ipynb
Nicolucas/TEAR
bbeb599cf2bab70fd7a82041336a1a918e8727f2
MIT
Qwen/Qwen-72B
1. YES
2. YES