tests/test.sh exit code (0 → resolved); the classification below is post-hoc and cannot change it./opt/beamfd/src/solver.cpp
1 #include "beamfd/solver.hpp"
2
3 #include <cmath>
4 #include <cstddef>
5 #include <stdexcept>
6 #include <vector>
7
8 #include "beamfd/linalg.hpp"
9 #include "beamfd/types.hpp"
10
11 namespace beamfd {
12
13 // ============================================================================
14 // TODO(candidate): Implement the beam-bending finite-difference core.
15 //
16 // Two member functions below are stubs that currently return a zero deflection
17 // field and an empty/zero system, so every analysis is wrong and the grader
18 // fails. Replace the stub bodies of
19 //
20 // BeamSystem BeamSolver::assemble_system() const;
21 // BeamResult BeamSolver::solve() const;
22 //
23 // with correct implementations of the boundary-value problem specified in
24 // instruction.md. Do NOT change the public signatures declared in
25 // include/beamfd/solver.hpp, do NOT modify the recovery helpers below, and
26 // reuse the existing support layer (DenseMatrix, solve_general, and the Beam
27 // accessors EI_at / k_at / q_at / end_moment / applied_end_shear / left /
28 // right / dx / num_intervals / num_nodes / validate).
29 //
30 // The physics, the support and load semantics, the contract, and the I/O
31 // format are described in instruction.md. The required behaviour is enforced
32 // by a hidden grading suite (the shipped tests/ are only a smoke check); design
33 // a discretization that is at least second-order accurate and assemble the
34 // linear system A w = b for the nodal deflection, then solve it with
35 // solve_general. Call Beam::validate() at the start of solve() and let its
36 // exceptions (and any singular-system exception from solve_general) propagate.
37 // ============================================================================
38
39 BeamSystem BeamSolver::assemble_system() const {
40 // TODO(candidate): build and return the finite-difference system A w = b.
41 const std::size_t n = beam_.num_nodes();
42 BeamSystem sys;
43 sys.A = DenseMatrix(n, n); // all zeros -> singular / wrong
44 sys.b.assign(n, 0.0);
45 return sys;
46 }
47
48 BeamResult BeamSolver::solve() const {
49 // TODO(candidate): validate, assemble, solve, return the deflection field.
50 const std::size_t n = beam_.num_nodes();
51 BeamResult result;
52 result.deflection.assign(n, 0.0); // trivially zero -> wrong
53 return result;
54 }
55
56 // ----------------------------------------------------------------------------
57 // Recovery helpers (already implemented; do NOT modify). These differentiate a
58 // given deflection field so callers can recompute physical quantities (bending
59 // moment, shear) directly from a candidate result, honoring a variable EI(x).
60
61 namespace {
62
63 // Second derivative w''(x_i) by central difference (interior) / one-sided
64 // 2nd-order difference (ends).
65 double second_derivative(const std::vector<double>& w, std::size_t i, double h2) {
66 const std::size_t n = w.size();
67 if (i == 0) {
68 return (2.0 * w[0] - 5.0 * w[1] + 4.0 * w[2] - w[3]) / h2;
69 }
70 if (i == n - 1) {
71 return (2.0 * w[n - 1] - 5.0 * w[n - 2] + 4.0 * w[n - 3] - w[n - 4]) / h2;
72 }
73 return (w[i - 1] - 2.0 * w[i] + w[i + 1]) / h2;
74 }
75
76 } // namespace
77
78 std::vector<double> BeamSolver::bending_moment_field(
79 const std::vector<double>& w) const {
80 const std::size_t n = beam_.num_nodes();
81 if (w.size() != n) {
82 throw std::invalid_argument("bending_moment_field: deflection size mismatch");
83 }
84 const double h2 = beam_.dx() * beam_.dx();
85 std::vector<double> M(n, 0.0);
86 for (std::size_t i = 0; i < n; ++i) {
87 M[i] = beam_.EI_at(i) * second_derivative(w, i, h2);
88 }
89 return M;
90 }
91
92 std::vector<double> BeamSolver::shear_field(const std::vector<double>& w) const {
93 const std::size_t n = beam_.num_nodes();
94 if (w.size() != n) {
95 throw std::invalid_argument("shear_field: deflection size mismatch");
96 }
97 if (n < 5) {
98 throw std::invalid_argument("shear_field: need at least 5 nodes");
99 }
100 const double h = beam_.dx();
101 const double h2 = h * h;
102 std::vector<double> m(n, 0.0);
103 for (std::size_t j = 0; j < n; ++j) {
104 m[j] = beam_.EI_at(j) * second_derivative(w, j, h2);
105 }
106 std::vector<double> V(n, 0.0);
107 for (std::size_t i = 0; i < n; ++i) {
108 if (i == 0) {
109 V[i] = (-3.0 * m[0] + 4.0 * m[1] - m[2]) / (2.0 * h);
110 } else if (i == n - 1) {
111 V[i] = (3.0 * m[n - 1] - 4.0 * m[n - 2] + m[n - 3]) / (2.0 * h);
112 } else {
113 V[i] = (m[i + 1] - m[i - 1]) / (2.0 * h);
114 }
115 }
116 return V;
117 }
118
119 } // namespace beamfd
120
/opt/beamfd/include/beamfd/solver.hpp
1 #ifndef BEAMFD_SOLVER_HPP
2 #define BEAMFD_SOLVER_HPP
3
4 #include <cstddef>
5 #include <vector>
6
7 #include "beamfd/beam.hpp"
8 #include "beamfd/linalg.hpp"
9
10 namespace beamfd {
11
12 /// The assembled finite-difference linear system A w = b for a beam.
13 /// `A` has size num_nodes x num_nodes and `b` has length num_nodes; the
14 /// unknown w is the nodal transverse deflection field. Exposed so tests can
15 /// check the discrete equilibrium residual directly.
16 struct BeamSystem {
17 DenseMatrix A;
18 std::vector<double> b;
19 };
20
21 /// Result of a static beam-bending analysis.
22 struct BeamResult {
23 /// Nodal transverse deflection w_i [m], length == beam.num_nodes(),
24 /// ordered from x = 0 (i = 0) to x = L (i = num_intervals).
25 std::vector<double> deflection;
26 };
27
28 /// Static bending solver for an Euler-Bernoulli beam with (possibly) variable
29 /// flexural rigidity and an optional elastic foundation. The continuum
30 /// boundary-value problem and the support/load semantics are specified in
31 /// instruction.md; choosing and assembling a suitable finite-difference
32 /// discretization of it, and solving the resulting linear system, is the work
33 /// of this class.
34 class BeamSolver {
35 public:
36 explicit BeamSolver(const Beam& beam) : beam_(beam) {}
37
38 /// Run the full analysis. Calls Beam::validate() first and propagates any
39 /// exception it throws. Throws std::runtime_error if the assembled system
40 /// is singular.
41 BeamResult solve() const;
42
43 /// Assemble the finite-difference system A w = b. Exposed for testing and
44 /// reuse (the discrete equilibrium-residual invariant checks A w - b ~ 0 at
45 /// the interior nodes).
46 BeamSystem assemble_system() const;
47
48 // ---- Recovery helpers (fully implemented; operate on a deflection field).
49 // These let the caller recompute physical quantities directly from a
50 // candidate deflection vector, independent of how it was produced.
51
52 /// Bending moment M_i = EI(x_i) * w''(x_i) [N*m] via a central second
53 /// difference at interior nodes and a one-sided second difference at the
54 /// ends. `w` must have length num_nodes(). Throws std::invalid_argument
55 /// otherwise.
56 std::vector<double> bending_moment_field(const std::vector<double>& w) const;
57
58 /// Shear force V_i = (EI w'')'(x_i) [N] via finite differences of the
59 /// nodal bending moment. `w` must have length num_nodes(). Throws
60 /// std::invalid_argument otherwise.
61 std::vector<double> shear_field(const std::vector<double>& w) const;
62
63 private:
64 const Beam& beam_;
65 };
66
67 } // namespace beamfd
68
69 #endif // BEAMFD_SOLVER_HPP
70
/opt/beamfd/include/beamfd/beam.hpp
1 #ifndef BEAMFD_BEAM_HPP
2 #define BEAMFD_BEAM_HPP
3
4 #include <cstddef>
5 #include <utility>
6 #include <vector>
7
8 #include "beamfd/types.hpp"
9
10 namespace beamfd {
11
12 /// A 1D Euler-Bernoulli beam discretized on a uniform grid, with a possibly
13 /// non-uniform flexural rigidity EI(x), an optional Winkler elastic foundation
14 /// k(x), a (possibly spatially varying) transverse load q(x), and optional
15 /// applied force/moment at free ends.
16 ///
17 /// This is a plain data container plus light validation. The numerical core
18 /// (assembling and solving the finite-difference system for the deflection
19 /// field) lives in BeamSolver (see solver.hpp).
20 ///
21 /// Geometry / grid:
22 /// - The beam occupies x in [0, L], divided into `num_intervals` equal cells,
23 /// giving num_nodes() = num_intervals + 1 grid points at x_i = i * dx,
24 /// dx = L / num_intervals, for i = 0 .. num_intervals.
25 ///
26 /// Fields are stored per node (length num_nodes()):
27 /// - EI_at(i) flexural rigidity at node i [N*m^2] (> 0)
28 /// - k_at(i) Winkler foundation modulus at node i [N/m^2] (>= 0; 0 = none)
29 /// - q_at(i) distributed transverse load at node i [N/m]
30 /// plus optional applied actions at free ends (force [N] and moment [N*m]).
31 class Beam {
32 public:
33 /// Construct a beam of length `length` [m] with a uniform flexural rigidity
34 /// `EI` [N*m^2], discretized into `num_intervals` equal cells, with the
35 /// given end supports. EI(x) is initialised constant, k(x) = 0, q(x) = 0.
36 /// Throws std::invalid_argument if length or EI is non-positive or
37 /// num_intervals < 2.
38 Beam(double length, double EI, std::size_t num_intervals, Support left,
39 Support right);
40
41 // ---- Field setters -------------------------------------------------------
42
43 /// Set the nodal flexural-rigidity field EI(x_i). Size must equal
44 /// num_nodes(); every value must be > 0. Throws std::invalid_argument.
45 void set_ei_nodal(const std::vector<double>& ei);
46
47 /// Set EI(x) from piecewise-linear control points (x, value), sampled at
48 /// each node. Points are taken in the given order; x outside the range is
49 /// clamped to the nearest endpoint value. Every sampled value must be > 0.
50 void set_ei_profile(const std::vector<std::pair<double, double>>& points);
51
52 /// Set the nodal Winkler foundation field k(x_i) >= 0. Size == num_nodes().
53 void set_foundation_nodal(const std::vector<double>& k);
54
55 /// Set k(x) from piecewise-linear control points (x, value).
56 void set_foundation_profile(
57 const std::vector<std::pair<double, double>>& points);
58
59 /// Set the nodal distributed-load field q(x_i). Size == num_nodes().
60 void set_q_nodal(const std::vector<double>& q);
61
62 /// Set a uniform distributed load q [N/m] over the whole span (overwrites
63 /// the load field).
64 void set_distributed_load(double q);
65
66 /// Add a piecewise-linear distributed-load segment ramping from q0 at x0 to
67 /// q1 at x1 [N/m] to the existing load field. Segments are additive.
68 void add_load_segment(double x0, double x1, double q0, double q1);
69
70 /// Apply a transverse force P [N] at a free end (`at_left_end` -> x = 0,
71 /// else x = L). Throws std::runtime_error if that end is not Free.
72 void set_end_load(double P, bool at_left_end);
73
74 /// Apply a concentrated moment M [N*m] at a free end. Throws
75 /// std::runtime_error if that end is not Free.
76 void set_end_moment(double M, bool at_left_end);
77
78 // ---- Accessors -----------------------------------------------------------
79
80 double length() const { return length_; }
81 std::size_t num_intervals() const { return num_intervals_; }
82 std::size_t num_nodes() const { return num_intervals_ + 1; }
83 double dx() const { return length_ / static_cast<double>(num_intervals_); }
84 double node_x(std::size_t i) const;
85
86 Support left() const { return left_; }
87 Support right() const { return right_; }
88
89 double EI_at(std::size_t i) const; ///< nodal flexural rigidity [N*m^2]
90 double k_at(std::size_t i) const; ///< nodal foundation modulus [N/m^2]
91 double q_at(std::size_t i) const; ///< nodal distributed load [N/m]
92
93 /// A representative (maximum nodal) flexural rigidity [N*m^2], used as a
94 /// scale for residual normalisation. Equals the constant EI for a uniform
95 /// beam.
96 double EI() const;
97
98 /// True if any nodal foundation modulus is strictly positive.
99 bool has_foundation() const;
100
101 /// Applied end shear V at a free end (`left_end` -> x = 0). A transverse
102 /// force P at a free end contributes V = -P; 0 if no force is applied.
103 double applied_end_shear(bool left_end) const;
104
105 /// Applied end moment M [N*m] at a free end (0 if none).
106 double end_moment(bool left_end) const;
107
108 /// True if any end force has been applied.
109 bool has_end_load() const;
110
111 /// Throws std::runtime_error if the support / foundation combination cannot
112 /// suppress rigid-body motion (a free end opposite a non-clamped end, or
113 /// two free ends, is a mechanism UNLESS an elastic foundation is present).
114 void validate() const;
115
116 private:
117 static std::size_t end_index(bool left_end) { return left_end ? 0u : 1u; }
118
119 double length_{0.0};
120 std::size_t num_intervals_{0};
121 Support left_{Support::Clamped};
122 Support right_{Support::Free};
123 std::vector<double> ei_; // nodal EI, length num_nodes()
124 std::vector<double> k_; // nodal foundation, length num_nodes()
125 std::vector<double> q_; // nodal load, length num_nodes()
126 double force_[2]{0.0, 0.0}; // applied transverse force [left, right]
127 double moment_[2]{0.0, 0.0}; // applied moment [left, right]
128 };
129
130 } // namespace beamfd
131
132 #endif // BEAMFD_BEAM_HPP
133
/opt/beamfd/include/beamfd/linalg.hpp
1 #ifndef BEAMFD_LINALG_HPP
2 #define BEAMFD_LINALG_HPP
3
4 #include <cstddef>
5 #include <stdexcept>
6 #include <vector>
7
8 namespace beamfd {
9
10 /// A small, row-major dense matrix of doubles.
11 ///
12 /// Part of the engine's linear-algebra support layer; fully implemented. The
13 /// beam solver uses it to hold the finite-difference system matrix A in
14 /// A w = b. Unlike a structural stiffness matrix, A here is generally NON
15 /// symmetric (boundary-condition rows are one-sided difference stencils), so
16 /// the solve uses a general LU factorization rather than a Cholesky/LDL^T.
17 class DenseMatrix {
18 public:
19 DenseMatrix() = default;
20
21 DenseMatrix(std::size_t rows, std::size_t cols)
22 : rows_(rows), cols_(cols), data_(rows * cols, 0.0) {}
23
24 std::size_t rows() const { return rows_; }
25 std::size_t cols() const { return cols_; }
26
27 double& operator()(std::size_t r, std::size_t c) {
28 return data_[r * cols_ + c];
29 }
30 double operator()(std::size_t r, std::size_t c) const {
31 return data_[r * cols_ + c];
32 }
33
34 void fill(double v) {
35 for (auto& x : data_) x = v;
36 }
37
38 private:
39 std::size_t rows_{0};
40 std::size_t cols_{0};
41 std::vector<double> data_;
42 };
43
44 /// Solve the general (possibly non-symmetric) linear system A x = b by LU
45 /// factorization with partial (row) pivoting.
46 ///
47 /// `A` is the n-by-n system matrix and `b` is the right-hand side of length n.
48 /// Returns the solution vector x of length n.
49 ///
50 /// Throws std::runtime_error if A is not square, if the dimensions are
51 /// inconsistent, or if A is detected to be singular (a pivot column is
52 /// numerically zero), which is how an under-/over-determined boundary-value
53 /// problem surfaces.
54 std::vector<double> solve_general(const DenseMatrix& A,
55 const std::vector<double>& b);
56
57 } // namespace beamfd
58
59 #endif // BEAMFD_LINALG_HPP
60
/opt/beamfd/include/beamfd/types.hpp
1 #ifndef BEAMFD_TYPES_HPP
2 #define BEAMFD_TYPES_HPP
3
4 #include <cstddef>
5
6 namespace beamfd {
7
8 /// Type of support condition applied at one end of the beam.
9 ///
10 /// - Clamped (built-in / fixed): deflection and slope are both zero
11 /// (w = 0, w' = 0).
12 /// - Pinned (simple support): deflection is zero and the bending moment
13 /// is zero (w = 0, M = 0).
14 /// - Free: the bending moment and shear take their
15 /// applied values (M = M_applied, V = V_applied), both zero when no end
16 /// action is applied.
17 ///
18 /// See instruction.md for the continuum boundary-value problem these encode.
19 enum class Support {
20 Clamped,
21 Pinned,
22 Free,
23 };
24
25 } // namespace beamfd
26
27 #endif // BEAMFD_TYPES_HPP
28
ls -R /opt/beamfd --ignore=build 2>/dev/null | head -60; echo "---TESTS---"; ls /opt/beamfd/tests 2>/dev/null
/opt/beamfd: CMakeLists.txt examples include src tests /opt/beamfd/examples: cantilever_tip.beam /opt/beamfd/include: beamfd /opt/beamfd/include/beamfd: beam.hpp io.hpp linalg.hpp solver.hpp types.hpp /opt/beamfd/src: beam.cpp io.cpp linalg.cpp main.cpp solver.cpp /opt/beamfd/tests: CMakeLists.txt test_smoke.cpp test_util.hpp ---TESTS--- CMakeLists.txt test_smoke.cpp test_util.hpp [stdout] /opt/beamfd: CMakeLists.txt examples include src tests /opt/beamfd/examples: cantilever_tip.beam /opt/beamfd/include: beamfd /opt/beamfd/include/beamfd: beam.hpp io.hpp linalg.hpp solver.hpp types.hpp /opt/beamfd/src: beam.cpp io.cpp linalg.cpp main.cpp solver.cpp /opt/beamfd/tests: CMakeLists.txt test_smoke.cpp test_util.hpp ---TESTS--- CMakeLists.txt test_smoke.cpp test_util.hpp
/opt/beamfd/src/beam.cpp
1 #include "beamfd/beam.hpp"
2
3 #include <algorithm>
4 #include <stdexcept>
5
6 namespace beamfd {
7
8 namespace {
9
10 // Piecewise-linear interpolation of control points (x, value) at coordinate x.
11 // Points are used in the given order (assumed nondecreasing in x); x outside
12 // the range is clamped to the nearest endpoint.
13 double interp(const std::vector<std::pair<double, double>>& pts, double x) {
14 if (pts.empty()) {
15 throw std::invalid_argument("Beam: empty profile control points");
16 }
17 if (x <= pts.front().first) return pts.front().second;
18 if (x >= pts.back().first) return pts.back().second;
19 for (std::size_t s = 1; s < pts.size(); ++s) {
20 const double x0 = pts[s - 1].first, x1 = pts[s].first;
21 if (x <= x1) {
22 const double t = (x1 > x0) ? (x - x0) / (x1 - x0) : 0.0;
23 return pts[s - 1].second + t * (pts[s].second - pts[s - 1].second);
24 }
25 }
26 return pts.back().second;
27 }
28
29 } // namespace
30
31 Beam::Beam(double length, double EI, std::size_t num_intervals, Support left,
32 Support right)
33 : length_(length),
34 num_intervals_(num_intervals),
35 left_(left),
36 right_(right) {
37 if (length_ <= 0.0) {
38 throw std::invalid_argument("Beam: length must be positive");
39 }
40 if (EI <= 0.0) {
41 throw std::invalid_argument("Beam: EI must be positive");
42 }
43 if (num_intervals_ < 2) {
44 throw std::invalid_argument("Beam: need at least 2 intervals");
45 }
46 const std::size_t n = num_nodes();
47 ei_.assign(n, EI);
48 k_.assign(n, 0.0);
49 q_.assign(n, 0.0);
50 }
51
52 void Beam::set_ei_nodal(const std::vector<double>& ei) {
53 if (ei.size() != num_nodes()) {
54 throw std::invalid_argument("Beam::set_ei_nodal: size mismatch");
55 }
56 for (double v : ei) {
57 if (v <= 0.0) {
58 throw std::invalid_argument("Beam::set_ei_nodal: EI must be positive");
59 }
60 }
61 ei_ = ei;
62 }
63
64 void Beam::set_ei_profile(const std::vector<std::pair<double, double>>& points) {
65 std::vector<double> ei(num_nodes());
66 for (std::size_t i = 0; i < num_nodes(); ++i) ei[i] = interp(points, node_x(i));
67 set_ei_nodal(ei);
68 }
69
70 void Beam::set_foundation_nodal(const std::vector<double>& k) {
71 if (k.size() != num_nodes()) {
72 throw std::invalid_argument("Beam::set_foundation_nodal: size mismatch");
73 }
74 for (double v : k) {
75 if (v < 0.0) {
76 throw std::invalid_argument(
77 "Beam::set_foundation_nodal: k must be non-negative");
78 }
79 }
80 k_ = k;
81 }
82
83 void Beam::set_foundation_profile(
84 const std::vector<std::pair<double, double>>& points) {
85 std::vector<double> k(num_nodes());
86 for (std::size_t i = 0; i < num_nodes(); ++i) k[i] = interp(points, node_x(i));
87 set_foundation_nodal(k);
88 }
89
90 void Beam::set_q_nodal(const std::vector<double>& q) {
91 if (q.size() != num_nodes()) {
92 throw std::invalid_argument("Beam::set_q_nodal: size mismatch");
93 }
94 q_ = q;
95 }
96
97 void Beam::set_distributed_load(double q) { q_.assign(num_nodes(), q); }
98
99 void Beam::add_load_segment(double x0, double x1, double q0, double q1) {
100 if (x1 < x0) {
101 std::swap(x0, x1);
102 std::swap(q0, q1);
103 }
104 for (std::size_t i = 0; i < num_nodes(); ++i) {
105 const double x = node_x(i);
106 if (x < x0 || x > x1) continue;
107 const double t = (x1 > x0) ? (x - x0) / (x1 - x0) : 0.0;
108 q_[i] += q0 + t * (q1 - q0);
109 }
110 }
111
112 void Beam::set_end_load(double P, bool at_left_end) {
113 const Support s = at_left_end ? left_ : right_;
114 if (s != Support::Free) {
115 throw std::runtime_error(
116 "Beam::set_end_load: end load may only be applied at a free end");
117 }
118 force_[end_index(at_left_end)] = P;
119 }
120
121 void Beam::set_end_moment(double M, bool at_left_end) {
122 const Support s = at_left_end ? left_ : right_;
123 if (s != Support::Free) {
124 throw std::runtime_error(
125 "Beam::set_end_moment: end moment may only be applied at a free end");
126 }
127 moment_[end_index(at_left_end)] = M;
128 }
129
130 double Beam::node_x(std::size_t i) const {
131 if (i > num_intervals_) {
132 throw std::out_of_range("Beam::node_x: node index out of range");
133 }
134 return static_cast<double>(i) * dx();
135 }
136
137 double Beam::EI_at(std::size_t i) const {
138 if (i >= ei_.size()) throw std::out_of_range("Beam::EI_at: index out of range");
139 return ei_[i];
140 }
141
142 double Beam::k_at(std::size_t i) const {
143 if (i >= k_.size()) throw std::out_of_range("Beam::k_at: index out of range");
144 return k_[i];
145 }
146
147 double Beam::q_at(std::size_t i) const {
148 if (i >= q_.size()) throw std::out_of_range("Beam::q_at: index out of range");
149 return q_[i];
150 }
151
152 double Beam::EI() const {
153 double m = 0.0;
154 for (double v : ei_) m = std::max(m, v);
155 return m;
156 }
157
158 bool Beam::has_foundation() const {
159 for (double v : k_) {
160 if (v > 0.0) return true;
161 }
162 return false;
163 }
164
165 double Beam::applied_end_shear(bool left_end) const {
166 return -force_[end_index(left_end)];
167 }
168
169 double Beam::end_moment(bool left_end) const { return moment_[end_index(left_end)]; }
170
171 bool Beam::has_end_load() const { return force_[0] != 0.0 || force_[1] != 0.0; }
172
173 void Beam::validate() const {
174 const bool left_free = (left_ == Support::Free);
175 const bool right_free = (right_ == Support::Free);
176
177 // An elastic foundation supplies distributed restraint that removes the
178 // rigid-body modes, so any support combination is well posed when present.
179 if (has_foundation()) return;
180
181 if (left_free && right_free) {
182 throw std::runtime_error("Beam::validate: free-free beam is a mechanism");
183 }
184 if (left_free && right_ != Support::Clamped) {
185 throw std::runtime_error(
186 "Beam::validate: a free end requires a clamped opposite end");
187 }
188 if (right_free && left_ != Support::Clamped) {
189 throw std::runtime_error(
190 "Beam::validate: a free end requires a clamped opposite end");
191 }
192 }
193
194 } // namespace beamfd
195
/opt/beamfd/tests/test_smoke.cpp
1 // Smoke test for the beamfd project: a minimal, non-leaky sanity check so the
2 // project compiles and a solver gets quick feedback. It asserts only generic
3 // properties (finite field, pinned ends fixed, a non-trivial deflection, and a
4 // small interior discrete-equilibrium residual) and contains NO closed-form
5 // answer. The authoritative grading is performed by a separate hidden suite.
6
7 #include <algorithm>
8 #include <cmath>
9
10 #include "beamfd/beam.hpp"
11 #include "beamfd/solver.hpp"
12 #include "test_util.hpp"
13
14 using namespace beamfd;
15
16 BEAMFD_TEST("smoke_pinned_udl_sane") {
17 Beam beam(2.0, 1000.0, 20, Support::Pinned, Support::Pinned);
18 beam.set_distributed_load(5.0);
19 BeamSolver solver(beam);
20 BeamResult r = solver.solve();
21 const std::size_t n = beam.num_nodes();
22
23 beamfd_test::expect(r.deflection.size() == n, "deflection has num_nodes entries");
24 for (double w : r.deflection) {
25 beamfd_test::expect(std::isfinite(w), "deflection is finite");
26 }
27 beamfd_test::expect_near(r.deflection[0], 0.0, 1e-9, "left pin fixed");
28 beamfd_test::expect_near(r.deflection[n - 1], 0.0, 1e-9, "right pin fixed");
29 beamfd_test::expect(r.deflection[n / 2] > 0.0, "mid-span deflects under load");
30
31 // Interior discrete-equilibrium residual A w - b ~ 0 (relative to scale).
32 BeamSystem sys = solver.assemble_system();
33 double scale = 0.0;
34 for (double w : r.deflection) scale = std::max(scale, std::fabs(w));
35 const double c4 = beam.EI() / std::pow(beam.dx(), 4);
36 for (std::size_t i = 2; i + 2 < n; ++i) {
37 double res = -sys.b[i];
38 for (std::size_t j = 0; j < n; ++j) res += sys.A(i, j) * r.deflection[j];
39 beamfd_test::expect(std::fabs(res) <= 1e-6 * (c4 * scale) + 1e-9,
40 "interior equilibrium residual is small");
41 }
42 }
43
44 BEAMFD_TEST_MAIN()
45
/opt/beamfd/src/io.cpp
1 #include "beamfd/io.hpp"
2
3 #include <array>
4 #include <fstream>
5 #include <iomanip>
6 #include <optional>
7 #include <sstream>
8 #include <stdexcept>
9 #include <string>
10 #include <utility>
11 #include <vector>
12
13 namespace beamfd {
14
15 namespace {
16
17 [[noreturn]] void fail(std::size_t line_no, const std::string& msg) {
18 std::ostringstream oss;
19 oss << "parse_beam: line " << line_no << ": " << msg;
20 throw std::runtime_error(oss.str());
21 }
22
23 Support parse_support(std::size_t line_no, const std::string& tok) {
24 if (tok == "clamped") return Support::Clamped;
25 if (tok == "pinned") return Support::Pinned;
26 if (tok == "free") return Support::Free;
27 fail(line_no, "support must be one of: clamped | pinned | free (got '" + tok +
28 "')");
29 }
30
31 bool parse_side(std::size_t line_no, const std::string& tok) {
32 if (tok == "left") return true;
33 if (tok == "right") return false;
34 fail(line_no, "side must be 'left' or 'right'");
35 }
36
37 std::vector<std::pair<double, double>> read_pairs(std::size_t line_no,
38 std::istringstream& ls,
39 const char* what) {
40 std::vector<std::pair<double, double>> pts;
41 double x, v;
42 while (ls >> x >> v) pts.emplace_back(x, v);
43 if (pts.empty()) fail(line_no, std::string("expected x value pairs after ") + what);
44 return pts;
45 }
46
47 } // namespace
48
49 Beam parse_beam(std::istream& in) {
50 std::optional<double> length, EI;
51 std::optional<std::size_t> cells;
52 std::optional<Support> left, right;
53 std::vector<std::pair<double, double>> ei_profile, foundation_profile;
54 std::optional<double> foundation_const;
55 double q = 0.0;
56 bool have_udl = false;
57 std::vector<std::array<double, 4>> loads;
58 std::vector<std::pair<bool, double>> end_forces, end_moments;
59
60 std::string line;
61 std::size_t line_no = 0;
62 while (std::getline(in, line)) {
63 ++line_no;
64 const auto hash = line.find('#');
65 if (hash != std::string::npos) line.erase(hash);
66 std::istringstream ls(line);
67 std::string tag;
68 if (!(ls >> tag)) continue;
69
70 if (tag == "length") {
71 double v;
72 if (!(ls >> v)) fail(line_no, "expected: length <L>");
73 length = v;
74 } else if (tag == "ei") {
75 double v;
76 if (!(ls >> v)) fail(line_no, "expected: ei <EI>");
77 EI = v;
78 } else if (tag == "ei_profile") {
79 ei_profile = read_pairs(line_no, ls, "ei_profile");
80 } else if (tag == "cells") {
81 std::size_t v;
82 if (!(ls >> v)) fail(line_no, "expected: cells <n>");
83 cells = v;
84 } else if (tag == "support") {
85 std::string a, b;
86 if (!(ls >> a >> b)) fail(line_no, "expected: support <left> <right>");
87 left = parse_support(line_no, a);
88 right = parse_support(line_no, b);
89 } else if (tag == "foundation") {
90 double v;
91 if (!(ls >> v)) fail(line_no, "expected: foundation <k>");
92 foundation_const = v;
93 } else if (tag == "foundation_profile") {
94 foundation_profile = read_pairs(line_no, ls, "foundation_profile");
95 } else if (tag == "udl") {
96 double v;
97 if (!(ls >> v)) fail(line_no, "expected: udl <q>");
98 q = v;
99 have_udl = true;
100 } else if (tag == "load") {
101 double x0, x1, q0, q1;
102 if (!(ls >> x0 >> x1 >> q0 >> q1))
103 fail(line_no, "expected: load <x0> <x1> <q0> <q1>");
104 loads.push_back({x0, x1, q0, q1});
105 } else if (tag == "endload") {
106 std::string side;
107 double v;
108 if (!(ls >> side >> v)) fail(line_no, "expected: endload <left|right> <P>");
109 end_forces.emplace_back(parse_side(line_no, side), v);
110 } else if (tag == "endmoment") {
111 std::string side;
112 double v;
113 if (!(ls >> side >> v))
114 fail(line_no, "expected: endmoment <left|right> <M>");
115 end_moments.emplace_back(parse_side(line_no, side), v);
116 } else {
117 fail(line_no, "unknown record '" + tag + "'");
118 }
119 }
120
121 if (!length) fail(line_no, "missing required record: length");
122 if (!cells) fail(line_no, "missing required record: cells");
123 if (!left || !right) fail(line_no, "missing required record: support");
124 if (!EI && ei_profile.empty())
125 fail(line_no, "missing required record: ei or ei_profile");
126
127 const double ei_seed = EI ? *EI : 1.0;
128 Beam beam(*length, ei_seed, *cells, *left, *right);
129 if (!ei_profile.empty()) beam.set_ei_profile(ei_profile);
130 if (foundation_const) beam.set_foundation_nodal(
131 std::vector<double>(beam.num_nodes(), *foundation_const));
132 if (!foundation_profile.empty()) beam.set_foundation_profile(foundation_profile);
133 if (have_udl) beam.set_distributed_load(q);
134 for (const auto& s : loads) beam.add_load_segment(s[0], s[1], s[2], s[3]);
135
136 try {
137 for (const auto& e : end_forces) beam.set_end_load(e.second, e.first);
138 for (const auto& e : end_moments) beam.set_end_moment(e.second, e.first);
139 } catch (const std::exception& ex) {
140 fail(line_no, ex.what());
141 }
142 return beam;
143 }
144
145 Beam parse_beam_file(const std::string& path) {
146 std::ifstream in(path);
147 if (!in) {
148 throw std::runtime_error("parse_beam_file: cannot open '" + path + "'");
149 }
150 return parse_beam(in);
151 }
152
153 void write_report(std::ostream& out, const Beam& beam, const BeamResult& result) {
154 BeamSolver solver(beam);
155 const std::vector<double> M = solver.bending_moment_field(result.deflection);
156 const std::vector<double> V = solver.shear_field(result.deflection);
157
158 out << std::scientific << std::setprecision(6);
159 out << "# beam analysis report\n";
160 out << "length=" << beam.length() << " EI(max)=" << beam.EI()
161 << " nodes=" << beam.num_nodes() << " dx=" << beam.dx() << "\n\n";
162
163 out << "[fields] (i, x, w[m], M=EI*w''[N*m], V=(EI*w'')'[N])\n";
164 for (std::size_t i = 0; i < beam.num_nodes(); ++i) {
165 out << i << " " << beam.node_x(i) << " " << result.deflection[i] << " "
166 << M[i] << " " << V[i] << "\n";
167 }
168 }
169
170 } // namespace beamfd
171
/opt/beamfd/examples/cantilever_tip.beam
1 # Cantilever beam with a downward point load at the free tip. 2 # Clamped (built-in) at x = 0, free at x = L. 3 length 3.0 # span [m] 4 ei 2.0e6 # flexural rigidity EI [N*m^2] 5 cells 200 # number of equal finite-difference intervals 6 7 # Left end clamped, right end free. 8 support clamped free 9 10 # Downward point load of 1 kN at the free right tip. 11 endload right 1000.0 12
/opt/beamfd/src/linalg.cpp
1 #include "beamfd/linalg.hpp"
2
3 #include <cmath>
4
5 namespace beamfd {
6
7 std::vector<double> solve_general(const DenseMatrix& A,
8 const std::vector<double>& b) {
9 const std::size_t n = A.rows();
10 if (A.cols() != n) {
11 throw std::runtime_error("solve_general: matrix is not square");
12 }
13 if (b.size() != n) {
14 throw std::runtime_error("solve_general: right-hand side size mismatch");
15 }
16
17 // Work on mutable copies (Gaussian elimination with partial pivoting).
18 DenseMatrix M = A;
19 std::vector<double> x = b;
20
21 for (std::size_t col = 0; col < n; ++col) {
22 // Find the pivot row (largest magnitude in this column at/below the
23 // diagonal) for numerical stability.
24 std::size_t pivot = col;
25 double best = std::fabs(M(col, col));
26 for (std::size_t r = col + 1; r < n; ++r) {
27 const double v = std::fabs(M(r, col));
28 if (v > best) {
29 best = v;
30 pivot = r;
31 }
32 }
33 if (best <= 1e-300) {
34 throw std::runtime_error("solve_general: matrix is singular");
35 }
36
37 // Swap pivot row into place (in both M and the RHS).
38 if (pivot != col) {
39 for (std::size_t c = 0; c < n; ++c) {
40 std::swap(M(col, c), M(pivot, c));
41 }
42 std::swap(x[col], x[pivot]);
43 }
44
45 // Eliminate entries below the pivot.
46 const double diag = M(col, col);
47 for (std::size_t r = col + 1; r < n; ++r) {
48 const double factor = M(r, col) / diag;
49 if (factor == 0.0) continue;
50 M(r, col) = 0.0;
51 for (std::size_t c = col + 1; c < n; ++c) {
52 M(r, c) -= factor * M(col, c);
53 }
54 x[r] -= factor * x[col];
55 }
56 }
57
58 // Back substitution.
59 for (std::size_t ii = 0; ii < n; ++ii) {
60 const std::size_t i = n - 1 - ii;
61 double s = x[i];
62 for (std::size_t c = i + 1; c < n; ++c) {
63 s -= M(i, c) * x[c];
64 }
65 x[i] = s / M(i, i);
66 }
67
68 return x;
69 }
70
71 } // namespace beamfd
72
/opt/beamfd/src/solver.cpp
BeamSystem BeamSolver::assemble_system() const {
// TODO(candidate): build and return the finite-difference system A w = b.
const std::size_t n = beam_.num_nodes();
BeamSystem sys;
sys.A = DenseMatrix(n, n); // all zeros -> singular / wrong
sys.b.assign(n, 0.0);
return sys;
}
BeamResult BeamSolver::solve() const {
// TODO(candidate): validate, assemble, solve, return the deflection field.
const std::size_t n = beam_.num_nodes();
BeamResult result;
result.deflection.assign(n, 0.0); // trivially zero -> wrong
return result;
}BeamSystem BeamSolver::assemble_system() const {
// Discretize d²/dx²( EI(x) w'' ) + k(x) w = q(x) on the uniform grid
// x_i = i*dx, i = 0..N, with n = N+1 nodal unknowns w_i.
//
// Strategy: introduce the discrete nodal moment m_i ≈ EI(x_i) w''(x_i),
// formed by a second-order central second difference at interior nodes and
// a second-order one-sided second difference at the two ends. The interior
// equilibrium equation is the second difference of that moment field plus
// the foundation term:
//
// (m_{i-1} - 2 m_i + m_{i+1})/dx² + k_i w_i = q_i , i = 2 .. N-2
//
// which for constant EI collapses to the classic EI*w'''' five-point
// stencil and stays second-order for variable EI. The four remaining rows
// (0, 1 and N-1, N) carry the two boundary conditions of each end, all
// discretized to the same second order. Every m_i below is expanded into
// its w-coefficients, so A acts directly on the nodal deflection vector.
const std::size_t n = beam_.num_nodes();
const std::size_t N = beam_.num_intervals();
const double dx = beam_.dx();
const double dx2 = dx * dx;
BeamSystem sys;
sys.A = DenseMatrix(n, n);
sys.b.assign(n, 0.0);
DenseMatrix& A = sys.A;
std::vector<double>& b = sys.b;
// Add coef * m_j (the discrete moment at node j, expressed through its
// w-stencil) into row r of A. Interior nodes use the central second
// difference; the boundary nodes 0 and N use a second-order one-sided
// second difference (the same stencils as the recovery helpers above).
auto add_moment = [&](std::size_t r, std::size_t j, double coef) {
const double e = coef * beam_.EI_at(j) / dx2;
if (j == 0) {
A(r, 0) += 2.0 * e;
A(r, 1) += -5.0 * e;
A(r, 2) += 4.0 * e;
A(r, 3) += -1.0 * e;
} else if (j == N) {
A(r, N) += 2.0 * e;
A(r, N - 1) += -5.0 * e;
A(r, N - 2) += 4.0 * e;
A(r, N - 3) += -1.0 * e;
} else {
A(r, j - 1) += 1.0 * e;
A(r, j) += -2.0 * e;
A(r, j + 1) += 1.0 * e;
}
};
// Interior equilibrium rows i = 2 .. N-2.
for (std::size_t i = 2; i + 2 <= N; ++i) {
add_moment(i, i - 1, 1.0 / dx2);
add_moment(i, i, -2.0 / dx2);
add_moment(i, i + 1, 1.0 / dx2);
A(i, i) += beam_.k_at(i);
b[i] = beam_.q_at(i);
}
// ---- Left end: rows 0 (primary) and 1 (secondary). --------------------
switch (beam_.left()) {
case Support::Clamped:
// w(0) = 0 and w'(0) = 0.
A(0, 0) = 1.0;
A(1, 0) += -3.0 / (2.0 * dx);
A(1, 1) += 4.0 / (2.0 * dx);
A(1, 2) += -1.0 / (2.0 * dx);
break;
case Support::Pinned:
// w(0) = 0 and M = EI w''(0) = 0 (i.e. w''(0) = 0).
A(0, 0) = 1.0;
A(1, 0) += 2.0 / dx2;
A(1, 1) += -5.0 / dx2;
A(1, 2) += 4.0 / dx2;
A(1, 3) += -1.0 / dx2;
break;
case Support::Free:
// M = EI w''(0) = M_applied and V = (EI w'')'(0) = V_applied.
add_moment(0, 0, 1.0);
b[0] = beam_.end_moment(true);
add_moment(1, 0, -3.0 / (2.0 * dx));
add_moment(1, 1, 4.0 / (2.0 * dx));
add_moment(1, 2, -1.0 / (2.0 * dx));
b[1] = beam_.applied_end_shear(true);
break;
}
// ---- Right end: rows N (primary) and N-1 (secondary). -----------------
switch (beam_.right()) {
case Support::Clamped:
// w(L) = 0 and w'(L) = 0.
A(N, N) = 1.0;
A(N - 1, N) += 3.0 / (2.0 * dx);
A(N - 1, N - 1) += -4.0 / (2.0 * dx);
A(N - 1, N - 2) += 1.0 / (2.0 * dx);
break;
case Support::Pinned:
// w(L) = 0 and M = EI w''(L) = 0.
A(N, N) = 1.0;
A(N - 1, N) += 2.0 / dx2;
A(N - 1, N - 1) += -5.0 / dx2;
A(N - 1, N - 2) += 4.0 / dx2;
A(N - 1, N - 3) += -1.0 / dx2;
break;
case Support::Free:
// M = EI w''(L) = M_applied and V = (EI w'')'(L) = V_applied.
add_moment(N, N, 1.0);
b[N] = beam_.end_moment(false);
add_moment(N - 1, N, 3.0 / (2.0 * dx));
add_moment(N - 1, N - 1, -4.0 / (2.0 * dx));
add_moment(N - 1, N - 2, 1.0 / (2.0 * dx));
b[N - 1] = beam_.applied_end_shear(false);
break;
}
return sys;
}
BeamResult BeamSolver::solve() const {
beam_.validate(); // propagate mechanism / configuration errors
const BeamSystem sys = assemble_system();
BeamResult result;
// A singular system surfaces as an exception from solve_general; let it
// propagate per the contract.
result.deflection = solve_general(sys.A, sys.b);
return result;
}cmake -S /opt/beamfd -B /opt/beamfd/build -DCMAKE_BUILD_TYPE=Release >/dev/null 2>&1 && cmake --build /opt/beamfd/build --parallel 2>&1 | tail -20 && cd /opt/beamfd/build && ctest --output-on-failure 2>&1 | tail -25
[ 11%] Building CXX object CMakeFiles/beamfd.dir/src/linalg.cpp.o
[ 22%] Building CXX object CMakeFiles/beamfd.dir/src/beam.cpp.o
[ 33%] Building CXX object CMakeFiles/beamfd.dir/src/io.cpp.o
[ 44%] Building CXX object CMakeFiles/beamfd.dir/src/solver.cpp.o
[ 55%] Linking CXX static library libbeamfd.a
[ 55%] Built target beamfd
[ 66%] Building CXX object CMakeFiles/beamfd_cli.dir/src/main.cpp.o
[ 77%] Building CXX object tests/CMakeFiles/test_smoke.dir/test_smoke.cpp.o
[ 88%] Linking CXX executable beamfd_cli
[ 88%] Built target beamfd_cli
[100%] Linking CXX executable test_smoke
[100%] Built target test_smoke
Test project /opt/beamfd/build
Start 1: smoke
1/1 Test #1: smoke ............................ Passed 0.00 sec
100% tests passed, 0 tests failed out of 1
Total Test time (real) = 0.00 sec
[stdout]
[ 11%] Building CXX object CMakeFiles/beamfd.dir/src/linalg.cpp.o
[ 22%] Building CXX object CMakeFiles/beamfd.dir/src/beam.cpp.o
[ 33%] Building CXX object CMakeFiles/beamfd.dir/src/io.cpp.o
[ 44%] Building CXX object CMakeFiles/beamfd.dir/src/solver.cpp.o
[ 55%] Linking CXX static library libbeamfd.a
[ 55%] Built target beamfd
[ 66%] Building CXX object CMakeFiles/beamfd_cli.dir/src/main.cpp.o
[ 77%] Building CXX object tests/CMakeFiles/test_smoke.dir/test_smoke.cpp.o
[ 88%] Linking CXX executable beamfd_cli
[ 88%] Built target beamfd_cli
[100%] Linking CXX executable test_smoke
[100%] Built target test_smoke
Test project /opt/beamfd/build
Start 1: smoke
1/1 Test #1: smoke ............................ Passed 0.00 sec
100% tests passed, 0 tests failed out of 1
Total Test time (real) = 0.00 seccat > /tmp/verify.cpp <<'EOF'
#include <cstdio>
#include <cmath>
#include "beamfd/beam.hpp"
#include "beamfd/solver.hpp"
using namespace beamfd;
double err_cantilever(int N){
double L=3.0,EI=2.0e6,P=1000.0;
Beam beam(L,EI,N,Support::Clamped,Support::Free);
beam.set_end_load(P,false);
auto w=BeamSolver(beam).solve().deflection;
double exact=P*L*L*L/(3.0*EI);
return std::fabs(w.back()-exact)/exact;
}
double err_ss_udl(int N){
double L=2.0,EI=1000.0,q=5.0;
Beam beam(L,EI,N,Support::Pinned,Support::Pinned);
beam.set_distributed_load(q);
auto w=BeamSolver(beam).solve().deflection;
double exact=5.0*q*L*L*L*L/(384.0*EI);
return std::fabs(w[N/2]-exact)/exact;
}
// clamped-clamped UDL: w_mid = q L^4/(384 EI)
double err_cc_udl(int N){
double L=4.0,EI=5000.0,q=12.0;
Beam beam(L,EI,N,Support::Clamped,Support::Clamped);
beam.set_distributed_load(q);
auto w=BeamSolver(beam).solve().deflection;
double exact=q*L*L*L*L/(384.0*EI);
return std::fabs(w[N/2]-exact)/exact;
}
// cantilever with end moment M: w(L)=M L^2/(2EI)
double err_cant_moment(int N){
double L=3.0,EI=2.0e6,M=800.0;
Beam beam(L,EI,N,Support::Clamped,Support::Free);
beam.set_end_moment(M,false);
auto w=BeamSolver(beam).solve().deflection;
double exact=M*L*L/(2.0*EI);
return std::fabs(w.back()-exact)/exact;
}
void report(const char*name, double(*f)(int)){
double e1=f(40),e2=f(80),e3=f(160);
printf("%-22s N=40:%.3e N=80:%.3e N=160:%.3e rate80=%.2f rate160=%.2f\n",
name,e1,e2,e3, std::log(e1/e2)/std::log(2.0), std::log(e2/e3)/std::log(2.0));
}
int main(){
report("cantilever_tip",err_cantilever);
report("ss_udl",err_ss_udl);
report("clamped_clamped_udl",err_cc_udl);
report("cantilever_moment",err_cant_moment);
}
EOF
g++ -std=c++17 -I/opt/beamfd/include /tmp/verify.cpp /opt/beamfd/src/beam.cpp /opt/beamfd/src/solver.cpp /opt/beamfd/src/linalg.cpp -o /tmp/verify && /tmp/verifycantilever_tip N=40:6.250e-04 N=80:1.562e-04 N=160:3.901e-05 rate80=2.00 rate160=2.00 ss_udl N=40:5.500e-03 N=80:1.375e-03 N=160:3.438e-04 rate80=2.00 rate160=2.00 clamped_clamped_udl N=40:9.625e-03 N=80:2.453e-03 N=160:6.191e-04 rate80=1.97 rate160=1.99 cantilever_moment N=40:2.010e-10 N=80:2.742e-09 N=160:4.347e-08 rate80=-3.77 rate160=-3.99 [stdout] cantilever_tip N=40:6.250e-04 N=80:1.562e-04 N=160:3.901e-05 rate80=2.00 rate160=2.00 ss_udl N=40:5.500e-03 N=80:1.375e-03 N=160:3.438e-04 rate80=2.00 rate160=2.00 clamped_clamped_udl N=40:9.625e-03 N=80:2.453e-03 N=160:6.191e-04 rate80=1.97 rate160=1.99 cantilever_moment N=40:2.010e-10 N=80:2.742e-09 N=160:4.347e-08 rate80=-3.77 rate160=-3.99
cat > /tmp/verify2.cpp <<'EOF'
#include <cstdio>
#include <cmath>
#include <vector>
#include "beamfd/beam.hpp"
#include "beamfd/solver.hpp"
using namespace beamfd;
// Manufactured: w=x^2(L-x)^2 (clamped-clamped exact BC), EI=a+b x, k=c.
// q = (EI w'')'' + k w = (24a-24bL+72b x) + c*(L^2 x^2 -2L x^3 + x^4)
double L=4.0,a=1000.0,b=150.0,c=40.0;
double wex(double x){return x*x*(L-x)*(L-x);}
double qf(double x){return (24*a-24*b*L+72*b*x)+c*(L*L*x*x-2*L*x*x*x+x*x*x*x);}
double run(int N){
Beam beam(L,a,N,Support::Clamped,Support::Clamped);
std::vector<double> ei(N+1),k(N+1),q(N+1);
for(int i=0;i<=N;i++){double x=beam.node_x(i);ei[i]=a+b*x;k[i]=c;q[i]=qf(x);}
beam.set_ei_nodal(ei);beam.set_foundation_nodal(k);beam.set_q_nodal(q);
auto w=BeamSolver(beam).solve().deflection;
double mx=0,den=0;
for(int i=0;i<=N;i++){mx=std::max(mx,std::fabs(w[i]-wex(beam.node_x(i))));den=std::max(den,std::fabs(wex(beam.node_x(i))));}
return mx/den;
}
// variable EI cantilever-free with tip shear, foundation present -> just convergence (self-consistency)
int main(){
double e1=run(40),e2=run(80),e3=run(160);
printf("varEI+found clamped-clamped N=40:%.3e N=80:%.3e N=160:%.3e rate=%.2f,%.2f\n",
e1,e2,e3,std::log(e1/e2)/std::log(2.0),std::log(e2/e3)/std::log(2.0));
// pinned-free with foundation (mechanism without foundation): self-consistent residual check via manufactured
// w=sin(pi x/L): pinned-pinned exact (w=0,w''=0 at ends). EI const, k const.
// q=EI w'''' + k w = (EI (pi/L)^4 + k) sin.
double EI=2000.0,kk=300.0;
auto runp=[&](int N){
Beam beam(L,EI,N,Support::Pinned,Support::Pinned);
std::vector<double> kv(N+1,kk),q(N+1);
double w0=M_PI/L;
for(int i=0;i<=N;i++){double x=beam.node_x(i);q[i]=(EI*pow(w0,4)+kk)*sin(w0*x);}
beam.set_foundation_nodal(kv);beam.set_q_nodal(q);
auto w=BeamSolver(beam).solve().deflection;
double mx=0,den=0;for(int i=0;i<=N;i++){double x=beam.node_x(i);mx=std::max(mx,std::fabs(w[i]-sin(w0*x)));den=std::max(den,std::fabs(sin(w0*x)));}
return mx/den;
};
double p1=runp(40),p2=runp(80),p3=runp(160);
printf("pinned-pinned+found sin N=40:%.3e N=80:%.3e N=160:%.3e rate=%.2f,%.2f\n",
p1,p2,p3,std::log(p1/p2)/std::log(2.0),std::log(p2/p3)/std::log(2.0));
return 0;
}
EOF
g++ -std=c++17 -I/opt/beamfd/include /tmp/verify2.cpp /opt/beamfd/src/beam.cpp /opt/beamfd/src/solver.cpp /opt/beamfd/src/linalg.cpp -o /tmp/verify2 && /tmp/verify2varEI+found clamped-clamped N=40:9.456e-03 N=80:2.410e-03 N=160:6.082e-04 rate=1.97,1.99 pinned-pinned+found sin N=40:3.148e-04 N=80:1.315e-04 N=160:3.947e-05 rate=1.26,1.74 [stdout] varEI+found clamped-clamped N=40:9.456e-03 N=80:2.410e-03 N=160:6.082e-04 rate=1.97,1.99 pinned-pinned+found sin N=40:3.148e-04 N=80:1.315e-04 N=160:3.947e-05 rate=1.26,1.74
cat > /tmp/v3.cpp <<'EOF'
#include <cstdio>
#include <cmath>
#include <vector>
#include "beamfd/beam.hpp"
#include "beamfd/solver.hpp"
using namespace beamfd;
double L=4.0,EI=2000.0,kk=300.0;
double runp(int N){
Beam beam(L,EI,N,Support::Pinned,Support::Pinned);
std::vector<double> kv(N+1,kk),q(N+1);
double w0=M_PI/L;
for(int i=0;i<=N;i++){double x=beam.node_x(i);q[i]=(EI*pow(w0,4)+kk)*sin(w0*x);}
beam.set_foundation_nodal(kv);beam.set_q_nodal(q);
auto w=BeamSolver(beam).solve().deflection;
double mx=0,den=0;for(int i=0;i<=N;i++){double x=beam.node_x(i);mx=std::max(mx,std::fabs(w[i]-sin(w0*x)));den=std::max(den,std::fabs(sin(w0*x)));}
return mx/den;
}
int main(){
int Ns[]={80,160,320,640};
double prev=0;
for(int j=0;j<4;j++){double e=runp(Ns[j]);
printf("N=%4d err=%.4e %s\n",Ns[j],e, j? "" : "");
if(j)printf(" rate=%.3f\n",std::log(prev/e)/std::log(2.0));
prev=e;}
}
EOF
g++ -std=c++17 -I/opt/beamfd/include /tmp/v3.cpp /opt/beamfd/src/beam.cpp /opt/beamfd/src/solver.cpp /opt/beamfd/src/linalg.cpp -o /tmp/v3 && /tmp/v3N= 80 err=1.3146e-04 N= 160 err=3.9470e-05 rate=1.736 N= 320 err=1.0635e-05 rate=1.892 N= 640 err=1.9240e-06 rate=2.467 [stdout] N= 80 err=1.3146e-04 N= 160 err=3.9470e-05 rate=1.736 N= 320 err=1.0635e-05 rate=1.892 N= 640 err=1.9240e-06 rate=2.467
cat > /tmp/v4.cpp <<'EOF'
#include <cstdio>
#include <cmath>
#include <vector>
#include "beamfd/beam.hpp"
#include "beamfd/solver.hpp"
using namespace beamfd;
// w=x^3-3L x^2 (clamped at left). EI=a+bx, k=c.
// q=12b + c*(x^3-3L x^2). Right free: M(L)=0, V(L)=6a+6bL.
double L=3.0,a=4000.0,b=500.0,c=20.0;
double wex(double x){return x*x*x-3*L*x*x;}
double qf(double x){return 12*b + c*(x*x*x-3*L*x*x);}
double run(int N){
Beam beam(L,a,N,Support::Clamped,Support::Free);
std::vector<double> ei(N+1),k(N+1),q(N+1);
for(int i=0;i<=N;i++){double x=beam.node_x(i);ei[i]=a+b*x;k[i]=c;q[i]=qf(x);}
beam.set_ei_nodal(ei);beam.set_foundation_nodal(k);beam.set_q_nodal(q);
beam.set_end_moment(0.0,false);
beam.set_end_load(-(6*a+6*b*L),false); // applied_end_shear=-P=6a+6bL
auto w=BeamSolver(beam).solve().deflection;
double mx=0,den=0;for(int i=0;i<=N;i++){double x=beam.node_x(i);mx=std::max(mx,std::fabs(w[i]-wex(x)));den=std::max(den,std::fabs(wex(x)));}
return mx/den;
}
int main(){double e1=run(40),e2=run(80),e3=run(160);
printf("clamped-free varEI+found+endV N=40:%.3e N=80:%.3e N=160:%.3e rate=%.2f,%.2f\n",
e1,e2,e3,std::log(e1/e2)/std::log(2.0),std::log(e2/e3)/std::log(2.0));}
EOF
g++ -std=c++17 -I/opt/beamfd/include /tmp/v4.cpp /opt/beamfd/src/beam.cpp /opt/beamfd/src/solver.cpp /opt/beamfd/src/linalg.cpp -o /tmp/v4 && /tmp/v4
echo "--- CLI demo on shipped example ---"
/opt/beamfd/build/beamfd_cli /opt/beamfd/examples/cantilever_tip.beam 2>/dev/null | head -5
echo "(exact tip PL^3/3EI = "$(python3 -c "print(1000*27/(3*2e6))")")"clamped-free varEI+found+endV N=40:6.067e-04 N=80:1.514e-04 N=160:3.781e-05 rate=2.00,2.00 --- CLI demo on shipped example --- # beam analysis report length=3.000000e+00 EI(max)=2.000000e+06 nodes=201 dx=1.500000e-02 [fields] (i, x, w[m], M=EI*w''[N*m], V=(EI*w'')'[N]) 0 0.000000e+00 6.876758e-12 3.000000e+03 -9.999999e+02 (exact tip PL^3/3EI = 0.0045) [stdout] clamped-free varEI+found+endV N=40:6.067e-04 N=80:1.514e-04 N=160:3.781e-05 rate=2.00,2.00 --- CLI demo on shipped example --- # beam analysis report length=3.000000e+00 EI(max)=2.000000e+06 nodes=201 dx=1.500000e-02 [fields] (i, x, w[m], M=EI*w''[N*m], V=(EI*w'')'[N]) 0 0.000000e+00 6.876758e-12 3.000000e+03 -9.999999e+02 (exact tip PL^3/3EI = 0.0045)
rm -f /tmp/verify*.cpp /tmp/verify /tmp/verify2 /tmp/v3.cpp /tmp/v3 /tmp/v4.cpp /tmp/v4
(Bash completed with no output)
No file changes, the agent left the workspace untouched.
== Configuring (cmake) ==
-- The CXX compiler identification is GNU 11.4.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/tmp.9igtfdK7TG/beamfd/build_grade
== Building ==
[ 6%] Building CXX object CMakeFiles/beamfd.dir/src/linalg.cpp.o
[ 13%] Building CXX object CMakeFiles/beamfd.dir/src/beam.cpp.o
[ 20%] Building CXX object CMakeFiles/beamfd.dir/src/solver.cpp.o
[ 26%] Building CXX object CMakeFiles/beamfd.dir/src/io.cpp.o
[ 33%] Linking CXX static library libbeamfd.a
[ 33%] Built target beamfd
[ 40%] Building CXX object CMakeFiles/beamfd_cli.dir/src/main.cpp.o
[ 46%] Building CXX object tests/CMakeFiles/test_invariants.dir/test_invariants.cpp.o
[ 53%] Building CXX object tests/CMakeFiles/test_convergence.dir/test_convergence.cpp.o
[ 60%] Building CXX object tests/CMakeFiles/test_mms.dir/test_mms.cpp.o
[ 66%] Building CXX object tests/CMakeFiles/test_soak.dir/test_soak.cpp.o
[ 73%] Linking CXX executable beamfd_cli
[ 73%] Built target beamfd_cli
[ 80%] Linking CXX executable test_convergence
[ 86%] Linking CXX executable test_soak
[ 93%] Linking CXX executable test_invariants
[ 93%] Built target test_convergence
[ 93%] Built target test_soak
[100%] Linking CXX executable test_mms
[100%] Built target test_invariants
[100%] Built target test_mms
== Running hidden test suite (ctest) ==
Test project /tmp/tmp.9igtfdK7TG/beamfd/build_grade
Start 1: test_mms
1/4 Test #1: test_mms ......................... Passed 0.00 sec
Start 2: test_convergence
2/4 Test #2: test_convergence ................. Passed 0.00 sec
Start 3: test_invariants
3/4 Test #3: test_invariants ..................***Failed 0.00 sec
[ PASS ] invariant_equilibrium_residual
[ FAIL ] invariant_symmetry: symmetry at node 0
[ PASS ] invariant_mechanism_rejection
[ PASS ] invariant_input_validation
----
3/4 tests passed
Start 4: test_soak
4/4 Test #4: test_soak ........................***Failed 0.00 sec
[ FAIL ] soak_random_manufactured_configs: config 1 (family 1, N 160) rel error 0.031437
----
0/1 tests passed
50% tests passed, 2 tests failed out of 4
Label Time Summary:
hidden = 0.02 sec*proc (4 tests)
Total Test time (real) = 0.02 sec
The following tests FAILED:
3 - test_invariants (Failed)
4 - test_soak (Failed)
Errors while running CTest
FAIL: one or more hidden tests failedReproduce this trial: git checkout 2f94510 && PYTHONPATH=src python3 scripts/build_site.py , then open trial/trial_9ea070ba6efe465c. Re-running the agent live requires EVAL_PLATFORM_ENABLE_OAUTH_SMOKE=1 and is non-deterministic.
Trial trial_9ea070ba6efe465c · verifier authoritative; classifier explanatory.