Skip to content

Commit

Permalink
interpolation functions verification
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricix committed Apr 15, 2023
1 parent 2a9dfff commit b993348
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
45 changes: 44 additions & 1 deletion modules/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,10 @@ def test_interpolation_functions(x1,x2,xI,L,shape_type):
import numpy as np
import matplotlib.pyplot as plt

# particle position
x = np.linspace(x1,x2,num=500);

# interpolation function values and its gradients
ni = np.zeros_like(x)
dni = np.zeros_like(x)

Expand All @@ -186,4 +188,45 @@ def test_interpolation_functions(x1,x2,xI,L,shape_type):
plt.legend()
plt.ylabel(r"$N_I$, $dN_I/dx$")
plt.xlabel(r"x")
plt.show()
plt.show()

def get_interpolation_functions(x1,x2,xI,L,shape_type):
"""
Returns the interpolation functions Ni and its gradients dNi
Arguments
---------
x1 : float
initial particle position
x2 : float
final particle position
xI : float
fixed node position
L : float
element length
shape_type: string
interpolation function type, may be 'linear' or 'cpGIMP'
"""
import numpy as np
import matplotlib.pyplot as plt

# particle position
x = np.linspace(x1,x2,num=500);

# interpolation function values and its gradients
ni = np.zeros_like(x)
dni = np.zeros_like(x)

# linear shape function
if (shape_type=='linear'):
for i in range(len(x)):
ni[i] = NiLinear(x[i],xI,L)
dni[i] = dNiLinear(x[i],xI,L)

# cpGIMP shape function
elif (shape_type=='cpGIMP'):
for i in range(len(x)):
ni[i] = NicpGIMP(L,L/4,x[i],xI)
dni[i] = dNicpGIMP(L,L/4,x[i],xI)

return [x, ni, dni]
55 changes: 55 additions & 0 deletions tests/interpolation_functions_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 17 08:52:30 2021
Author
------
Fabricio Fernandez (<[email protected]>)
Purpose
-------
This examples tests the interpolation functions and its derivative.
"""

import matplotlib.pyplot as plt

# include the modules' path to the current path
import sys
sys.path.append("..")

# local modules
from modules import shape as shape# for interpolation

# domain
L = 2

# elements
nelem = 2

# cell dimension
le = L/nelem

# get linear interpolation functions values in nodes
[x1,n1_linear,dn1_linear] = shape.get_interpolation_functions(x1=0,x2=L,xI=0,L=L/nelem,shape_type="linear")
[x2,n2_linear,dn2_linear] = shape.get_interpolation_functions(x1=0,x2=L,xI=L/2,L=L/nelem,shape_type="linear")
[x3,n3_linear,dn3_linear] = shape.get_interpolation_functions(x1=0,x2=L,xI=L,L=L/nelem,shape_type="linear")

plt.plot(x1,n1_linear,"--",label="n1-linear")
plt.plot(x2,n2_linear,"--",label="n2-linear")
plt.plot(x3,n3_linear,"--",label="n3-linear")
plt.plot(x1,n1_linear+n2_linear+n3_linear,"--",label="n1+n2+n3-linear")
plt.legend()

# get cpgimp interpolation functions values
[x1,n1_cpgimp,dn1_cpgimp] = shape.get_interpolation_functions(x1=0,x2=L,xI=0,L=L/nelem,shape_type="cpGIMP")
[x2,n2_cpgimp,dn2_cpgimp] = shape.get_interpolation_functions(x1=0,x2=L,xI=L/2,L=L/nelem,shape_type="cpGIMP")
[x3,n3_cpgimp,dn3_cpgimp] = shape.get_interpolation_functions(x1=0,x2=L,xI=L,L=L/nelem,shape_type="cpGIMP")

plt.plot(x1,n1_cpgimp, label="n1-cpGIMP")
plt.plot(x2,n2_cpgimp, label="n2-cpGIMP")
plt.plot(x3,n3_cpgimp, label="n3-cpGIMP")
plt.plot(x1,n1_cpgimp+n2_cpgimp+n3_cpgimp, label="n1+n2+n3-cpGIMP")
plt.legend()

0 comments on commit b993348

Please sign in to comment.