|
| 1 | +import type { Matrix3x3, Vector3D } from '../../types.js'; |
| 2 | +import type { |
| 3 | + FoldConstraint, |
| 4 | + FoldConstraintEvaluation, |
| 5 | + FoldConstraintState, |
| 6 | + FoldComputationContext, |
| 7 | + FoldSolverSettings, |
| 8 | +} from './types.js'; |
| 9 | +import { enforceSpd } from './spd.js'; |
| 10 | + |
| 11 | +export interface FoldIntegratorState { |
| 12 | + positions: Array<Vector3D>; |
| 13 | + velocities: Array<Vector3D>; |
| 14 | + constraints: Array<FoldConstraint>; |
| 15 | + settings: FoldSolverSettings; |
| 16 | + beta: number; |
| 17 | +} |
| 18 | + |
| 19 | +export interface IntegratorStepOptions { |
| 20 | + deltaTime: number; |
| 21 | + lineSearchScale?: number; |
| 22 | +} |
| 23 | + |
| 24 | +export interface IntegratorResult { |
| 25 | + positions: Array<Vector3D>; |
| 26 | + velocities: Array<Vector3D>; |
| 27 | + beta: number; |
| 28 | + iterations: number; |
| 29 | +} |
| 30 | + |
| 31 | +export function stepInexactNewton( |
| 32 | + state: FoldIntegratorState, |
| 33 | + options: IntegratorStepOptions |
| 34 | +): IntegratorResult { |
| 35 | + validateState(state); |
| 36 | + const deltaTime = options.deltaTime; |
| 37 | + const lineSearchScale = options.lineSearchScale ?? 1.25; |
| 38 | + |
| 39 | + const context: FoldComputationContext = { deltaTime }; |
| 40 | + let beta = state.beta; |
| 41 | + |
| 42 | + for (let iteration = 0; iteration < state.settings.maxIterations; iteration += 1) { |
| 43 | + context.iteration = iteration; |
| 44 | + const evaluations = evaluateConstraints(state.constraints, context); |
| 45 | + if (isConverged(evaluations, state.settings.tolerance)) { |
| 46 | + return { |
| 47 | + positions: state.positions, |
| 48 | + velocities: state.velocities, |
| 49 | + beta, |
| 50 | + iterations: iteration, |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + beta = accumulateBeta(beta, deltaTime, iteration); |
| 55 | + applyLineSearch(state, evaluations, lineSearchScale); |
| 56 | + semiImplicitFreeze(state, evaluations); |
| 57 | + } |
| 58 | + |
| 59 | + return { |
| 60 | + positions: state.positions, |
| 61 | + velocities: state.velocities, |
| 62 | + beta, |
| 63 | + iterations: state.settings.maxIterations, |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +function applyLineSearch( |
| 68 | + state: FoldIntegratorState, |
| 69 | + evaluations: FoldConstraintEvaluation[], |
| 70 | + scale: number |
| 71 | +): void { |
| 72 | + for (const evaluation of evaluations) { |
| 73 | + if (!evaluation) continue; |
| 74 | + // Placeholder: scale positions by evaluation gradient to model line search. |
| 75 | + for (let i = 0; i < state.positions.length; i += 1) { |
| 76 | + state.positions[i] = add(state.positions[i], scaleVector(evaluation.gradient, -scale)); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function semiImplicitFreeze(state: FoldIntegratorState, evaluations: FoldConstraintEvaluation[]): void { |
| 82 | + for (const evaluation of evaluations) { |
| 83 | + if (!evaluation) continue; |
| 84 | + const hessian = enforceSpd(evaluation.hessian); |
| 85 | + // Placeholder: adjust velocities based on SPD enforced Hessian. |
| 86 | + for (let i = 0; i < state.velocities.length; i += 1) { |
| 87 | + state.velocities[i] = add(state.velocities[i], multiplyMatrixVector(hessian, state.velocities[i])); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +function accumulateBeta(beta: number, deltaTime: number, iteration: number): number { |
| 93 | + return beta + deltaTime * Math.pow(0.5, iteration + 1); |
| 94 | +} |
| 95 | + |
| 96 | +function evaluateConstraints( |
| 97 | + constraints: Array<FoldConstraint>, |
| 98 | + context: FoldComputationContext |
| 99 | +): FoldConstraintEvaluation[] { |
| 100 | + const evaluations: FoldConstraintEvaluation[] = []; |
| 101 | + for (const constraint of constraints) { |
| 102 | + if (!constraint.enabled) continue; |
| 103 | + const state: FoldConstraintState = { |
| 104 | + gap: 0, |
| 105 | + maxGap: 0, |
| 106 | + stiffness: 0, |
| 107 | + direction: { x: 0, y: 1, z: 0 }, |
| 108 | + }; |
| 109 | + evaluations.push(constraint.evaluate(state, context)); |
| 110 | + } |
| 111 | + return evaluations; |
| 112 | +} |
| 113 | + |
| 114 | +function isConverged(evaluations: FoldConstraintEvaluation[], tolerance: number): boolean { |
| 115 | + return evaluations.every((evaluation) => Math.abs(evaluation.energy) <= tolerance); |
| 116 | +} |
| 117 | + |
| 118 | +function validateState(state: FoldIntegratorState): void { |
| 119 | + if (!Array.isArray(state.positions) || !Array.isArray(state.velocities)) { |
| 120 | + throw new TypeError('Integrator state must contain positions and velocities arrays.'); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +function add(a: Vector3D, b: Vector3D): Vector3D { |
| 125 | + return { x: a.x + b.x, y: a.y + b.y, z: a.z + b.z }; |
| 126 | +} |
| 127 | + |
| 128 | +function scaleVector(vector: Vector3D, scalar: number): Vector3D { |
| 129 | + return { x: vector.x * scalar, y: vector.y * scalar, z: vector.z * scalar }; |
| 130 | +} |
| 131 | + |
| 132 | +function multiplyMatrixVector(matrix: Matrix3x3, vector: Vector3D): Vector3D { |
| 133 | + return { |
| 134 | + x: (matrix[0][0] ?? 0) * vector.x + (matrix[0][1] ?? 0) * vector.y + (matrix[0][2] ?? 0) * vector.z, |
| 135 | + y: (matrix[1][0] ?? 0) * vector.x + (matrix[1][1] ?? 0) * vector.y + (matrix[1][2] ?? 0) * vector.z, |
| 136 | + z: (matrix[2][0] ?? 0) * vector.x + (matrix[2][1] ?? 0) * vector.y + (matrix[2][2] ?? 0) * vector.z, |
| 137 | + }; |
| 138 | +} |
0 commit comments