Physics Simulation & Visualization Tool 0.1
A C++ physics simulation engine with real-time 3D visualization
Loading...
Searching...
No Matches
OneUnknownSolver.cpp
Go to the documentation of this file.
1#include "OneUnknownSolver.h"
2
3#include <iostream>
4#include <glm/vec3.hpp>
5
6template<typename InputT, typename OutputT>
7OneUnknownSolver<InputT, OutputT>::OneUnknownSolver(ParamSetter setParameter, SimulationRun runSimulation, ResultExtractor extractResult, OutputT tgt, OutputT toler, int maxIter)
8 : setParam(std::move(setParameter)), runSim(std::move(runSimulation)), extract(std::move(extractResult)), target(tgt), tolerance(toler), current(static_cast<InputT>(0)) {
9 setParam(current);
10}
11
12template<typename InputT, typename OutputT>
14 if (runSim()) {
15 // if stop condition is reached
16 OutputT measured = extract();
17 OutputT error = measured - target;
18
19 if (intervalFound) {
20 if (std::abs(low - high) < tolerance && std::abs(error) < 0.1) {
21 return true;
22 }
23
24 if (error < 0) {
25 // Measured > target → current guess too low
26 low = current;
27 } else {
28 // Measured < target → current guess too high
29 high = current;
30 }
31
32 // Bisection step (midpoint)
33 current = static_cast<InputT>((low + high) * static_cast<OutputT>(0.5));
34 setParam(current); // Apply next guess
35 } else { // Work on finding interval
36 if (prevError != 0 && expandIterations % 2 == 0 && prevError * error <= 0) {
37 intervalFound = true;
38 current = high + low * static_cast<OutputT>(0.5);
39 setParam(current);
40 return false;
41 }
42 if (expandIterations % 2 == 0) {
43 delta *= 2;
44 low -= delta;
45 current = low;
46 } else {
47 high += delta;
48 current = high;
49 }
50 expandIterations++;
51 setParam(current);
52 }
53 prevError = error;
54 }
55 return false;
56}
57
std::function< OutputT()> ResultExtractor
OneUnknownSolver(ParamSetter setParameter, SimulationRun runSimulation, ResultExtractor extractResult, OutputT target, OutputT tolerance=static_cast< OutputT >(0.001), int maxIterations=30)
std::function< void(InputT)> ParamSetter
std::function< bool()> SimulationRun
Hash specialization for Vertex to enable use in unordered containers.
Definition Mesh.h:33