24 #define TOSENSVEC(val, ind) rsvs3d::SignedLogScale((ind != rsvs3d::constants::__notfound ? val : 0.0))
41 nConstr = triRSVS.meshDep->CountVoluParent();
42 if (triRSVS.snakeDep != NULL)
44 nDv = triRSVS.snakeDep->snaxs.size();
53 BuildConstrMap(triRSVS);
57 for (ii = 0; ii < nDv; ++ii)
59 if (!triRSVS.snakeDep->snaxs(ii)->isfreeze)
61 vecin.push_back(triRSVS.snakeDep->snaxs(ii)->index);
63 else if (this->SnakDVcond(triRSVS, ii))
65 vecin.push_back(triRSVS.snakeDep->snaxs(ii)->index);
68 nDv = this->BuildDVMap(vecin);
70 BuildMathArrays(nDv, nConstr);
75 bool allNeighFroze =
true;
79 nEdges = triRSVS.snakeDep->snakeconn.verts(ii)->edgeind.size();
83 while (kk < nEdges && allNeighFroze)
85 tempEdge = triRSVS.snakeDep->snakeconn.edges.isearch(triRSVS.snakeDep->snakeconn.verts(ii)->edgeind[kk]);
86 allNeighFroze &= triRSVS.snakeDep->snaxs.isearch(tempEdge->vertind[0])->isfreeze;
87 allNeighFroze &= triRSVS.snakeDep->snaxs.isearch(tempEdge->vertind[1])->isfreeze;
92 return (!allNeighFroze);
99 this->returnDeriv =
true;
102 this->PrepTriangulationCalc(triRSVS);
117 ni = triRSVS.dynatri.size();
118 for (ii = 0; ii < ni; ii++)
120 (this->*calcTriFunc)(*(triRSVS.dynatri(ii)), triRSVS,
true,
true,
true);
123 ni = triRSVS.intertri.size();
124 for (ii = 0; ii < ni; ii++)
126 (this->*calcTriFunc)(*(triRSVS.intertri(ii)), triRSVS,
false,
true,
true);
129 ni = triRSVS.acttri.size();
130 for (ii = 0; ii < ni; ii++)
132 (this->*calcTriFunc)(*(triRSVS.stattri.isearch(triRSVS.acttri[ii])), triRSVS,
false,
true,
false);
147 ni = triRSVS.snakeDep->snaxs.size();
148 for (ii = 0; ii < ni; ii++)
150 temp = this->dvMap.find(triRSVS.snakeDep->snaxs(ii)->index);
151 triRSVS.snakeDep->snaxs[ii].v = temp != -1 ? this->deltaDV[temp] : 0;
153 triRSVS.snakeDep->snaxs.PrepareForUse();
156 void RSVScalc::ReturnSensitivities(
const triangulation &triRSVS, std::vector<double> &sensVec,
int constrNum)
const
160 if (constrNum >= this->nConstr)
164 ni = triRSVS.snakeDep->snaxs.size();
165 sensVec.assign(ni, 0);
166 for (ii = 0; ii < ni; ii++)
168 temp = this->dvMap.find(triRSVS.snakeDep->snaxs(ii)->index);
169 sensVec[ii] = TOSENSVEC(this->sensDv(temp, constrNum), temp);
173 void RSVScalc::ReturnGradient(
const triangulation &triRSVS, std::vector<double> &sensVec,
int constrNum)
const
182 if (constrNum >= this->nConstr || constrNum < -nNegOpts)
186 ni = triRSVS.snakeDep->snaxs.size();
187 sensVec.assign(ni, 0);
188 int nNegTest = -nNegOpts;
189 if (constrNum == nNegTest++)
191 for (ii = 0; ii < ni; ii++)
193 temp = this->dvMap.find(triRSVS.snakeDep->snaxs(ii)->index);
194 sensVec[ii] = TOSENSVEC(this->dObj[temp], temp);
197 else if (constrNum == nNegTest++)
199 auto dLagTemp = this->dObj + this->lagMult.transpose() * this->dConstr;
200 for (ii = 0; ii < ni; ii++)
202 temp = this->dvMap.find(triRSVS.snakeDep->snaxs(ii)->index);
203 sensVec[ii] = TOSENSVEC(dLagTemp[temp], temp);
206 else if (constrNum == nNegTest++)
208 if (this->UseFullMath())
210 for (ii = 0; ii < ni; ii++)
212 temp = this->dvMap.find(triRSVS.snakeDep->snaxs(ii)->index);
213 sensVec[ii] = TOSENSVEC(this->HObj(temp, temp), temp);
218 for (ii = 0; ii < ni; ii++)
220 temp = this->dvMap.find(triRSVS.snakeDep->snaxs(ii)->index);
221 sensVec[ii] = TOSENSVEC(this->HObj_sparse(temp, temp), temp);
225 else if (constrNum == nNegTest++)
227 if (this->UseFullMath())
229 for (ii = 0; ii < ni; ii++)
231 temp = this->dvMap.find(triRSVS.snakeDep->snaxs(ii)->index);
232 sensVec[ii] = TOSENSVEC(this->HConstr(temp, temp), temp);
237 for (ii = 0; ii < ni; ii++)
239 temp = this->dvMap.find(triRSVS.snakeDep->snaxs(ii)->index);
240 sensVec[ii] = TOSENSVEC(this->HConstr_sparse(temp, temp), temp);
244 else if (constrNum == nNegTest++)
246 if (this->UseFullMath())
248 auto HLagTemp = this->HConstr + this->HObj;
249 for (ii = 0; ii < ni; ii++)
251 temp = this->dvMap.find(triRSVS.snakeDep->snaxs(ii)->index);
252 sensVec[ii] = TOSENSVEC(HLagTemp(temp, temp), temp);
257 for (ii = 0; ii < ni; ii++)
259 temp = this->dvMap.find(triRSVS.snakeDep->snaxs(ii)->index);
260 sensVec[ii] = TOSENSVEC((this->HConstr_sparse(temp, temp) + this->HObj_sparse(temp, temp)), temp);
264 else if (constrNum == nNegTest++)
266 if (this->UseFullMath())
268 for (ii = 0; ii < ni; ii++)
270 temp = this->dvMap.find(triRSVS.snakeDep->snaxs(ii)->index);
271 sensVec[ii] = TOSENSVEC(this->deltaDV[temp], temp);
284 " before this case.");
286 for (ii = 0; ii < ni; ii++)
288 temp = this->dvMap.find(triRSVS.snakeDep->snaxs(ii)->index);
289 sensVec[ii] = TOSENSVEC(this->dConstr(constrNum, temp), temp);
301 triRSVS.PrepareForUse();
302 this->returnDeriv =
false;
303 nConstr = meshin.volus.size();
307 BuildMathArrays(nDv, nConstr);
312 BuildConstrMap(meshin);
315 nj = meshin.surfs.size();
316 for (jj = 0; jj < nj; jj++)
318 TriangulateSurface(*(meshin.surfs(jj)), meshin, triRSVS.stattri, triRSVS.trivert, 1, 1);
319 triRSVS.PrepareForUse();
320 triRSVS.CalcTriVertPos();
321 triRSVS.PrepareForUse();
322 ni = triRSVS.stattri.size();
323 for (ii = 0; ii < ni; ii++)
325 CalcTriangle(*(triRSVS.stattri(ii)), triRSVS,
true,
true,
false);
327 triRSVS.stattri.clear();
328 triRSVS.trivert.clear();
336 cout <<
"Math result: obj " << obj <<
" false access " << falseaccess << endl;
338 for (
int i = 0; i < nConstr; ++i)
340 cout << constr[i] <<
" ";
342 cout <<
"constrTarg :" << endl;
343 PrintMatrix(constrTarg);
345 if ((nConstr < 10 && nDv < 20 && outType == 1) || outType == 3)
347 cout <<
"constr :" << endl;
349 cout <<
"dObj :" << endl;
351 cout <<
"lagMult :" << endl;
352 PrintMatrix(lagMult);
354 cout <<
"dConstr :" << endl;
355 PrintMatrix(dConstr);
356 cout <<
"HConstr :" << endl;
357 PrintMatrix(HConstr);
358 cout <<
"HObj :" << endl;
360 cout <<
"constrTarg :" << endl;
361 PrintMatrix(constrTarg);
365 cout <<
"constrTarg :" << endl;
366 PrintMatrix(constrTarg);
368 for (
int i = 0; i < nDv; ++i)
370 cout << deltaDV[i] <<
" ";
373 for (
int i = 0; i < nConstr; ++i)
375 cout << lagMult[i] <<
" ";
381 const char *file =
"matrices/dumpmatout.txt";
382 cout <<
"constr :" << endl;
383 PrintMatrixFile(constr, file);
384 cout <<
"dObj :" << endl;
385 PrintMatrixFile(dObj, file);
386 cout <<
"lagMult :" << endl;
387 PrintMatrixFile(lagMult, file);
389 cout <<
"dConstr :" << endl;
390 PrintMatrixFile(dConstr, file);
391 cout <<
"HConstr :" << endl;
392 PrintMatrixFile(HConstr, file);
393 cout <<
"HObj :" << endl;
394 PrintMatrixFile(HObj, file);
395 cout <<
"constrTarg :" << endl;
396 PrintMatrixFile(constrTarg, file);
408 for (ii = 0; ii < ni; ii++)
410 temp.push_back(constr[ii]);
412 triRSVS.meshDep->MapVolu2Parent(temp, this->constrList, &volu::fill);
414 for (ii = 0; ii < ni; ii++)
416 tempVal = fabs(constrTarg[ii] - constr[ii]);
419 temp.push_back(log10(tempVal));
426 triRSVS.meshDep->MapVolu2Parent(temp, this->constrList, &volu::error);
436 for (ii = 0; ii < ni; ii++)
438 temp.push_back(constr[ii]);
440 meshin.MapVolu2Self(temp, constrMap.vec, mp);
448 this->nConstr = nConstrIn;
449 this->isConstrAct.clear();
450 this->isDvAct.clear();
451 this->isConstrAct.assign(nConstr,
false);
452 this->isDvAct.assign(nDv,
false);
454 this->constr.setZero(nConstr);
457 this->dObj.setZero(nDv);
459 if (this->nConstr != this->lagMult.size())
461 this->lagMult.setZero(nConstr);
463 this->deltaDV.setZero(nDv);
465 this->dvCallConstr.setZero(nDv, 1);
467 if (this->UseFullMath())
469 this->dConstr.setZero(nConstr, nDv);
470 this->HConstr.setZero(nDv, nDv);
471 this->HObj.setZero(nDv, nDv);
475 this->dConstr_sparse.setZero();
476 this->HConstr_sparse.setZero();
477 this->HObj_sparse.setZero();
478 this->HLag_sparse.setZero();
479 this->dConstr_sparse.resize(nConstr, nDv);
480 this->dConstr_sparse.reserve(nDv * 2);
481 this->HConstr_sparse.resize(nDv, nDv);
482 this->HConstr_sparse.reserve(nDv * 13);
483 this->HObj_sparse.resize(nDv, nDv);
484 this->HObj_sparse.reserve(nDv * 13);
494 vector<double> voluVals;
495 triangleRSVS.meshDep->ReturnParentMap(constrMap.vec, constrMap.targ, constrList, voluVals);
497 constrMap.GenerateHash();
498 constrTarg.setZero(voluVals.size());
499 for (
int i = 0; i < int(voluVals.size()); ++i)
501 constrTarg[i] = voluVals[i];
508 ni = meshin.volus.size();
509 constrMap.vec.clear();
510 constrMap.targ.clear();
511 constrMap.vec.reserve(ni);
512 constrMap.targ.reserve(ni);
513 for (ii = 0; ii < ni; ++ii)
515 constrMap.vec.push_back(meshin.volus(ii)->index);
516 constrMap.targ.push_back(ii);
519 constrMap.GenerateHash();
524 this->dvMap.vec = vecin;
525 sort(this->dvMap.vec);
526 unique(this->dvMap.vec);
527 this->dvMap.GenerateHash();
528 return this->dvMap.vec.size();
540 double normConstr, normVel, normObjDeriv;
543 out <<
"> constraint residual :, ";
544 normConstr = StreamStatistics(abs(this->constr.array() - this->constrTarg.array()), out,
string(
", "));
545 out <<
"> constraint delta :, ";
546 StreamStatistics((this->constr.array() - this->constrTarg.array()), out,
string(
", "));
547 out <<
"> objective residual :, ";
548 StreamStatistics(abs(this->deltaDV.array()), out,
string(
", "));
549 out <<
"> objective delta :, ";
550 normVel = StreamStatistics(this->deltaDV.array(), out,
string(
", "));
551 out <<
"> objective derivative :, ";
552 normObjDeriv = StreamStatistics(this->dObj.array(), out,
string(
", "));
553 out <<
"> objective value:," << this->obj << std::endl;
554 auto prec = std::cout.precision();
555 std::cout <<
" conv:" << std::setprecision(3) << std::left <<
" (vol) " << std::setw(8) << normConstr
556 <<
" (vel) " << std::setw(8) << normVel <<
" (Dobj) " << std::setw(8) << normObjDeriv <<
"; "
557 << std::setprecision(prec) << std::right;
562 out <<
"> constraint residuals :, ";
563 StreamOutVector(abs(this->constr.array() - this->constrTarg.array()), out,
string(
", "));
565 out <<
"> volume values :, ";
566 StreamOutVector(this->constr.array(), out,
string(
", "));
568 out <<
"> constraint deltas :, ";
569 StreamOutVector((this->constr.array() - this->constrTarg.array()), out,
string(
", "));
573 out <<
"> snaxel velocity :, ";
574 StreamOutVector(move(deltaDV), out,
string(
", "));
578 void RSVScalc::PrintTimers()
const
580 std::cout <<
" t1 " << rsvs3d::Clock2ms(this->timer1) <<
",";
581 std::cout <<
" t2 " << rsvs3d::Clock2ms(this->timer2) <<
",";
582 std::cout <<
" t3 " << rsvs3d::Clock2ms(this->timer3) <<
",";
587 this->limLag = devset.limitlagrangian;
588 this->SetUseSurfCentreDeriv(devset.surfcentrejacobian);
589 this->SetUseSurfCentreHessian(devset.surfcentrehessian);
590 this->SetSparseDVcutoff(devset.mindesvarsparse);
593 void RSVScalc::CalculateVelocities(
triangulation &triRSVS,
int calculationMethod,
bool calculateDerivatives,
594 int derivativeMethod)
596 this->CalculateTriangulation(triRSVS, derivativeMethod);
598 this->CheckAndCompute(calculationMethod, calculateDerivatives);
606 this->ReturnConstrToMesh(triRSVS);
607 this->ReturnVelocities(triRSVS);
610 void SparseMatrixTriplet::SetEqual(MatrixXd_sparse &targ)
612 int n = this->nonZeros();
614 targ.resize(this->nRow, this->nCol);
616 for (
int i = 0; i < n; ++i)
618 auto it = this->operator[](i);
619 targ.insert(it.row(), it.col()) = it.value();
623 void SparseMatrixTriplet::SetEqual(Eigen::MatrixXd &targ)
625 int n = this->nonZeros();
626 targ.resize(this->nRow, this->nCol);
627 targ.setZero(this->nRow, this->nCol);
628 for (
int i = 0; i < n; ++i)
630 auto it = this->operator[](i);
631 targ(it.row(), it.col()) = it.value();
635 void SparseMatrixTriplet::reserve(
size_t a)
637 this->TripletMap::reserve(a);
643 if (a < 0 || a >=
int(this->TripletMap::size()))
Provides the infrastructure for calculation of the RSVS equations.
void CalculateMesh(mesh &meshin)
Calculates the mesh volumes.
void PrepTriangulationCalc(const triangulation &triRSVS)
Groups actions needed before the calculation of triangular quantities.
void CalcTriangle(const triangle &triIn, const triangulation &triRSVS, bool isObj=true, bool isConstr=true, bool isDeriv=true)
Calculates the properties of single triangle.
int BuildDVMap(const std::vector< int > &vecin)
Builds a Design variable map.
void ReturnConstrToMesh(triangulation &triRSVS) const
Returns a constraint to the triangulation::meshDep.
void BuildConstrMap(const triangulation &triangleRSVS)
Builds the constraint mapping.
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.
void setDevParameters(const param::dev::devparam &devset) override
Set the Development parameters of the Calculator object.
bool SnakDVcond(const triangulation &triRSVS, int ii)
Returns wether a snaxel is a design variable or not.
void Print2Screen(int outType=0) const
Prints different amounts of RSVScalc owned data to the screen.
void ReturnVelocities(triangulation &triRSVS)
Returns velocities to the snaxels.
void BuildMathArrays(int nDv, int nConstr)
Builds mathematics arrays.
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.
Class for an edge object in a mesh.
Class for development parameters.
Class for volume cell objects in a mesh.
Parameters for the integrated 3DRSVS.
Provides the core restricted surface snake container.
Provides a triangulation for snake and meshes.
Provides a 2D std::vector based container.
#define RSVS3D_ERROR_LOGIC(M)
Throw a logic_error.
#define RSVS3D_ERROR_NOTHROW(M)
Generic rsvs warning.
#define RSVS3D_ERROR_RANGE(M)
Throw a range_error.