Making function from numpy array at discrete points


74
views
0
3 months ago by
Suppose I made a numpy array which each element is the value of the function at a location xi, yj . Is there a way I can interpolate it somehow into an expression (call it w) in order to be used in the variational form?

Right now I set w to constant 1, and it works great.  Just want to have a little bit more general code.

Thanks a ton.


from dolfin import *
import matplotlib.pyplot as plt
import numpy as np


def helmholtz_state(w,x_size,y_size):

	# Create mesh and define function space
	mesh = UnitSquareMesh(x_size, y_size)
	element = VectorElement('P', triangle, 1, dim=2)
	V = FunctionSpace(mesh, element)

	# Define variational problem
	v_r,v_i = TestFunctions(V)
	u = Function(V)
	u_r,u_i = split(u)
	uo_r = Expression("36*pi*pi*cos(6*pi*x[1])",degree=2)
	uo_i = Expression("36*pi*pi*sin(6*pi*x[1])",degree=2)
	ko= Constant(6*pi)
	kosquared= Constant(36*pi*pi)
	q= Constant(.75)
	w=Expression("1.0*( pow(x[0],2) + pow(x[1],2) < 0.707)",degree=2)
	
	F = inner(grad(u_r),grad(v_r))*dx + inner(grad(u_i),grad(v_i))*dx + ko*u_i*v_i*ds -ko*u_r*v_r*ds - kosquared*(1+q*w)*u_r*v_r*dx -kosquared*(1+q*w)*u_i*v_i*dx - kosquared*uo_r*q*w*v_r*dx - kosquared*uo_i*q*w*v_i*dx  

	# Compute solution
	solve(F == 0, u)

	u_r,u_i = u.split()

	# Save solution in VTK format
	# file = File("poisson.pvd")
	# file << u

	file = File("ur.pvd")
	file << u_r

	file = File("ui.pvd")
	file << u_i
	dolfin,plot(u_r)
	plt.show()
	dolfin,plot(u_i)
	plt.show()

	# Plot solution
	# plot(u, interactive=True)


​
Community: FEniCS Project
Please login to add an answer/comment or follow this question.

Similar posts:
Search »