31 #ifdef gsHLBFGS_ENABLED
32 #include <gsHLBFGS/gsHLBFGS.h>
48 template<
typename T = real_t>
54 for (
size_t iptch = 0; iptch != mp.
nPatches(); iptch++) {
56 for (
size_t idof = 0; idof != mapper.
patchSize(iptch, idim); idof++)
59 if (mapper.
is_free(idof, iptch, idim)) {
61 freeVec(idx) = mp.
patch(iptch).coefs()(idof, idim);
69 template<
typename T = real_t>
73 for (
size_t iptch = 0; iptch != mp.
nPatches(); iptch++) {
75 for (
size_t idof = 0; idof != mapper.
patchSize(iptch, idim); idof++)
78 if (mapper.
is_free(idof, iptch, idim)) {
80 mp.
patch(iptch).coefs()(idof, idim) = gsFreeVec(idx);
87 #ifdef gsHLBFGS_ENABLED
89 void setOptimizerOptions(gsHLBFGS<T> &optimizer,
const gsOptionList &options) {
90 optimizer.options().setInt(
"MaxIterations",
91 options.askInt(
"qi_MaxIterations", 1e4));
92 optimizer.options().setReal(
"MinGradientLength",
93 options.askReal(
"qi_MinGradientLength", 1e-4));
94 optimizer.options().setReal(
"MinStepLength",
95 options.askReal(
"qi_MinStepLength", 1e-4));
96 optimizer.options().setInt(
"Verbose", options.askInt(
"Verbose", 0));
102 if (verbose > 0) {
gsInfo << message <<
"\n"; }
106 template<
short_t d,
typename T= real_t>
185 #ifdef gsHLBFGS_ENABLED
186 template<
short_t d,
typename T = real_t>
205 void setDelta(
const T &delta) { m_eps = delta; };
208 gsOptionList options() {
return m_options; }
211 void defaultOptions();
214 void addOptions(
const gsOptionList &options);
217 void applyOptions(
const gsOptionList &options);
220 mutable gsMultiPatch<T> m_mp;
221 const gsDofMapper m_mapper;
222 const gsMultiBasis<T> m_mb;
224 mutable gsExprEvaluator<T> m_evaluator;
225 mutable gsExprAssembler<T> m_assembler;
227 gsOptionList m_options;
232 template<
short_t d,
typename T>
233 class gsObjQualityImprovePt :
public gsOptProblem<T> {
235 typedef typename gsExprAssembler<T>::geometryMap geometryMap;
236 typedef typename gsExprAssembler<T>::space space;
237 typedef typename gsExprAssembler<T>::solution solution;
240 gsObjQualityImprovePt(
const gsMultiPatch<T> &patches, gsDofMapper mapper);
243 T evalObj(
const gsAsConstVector<T> &u)
const override;
247 void gradObj_into(
const gsAsConstVector<T> &u,
248 gsAsVector<T> &result)
const override;
251 gsOptionList options() {
return m_options; }
254 void defaultOptions();
257 void addOptions(
const gsOptionList &options);
260 void applyOptions(
const gsOptionList &options);
264 typename std::enable_if<_d == 2, T>::type
265 evalObj_impl(
const gsAsConstVector<T> &u)
const;
268 typename std::enable_if<_d == 3, T>::type
269 evalObj_impl(
const gsAsConstVector<T> &u)
const;
272 typename std::enable_if<_d != 2 && _d != 3, T>::type
273 evalObj_impl(
const gsAsConstVector<T> &u)
const {
274 GISMO_ERROR(
"The dimension of target domain should be 2 or 3.");
278 typename std::enable_if<_d == 2, T>::type
279 gradObj_into_impl(
const gsAsConstVector<T> &u,
280 gsAsVector<T> &result)
const;
283 typename std::enable_if<_d == 3, T>::type
284 gradObj_into_impl(
const gsAsConstVector<T> &u,
285 gsAsVector<T> &result)
const;
288 typename std::enable_if<_d != 2 && _d != 3, T>::type
289 gradObj_into_impl(
const gsAsConstVector<T> &u,
290 gsAsVector<T> &result)
const {
291 GISMO_ERROR(
"The dimension of target domain should be 2 or 3.");
295 mutable gsMultiPatch<T> m_mp;
296 const gsDofMapper m_mapper;
297 const gsMultiBasis<T> m_mb;
299 mutable gsExprEvaluator<T> m_evaluator;
300 mutable gsExprAssembler<T> m_assembler;
302 gsOptionList m_options;
304 T m_lambda1 = 1.0, m_lambda2 = 1.0;
307 template<
short_t d,
typename T>
308 class gsObjVHPt :
public gsOptProblem<T> {
310 typedef typename gsExprAssembler<T>::geometryMap geometryMap;
311 typedef typename gsExprAssembler<T>::space space;
312 typedef typename gsExprAssembler<T>::solution solution;
315 gsObjVHPt(
const gsMultiPatch<T> &patches,
318 T evalObj(
const gsAsConstVector<T> &u)
const final;
320 void gradObj2_into(
const gsAsConstVector<T> &u,
321 gsAsVector<T> &result)
const;
323 gsOptionList &options() {
return m_options; }
325 void setEps(T tol) { m_eps = tol; }
327 void defaultOptions();
329 void addOptions(
const gsOptionList &options);
331 void applyOptions(
const gsOptionList &options);
335 typename std::enable_if<_d == 2, T>::type
336 evalObj_impl(
const gsAsConstVector<T> &u)
const;
339 typename std::enable_if<_d == 3, T>::type
340 evalObj_impl(
const gsAsConstVector<T> &u)
const;
343 typename std::enable_if<_d != 2 && _d != 3, T>::type
348 typename std::enable_if<_d == 2, T>::type
349 gradObj_into_impl(
const gsAsConstVector<T> &u,
350 gsAsVector<T> &result)
const;
353 typename std::enable_if<_d == 3, T>::type
354 gradObj_into_impl(
const gsAsConstVector<T> &u,
355 gsAsVector<T> &result)
const;
358 typename std::enable_if<_d != 2 && _d != 3, T>::type
359 gradObj_into_impl(
const gsAsConstVector<T> &u,
363 mutable gsMultiPatch<T> m_mp;
364 const gsDofMapper m_mapper;
365 const gsMultiBasis<T> m_mb;
367 mutable gsExprEvaluator<T> m_evaluator;
368 mutable gsExprAssembler<T> m_assembler;
370 gsOptionList m_options;
372 T m_lambda1, m_lambda2, m_eps = 4e-4;
375 template<
short_t d,
typename T>
376 class gsObjPenaltyPt :
public gsOptProblem<T> {
378 typedef typename gsExprAssembler<T>::geometryMap geometryMap;
379 typedef typename gsExprAssembler<T>::space space;
380 typedef typename gsExprAssembler<T>::solution solution;
383 gsObjPenaltyPt(
const gsMultiPatch<T> &patches,
386 T evalObj(
const gsAsConstVector<T> &u)
const final;
388 void gradObj_into(
const gsAsConstVector<T> &u,
389 gsAsVector<T> &result)
const final;
391 gsOptionList &options() {
return m_options; }
393 void setEps(T tol) { m_eps = tol; }
395 void defaultOptions();
397 void addOptions(
const gsOptionList &options);
399 void applyOptions(
const gsOptionList &options);
404 typename std::enable_if<_d == 2, T>::type
405 gradObj_into_impl(
const gsAsConstVector<T> &u,
406 gsAsVector<T> &result)
const;
409 typename std::enable_if<_d == 3, T>::type
410 gradObj_into_impl(
const gsAsConstVector<T> &u,
411 gsAsVector<T> &result)
const;
414 typename std::enable_if<_d != 2 && _d != 3, T>::type
415 gradObj_into_impl(
const gsAsConstVector<T> &u,
419 mutable gsMultiPatch<T> m_mp;
420 const gsDofMapper m_mapper;
421 const gsMultiBasis<T> m_mb;
423 mutable gsExprEvaluator<T> m_evaluator;
424 mutable gsExprAssembler<T> m_assembler;
426 gsOptionList m_options;
428 T m_lambda1, m_lambda2, m_eps = 1e-4;
431 template<
short_t d,
typename T>
432 class gsObjPenaltyPt2 :
public gsOptProblem<T> {
434 typedef typename gsExprAssembler<T>::geometryMap geometryMap;
435 typedef typename gsExprAssembler<T>::space space;
436 typedef typename gsExprAssembler<T>::solution solution;
439 gsObjPenaltyPt2(
const gsMultiPatch<T> &patches,
442 T evalObj(
const gsAsConstVector<T> &u)
const final;
444 void gradObj_into(
const gsAsConstVector<T> &u,
445 gsAsVector<T> &result)
const;
447 void setEps(T tol) { m_eps = tol; }
449 gsOptionList &options() {
return m_options; }
451 void defaultOptions();
453 void addOptions(
const gsOptionList &options);
455 void applyOptions(
const gsOptionList &options);
459 typename std::enable_if<_d == 2, T>::type
460 evalObj_impl(
const gsAsConstVector<T> &u)
const;
463 typename std::enable_if<_d == 3, T>::type
464 evalObj_impl(
const gsAsConstVector<T> &u)
const;
467 typename std::enable_if<_d != 2 && _d != 3, T>::type
472 typename std::enable_if<_d == 2, T>::type
473 gradObj_into_impl(
const gsAsConstVector<T> &u,
474 gsAsVector<T> &result)
const;
477 typename std::enable_if<_d == 3, T>::type
478 gradObj_into_impl(
const gsAsConstVector<T> &u,
479 gsAsVector<T> &result)
const;
482 typename std::enable_if<_d != 2 && _d != 3, T>::type
483 gradObj_into_impl(
const gsAsConstVector<T> &u,
487 mutable gsMultiPatch<T> m_mp;
488 const gsDofMapper m_mapper;
489 const gsMultiBasis<T> m_mb;
491 mutable gsExprEvaluator<T> m_evaluator;
492 mutable gsExprAssembler<T> m_assembler;
494 gsOptionList m_options;
496 T m_lambda1 = 1.0, m_lambda2 = 1.0, m_eps = 1e-3;
503 template <
typename Scalar =
double>
578 throw std::invalid_argument(
"'m' must be non-negative");
580 throw std::invalid_argument(
"'epsilon' must be non-negative");
582 throw std::invalid_argument(
"'epsilon_rel' must be non-negative");
584 throw std::invalid_argument(
"'max_iterations' must be non-negative");
592 template<
typename Func>
593 inline void measureTime(Func func) {
594 auto beforeTime = std::chrono::high_resolution_clock::now();
598 auto afterTime = std::chrono::high_resolution_clock::now();
599 auto duration = std::chrono::duration<double>(afterTime - beforeTime).count();
600 printf(
"\nTime passed by %.8f sec. \n", duration);
606 template<
typename Scalar =
double>
609 typedef std::function<gsSparseMatrix<Scalar>(
gsVector<Scalar> const &)> Jacobian_t;
610 typedef std::function<gsVector<Scalar>(
gsVector<Scalar> const &)> Residual_t;
619 const Jacobian_t &Jacobian) {
621 if (m_printInfo) { printSolverInfo();}
630 if (m_currResidualNorm < m_param.epsilon) {
631 printf(
"You are lucky, the initial guess is exactly the solution.\n\n\n");
634 m_solution = m_currentG;
639 printf(
"Iter. ||F(x)|| \n");
640 printIterationInfo();
644 startIteration(F, Jacobian);
649 inline void enableIterationInfoPrinting() { m_printInfo =
true; }
650 inline void disableIterationInfoPrinting() { m_printInfo =
false; }
653 inline void initialize(
const gsVector<Scalar> &u0) {
655 m_dim = m_solution.size();
659 m_currentF.resize(m_dim);
660 m_prevdG.resize(m_dim, m_param.m);
661 m_prevdF.resize(m_dim, m_param.m);
663 m_normalEquationMatrix.resize(m_param.m, m_param.m);
664 m_alpha.resize(m_param.m);
665 m_scaledF.resize(m_param.m);
667 if (m_param.usePreconditioning) { m_preconditioner.resize(m_dim, m_dim); }
670 inline void checkParam() {
671 m_param.check_param();
674 inline void printSolverInfo() {
675 printf(
"\nAnderson Acceleration SOLVER: parameter settings... \n");
676 printf(
"depth = %d\n", m_param.m);
677 printf(
"use preconditioner = %d\n",
678 m_param.usePreconditioning);
679 printf(
"update preconditioner step = %d\n\n",
680 m_param.updatePreconditionerStep);
683 inline void startIteration(
const Residual_t &F,
const Jacobian_t &Jacobian) {
685 if (m_param.m == 0) {
686 performPicardIteration(F, Jacobian);
688 performAAIteration(F, Jacobian);
692 inline void performPicardIteration(
const Residual_t &F,
693 const Jacobian_t &Jacobian) {
694 while (m_iter < m_param.max_iterations
695 && m_currResidualNorm > m_param.epsilon) {
699 m_solution = m_currentG;
701 printIterationInfo();
704 trackIterationInfo();
708 inline void performAAIteration(
const Residual_t &F,
709 const Jacobian_t &Jacobian) {
710 m_prevdF.col(0) = -m_currentF;
711 m_prevdG.col(0) = -m_currentG;
713 while (m_iter < m_param.max_iterations &&
714 m_currResidualNorm > m_param.epsilon) {
717 printIterationInfo();
719 updatePrevdFAndPrevdG();
726 updateColumnIndices();
729 trackIterationInfo();
734 inline void updateG(
const Residual_t &F,
const Jacobian_t &Jacobian) {
735 if (m_param.usePreconditioning) {
740 m_currentG = m_currentF + m_solution;
745 const Jacobian_t &Jacobian) {
746 if (!(m_iter % m_param.updatePreconditionerStep)) {
747 m_preconditioner = Jacobian(m_solution);
748 m_linearSolverPreconditioning.compute(m_preconditioner);
751 m_currResidualNorm = residual.norm();
753 m_currentF = -m_linearSolverPreconditioning.solve(residual);
758 m_currentF = F(m_solution);
759 m_currResidualNorm = m_currentF.norm();
762 inline void printIterationInfo() {
764 printf(
" %d %.4e\n", m_iter, cast<Scalar,double>( m_currResidualNorm ));
768 inline void updatePrevdFAndPrevdG() {
769 m_prevdF.col(m_columnIndex) += m_currentF;
770 m_prevdG.col(m_columnIndex) += m_currentG;
773 Scalar scale = std::max(EPSILON, m_prevdF.col(m_columnIndex).norm());
774 m_scaledF(m_columnIndex) = scale;
775 m_prevdF.col(m_columnIndex) /= scale;
781 m_mk = std::min(m_param.m, m_iter);
785 Scalar dF_squaredNorm = m_prevdF.col(m_columnIndex).squaredNorm();
786 m_normalEquationMatrix(0, 0) = dF_squaredNorm;
787 Scalar dF_norm = math::sqrt(dF_squaredNorm);
790 if (dF_norm > EPSILON) {
792 m_alpha(0) = (m_prevdF.col(m_columnIndex) / dF_norm).dot(
793 m_currentF / dF_norm);
800 * m_prevdF.block(0, 0, m_dim, m_mk)).transpose();
801 m_normalEquationMatrix.block(m_columnIndex, 0, 1, m_mk) =
802 new_inner_prod.transpose();
803 m_normalEquationMatrix.block(0, m_columnIndex, m_mk, 1) =
807 m_linearSolver.compute(m_normalEquationMatrix.block(0, 0, m_mk, m_mk));
808 m_alpha.head(m_mk) = m_linearSolver.solve(
809 m_prevdF.block(0, 0, m_dim, m_mk).transpose() * m_currentF);
816 m_solution = m_currentG - m_prevdG.block(0, 0, m_dim, m_mk) *
817 ((m_alpha.head(m_mk).array()
818 / m_scaledF.head(m_mk).array()).matrix());
821 void updateColumnIndices() {
822 m_columnIndex = (m_columnIndex + 1) % m_param.m;
823 m_prevdF.col(m_columnIndex) = -m_currentF;
824 m_prevdG.col(m_columnIndex) = -m_currentG;
827 inline void trackIterationInfo() {
828 if (m_trackResidualNorm) { m_residualList.push_back(m_currResidualNorm); }
832 gsEigen::FullPivLU<gsMatrix<Scalar>> m_linearSolver;
834 const preAAParam<Scalar> m_param;
839 int m_columnIndex = 0;
841 Scalar m_currResidualNorm;
843 gsVector<Scalar> m_solution;
844 gsVector<Scalar> m_currentF;
845 gsVector<Scalar> m_currentG;
847 gsMatrix<Scalar> m_prevdG;
848 gsMatrix<Scalar> m_prevdF;
849 gsMatrix<Scalar> m_normalEquationMatrix;
850 gsVector<Scalar> m_alpha;
851 gsVector<Scalar> m_scaledF;
853 gsSparseMatrix<Scalar> m_preconditioner;
854 gsEigen::SparseLU<gsSparseMatrix<Scalar>> m_linearSolverPreconditioning;
856 bool m_printInfo =
true;
859 bool m_trackResidualNorm =
false;
860 std::vector<Scalar> m_residualList;
862 const Scalar EPSILON = 1e-14;
869 #ifndef GISMO_BUILD_LIB
870 #include GISMO_HPP_HEADER(gsBarrierCore.hpp)
gsBarrierCore
Definition: gsBarrierCore.h:107
static T computeAreaBoundary(const gsMultiPatch< T > &mp)
Computes the area of a multipatch representing boundary curves.
Definition: gsBarrierCore.hpp:114
int max_iterations
Definition: gsBarrierCore.h:537
void check_param() const
Definition: gsBarrierCore.h:576
Class defining an optimization problem.
Definition: gsOptProblem.h:24
Scalar epsilon
Definition: gsBarrierCore.h:521
#define GISMO_NO_IMPLEMENTATION
Definition: gsDebug.h:129
gsExprHelper< T >::geometryMap geometryMap
Geometry map type.
Definition: gsExprAssembler.h:58
Definition: gsBarrierCore.h:504
void updateAlpha()
update the coefficients by solving a Least-Square problem
Definition: gsBarrierCore.h:779
static gsMultiPatch< T > computePDEPatch(const gsMultiPatch< T > &mp, const gsDofMapper &mapper, const gsOptionList &options)
PDE-based methods, including H2 and H1.
Definition: gsBarrierCore.hpp:1046
bool is_free(index_t i, index_t k=0, index_t c=0) const
Returns true if local dof i of patch k is not eliminated.
Definition: gsDofMapper.h:382
const gsVector< Scalar > & compute(const gsVector< Scalar > &u0, const Residual_t &F, const Jacobian_t &Jacobian)
perform Anderson acceleration iteration
Definition: gsBarrierCore.h:618
Provides declaration of Basis abstract interface.
Anderson acceleration solver and its (preconditioned) variants.
Definition: gsBarrierCore.h:607
#define index_t
Definition: gsConfig.h:32
Generic expressions helper.
void updateGWithPreconditioning(const Residual_t &F, const Jacobian_t &Jacobian)
update fixed point function with preconditioning
Definition: gsBarrierCore.h:744
void updateGWithoutPreconditioning(const Residual_t &F)
update fixed point function without preconditioning
Definition: gsBarrierCore.h:757
void updateG(const Residual_t &F, const Jacobian_t &Jacobian)
update fixed point function
Definition: gsBarrierCore.h:734
Maintains a mapping from patch-local dofs to global dof indices and allows the elimination of individ...
Definition: gsDofMapper.h:68
void verboseLog(const std::string &message, const index_t &verbose)
helper function to set optimizer options
Definition: gsBarrierCore.h:101
static gsMultiPatch< T > computeVHPatch(const gsMultiPatch< T > &mp, const gsDofMapper &mapper, const gsOptionList &options)
variational harmonic method
Scalar epsilon_rel
Definition: gsBarrierCore.h:530
Generic expressions evaluator.
index_t index(index_t i, index_t k=0, index_t c=0) const
Returns the global dof index associated to local dof i of patch k.
Definition: gsDofMapper.h:325
Creates a mapped object or data pointer to a vector without copying data.
Definition: gsLinearAlgebra.h:129
void convertFreeVectorToMultiPatch(const gsVector< T > &gsFreeVec, const gsDofMapper &mapper, gsMultiPatch< T > &mp)
Convert free control points from a vector into a multi-patch.
Definition: gsBarrierCore.h:70
static T computeAreaInterior(const gsMultiPatch< T > &mp)
Computes the area of a multipatch representing a full domain.
Definition: gsBarrierCore.hpp:94
short_t targetDim() const
Dimension of the target space.
Definition: gsMultiPatch.h:192
static gsMultiPatch< T > computeBarrierPatch(const gsMultiPatch< T > &mp, const gsDofMapper &mapper, const gsOptionList &options)
Barrier function-based method.
gsGeometry< T > & patch(size_t i) const
Return the i-th patch.
Definition: gsMultiPatch.h:226
size_t nPatches() const
Number of patches.
Definition: gsMultiPatch.h:208
void updateSolution()
update the solution
Definition: gsBarrierCore.h:814
static gsMultiPatch< T > compute(const gsMultiPatch< T > &mp, const gsDofMapper &mapper, const gsOptionList &options)
construct analysis-suitable parameterization
Definition: gsBarrierCore.hpp:129
#define gsInfo
Definition: gsDebug.h:43
int updatePreconditionerStep
Definition: gsBarrierCore.h:542
Definition: gsDirichletValues.h:23
Container class for a set of geometry patches and their topology, that is, the interface connections ...
Definition: gsMultiPatch.h:33
size_t patchSize(const index_t k, const index_t c=0) const
Returns the total number of patch-local DoFs that live on patch k for component c.
Definition: gsDofMapper.h:480
static gsOptionList defaultOptions()
Default options.
Definition: gsBarrierCore.hpp:24
#define GISMO_ERROR(message)
Definition: gsDebug.h:118
Creates a mapped object or data pointer to a const vector without copying data.
Definition: gsLinearAlgebra.h:130
This is the main header file that collects wrappers of Eigen for linear algebra.
static gsMultiPatch< T > computePenaltyPatch2(const gsMultiPatch< T > &mp, const gsDofMapper &mapper, const gsOptionList &options)
Penalty function-based method (2)
preAAParam()
Definition: gsBarrierCore.h:559
Class which holds a list of parameters/options, and provides easy access to them. ...
Definition: gsOptionList.h:32
index_t freeSize() const
Returns the number of free (not eliminated) dofs.
Definition: gsDofMapper.h:436
gsVector< T > convertMultiPatchToFreeVector(const gsMultiPatch< T > &mp, const gsDofMapper &mapper)
Computes a patch parametrization given a set of boundary geometries. Parametrization is not guarantee...
Definition: gsBarrierCore.h:50
bool usePreconditioning
Definition: gsBarrierCore.h:552
expr::gsFeSolution< T > solution
Solution type.
Definition: gsExprAssembler.h:61
static T computeArea(const gsMultiPatch< T > &mp)
Compute the area of the computational domain.
Definition: gsBarrierCore.hpp:88
Generic expressions matrix assembly.
int m
Definition: gsBarrierCore.h:513
static gsMultiPatch< T > computePenaltyPatch(const gsMultiPatch< T > &mp, const gsDofMapper &mapper, const gsOptionList &options)
Penalty function-based method (1)