rsvs3D  0.0.0
Codes for the c++ implementation of the 3D RSVS
RSVScalc.hpp
Go to the documentation of this file.
1 
7 #ifndef RSVSCALC_H_INCLUDED
8 #define RSVSCALC_H_INCLUDED
9 
10 //=================================
11 // forward declared dependencies
12 // class foo; //when you only need a pointer not the actual object
13 // and to avoid circular dependencies
14 
15 namespace param
16 {
17 namespace dev
18 {
19 class devparam;
20 }
21 } // namespace param
22 
23 //=================================
24 // included dependencies
25 #include <Eigen>
26 #include <fstream>
27 #include <iostream>
28 #include <vector>
29 
31 #include "arraystructures.hpp"
32 #include "mesh.hpp"
33 #include "snake.hpp"
34 #include "triangulate.hpp"
35 #include "warning.hpp"
36 
37 typedef Eigen::SparseMatrix<double, Eigen::ColMajor> MatrixXd_sparse;
39 
40 template <typename T> class sparsetripletelement
41 {
42  int rowVal = 0;
43  int colVal = 0;
44  T valueVal = 0;
45 
46  public:
47  sparsetripletelement<T>(int r, int c, T v)
48  {
49 #ifdef SAFE_ACCESS
50  if (r < 0 || c < 0)
51  {
52  RSVS3D_ERROR_RANGE("Indices out of range.");
53  }
54 #endif
55  this->rowVal = r;
56  this->colVal = c;
57  this->valueVal = v;
58  }
59  int row() const
60  {
61  return this->rowVal;
62  }
63  int col() const
64  {
65  return this->colVal;
66  }
67  T value() const
68  {
69  return this->valueVal;
70  }
71 };
72 
74 {
75  int nRow = 0;
76  int nCol = 0;
77 
78  public:
79  double &operator()(int a, int b)
80  {
81 #ifdef SAFE_ACCESS
82  if (a < 0 || a >= this->nRow || b < 0 || b >= this->nCol)
83  {
84  RSVS3D_ERROR_RANGE("Indices out of range.");
85  }
86 #endif
87  return TripletMap::operator()(a + this->nRow * b);
88  }
89  const double &operator()(int a, int b) const
90  {
91 #ifdef SAFE_ACCESS
92  if (a < 0 || a >= this->nRow || b < 0 || b >= this->nCol)
93  {
94  RSVS3D_ERROR_RANGE("Indices out of range.");
95  }
96 #endif
97  return TripletMap::operator()(a + this->nRow * b);
98  }
99  sparsetripletelement<double> operator[](int a);
100  double &coeffRef(int a, int b)
101  {
102  return this->operator()(a, b);
103  }
104  void resize(int row, int col)
105  {
106  this->nRow = row;
107  this->nCol = col;
108  }
109  void setZero()
110  {
111  this->TripletMap::clear();
112  }
113  int nonZeros() const
114  {
115  return this->TripletMap::size();
116  }
117  void reserve(size_t a);
118  void SetEqual(MatrixXd_sparse &targ);
119  void SetEqual(Eigen::MatrixXd &targ);
120 };
121 
130 {
131  protected:
133  int nDv = 0;
135  int nConstr = 0;
137  int falseaccess = 0;
139  int sparseDVcutoff = 200;
141  int nonZeroPerDV = 10;
143  bool returnDeriv = true;
145  bool useSurfCentreDeriv = true;
147  bool useSurfCentreHessian = false;
148 
149  int timer1 = 0;
150  int timer2 = 0;
151  int timer3 = 0;
152  void ZeroTimers()
153  {
154  this->timer1 = 0;
155  this->timer2 = 0;
156  this->timer3 = 0;
157  }
158  void PrintTimers() const;
159 
160  public:
162  Eigen::MatrixXd dConstr;
163  SparseMatrixTriplet dConstr_sparse;
165  Eigen::MatrixXd HConstr;
166  SparseMatrixTriplet HConstr_sparse;
168  Eigen::MatrixXd HObj;
169  SparseMatrixTriplet HObj_sparse;
171  Eigen::MatrixXd HLag;
172  MatrixXd_sparse HLag_sparse;
173  Eigen::RowVectorXd dLag;
175  Eigen::RowVectorXd dObj;
177  Eigen::VectorXd constr;
179  Eigen::VectorXd lagMult;
181  Eigen::VectorXd deltaDV;
183  Eigen::VectorXd constrTarg;
185  Eigen::MatrixXd dvCallConstr;
187  Eigen::MatrixXd sensDv;
189  double obj = 0.0;
192  double limLag = INFINITY;
194  std::vector<bool> isConstrAct;
196  std::vector<bool> isDvAct;
206  std::vector<std::pair<int, int>> constrList;
207 
215  bool UseFullMath() const
216  {
217  return (this->nDv < this->sparseDVcutoff);
218  }
225  void BuildMathArrays(int nDv, int nConstr);
226 
232  void BuildConstrMap(const triangulation &triangleRSVS);
233 
239  void BuildConstrMap(const mesh &meshin);
240 
248  int BuildDVMap(const std::vector<int> &vecin);
249 
261  bool SnakDVcond(const triangulation &triRSVS, int ii);
262 
269  void PrepTriangulationCalc(const triangulation &triRSVS);
270 
276  void CalculateMesh(mesh &meshin);
277 
286  void CalculateTriangulation(const triangulation &triRSVS, int derivMethod = 0);
287 
299  void CalcTriangle(const triangle &triIn, const triangulation &triRSVS, bool isObj = true, bool isConstr = true,
300  bool isDeriv = true);
301 
313  void CalcTriangleFD(const triangle &triIn, const triangulation &triRSVS, bool isObj = true, bool isConstr = true,
314  bool isDeriv = true);
315 
327  void CalcTriangleDirectVolume(const triangle &triIn, const triangulation &triRSVS, bool isObj = true,
328  bool isConstr = true, bool isDeriv = true);
329 
341  void CalcTriangleEdgeLength(const triangle &triIn, const triangulation &triRSVS, bool isObj = true,
342  bool isConstr = true, bool isDeriv = true);
343 
349  void ReturnConstrToMesh(triangulation &triRSVS) const;
350 
358  void ReturnConstrToMesh(mesh &meshin, double volu::*mp = &volu::volume) const;
359 
367  void CheckAndCompute(int calcMethod = 0, bool sensCalc = false);
368 
388  void ComputeSQPstep(int calcMethod, Eigen::MatrixXd &dConstrAct, Eigen::RowVectorXd &dObjAct,
389  Eigen::VectorXd &constrAct, Eigen::VectorXd &lagMultAct);
390  void ComputeSQPstep(int calcMethod, MatrixXd_sparse &dConstrAct, Eigen::RowVectorXd &dObjAct,
391  Eigen::VectorXd &constrAct, Eigen::VectorXd &lagMultAct);
392 
393  void ComputeSQPsens(int calcMethod, const Eigen::MatrixXd &sensMult, const Eigen::MatrixXd &sensInv,
394  Eigen::MatrixXd &sensRes);
395  void ComputeSQPsens(int calcMethod, Eigen::MatrixXd &sensMult, MatrixXd_sparse &sensInv, Eigen::MatrixXd &sensRes);
396 
409  bool PrepareMatricesForSQP(Eigen::MatrixXd &dConstrAct, Eigen::MatrixXd &HConstrAct, Eigen::MatrixXd &HObjAct,
410  MatrixXd_sparse &dConstrAct_sparse, MatrixXd_sparse &HConstrAct_sparse,
411  MatrixXd_sparse &HObjAct_sparse, Eigen::RowVectorXd &dObjAct, Eigen::VectorXd &constrAct,
412  Eigen::VectorXd &lagMultAct);
413 
414  void PrepareMatricesForSQPFull(Eigen::MatrixXd &dConstrAct, Eigen::MatrixXd &HConstrAct, Eigen::MatrixXd &HObjAct);
415  void PrepareMatricesForSQPSparse(MatrixXd_sparse &dConstrAct_sparse, MatrixXd_sparse &HConstrAct_sparse,
416  MatrixXd_sparse &HObjAct_sparse);
434  bool PrepareMatricesForSQPSensitivity(const Eigen::MatrixXd &dConstrAct, const Eigen::MatrixXd &HConstrAct,
435  const Eigen::MatrixXd &HObjAct, Eigen::MatrixXd &sensMult,
436  Eigen::MatrixXd &sensInv, Eigen::MatrixXd &sensRes) const;
437  bool PrepareMatricesForSQPSensitivity(const MatrixXd_sparse &dConstrAct, const MatrixXd_sparse &HConstrAct,
438  MatrixXd_sparse &HObjAct, Eigen::MatrixXd &sensMult, MatrixXd_sparse &sensInv,
439  Eigen::MatrixXd &sensRes) const;
440 
447  void ReturnVelocities(triangulation &triRSVS);
448  void ReturnSensitivities(const triangulation &triRSVS, std::vector<double> &sensVec, int constrNum) const;
449  void ReturnGradient(const triangulation &triRSVS, std::vector<double> &sensVec, int constrNum) const;
455  int numConstr() const
456  {
457  return (this->nConstr);
458  }
459  // Output functions
460 
467  void Print2Screen(int outType = 0) const;
468 
477  void ConvergenceLog(std::ofstream &out, int loglvl = 3) const;
478  void CalculateVelocities(triangulation &triRSVS, int calculationMethod = 0, bool calculateDerivatives = true,
479  int derivativeMethod = 1) override;
480  void setDevParameters(const param::dev::devparam &devset) override;
481  void SetUseSurfCentreDeriv(int in)
482  {
483  this->useSurfCentreDeriv = in;
484  }
485  bool GetUseSurfCentreDeriv() const
486  {
487  return (this->useSurfCentreDeriv);
488  }
489  void SetUseSurfCentreHessian(int in)
490  {
491  this->useSurfCentreHessian = in;
492  }
493  bool SetUseSurfCentreHessian() const
494  {
495  return (this->useSurfCentreHessian);
496  }
497  void SetSparseDVcutoff(int in)
498  {
499  this->sparseDVcutoff = in;
500  }
501  int GetSparseDVcutoff() const
502  {
503  return (this->sparseDVcutoff);
504  }
505 };
506 
507 #endif
Interface for the Snaking algorithm.
Provide std::vector container with hashed index mapping.
Class to handle the RSVS calculation.
Definition: RSVScalc.hpp:130
void CalculateMesh(mesh &meshin)
Calculates the mesh volumes.
Definition: RSVScalc.cpp:294
Eigen::RowVectorXd dObj
Objective Jacobian, size: [1, nDv].
Definition: RSVScalc.hpp:175
Eigen::VectorXd constr
Constraint value vector, size: [nConstr, 1].
Definition: RSVScalc.hpp:177
void CalcTriangleEdgeLength(const triangle &triIn, const triangulation &triRSVS, bool isObj=true, bool isConstr=true, bool isDeriv=true)
Calculates the properties of single triangle for 2D RSVS.
void PrepTriangulationCalc(const triangulation &triRSVS)
Groups actions needed before the calculation of triangular quantities.
Definition: RSVScalc.cpp:35
Eigen::MatrixXd dConstr
Constraint Jacobian, size: [nConstr, nDv].
Definition: RSVScalc.hpp:162
Eigen::MatrixXd sensDv
Sensitivity of the optimum design variables to the constraint.
Definition: RSVScalc.hpp:187
Eigen::MatrixXd HConstr
Constraint Hessian, size: [nDv, nDv].
Definition: RSVScalc.hpp:165
void CalcTriangle(const triangle &triIn, const triangulation &triRSVS, bool isObj=true, bool isConstr=true, bool isDeriv=true)
Calculates the properties of single triangle.
HashedMap< int, int, int > constrMap
maps snakemesh() volu onto constr
Definition: RSVScalc.hpp:204
int BuildDVMap(const std::vector< int > &vecin)
Builds a Design variable map.
Definition: RSVScalc.cpp:522
Eigen::VectorXd deltaDV
Change in design variable, assigned to snake velocity, size: [nDv, 1].
Definition: RSVScalc.hpp:181
void ReturnConstrToMesh(triangulation &triRSVS) const
Returns a constraint to the triangulation::meshDep.
Definition: RSVScalc.cpp:400
void CheckAndCompute(int calcMethod=0, bool sensCalc=false)
Prepare the active arrays for SQP calculation and calculate the SQP step.
int falseaccess
Number of false access operations.
Definition: RSVScalc.hpp:137
bool PrepareMatricesForSQP(Eigen::MatrixXd &dConstrAct, Eigen::MatrixXd &HConstrAct, Eigen::MatrixXd &HObjAct, MatrixXd_sparse &dConstrAct_sparse, MatrixXd_sparse &HConstrAct_sparse, MatrixXd_sparse &HObjAct_sparse, Eigen::RowVectorXd &dObjAct, Eigen::VectorXd &constrAct, Eigen::VectorXd &lagMultAct)
Prepares the matrices needed for the SQP step calculation.
std::vector< bool > isConstrAct
is the corresponding constraint active?
Definition: RSVScalc.hpp:194
void BuildConstrMap(const triangulation &triangleRSVS)
Builds the constraint mapping.
Definition: RSVScalc.cpp:488
Eigen::MatrixXd HObj
Objective Hessian, size: [nDv, nDv].
Definition: RSVScalc.hpp:168
int sparseDVcutoff
Number of design variables to start using sparse mathematics.
Definition: RSVScalc.hpp:139
bool useSurfCentreDeriv
Enable or disable surfcentroid derivatives.
Definition: RSVScalc.hpp:145
void CalcTriangleDirectVolume(const triangle &triIn, const triangulation &triRSVS, bool isObj=true, bool isConstr=true, bool isDeriv=true)
Calculates the properties of single triangle using direct calculation.
void CalculateTriangulation(const triangulation &triRSVS, int derivMethod=0)
Calculates the triangulation volume and area derivatives.
Definition: RSVScalc.cpp:95
Eigen::MatrixXd HLag
Lagrangian Hessian, size: [nDv, nDv].
Definition: RSVScalc.hpp:171
HashedVector< int, int > subConstrAct
Vector of subscripts of the active constraints.
Definition: RSVScalc.hpp:198
int nConstr
Number of constraints.
Definition: RSVScalc.hpp:135
void setDevParameters(const param::dev::devparam &devset) override
Set the Development parameters of the Calculator object.
Definition: RSVScalc.cpp:585
std::vector< bool > isDvAct
Is the corresponding design variable active?
Definition: RSVScalc.hpp:196
bool useSurfCentreHessian
Enable or disable surfcentroid derivatives.
Definition: RSVScalc.hpp:147
bool SnakDVcond(const triangulation &triRSVS, int ii)
Returns wether a snaxel is a design variable or not.
Definition: RSVScalc.cpp:73
int nDv
Number of design variables.
Definition: RSVScalc.hpp:133
double obj
Objective function value.
Definition: RSVScalc.hpp:189
void Print2Screen(int outType=0) const
Prints different amounts of RSVScalc owned data to the screen.
Definition: RSVScalc.cpp:334
bool returnDeriv
Return the derivatives (obsolete/unused)
Definition: RSVScalc.hpp:143
bool UseFullMath() const
Decides wether full or sparse math should be used.
Definition: RSVScalc.hpp:215
int numConstr() const
Getter for the number of constraints.
Definition: RSVScalc.hpp:455
void ComputeSQPstep(int calcMethod, Eigen::MatrixXd &dConstrAct, Eigen::RowVectorXd &dObjAct, Eigen::VectorXd &constrAct, Eigen::VectorXd &lagMultAct)
Calculates the next SQP step.
bool PrepareMatricesForSQPSensitivity(const Eigen::MatrixXd &dConstrAct, const Eigen::MatrixXd &HConstrAct, const Eigen::MatrixXd &HObjAct, Eigen::MatrixXd &sensMult, Eigen::MatrixXd &sensInv, Eigen::MatrixXd &sensRes) const
Prepares the matrices needed for the calculation of the sensitivity of the SQP.
std::vector< std::pair< int, int > > constrList
keeps pairs with parentindex and voluindex
Definition: RSVScalc.hpp:206
void ReturnVelocities(triangulation &triRSVS)
Returns velocities to the snaxels.
Definition: RSVScalc.cpp:143
HashedVector< int, int > subDvAct
Vector of subscripts of the active design variables.
Definition: RSVScalc.hpp:200
void BuildMathArrays(int nDv, int nConstr)
Builds mathematics arrays.
Definition: RSVScalc.cpp:443
Eigen::VectorXd lagMult
Lagrangian multiplier, size: [nConstr, 1].
Definition: RSVScalc.hpp:179
double limLag
Value at which a Lagrangian multiplier is considered problematically large.
Definition: RSVScalc.hpp:192
Eigen::VectorXd constrTarg
Constraint target values, size: [nConstr, 1].
Definition: RSVScalc.hpp:183
HashedVector< int, int > dvMap
Maps the snake indices to the position in the design variable vector.
Definition: RSVScalc.hpp:202
void CalcTriangleFD(const triangle &triIn, const triangulation &triRSVS, bool isObj=true, bool isConstr=true, bool isDeriv=true)
Calculates the properties of single triangle using Finite difference.
void ConvergenceLog(std::ofstream &out, int loglvl=3) const
Print convergence information to file stream.
Definition: RSVScalc.cpp:531
Class for mesh handling.
Definition: mesh.hpp:592
Class for development parameters.
Definition: parameters.hpp:272
A base class which needs to be inherited from to implement a new velocity calculation.
Class for volume cell objects in a mesh.
Definition: mesh.hpp:197
Provides all the mesh tools used for the generation of 3D grids and geometries.
Namespace containing the parameter classes used to control execution of the 3D-RSVS program.
Definition: main.hpp:15
Provides the core restricted surface snake container.
Provides a triangulation for snake and meshes.
Provides the error and warning system used by the RSVS3D project.
#define RSVS3D_ERROR_RANGE(M)
Throw a range_error.
Definition: warning.hpp:173