Skip to content

Commit 30df38d

Browse files
fix(core): Initialize the state variables as zeroes by default
1 parent c2ac1e9 commit 30df38d

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

source/State.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ State::addInstance(moordyn::Instance* obj)
4747
new_var(i) = _var(i);
4848
}
4949
InstanceStateVar obj_var(obj->stateN(), obj->stateDims());
50+
obj_var.setZero();
5051
new_var(_objs.size()) = obj_var;
5152
_var = new_var;
5253
_objs.push_back(obj);

source/Time.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,8 @@ HeunScheme::Step(real& dt)
423423
Update(0.0, 0);
424424
CalcStateDeriv(0);
425425
// Correct the integration
426-
r0 += 0.5 * dt * (drdt0 - drdt1);
426+
r0 += (0.5 * dt) * (drdt0 - drdt1);
427+
427428
t += dt;
428429
Update(dt, 0);
429430
SchemeBase::Step(dt);

source/Time.hpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,8 @@ class SchemeBase : public Scheme
517517
}
518518

519519
/** @brief Create an initial state for all the entities
520-
* @note Just the first state is written. None of the following states are
521-
* initialized in any way.
522-
* @note All the derivatives are nullified
520+
* @note Just the first state is written. None of the following states nor
521+
* the derivatives are initialized in any way.
523522
* @note It is assumed that the coupled entities were already initialized
524523
*/
525524
virtual void Init()
@@ -534,18 +533,18 @@ class SchemeBase : public Scheme
534533
(bodies[i]->type != Body::CPLDPIN))
535534
continue;
536535
auto [pos, vel] = bodies[i]->initialize();
537-
AS_STATE(_r[0])->get(bodies[i]).row(0).head<7>() = pos.toVec7();
538-
AS_STATE(_r[0])->get(bodies[i]).row(0).tail<6>() = vel;
539-
for (unsigned int j = 0; j < NDERIV; j++)
540-
AS_STATE(_rd[j])->get(bodies[i]).setZero();
536+
auto r = AS_STATE(_r[0])->get(bodies[i]);
537+
r.row(0).head(7) = pos.toVec7();
538+
r.row(0).tail(6) = vel;
541539
}
542540

543541
for (unsigned int i = 0; i < rods.size(); i++) {
544542
if ((rods[i]->type != Rod::FREE) && (rods[i]->type != Rod::PINNED))
545543
continue;
546544
auto [pos, vel] = rods[i]->initialize();
547-
AS_STATE(_r[0])->get(rods[i]).row(0).head<7>() = pos.toVec7();
548-
AS_STATE(_r[0])->get(rods[i]).row(0).tail<6>() = vel;
545+
auto r = AS_STATE(_r[0])->get(rods[i]);
546+
r.row(0).head(7) = pos.toVec7();
547+
r.row(0).tail(6) = vel;
549548
for (unsigned int j = 0; j < NDERIV; j++)
550549
AS_STATE(_rd[j])->get(rods[i]).setZero();
551550
}
@@ -554,17 +553,19 @@ class SchemeBase : public Scheme
554553
if (points[i]->type != Point::FREE)
555554
continue;
556555
auto [pos, vel] = points[i]->initialize();
557-
AS_STATE(_r[0])->get(points[i]).row(0).head<3>() = pos;
558-
AS_STATE(_r[0])->get(points[i]).row(0).tail<3>() = vel;
556+
auto r = AS_STATE(_r[0])->get(points[i]);
557+
r.row(0).head(3) = pos;
558+
r.row(0).tail(3) = vel;
559559
for (unsigned int j = 0; j < NDERIV; j++)
560560
AS_STATE(_rd[j])->get(points[i]).setZero();
561561
}
562562

563563
for (unsigned int i = 0; i < lines.size(); i++) {
564564
auto [pos, vel] = lines[i]->initialize();
565+
auto r = AS_STATE(_r[0])->get(lines[i]);
565566
for (unsigned int j = 0; j < pos.size(); j++) {
566-
AS_STATE(_r[0])->get(lines[i]).row(j).head<3>() = pos[j];
567-
AS_STATE(_r[0])->get(lines[i]).row(j).segment<3>(3) = vel[j];
567+
r.row(j).head(3) = pos[j];
568+
r.row(j).segment(3, 3) = vel[j];
568569
}
569570
for (unsigned int j = 0; j < NDERIV; j++)
570571
AS_STATE(_rd[j])->get(lines[i]).setZero();
@@ -616,7 +617,7 @@ class SchemeBase : public Scheme
616617
{
617618
if (i >= NSTATE) {
618619
LOGERR << "State " << i << " cannot be setted on a '" << name
619-
<< "' scheme that has " << NSTATE << "states"
620+
<< "' scheme that has " << NSTATE << " states"
620621
<< endl;
621622
throw moordyn::invalid_value_error("Invalid state");
622623
}

0 commit comments

Comments
 (0)