Ploting the magnitude of a vectorField

Hello everyone, Im currently trying to solve the Navier Stokes equations, however whenever I plot the velocity results, I get a vector field (as expected). I was wondering if there is any way to plot the magnitude of the velocities instead of the vectorfield itsefl?

My code:

from dolfin import *

#Creación de un mallado. Se va a refinar en los bordes inferiores y superiores
n = 50
mesh = UnitSquareMesh(n,n)

#La malla puede refinarse si es necesario

#Define Taylor-Hood elements (For now, they work best for Navier-Stokes problems)
V = VectorFunctionSpace(mesh, ‘P’, 2) #Function space that solves for velocity
Q = FunctionSpace(mesh, ‘P’, 1) #Function space that solves for pressure

#Define Two sets of Test/Trial functions, as we are working with two unknowns.
u = TrialFunction(V)
v = TestFunction(V)
p = TrialFunction(Q)
q = TestFunction(Q)

def up_boundary(x, on_boundary):
tol = 1E-8
return(on_boundary and abs(x[1]-1)< tol)

def down_boundary(x, on_boundary):
tol = 1E-8
return(on_boundary and abs(x[1])< tol)

def left_boundary(x, on_boundary):
tol = 1E-8
return(on_boundary and abs(x[0])< tol)

def right_boundary(x, on_boundary):
tol = 1E-8
return(on_boundary and abs(x[0]-1)< tol)

DBup = DirichletBC(V, Constant((0,0)) , up_boundary) #Note that constants must be two dimentional
DBdown = DirichletBC(V, Constant((0,0)) , down_boundary)
DBleft = DirichletBC(V, Constant((1,0)) , left_boundary)
DBright = DirichletBC(V, Constant((0,0)) , right_boundary)

bcs = [DBup, DBdown, DBleft, DBright] #Add all Dirichlet BC into a list

Define functions for solutions at previous and current time steps

u_n = Function(V) #Es importante definirlas puesto que voy a iterar en el tiempo
u_ = Function(V)
p_n = Function(Q)
p_ = Function(Q)

#Formulation of the variational form of NavierStokes

#Parameter Creation

T = 1 # final time
num_steps = 100 # number of time steps
dt = T / num_steps # time step size
mu = 10 # dynamic viscosity
rho = 1 # density

Define expressions used in variational forms

U = 0.5*(u_n + u)
n = FacetNormal(mesh) #Esto me crea un vector normal a la superficie planteada
f = Constant((0,0)) #Fuerza constante, la idea es ir cambiando este parametro a ver que ocurre
k = Constant(dt)
mu = Constant(mu)
rho = Constant(rho) #Integers must be changed into constant type

Define symmetric gradient

def epsilon(u):
return sym(nabla_grad(u))

Define stress tensor

def sigma(u, p):
return 2muepsilon(u) - p*Identity(len(u))

#Variational form due to Chorin’s Projection.

#Velocity without pressure calculation
F1 = rho*dot((u - u_n) / k, v)dx + rhodot(dot(u_n, nabla_grad(u_n)), v)*dx + inner(sigma(U, p_n), epsilon(v))dx + dot(p_nn, v)ds - dot(munabla_grad(U)*n, v)*ds - dot(f, v)*dx
a1 = lhs(F1)
L1 = rhs(F1)

#Pressure Correction
a2 = dot(nabla_grad(p), nabla_grad(q))*dx
L2 = dot(nabla_grad(p_n), nabla_grad(q))*dx - (1/k)*div(u_)qdx

#Velocity correction due to pressure
a3 = dot(u, v)*dx
L3 = dot(u_, v)dx - kdot(nabla_grad(p_ - p_n), v)*dx

Assemble matrices

A1 = assemble(a1)
A2 = assemble(a2)
A3 = assemble(a3)

[bc.apply(A1) for bc in bcs] #Apply boundary conditions to A

Create XDMF files for visualization output

xdmffile_u = XDMFFile(‘navier_stokes_cylinder/velocity.xdmf’)
xdmffile_p = XDMFFile(‘navier_stokes_cylinder/pressure.xdmf’)

Save mesh to file (for use in reaction_system.py)

File(‘navier_stokes_cylinder/cylinder.xml.gz’) << mesh

Time-stepping

import matplotlib.pyplot as plt

t = 0
for n in range(num_steps):

# Update current time
t += dt

# Step 1: Tentative velocity step
b1 = assemble(L1)
[bc.apply(b1) for bc in bcs]
solve(A1, u_.vector(), b1 ,"gmres", "ilu",) #Preconditioned settings for linear solving methods that accelerate process

# Step 2: Pressure correction step
b2 = assemble(L2)
#[bc.apply(b2) for bc in bc2]
solve(A2, p_.vector(), b2, "gmres", "amg",)

# Step 3: Velocity correction step
b3 = assemble(L3)
solve(A3, u_.vector(), b3, "gmres", "ilu",)

a  = plot(u_ , title = "Trial simulation")
plt.colorbar(a)
#plt.draw()
plt.savefig("Instance" + str(round(t,2)) + ".jpg") #save as jpg
#plt.pause(0.02)
plt.clf()

# Update previous solution
u_n.assign(u_)
p_n.assign(p_)

# Update progress bar

First of all, you should use 3x` encapsulation to make sure your code is rendered properly.

Secondly, I would advice usage of paraview for such plots.

1 Like

Thanks a lot, that is exactly what I did