Maybe this can help to address it, at least one of the questions I am asking:::

```
import numpy as np
from mpi4py import MPI
import ufl
from dolfinx import fem, mesh, io
import dolfinx.fem.petsc
from dolfinx.fem.petsc import LinearProblem
import matplotlib.pyplot as plt
from matplotlib.tri import Triangulation
# Define the beam properties
L = 120.0 # Length in inches
H = 8.0 # Height in inches
W = 4.0 # Width in inches
I = (W * H**3) / 12 # Moment of inertia in inches^4
# Mesh properties
Nx = 301 # Finer mesh along the length
Ny = 13 # Finer mesh along the height
print('NX:', Nx)
print('Ny:', Ny)
# Create the mesh
domain = mesh.create_rectangle(
MPI.COMM_WORLD,
[(0.0, -H / 2), (L, H / 2)],
(Nx, Ny),
diagonal=mesh.DiagonalType.crossed,
)
# Material properties
E = fem.Constant(domain, 2000000.0)
nu = fem.Constant(domain, 0.3)
mu = (E / (2 * (1 + nu)))
lmbda = (E * nu) / ((1 + nu) * (1 - 2 * nu))
# --- Identify unique X-values in the mesh ---
# Get the mesh points (geometry of the domain)
points = domain.geometry.x
# Extract unique X-values
unique_x_values = np.unique(points[:, 0]) # Get unique X values (sorted automatically)
#print("Unique X-values in the mesh:", unique_x_values)
# Define strain and stress
def eps(v):
return ufl.sym(ufl.grad(v))
def sigma(v):
return lmbda * ufl.tr(eps(v)) * ufl.Identity(2) + 2.0 * mu * eps(v)
# Applied load
fx = 0.0
fy = 0.25 * W
fy_analytic = 0.25
f = fem.Constant(domain, (fx, -fy)) # Uniformly distributed load over the length
# Define function space
V = fem.functionspace(domain, ("P", 2, (2,)))
# Define variational problem
du = ufl.TrialFunction(V)
u_ = ufl.TestFunction(V)
a = ufl.inner(sigma(du), eps(u_)) * ufl.dx
l = ufl.inner(f, u_) * ufl.dx
# Boundary conditions
def clamped_end(x):
return np.isclose(x[0], L)
fdim = domain.topology.dim - 1
# Locate facets on the left (fixed end)
left_facets = mesh.locate_entities_boundary(domain, fdim, clamped_end)
marked_values = np.full_like(left_facets, 1)
facet_tag = mesh.meshtags(domain, fdim, left_facets, marked_values)
# Measure for boundary integration
ds = ufl.Measure("ds", domain=domain, subdomain_data=facet_tag)
# Apply Dirichlet BCs (fixing the left end)
left_dofs = fem.locate_dofs_geometrical(V, clamped_end)
u_bc = fem.Function(V)
bcs = [fem.dirichletbc(u_bc, left_dofs)]
# Solve the problem
u = fem.Function(V, name="Displacement")
problem = LinearProblem(a, l, bcs, u=u)
problem.solve()
# Calculate reaction forces and moments at the fixed end
x = ufl.SpatialCoordinate(domain)
Rx = fem.assemble_scalar(fem.form(-sigma(u)[0, 0] * ds(1)))
Ry = fem.assemble_scalar(fem.form(-sigma(u)[1, 1] * ds(1)))
Mz = fem.assemble_scalar(fem.form(-x[1] * sigma(u)[0, 0] * ds(1)))
Mz*=W
# Print results
print(f"Horizontal reaction Rx = {Rx:.6f}")
print(f" (analytic = {-L * H * fx})")
print("-" * 50)
print(f"Vertical reaction Ry = {Ry:.6f}")
print(f" (analytic = {-fy * L})")
print("-" * 50)
print(f"Bending moment Mz = {Mz:.6f}")
print(f" (analytic = {(fy_analytic * L**2) / 2})")
print("-" * 50)
# --- Stress Tensor Calculation ---
# Define a function space for stress (DG0 element for discontinuous fields)
W = fem.functionspace(domain, ("DG", 0, (2, 2))) # Tensor space
# Compute stress tensor field from displacement
stress = fem.Function(W, name="Stress")
stress_expr = fem.Expression(sigma(u), W.element.interpolation_points())
stress.interpolate(stress_expr)
# Extract the sigma_xx component
stress_values = stress.x.array # Extract all values from the Function array
# Since stress is a 2x2 tensor, we want to extract the sigma_xx component
# which is the first component of each tensor (index 0)
# The tensor has a structure like [[sigma_xx, sigma_xy], [sigma_yx, sigma_yy]]
# sigma_xx is stored in stress_values at positions corresponding to the first component
stress_xx_values = stress_values[::4] # Take every fourth value starting from 0 (for sigma_xx)
# --- Print stress tensor values at vertices ---
# Get the mesh points
points = domain.geometry.x
# --- Print stress tensor values at vertices where x = 120.0 ---
target_x = 120.0
parray =[]
# Iterate over mesh points and print stress only for points where x = 120.0
for i, point in enumerate(points):
if np.isclose(point[0], target_x):
print(f"Stress at point {point}: {stress_xx_values[i]}")
parray.append([[point], [stress_xx_values[i]]])
moment = 0
for i, parray_item in enumerate(parray):
point = parray_item[0][0]
sigmav = parray_item[1][0]
sumv = sigmav * point[1] * 4.0
moment+=sumv
print('moment:', moment)
```

```
NX: 301
Ny: 13
Horizontal reaction Rx = -18.905485
(analytic = -0.0)
--------------------------------------------------
Vertical reaction Ry = -8.102351
(analytic = -120.0)
--------------------------------------------------
Bending moment Mz = -1728.174038
(analytic = 1800.0)
--------------------------------------------------
Stress at point [120. 3.38461538 0. ]: 49.673508529903074
Stress at point [120. 4. 0.]: 65.37257034690427
Stress at point [120. 2.76923077 0. ]: 35.54221486740928
Stress at point [120. 2.15384615 0. ]: 46.50190175001872
Stress at point [120. 1.53846154 0. ]: 15.527938604478791
Stress at point [120. 0.92307692 0. ]: 13.583349708020325
Stress at point [120. 0.30769231 0. ]: 21.751232787833867
Stress at point [120. -0.30769231 0. ]: 31.233044324898543
Stress at point [120. -0.92307692 0. ]: 38.58220388722755
Stress at point [120. -1.53846154 0. ]: 6.376374255143272
Stress at point [120. -2.15384615 0. ]: 15.95482729120869
Stress at point [120. -2.76923077 0. ]: 24.719743311336074
Stress at point [120. -3.38461538 0. ]: -7.883703141590987
Stress at point [120. -4. 0.]: 3.8237744532083573
moment: 2099.415246637295
```

```
Here the same beam is mocked-up in 2D. One thing that I noticed is that in terms of using the stress tensor to integrate the moment of force that just small changes to Nx or Ny seems to throw off the computations pretty wildy. Anyone can shed some light on this, why that changing Nx,Ny at all seem to throw off the computations so far?
I had to really jimmy jack the Nx,Ny to achieve the output moment: 2099.415246637295, which does seem a little bit high albeit in a type of range I personally find acceptable in terms of matching the analytic... The reason for using moment+=sumv, is because that is the only way I was able to find, to walk the length of the beam and output the moment at a slice at some location x...Anyone can shed some light on this why 2099.41 is coming out just a little bit too high there?
Here in the 2D sample stress_xx_values values or used, so it seems that when integrating the moment from the stress tensor in 3D space that there is a 3x3 stress tensor to be using which is different from the one shown...
```

Just for some reason in terms of:

```
# Boundary conditions
def clamped_end(x):
return np.isclose(x[0], L)
```

things have to be L there as the clamped end because, as much as I had tried to use 0.0 for the clamped end, I found that in doing so that the computations where no longer good… Anyone can shed some light on this, why the clamped end doesn’t seem to want to walk over to 0.0 where I wanted it originally?

So in terms of doing the same thing in 3D space, it seems there is a different type of stress tensor with different dimensions, however at this point I am still a little bit left out as to how exactly the integration of the stress tensor can or should take place in 3D realm… This is the question I am asking here:

```
So far I am finding with 3D as with 2D the value for Mz albeit not correct at all yet is also seems to swing fairly wildly depending on Nx,Nz of the mesh, but I don't really understand yet how to set one up so that Mz can come out in a more stable type of way or if there is some logic behind it which Nx, Nz to choose to have the best results for integrating stress tensor for moment to come out...
```

If there are any answers to these questions and how to relate this 2D sample to doing the same integration for moment in 3D, I would really appreciate to hear about…