Biorbd Release_1.13.0
Loading...
Searching...
No Matches
FatigueState.cpp
1#define BIORBD_API_EXPORTS
2#include "InternalForces/Muscles/FatigueState.h"
3
4#include <cmath>
5
6#include "Utils/Error.h"
7
8#if defined(_WIN32) || defined(_WIN64)
9#include <sstream>
10namespace std {
11template <typename T>
12std::string to_string(const T& n) {
13 std::ostringstream stm;
14 stm << n;
15 return stm.str();
16}
17} // namespace std
18#endif
19
20using namespace BIORBD_NAMESPACE;
21
23 const utils::Scalar& active,
24 const utils::Scalar& fatigued,
25 const utils::Scalar& resting)
26 : m_activeFibers(std::make_shared<utils::Scalar>(active)),
27 m_fatiguedFibers(std::make_shared<utils::Scalar>(fatigued)),
28 m_restingFibers(std::make_shared<utils::Scalar>(resting)),
29 m_type(std::make_shared<internal_forces::muscles::STATE_FATIGUE_TYPE>()) {
30 setType();
31}
32
39
41 const std::shared_ptr<FatigueState> other)
45 m_type(other->m_type) {}
46
48
55
63
64#ifndef BIORBD_USE_CASADI_MATH
66 utils::Scalar active,
67 utils::Scalar fatigued,
68 utils::Scalar resting,
69 bool turnOffWarnings) {
70 // Sanity check for active fibers
71 //
72 // In order to get the quantity of active fibers to 0 or 1, it has to come
73 // from the input command. The input command manage fiber recruitment from
74 // resting to active. Hence any exceeding because of integration can be
75 // corrected by putting back exceeding active fibers quantity into resting
76 // fibers quantity.
77 //
78 if (active < 0) {
79 if (!turnOffWarnings) {
81 0,
82 "Active Fibers Quantity can't be lower than 0, 0 is used then\n"
83 "Previous Active Fibers Quantity before set to 0:" +
84 std::to_string(active));
85 }
86 resting += active;
87 active = 0;
88 } else if (active > 1) {
89 if (!turnOffWarnings) {
91 0,
92 "Active Fibers Quantity can't be higher than 1, 1 is used then\n"
93 "Previous Active Fibers Quantity before set to 1: " +
94 std::to_string(active));
95 }
96 resting += active - 1;
97
98 active = 1;
99 }
100
101 // Sanity check for fatigued fibers
102 if (fatigued < 0) {
103 utils::Error::raise("Fatigued Fibers Quantity can't be lower than 0");
104 } else if (fatigued > 1) {
105 utils::Error::raise("Fatigued Fibers Quantity can't be higher than 1");
106 }
107
108 // Sanity check for resting fibers
109 //
110 // In order to get the quantity of resting fibers to 0, it has to come from
111 // the input command. The input command manage fiber recruitment from resting
112 // to active. Hence any exceeding because of integration can be corrected by
113 // putting back exceeding resting fibers quantity into active fibers quantity.
114 //
115 if (resting < 0) {
116 if (!turnOffWarnings) {
118 0,
119 "Resting Fibers Quantity can't be lower than 0, 0 is used then\n"
120 "Previous Resting Fibers Quantity before set to 0: " +
121 std::to_string(resting));
122 }
123 active += resting;
124 resting = 0;
125 } else if (resting > 1) {
126 utils::Error::raise("Resting Fibers Quantity can't be higher than 1");
127 }
128
129 if (fabs(active + fatigued + resting - 1.0) > 0.1) {
130 utils::Error::raise("Sum of the fatigued states must be equal to 1");
131 }
132
133 *m_activeFibers = active;
134 *m_fatiguedFibers = fatigued;
135 *m_restingFibers = resting;
136}
137#endif
138
140 const {
141 return *m_activeFibers;
142}
143
145 const {
146 return *m_fatiguedFibers;
147}
148
150 const {
151 return *m_restingFibers;
152}
153
154internal_forces::muscles::STATE_FATIGUE_TYPE
158
160 *m_type = internal_forces::muscles::STATE_FATIGUE_TYPE::SIMPLE_STATE_FATIGUE;
161}
STATE_FATIGUE_TYPE getType() const
Return the type of muscle fatigue model.
FatigueState DeepCopy() const
Deep copy of the fatigue state.
std::shared_ptr< STATE_FATIGUE_TYPE > m_type
Type of the muscle fatigue model.
std::shared_ptr< utils::Scalar > m_fatiguedFibers
Proportion of fatigued muscle fibers.
const utils::Scalar & fatiguedFibers() const
Return the propotion of fatigued muscle fibers.
std::shared_ptr< utils::Scalar > m_activeFibers
Proportion of active muscle fibers.
virtual void setState(utils::Scalar active, utils::Scalar fatigued, utils::Scalar resting, bool turnOffWarnings=false)
Safe set of the state (ensure that sum of all activation is 1).
const utils::Scalar & activeFibers() const
Return the propotion of active muscle fibers.
const utils::Scalar & restingFibers() const
Return the propotion of resting muscle fibers.
std::shared_ptr< utils::Scalar > m_restingFibers
Proportion of resting muscle fibers.
FatigueState(const utils::Scalar &active=1, const utils::Scalar &fatigued=0, const utils::Scalar &resting=0)
Construct fatigue state.
static void raise(const String &message)
Throw an error message.
Definition Error.cpp:6
static void warning(bool cond, const String &message)
Non-blocking assert that displays the error message if false.
Definition Error.cpp:16