1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PlaneGCSSolver_Solver.cpp
4 // Created: 14 Dec 2014
5 // Author: Artem ZHIDKOV
7 #include <PlaneGCSSolver_Solver.h>
8 #include <Events_LongOp.h>
11 PlaneGCSSolver_Solver::PlaneGCSSolver_Solver()
12 : myEquationSystem(new GCS::System),
13 myDiagnoseBeforeSolve(false),
14 myConfCollected(false),
19 PlaneGCSSolver_Solver::~PlaneGCSSolver_Solver()
24 void PlaneGCSSolver_Solver::clear()
26 myEquationSystem->clear();
28 myConstraints.clear();
29 myConflictingIDs.clear();
33 void PlaneGCSSolver_Solver::addConstraint(GCSConstraintPtr theConstraint)
35 myEquationSystem->addConstraint(theConstraint.get());
36 myConstraints[theConstraint->getTag()].insert(theConstraint);
40 void PlaneGCSSolver_Solver::removeConstraint(ConstraintID theID)
42 myConstraints.erase(theID);
43 if (myConstraints.empty()) {
44 myEquationSystem->clear();
45 myDOF = (int)myParameters.size();
47 myEquationSystem->clearByTag(theID);
52 double* PlaneGCSSolver_Solver::createParameter()
54 double* aResult = new double(0);
55 myParameters.push_back(aResult);
56 if (myConstraints.empty() && myDOF >= 0)
57 ++myDOF; // calculate DoF by hand if and only if there is no constraints yet
59 myDiagnoseBeforeSolve = true;
63 void PlaneGCSSolver_Solver::removeParameters(const GCS::SET_pD& theParams)
65 for (int i = (int)myParameters.size() - 1; i >= 0; --i)
66 if (theParams.find(myParameters[i]) != theParams.end()) {
67 myParameters.erase(myParameters.begin() + i);
72 PlaneGCSSolver_Solver::SolveStatus PlaneGCSSolver_Solver::solve()
74 // clear list of conflicting constraints
75 if (myConfCollected) {
76 myConflictingIDs.clear();
77 myConfCollected = false;
80 if (myParameters.empty())
81 return STATUS_INCONSISTENT;
83 Events_LongOp::start(this);
84 if (myDiagnoseBeforeSolve)
87 GCS::SolveStatus aResult = (GCS::SolveStatus)myEquationSystem->solve(myParameters);
88 Events_LongOp::end(this);
90 GCS::VEC_I aRedundant;
91 myEquationSystem->getRedundant(aRedundant);
92 if (!aRedundant.empty()) {
94 if (!myConflictingIDs.empty())
95 aResult = GCS::Failed;
99 if (aResult == GCS::Failed)
100 aStatus = STATUS_FAILED;
102 myEquationSystem->applySolution();
104 myDOF = myEquationSystem->dofsNumber();
111 void PlaneGCSSolver_Solver::undo()
113 myEquationSystem->undoSolution();
116 bool PlaneGCSSolver_Solver::isConflicting(const ConstraintID& theConstraint) const
118 if (!myConfCollected)
119 const_cast<PlaneGCSSolver_Solver*>(this)->collectConflicting();
120 return myConflictingIDs.find((int)theConstraint) != myConflictingIDs.end();
123 void PlaneGCSSolver_Solver::collectConflicting()
125 GCS::VEC_I aConflict;
126 myEquationSystem->getConflicting(aConflict);
127 myConflictingIDs.insert(aConflict.begin(), aConflict.end());
129 myEquationSystem->getRedundant(aConflict);
130 myConflictingIDs.insert(aConflict.begin(), aConflict.end());
132 myConfCollected = true;
135 int PlaneGCSSolver_Solver::dof()
137 if (myDOF < 0 && !myConstraints.empty())
142 void PlaneGCSSolver_Solver::diagnose()
144 myEquationSystem->declareUnknowns(myParameters);
145 myDOF = myEquationSystem->diagnose();
146 myDiagnoseBeforeSolve = false;