Repository navigation
Add FEniCS as a solid participant for elastic-tube-3d #222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
39245cf
Initial case setup
IshaanDesai 16fe66a
Defining correct cylinder mesh
IshaanDesai 2829b72
Rectifying autopep8 violation
IshaanDesai 47249ec
Changing conditions to filter boudary nodes
IshaanDesai cddd741
Removing debugging statements
IshaanDesai f94ab3d
Correct fixed boundary filtering condition
IshaanDesai 87975d2
Simplifying fixed boundary filter condition
IshaanDesai 1c726ba
Port CalculiX configuration
IshaanDesai 854ef47
Updating README and adding watchpoint in config xml
IshaanDesai 4ef7352
Merge branch 'develop' into add-fenics-elastic-tube-3d
IshaanDesai a26cd86
Merge branch 'develop' into add-fenics-elastic-tube-3d
IshaanDesai a1ab338
Merge branch 'develop' into add-fenics-elastic-tube-3d
IshaanDesai 4145e3b
Merge branch 'develop' into add-fenics-elastic-tube-3d
IshaanDesai fbbe9b1
Merge branch 'develop' into add-fenics-elastic-tube-3d
IshaanDesai 891ee05
Reverting to DisplacementDelta data exchange and testing new implemen…
IshaanDesai 270d718
Add script for plotting watchpoints.
BenjaminRodenberg 635f039
Plot correct data and all three dimensions.
BenjaminRodenberg 9503b4d
Minor corrections
IshaanDesai 6227ca2
Merge branch 'add-fenics-elastic-tube-3d' of github.com:precice/tutor…
IshaanDesai e599e5d
Adding displacement plot to README
IshaanDesai 9884053
Markdown stuff and renaming output folder
IshaanDesai c5a4bcc
Correct image name
IshaanDesai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 . |
9 changes: 9 additions & 0 deletions
9
elastic-tube-3d/solid-fenics/precice-adapter-config-fsi-s.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #!/bin/sh | ||
| set -e -u | ||
|
|
||
| python3 solid.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.mdof 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).There was a problem hiding this comment.
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:
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
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?