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

View File

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