Use unique_ptr in main system jacobian.

pull/1170/head
Ryan Pavlik 2021-08-08 15:13:30 -05:00 committed by phkahler
parent c0f075671b
commit 605b48e6c0
2 changed files with 12 additions and 11 deletions

View File

@ -36,7 +36,7 @@
#define EIGEN_NO_DEBUG
#undef Success
#include "Eigen/SparseCore"
#include <Eigen/SparseCore>
// We declare these in advance instead of simply using FT_Library
// (defined as typedef FT_LibraryRec_* FT_Library) because including
@ -245,14 +245,16 @@ public:
// We're solving AX = B
int m, n;
struct {
Eigen::SparseMatrix<Expr*> *sym;
Eigen::SparseMatrix<double> *num;
// This only observes the Expr - does not own them!
std::unique_ptr<Eigen::SparseMatrix<Expr *>> sym;
std::unique_ptr<Eigen::SparseMatrix<double>> num;
} A;
Eigen::VectorXd scale;
Eigen::VectorXd X;
struct {
// This only observes the Expr - does not own them!
std::vector<Expr *> sym;
Eigen::VectorXd num;
} B;

View File

@ -8,6 +8,8 @@
//-----------------------------------------------------------------------------
#include "solvespace.h"
#include <memory>
#include <Eigen/Core>
#include <Eigen/SparseQR>
// The solver will converge all unknowns to within this tolerance. This must
@ -31,8 +33,7 @@ bool System::WriteJacobian(int tag) {
mat.eq.push_back(&e);
}
mat.m = mat.eq.size();
delete mat.A.sym;
mat.A.sym = new Eigen::SparseMatrix<Expr *>(mat.m, mat.n);
mat.A.sym.reset(new Eigen::SparseMatrix<Expr *>(mat.m, mat.n));
mat.A.sym->reserve(Eigen::VectorXi::Constant(mat.n, 10));
// Fill the param id to index map
@ -70,8 +71,7 @@ bool System::WriteJacobian(int tag) {
void System::EvalJacobian() {
using namespace Eigen;
delete mat.A.num;
mat.A.num = new Eigen::SparseMatrix<double>(mat.m, mat.n);
mat.A.num.reset(new Eigen::SparseMatrix<double>(mat.m, mat.n));
int size = mat.A.sym->outerSize();
for(int k = 0; k < size; k++) {
@ -503,6 +503,7 @@ SolveResult System::Solve(Group *g, int *rank, int *dof, List<hConstraint> *bad,
didnt_converge:
SK.constraint.ClearTags();
// Not using range-for here because index is used in additional ways
for(i = 0; i < mat.eq.size(); i++) {
if(fabs(mat.B.num[i]) > CONVERGE_TOLERANCE || IsReasonable(mat.B.num[i])) {
// This constraint is unsatisfied.
@ -556,10 +557,8 @@ void System::Clear() {
param.Clear();
eq.Clear();
dragged.Clear();
delete mat.A.num;
mat.A.num = NULL;
delete mat.A.sym;
mat.A.sym = NULL;
mat.A.num.reset();
mat.A.sym.reset();
}
void System::MarkParamsFree(bool find) {