Files
ViewDesignEngine/tests/cae/shell_reference_solver.h

352 lines
13 KiB
C++

#pragma once
// Reference Mindlin shell solver used only by the VDE-1360 tests to verify that
// the generated Quad4/Quad8 shell mesh reproduces the cantilever beam
// deflection. It is deliberately small, supports planar shells only, and is
// independent numerical evidence rather than a closed-form substitute for a
// finite element result.
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <Eigen/Dense>
#include "vde/cae/cae_preprocess.h"
namespace shell_ref {
struct TipDeflectionResult {
bool valid = false;
std::string message;
double tip_deflection = 0.0;
double beam_theory_deflection = 0.0;
double relative_error = 0.0;
};
namespace detail {
struct GaussRule {
std::vector<double> points;
std::vector<double> weights;
};
GaussRule gauss_1x1() { return {{0.0}, {2.0}}; }
GaussRule gauss_2x2() {
const double a = 1.0 / std::sqrt(3.0);
return {{-a, a}, {1.0, 1.0}};
}
GaussRule gauss_3x3() {
const double a = std::sqrt(0.6);
return {{-a, 0.0, a}, {5.0 / 9.0, 8.0 / 9.0, 5.0 / 9.0}};
}
// Shape functions and derivatives at (xi, eta). Q4 or serendipity Q8.
void shape_functions(int node_count, double xi, double eta,
std::vector<double>& n, std::vector<double>& dndxi,
std::vector<double>& dndeta) {
if (node_count == 4) {
static const double sx[4] = {-1.0, 1.0, 1.0, -1.0};
static const double sy[4] = {-1.0, -1.0, 1.0, 1.0};
n.resize(4);
dndxi.resize(4);
dndeta.resize(4);
for (int i = 0; i < 4; ++i) {
n[i] = 0.25 * (1.0 + xi * sx[i]) * (1.0 + eta * sy[i]);
dndxi[i] = 0.25 * sx[i] * (1.0 + eta * sy[i]);
dndeta[i] = 0.25 * sy[i] * (1.0 + xi * sx[i]);
}
return;
}
static const double sx[4] = {-1.0, 1.0, 1.0, -1.0};
static const double sy[4] = {-1.0, -1.0, 1.0, 1.0};
n.assign(8, 0.0);
dndxi.assign(8, 0.0);
dndeta.assign(8, 0.0);
for (int i = 0; i < 4; ++i) {
n[i] = 0.25 * (1.0 + xi * sx[i]) * (1.0 + eta * sy[i]) *
(xi * sx[i] + eta * sy[i] - 1.0);
dndxi[i] = 0.25 * sx[i] * (1.0 + eta * sy[i]) *
(2.0 * xi * sx[i] + eta * sy[i]);
dndeta[i] = 0.25 * sy[i] * (1.0 + xi * sx[i]) *
(xi * sx[i] + 2.0 * eta * sy[i]);
}
n[4] = 0.5 * (1.0 - xi * xi) * (1.0 - eta);
dndxi[4] = -xi * (1.0 - eta);
dndeta[4] = -0.5 * (1.0 - xi * xi);
n[5] = 0.5 * (1.0 + xi) * (1.0 - eta * eta);
dndxi[5] = 0.5 * (1.0 - eta * eta);
dndeta[5] = -eta * (1.0 + xi);
n[6] = 0.5 * (1.0 - xi * xi) * (1.0 + eta);
dndxi[6] = -xi * (1.0 + eta);
dndeta[6] = 0.5 * (1.0 - xi * xi);
n[7] = 0.5 * (1.0 - xi) * (1.0 - eta * eta);
dndxi[7] = -0.5 * (1.0 - eta * eta);
dndeta[7] = -eta * (1.0 - xi);
}
struct ShapeEval {
std::vector<double> n;
std::vector<double> dxi;
std::vector<double> deta;
double xi = 0.0;
double eta = 0.0;
double xxi = 0.0;
double xeta = 0.0;
double yxi = 0.0;
double yeta = 0.0;
double detJ = 0.0;
};
ShapeEval evaluate(int n, double xi, double eta, const std::vector<double>& xs,
const std::vector<double>& ys) {
ShapeEval e;
e.xi = xi;
e.eta = eta;
shape_functions(n, xi, eta, e.n, e.dxi, e.deta);
for (int i = 0; i < n; ++i) {
e.xxi += e.dxi[i] * xs[i];
e.xeta += e.deta[i] * xs[i];
e.yxi += e.dxi[i] * ys[i];
e.yeta += e.deta[i] * ys[i];
}
e.detJ = e.xxi * e.yeta - e.xeta * e.yxi;
return e;
}
Eigen::MatrixXd bending_matrix(int n, const ShapeEval& e) {
Eigen::MatrixXd b = Eigen::MatrixXd::Zero(3, 3 * n);
for (int i = 0; i < n; ++i) {
const double dNdx = (e.yeta * e.dxi[i] - e.xeta * e.deta[i]) / e.detJ;
const double dNdy = (-e.yxi * e.dxi[i] + e.xxi * e.deta[i]) / e.detJ;
const int c0 = 3 * i;
b(0, c0 + 2) = dNdx;
b(1, c0 + 1) = -dNdy;
b(2, c0 + 2) = dNdy;
b(2, c0 + 1) = -dNdx;
}
return b;
}
Eigen::MatrixXd shear_matrix_cartesian(int n, const ShapeEval& e) {
Eigen::MatrixXd b = Eigen::MatrixXd::Zero(2, 3 * n);
for (int i = 0; i < n; ++i) {
const double dNdx = (e.yeta * e.dxi[i] - e.xeta * e.deta[i]) / e.detJ;
const double dNdy = (-e.yxi * e.dxi[i] + e.xxi * e.deta[i]) / e.detJ;
const int c0 = 3 * i;
b(0, c0 + 0) = dNdx;
b(0, c0 + 2) = e.n[i];
b(1, c0 + 0) = dNdy;
b(1, c0 + 1) = -e.n[i];
}
return b;
}
// MITC4 assumed transverse shear strains (Dvorkin-Bathe). The covariant shear
// strains are sampled at the four mid-side points and interpolated linearly.
Eigen::MatrixXd mitc4_shear_matrix_cartesian(
int n, const ShapeEval& e, const std::vector<double>& xs,
const std::vector<double>& ys) {
ShapeEval a = evaluate(n, 0.0, -1.0, xs, ys);
ShapeEval c = evaluate(n, 0.0, 1.0, xs, ys);
ShapeEval d = evaluate(n, -1.0, 0.0, xs, ys);
ShapeEval b = evaluate(n, 1.0, 0.0, xs, ys);
Eigen::MatrixXd cov = Eigen::MatrixXd::Zero(2, 3 * n);
const double wa = 0.5 * (1.0 - e.eta);
const double wc = 0.5 * (1.0 + e.eta);
const double wd = 0.5 * (1.0 - e.xi);
const double wb = 0.5 * (1.0 + e.xi);
for (int i = 0; i < n; ++i) {
const int c0 = 3 * i;
cov(0, c0 + 0) += wa * a.dxi[i];
cov(0, c0 + 1) += wa * (-a.yxi * a.n[i]);
cov(0, c0 + 2) += wa * (a.xxi * a.n[i]);
cov(0, c0 + 0) += wc * c.dxi[i];
cov(0, c0 + 1) += wc * (-c.yxi * c.n[i]);
cov(0, c0 + 2) += wc * (c.xxi * c.n[i]);
cov(1, c0 + 0) += wd * d.deta[i];
cov(1, c0 + 1) += wd * (-d.yeta * d.n[i]);
cov(1, c0 + 2) += wd * (d.xeta * d.n[i]);
cov(1, c0 + 0) += wb * b.deta[i];
cov(1, c0 + 1) += wb * (-b.yeta * b.n[i]);
cov(1, c0 + 2) += wb * (b.xeta * b.n[i]);
}
// [gamma_xz; gamma_yz] = J^{-T} [gamma_xi; gamma_eta]
Eigen::Matrix2d jt;
jt << e.xxi, e.yxi,
e.xeta, e.yeta;
const Eigen::Matrix2d jtinv = jt.inverse();
return jtinv * cov;
}
} // namespace detail
inline TipDeflectionResult cantilever_beam_deflection(
const vde::cae::ShellMesh& mesh, double young, double poisson,
double total_tip_load, double beam_length, double beam_width) {
using namespace detail;
TipDeflectionResult out;
if (mesh.elements.empty()) {
out.message = "reference solve: empty shell mesh";
return out;
}
if (mesh.element_type != vde::cae::ShellElementType::Quad4 &&
mesh.element_type != vde::cae::ShellElementType::Quad8) {
out.message = "reference solve: only Quad4/Quad8 are supported";
return out;
}
const double thickness = mesh.elements.front().thickness;
if (!(thickness > 0.0)) {
out.message = "reference solve: non-positive shell thickness";
return out;
}
double min_x = 1e300, max_x = -1e300;
for (const auto& v : mesh.vertices) {
min_x = std::min(min_x, v.x());
max_x = std::max(max_x, v.x());
}
const double tol = 1e-7 * std::max(1.0, max_x - min_x);
const int ndof = 3 * static_cast<int>(mesh.vertices.size());
Eigen::MatrixXd K = Eigen::MatrixXd::Zero(ndof, ndof);
const double E = young;
const double nu = poisson;
const double D0 = E * thickness * thickness * thickness /
(12.0 * (1.0 - nu * nu));
Eigen::Matrix3d Db;
Db << 1.0, nu, 0.0,
nu, 1.0, 0.0,
0.0, 0.0, (1.0 - nu) * 0.5;
Db *= D0;
const double G = E / (2.0 * (1.0 + nu));
Eigen::Matrix2d Cs = Eigen::Matrix2d::Zero();
Cs(0, 0) = (5.0 / 6.0) * G * thickness;
Cs(1, 1) = Cs(0, 0);
for (const auto& elem : mesh.elements) {
const int n = elem.node_count;
std::vector<double> xs(static_cast<size_t>(n));
std::vector<double> ys(static_cast<size_t>(n));
for (int i = 0; i < n; ++i) {
const auto& p = mesh.vertices[static_cast<size_t>(elem.nodes[i])];
xs[static_cast<size_t>(i)] = p.x();
ys[static_cast<size_t>(i)] = p.y();
}
Eigen::MatrixXd ke = Eigen::MatrixXd::Zero(3 * n, 3 * n);
auto integrate = [&](const GaussRule& rule, bool bending,
bool use_mitc4) {
for (size_t qi = 0; qi < rule.points.size(); ++qi) {
const double xi = rule.points[qi];
for (size_t qj = 0; qj < rule.points.size(); ++qj) {
const double eta = rule.points[qj];
const ShapeEval e = evaluate(n, xi, eta, xs, ys);
if (std::abs(e.detJ) <= 1e-15) continue;
const double dA =
std::abs(e.detJ) * rule.weights[qi] * rule.weights[qj];
if (bending) {
const Eigen::MatrixXd b =
bending_matrix(n, e);
ke.noalias() += b.transpose() * Db * b * dA;
} else {
Eigen::MatrixXd b;
if (use_mitc4)
b = mitc4_shear_matrix_cartesian(n, e, xs, ys);
else
b = shear_matrix_cartesian(n, e);
ke.noalias() += b.transpose() * Cs * b * dA;
}
}
}
};
integrate(gauss_2x2(), true, false);
if (n == 4)
integrate(gauss_2x2(), false, true);
else
integrate(gauss_2x2(), false, false);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
for (int a = 0; a < 3; ++a)
for (int b = 0; b < 3; ++b)
K(3 * elem.nodes[i] + a, 3 * elem.nodes[j] + b) +=
ke(3 * i + a, 3 * j + b);
}
Eigen::VectorXd F = Eigen::VectorXd::Zero(ndof);
std::vector<int> tip_nodes;
std::vector<int> fixed_dofs;
for (size_t i = 0; i < mesh.vertices.size(); ++i) {
const double x = mesh.vertices[i].x();
if (std::abs(x - min_x) <= tol) {
fixed_dofs.push_back(3 * static_cast<int>(i) + 0);
fixed_dofs.push_back(3 * static_cast<int>(i) + 1);
fixed_dofs.push_back(3 * static_cast<int>(i) + 2);
}
if (std::abs(x - max_x) <= tol) tip_nodes.push_back(static_cast<int>(i));
}
if (fixed_dofs.empty() || tip_nodes.empty()) {
out.message = "reference solve: could not identify fixed/tip nodes";
return out;
}
const double per_node = total_tip_load / static_cast<double>(tip_nodes.size());
for (int ni : tip_nodes) F(3 * ni + 0) = -per_node;
std::vector<int> free_index(static_cast<size_t>(ndof), -1);
std::vector<char> is_fixed(static_cast<size_t>(ndof), 0);
for (int d : fixed_dofs) is_fixed[static_cast<size_t>(d)] = 1;
int nfree = 0;
for (int d = 0; d < ndof; ++d)
if (!is_fixed[static_cast<size_t>(d)]) free_index[static_cast<size_t>(d)] = nfree++;
Eigen::MatrixXd Kff(nfree, nfree);
Eigen::VectorXd Ff(nfree);
Kff.setZero();
Ff.setZero();
for (int d = 0; d < ndof; ++d) {
const int a = free_index[static_cast<size_t>(d)];
if (a < 0) continue;
Ff(a) = F(d);
for (int e = 0; e < ndof; ++e) {
const int b = free_index[static_cast<size_t>(e)];
if (b >= 0) Kff(a, b) = K(d, e);
}
}
Eigen::LDLT<Eigen::MatrixXd> solver(Kff);
if (solver.info() != Eigen::Success) {
out.message = "reference solve: factorization failed";
return out;
}
const Eigen::VectorXd uf = solver.solve(Ff);
if (solver.info() != Eigen::Success) {
out.message = "reference solve: solve failed";
return out;
}
double best = 0.0;
double best_y = 1e300;
for (int ni : tip_nodes) {
const int a = free_index[static_cast<size_t>(3 * ni + 0)];
if (a < 0) continue;
const double w = uf(a);
const double y = std::abs(mesh.vertices[static_cast<size_t>(ni)].y());
if (y < best_y) {
best_y = y;
best = w;
}
}
out.tip_deflection = -best;
const double inertia = beam_width * thickness * thickness * thickness / 12.0;
out.beam_theory_deflection =
total_tip_load * beam_length * beam_length * beam_length /
(3.0 * young * inertia);
out.relative_error =
std::abs(out.tip_deflection - out.beam_theory_deflection) /
out.beam_theory_deflection;
out.valid = std::isfinite(out.tip_deflection) && out.tip_deflection > 0.0;
if (!out.valid) out.message = "reference solve: non-finite tip deflection";
return out;
}
} // namespace shell_ref