# Conversion to ufl form

I have to convert the following equations into ufl form.

I am using the following code.

from dolfin import *
import numpy as np

L, B, H = 20., 0.5, 1.

Nx = 200
Ny = int(B/L*Nx)+1
Nz = int(H/L*Nx)+1

mesh = BoxMesh(Point(0.,0.,0.),Point(L,B,H), Nx, Ny, Nz)

E, nu = Constant(1e5), Constant(0.)
rho = Constant(1e-3)

# Lame coefficient for constitutive relation
mu = E/2./(1+nu)
lmbda = E*nu/(1+nu)/(1-2*nu)

def eps(v):
def sigma(v):
dim = v.geometric_dimension()
return 2.0*mu*eps(v) + lmbda*tr(eps(v))*Identity(dim)

V = VectorFunctionSpace(mesh, 'Lagrange', degree=1)
u_ = TrialFunction(V)
du = TestFunction(V)

def left(x, on_boundary):
return near(x[0],0.)

bc = DirichletBC(V, Constant((0.,0.,0.)), left)

k_form = inner(sigma(du),eps(u_))*dx
l_form = Constant(1.)*u_[0]*dx
K = PETScMatrix()
b = PETScVector()
assemble_system(k_form, l_form, bc, A_tensor=K, b_tensor=b)

m_form = rho*dot(du,u_)*dx
M = PETScMatrix()
assemble(m_form, tensor=M)

eigensolver = SLEPcEigenSolver(K, M)
eigensolver.parameters['problem_type'] = 'gen_hermitian'
eigensolver.parameters['spectral_transform'] = 'shift-and-invert'
eigensolver.parameters['spectral_shift'] = 0.

N_eig = 6   # number of eigenvalues
print("Computing {} first eigenvalues...".format(N_eig))
eigensolver.solve(N_eig)

# Exact solution computation
from scipy.optimize import root
from math import cos, cosh
falpha = lambda x: cos(x)*cosh(x)+1
alpha = lambda n: root(falpha, (2*n+1)*pi/2.)['x'][0]

# Set up file for exporting results
file_results = XDMFFile("modal_analysis.xdmf")
file_results.parameters["flush_output"] = True
file_results.parameters["functions_share_mesh"] = True

eig_l = []
eigenmodes = []
# Extraction
for i in range(N_eig):
# Extract eigenpair
r, c, rx, cx = eigensolver.get_eigenpair(i)

# 3D eigenfrequency
freq_3D = sqrt(r)/2/pi

# Beam eigenfrequency
if i % 2 == 0: # exact solution should correspond to weak axis bending
I_bend = H*B**3/12.
else:          #exact solution should correspond to strong axis bending
I_bend = B*H**3/12.
freq_beam = alpha(i/2)**2*sqrt(float(E)*I_bend/(float(rho)*B*H*L**4))/2/pi

print("Solid FE: {:8.5f} [Hz]   Beam theory: {:8.5f} [Hz]".format(freq_3D, freq_beam))

# Initialize function and assign eigenvector
eigenmode = Function(V,name="Eigenvector "+str(i))
eigenmode.vector()[:] = rx

eigenmodes.append(eigenmode)
eig_l.append(freq_3D)

eff_m=[]
u = Function(V)
u.interpolate(Constant((0, 1, 0)))
combined_mass = 0
for i, xi in enumerate(eigenmodes):
mi = assemble(action(action(m_form, xi), xi))
eff_m.append(mi)
print(mi)

zi=0.05
def tff(fi,f,zi):
return 1/((2*np.pi)**2*(fi**2-f**2+2j*zi*f*fi))

for f in range(10,15):
tf=[tff(eig_l[i],f,zi)/eff_m[i] for i in range(0,N_eig)]
tfd=np.diag(tf)
print(tfd)


This part needs to be converted to ufl form

here fi is the natural frequency of i^{th} mode. f is the external frequency. \zeta_{i} is the damping ratio. Itâ€™s python implementation is as follows:

zi=0.05
def tff(fi,f,zi):
return 1/((2*np.pi)**2*(fi**2-f**2+2j*zi*f*fi))

for f in range(10,15):
tf=[tff(eig_l[i],f,zi)/eff_m[i] for i in range(0,N_eig)]
tfd=np.diag(tf)
print(tfd)


I want to reach up to tfd using ufl form.