check_traits.h 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // This file is part of libhedra, a library for polyhedral mesh processing
  2. //
  3. // Copyright (C) 2016 Amir Vaxman <avaxman@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #ifndef HEDRA_CHECK_TRAITS_H
  9. #define HEDRA_CHECK_TRAITS_H
  10. #include <igl/igl_inline.h>
  11. #include <Eigen/Core>
  12. #include <string>
  13. #include <vector>
  14. #include <cstdio>
  15. namespace hedra {
  16. namespace optimization
  17. {
  18. //This function checks the Jacobian of a traits class that is put for optimization by approximate finite differences, and reports the difference. It is important to use after coding, but it is not for the actual optimization process, since it is really brute-force and slow.
  19. template<class SolverTraits>
  20. void check_traits(SolverTraits& Traits){
  21. using namespace Eigen;
  22. using namespace std;
  23. cout<<"WARNING: FE gradient checking, reducing performance!!!"<<endl;
  24. cout<<"******************************************************"<<endl;
  25. VectorXd CurrSolution(Traits.xSize);
  26. CurrSolution<<VectorXd::Random(Traits.xSize, 1);
  27. cout<<"Solution Size: "<<Traits.xSize<<endl;
  28. Traits.update_energy(CurrSolution);
  29. Traits.update_jacobian(CurrSolution);
  30. int MaxRow=Traits.JRows.maxCoeff()+1;
  31. vector<Triplet<double> > GradTris;
  32. for (int i=0;i<Traits.JRows.size();i++)
  33. GradTris.push_back(Triplet<double>(Traits.JRows(i), Traits.JCols(i), Traits.JVals(i)));
  34. SparseMatrix<double> TraitGradient(MaxRow, CurrSolution.size());
  35. TraitGradient.setFromTriplets(GradTris.begin(),GradTris.end());
  36. SparseMatrix<double> FEGradient(MaxRow, CurrSolution.size());
  37. vector<Triplet<double> > FEGradientTris;
  38. for (int i=0;i<CurrSolution.size();i++){
  39. VectorXd vh(CurrSolution.size()); vh.setZero(); vh(i)=10e-5;
  40. Traits.update_energy(CurrSolution+vh);
  41. Traits.update_jacobian(CurrSolution+vh);
  42. VectorXd EnergyPlus=Traits.EVec;
  43. Traits.update_energy(CurrSolution-vh);
  44. Traits.update_jacobian(CurrSolution-vh);
  45. VectorXd EnergyMinus=Traits.EVec;
  46. VectorXd CurrGradient=(EnergyPlus-EnergyMinus)/(2*10e-5);
  47. //cout<<CurrGradient<<endl;
  48. for (int j=0;j<CurrGradient.size();j++)
  49. if (abs(CurrGradient(j))>10e-7)
  50. FEGradientTris.push_back(Triplet<double>(j,i,CurrGradient(j)));
  51. }
  52. FEGradient.setFromTriplets(FEGradientTris.begin(), FEGradientTris.end());
  53. SparseMatrix<double> DiffMat=FEGradient-TraitGradient;
  54. double maxcoeff=-32767.0;
  55. int Maxi,Maxj;
  56. for (int k=0; k<DiffMat.outerSize(); ++k)
  57. for (SparseMatrix<double>::InnerIterator it(DiffMat,k); it; ++it){
  58. if (maxcoeff<abs(it.value())){
  59. maxcoeff=abs(it.value());
  60. Maxi=it.row();
  61. Maxj=it.col();
  62. }
  63. if (abs(it.value())>10e-7){
  64. cout<<"Gradient Discrepancy at: ("<<it.row()<<","<<it.col()<<") of "<<it.value()<<endl;
  65. cout<<"Our Value: "<<TraitGradient.coeffRef(it.row(), it.col())<<endl;
  66. cout<<"Computed Value: "<<FEGradient.coeffRef(it.row(), it.col())<<endl;
  67. }
  68. }
  69. cout<<"Maximum gradient difference: "<<maxcoeff<<endl;
  70. cout<<"At Location: ("<<Maxi<<","<<Maxj<<")"<<endl;
  71. }
  72. }
  73. }
  74. #endif