Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
39245cf
Initial case setup
IshaanDesai Jun 22, 2021
16fe66a
Defining correct cylinder mesh
IshaanDesai Jun 22, 2021
2829b72
Rectifying autopep8 violation
IshaanDesai Jun 27, 2021
47249ec
Changing conditions to filter boudary nodes
IshaanDesai Jul 3, 2021
cddd741
Removing debugging statements
IshaanDesai Jul 6, 2021
f94ab3d
Correct fixed boundary filtering condition
IshaanDesai Jul 6, 2021
87975d2
Simplifying fixed boundary filter condition
IshaanDesai Jul 6, 2021
1c726ba
Port CalculiX configuration
IshaanDesai Jul 8, 2021
854ef47
Updating README and adding watchpoint in config xml
IshaanDesai Jul 14, 2021
4ef7352
Merge branch 'develop' into add-fenics-elastic-tube-3d
IshaanDesai Jul 26, 2021
a26cd86
Merge branch 'develop' into add-fenics-elastic-tube-3d
IshaanDesai Jul 29, 2021
a1ab338
Merge branch 'develop' into add-fenics-elastic-tube-3d
IshaanDesai Aug 7, 2021
4145e3b
Merge branch 'develop' into add-fenics-elastic-tube-3d
IshaanDesai Jan 16, 2022
fbbe9b1
Merge branch 'develop' into add-fenics-elastic-tube-3d
IshaanDesai Feb 3, 2022
891ee05
Reverting to DisplacementDelta data exchange and testing new implemen…
IshaanDesai Feb 4, 2022
270d718
Add script for plotting watchpoints.
BenjaminRodenberg Feb 5, 2022
635f039
Plot correct data and all three dimensions.
BenjaminRodenberg Feb 5, 2022
9503b4d
Minor corrections
IshaanDesai Feb 6, 2022
6227ca2
Merge branch 'add-fenics-elastic-tube-3d' of github.com:precice/tutor…
IshaanDesai Feb 6, 2022
e599e5d
Adding displacement plot to README
IshaanDesai Feb 7, 2022
9884053
Markdown stuff and renaming output folder
IshaanDesai Feb 7, 2022
c5a4bcc
Correct image name
IshaanDesai Feb 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion elastic-tube-3d/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Elastic tube 3D
permalink: tutorials-elastic-tube-3d.html
keywords: FSI, OpenFOAM, CalculiX, nearest-projection, IMVJ
keywords: FSI, OpenFOAM, CalculiX, FEniCS, nearest-projection, IMVJ
summary: Tutorial for an FSI simulation of a three-dimensional expanding tube scenario
---

Expand All @@ -25,6 +25,8 @@ Solid participant:

* CalculiX. This tutorial is known to work with CalculiX 2.15, but it should also work with newer versions. For more information, have a look at the [CalculiX adapter documentation](https://www.precice.org/adapter-calculix-overview.html).

* FEniCS. The structural model is currently limited to linear elasticity. Currently 3D functionality is experimental in the FEniCS adapter and more details can be found [here](https://github.com/precice/fenics-adapter/pull/133) For more information, have a look at the [FeniCS adapter documentation](https://www.precice.org/adapter-fenics.html).

## Running the simulation

You can start the simulation by running the script `./run.sh` located in each participant directory. OpenFOAM can be executed in parallel by using an additional `run.sh -parallel` flag. The default setting uses 4 MPI ranks.
Expand All @@ -35,6 +37,10 @@ You can visualize the results using paraView or `cgx`(for native CalculiX resul

![result tube](images/tutorials-elastic-tube-3d-tube-result.png)

You can also plot the displacement of the midpoint of the tube by running `sh plot-displacement.sh <filename>`. The displacement plot for each solver combination looks like:

![plot tube](images/tutorials-elastic-tube-3d-plot.png)

{% disclaimer %}
This offering is not approved or endorsed by OpenCFD Limited, producer and distributor of the OpenFOAM software via www.openfoam.com, and owner of the OPENFOAM® and OpenCFD® trade marks.
{% enddisclaimer %}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions elastic-tube-3d/plot-all-displacements.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh

gnuplot -p << EOF
set grid
set title 'displacement at the middle of the tube
set xlabel 'time [s]'
set ylabel 'displacement [m]'
set term pngcairo enhanced size 900,654
set output "images/tutorials-elastic-tube-3d-displacement-all-watchpoints.png"
plot "solid-calculix/precice-Solid-watchpoint-Tube-Midpoint.log" using 1:5 with lines title "OpenFOAM-CalculiX circumferential", \
"solid-calculix/precice-Solid-watchpoint-Tube-Midpoint.log" using 1:6 with lines title "OpenFOAM-CalculiX radial", \
"solid-calculix/precice-Solid-watchpoint-Tube-Midpoint.log" using 1:7 with lines title "OpenFOAM-CalculiX axial", \
"solid-fenics/precice-Solid-watchpoint-Tube-Midpoint.log" using 1:5 with lines title "OpenFOAM-FEniCS circumferential", \
"solid-fenics/precice-Solid-watchpoint-Tube-Midpoint.log" using 1:6 with lines title "OpenFOAM-FEniCS radial", \
"solid-fenics/precice-Solid-watchpoint-Tube-Midpoint.log" using 1:7 with lines title "OpenFOAM-FEniCS axial
EOF
1 change: 1 addition & 0 deletions elastic-tube-3d/precice-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<use-mesh name="Fluid-Mesh-Faces" from="Fluid" />
<write-data name="DisplacementDelta" mesh="Solid-Mesh" />
<read-data name="Force" mesh="Solid-Mesh" />
<watch-point mesh="Solid-Mesh" name="Tube-Midpoint" coordinate="0.0;0.005;0.025" />

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to generally keep this watchpoint or did you just introduce it for verification of the results? Please add an image of the results to the README.md of this case similar to https://github.com/precice/tutorials/tree/master/perpendicular-flap#post-processing. This helps a lot when releasing and checking the cases. Please also provide a script (you can probably just use a modified version of https://github.com/precice/tutorials/blob/master/perpendicular-flap/plot-all-displacements.sh).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already added a script to this PR. Here is the plot that I created:

tutorials-elastic-tube-3d-displacement-all-watchpoints

The circumferential displacement is (for both solid solvers) negligible as expected. The radial and axial displacement also looks good (as far as I can judge here...). What worries me a bit is the large difference between the CalculiX and the FEniCS case. Do you also observe this? It might also be a problem with my system, because I did not update CalculiX or the adapter for quite a while.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What worries me a bit is the large difference between the CalculiX and the FEniCS case. Do you also observe this? It might also be a problem with my system, because I did not update CalculiX or the adapter for quite a while.

I am aware that there is a large difference in the results of CalculiX and FEniCS. I think this is the case because of the choice of the elements and also the solvers in CalculiX and FEniCS. The CalculiX participant has C3D10 elements. For the FEniCS participant we use the Cylinder function to construct the geometry. The FEniCS version we use offers very little control over the type of elements we can have, especially when we use in-built geometry constructing functions. In FEniCS-X there is greater control over the choice of elements.

I think we should not worry too much about having the same results with CalculiX and FEniCS for this case as it is a complex case which also takes a long time to run. We can present this tutorial merely as a case to showcase the 3D functionality.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. It's already good to have an example for 3D the disagreement between FEniCS and CalculiX is a different story. Can you document it in an issue and also add the possible reasons you mentioned above?

</participant>

<m2n:sockets from="Fluid" to="Solid" exchange-directory=".." />
Expand Down
6 changes: 6 additions & 0 deletions elastic-tube-3d/solid-fenics/clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
set -e -u

. ../../tools/cleaning-tools.sh

clean_fenics .
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"participant_name": "Solid",
"config_file_name": "../precice-config.xml",
"interface": {
"coupling_mesh_name": "Solid-Mesh",
"write_data_name": "DisplacementDelta",
"read_data_name": "Force"
}
}
4 changes: 4 additions & 0 deletions elastic-tube-3d/solid-fenics/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
set -e -u

python3 solid.py
226 changes: 226 additions & 0 deletions elastic-tube-3d/solid-fenics/solid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
# Import required libs
from fenics import Constant, Function, AutoSubDomain, VectorFunctionSpace, interpolate, \
TrialFunction, TestFunction, Point, Expression, DirichletBC, nabla_grad, \
Identity, inner, dx, ds, sym, grad, lhs, rhs, dot, File, solve, assemble_system
from mshr import Cylinder, generate_mesh
from ufl import nabla_div
import numpy as np
from fenicsprecice import Adapter
import math


# define the two kinds of boundary: clamped and coupling Neumann Boundary
def clamped_boundary(x, on_boundary):
"""
Filter nodes at both ends of tube as they are fixed
"""
tol = 1E-14
return on_boundary and (((abs(x[2]) - 0.0) < tol) or ((L - abs(x[2])) < tol))


def neumann_boundary(x, on_boundary):
"""
Filter nodes which lie on the inner surface of the tube and excluding end nodes
"""
tol = 1E-14
return on_boundary and ((math.sqrt(x[0]**2 + x[1]**2) - R) < tol) and ((L - x[2]) > tol) and ((x[2] - 0.0) > tol)


# Geometry and material properties
dim = 3 # number of dimensions
R = 0.005
L = 0.05
rho = 3000
E = 4000000
nu = 0.3

mu = Constant(E / (2.0 * (1.0 + nu)))

lambda_ = Constant(E * nu / ((1.0 + nu) * (1.0 - 2.0 * nu)))

# create Mesh
outer_tube = Cylinder(Point(0, 0, L), Point(0, 0, 0), R + 0.001, R + 0.001)
inner_tube = Cylinder(Point(0, 0, L), Point(0, 0, 0), R, R)
mesh = generate_mesh(outer_tube - inner_tube, 20)

# create Function Space
V = VectorFunctionSpace(mesh, 'P', 2)

# Trial and Test Functions
du = TrialFunction(V)
v = TestFunction(V)

u_np1 = Function(V)
saved_u_old = Function(V)
u_delta = Function(V)

# function known from previous timestep
u_n = Function(V)
v_n = Function(V)
a_n = Function(V)

coupling_boundary = AutoSubDomain(neumann_boundary)
fixed_boundary = AutoSubDomain(clamped_boundary)

precice = Adapter(adapter_config_filename="precice-adapter-config-fsi-s.json")

# Initialize the coupling interface
precice_dt = precice.initialize(coupling_boundary, read_function_space=V, write_object=V, fixed_boundary=fixed_boundary)

fenics_dt = precice_dt # if fenics_dt == precice_dt, no subcycling is applied
dt = Constant(np.min([precice_dt, fenics_dt]))

# clamp the tube on both sides
bc = DirichletBC(V, Constant((0, 0, 0)), fixed_boundary)

# alpha method parameters
alpha_m = Constant(0)
alpha_f = Constant(0)
gamma = Constant(0.5 + alpha_f - alpha_m)
beta = Constant((gamma + 0.5) ** 2 / 4.)


# Define strain
def epsilon(u):
return 0.5 * (nabla_grad(u) + nabla_grad(u).T)


# Define Stress tensor
def sigma(u):
return lambda_ * nabla_div(u) * Identity(dim) + 2 * mu * epsilon(u)


# Define Mass form
def m(u, v):
return rho * inner(u, v) * dx


# Elastic stiffness form
def k(u, v):
return inner(sigma(u), sym(grad(v))) * dx


# External Work
def Wext(u_):
return dot(u_, p) * ds


# Functions for updating system state

# Update acceleration
def update_a(u, u_old, v_old, a_old, ufl=True):
if ufl:
dt_ = dt
beta_ = beta
else:
dt_ = float(dt)
beta_ = float(beta)

return ((u - u_old - dt_ * v_old) / beta / dt_ ** 2
- (1 - 2 * beta_) / 2 / beta_ * a_old)


# Update velocity
def update_v(a, u_old, v_old, a_old, ufl=True):
if ufl:
dt_ = dt
gamma_ = gamma
else:
dt_ = float(dt)
gamma_ = float(gamma)

return v_old + dt_ * ((1 - gamma_) * a_old + gamma_ * a)


def update_fields(u, u_old, v_old, a_old):
"""Update all fields at the end of a timestep."""

u_vec, u0_vec = u.vector(), u_old.vector()
v0_vec, a0_vec = v_old.vector(), a_old.vector()

# call update functions
a_vec = update_a(u_vec, u0_vec, v0_vec, a0_vec, ufl=False)
v_vec = update_v(a_vec, u0_vec, v0_vec, a0_vec, ufl=False)

# assign u->u_old
v_old.vector()[:], a_old.vector()[:] = v_vec, a_vec
u_old.vector()[:] = u.vector()


def avg(x_old, x_new, alpha):
return alpha * x_old + (1 - alpha) * x_new


a_np1 = update_a(du, u_n, v_n, a_n, ufl=True)
v_np1 = update_v(a_np1, u_n, v_n, a_n, ufl=True)

res = m(avg(a_n, a_np1, alpha_m), v) + k(avg(u_n, du, alpha_f), v)

a_form = lhs(res)
L_form = rhs(res)

# parameters for Time-Stepping
t = 0.0
n = 0
E_ext = 0

displacement_out = File("output/u_fsi.pvd")

u_n.rename("Displacement", "")
u_np1.rename("Displacement", "")
displacement_out << u_n

while precice.is_coupling_ongoing():

if precice.is_action_required(precice.action_write_iteration_checkpoint()): # write checkpoint
precice.store_checkpoint(u_n, t, n)

# read data from preCICE and get a new coupling expression
read_data = precice.read_data()

# Update the point sources on the coupling boundary with the new read data
forces_x, forces_y, forces_z = precice.get_point_sources(read_data)

A, b = assemble_system(a_form, L_form, bc)

b_forces = b.copy() # b is the same for every iteration, only forces change

for ps in forces_x:
ps.apply(b_forces)
for ps in forces_y:
ps.apply(b_forces)
for ps in forces_z:
ps.apply(b_forces)

assert (b is not b_forces)
solve(A, u_np1.vector(), b_forces)

dt = Constant(np.min([precice_dt, fenics_dt]))

# Write relative displacements to preCICE
u_delta.vector()[:] = u_np1.vector()[:] - u_n.vector()[:]
precice.write_data(u_delta)

# Call to advance coupling, also returns the optimum time step value
precice_dt = precice.advance(dt(0))

# Either revert to old step if timestep has not converged or move to next timestep
if precice.is_action_required(precice.action_read_iteration_checkpoint()): # roll back to checkpoint
u_cp, t_cp, n_cp = precice.retrieve_checkpoint()
u_n.assign(u_cp)
t = t_cp
n = n_cp
else:
u_n.assign(u_np1)
t += float(dt)
n += 1

if precice.is_time_window_complete():
update_fields(u_np1, saved_u_old, v_n, a_n)
if n % 10 == 0:
displacement_out << (u_n, t)

# Plot tip displacement evolution
displacement_out << u_n

precice.finalize()