From a9dfb6767fe59bec3ffcbff62c2637b2751c2afb Mon Sep 17 00:00:00 2001 From: fromor Date: Fri, 21 Oct 2022 00:22:06 +0200 Subject: [PATCH 01/59] init hyper-reduction class --- src/ITHACA_HR/Make/files | 3 + src/ITHACA_HR/Make/options | 34 +++ src/ITHACA_HR/hyperReduction.C | 531 +++++++++++++++++++++++++++++++++ src/ITHACA_HR/hyperReduction.H | 211 +++++++++++++ 4 files changed, 779 insertions(+) create mode 100644 src/ITHACA_HR/Make/files create mode 100644 src/ITHACA_HR/Make/options create mode 100644 src/ITHACA_HR/hyperReduction.C create mode 100644 src/ITHACA_HR/hyperReduction.H diff --git a/src/ITHACA_HR/Make/files b/src/ITHACA_HR/Make/files new file mode 100644 index 000000000..a30075081 --- /dev/null +++ b/src/ITHACA_HR/Make/files @@ -0,0 +1,3 @@ +hyperReduction.C + +LIB = $(FOAM_USER_LIBBIN)/libITHACA_HR diff --git a/src/ITHACA_HR/Make/options b/src/ITHACA_HR/Make/options new file mode 100644 index 000000000..d51b789a2 --- /dev/null +++ b/src/ITHACA_HR/Make/options @@ -0,0 +1,34 @@ +sinclude $(GENERAL_RULES)/module-path-user + +/* Failsafe - user location */ +ifeq (,$(strip $(FOAM_MODULE_APPBIN))) + FOAM_MODULE_APPBIN = $(FOAM_USER_APPBIN) +endif +ifeq (,$(strip $(FOAM_MODULE_LIBBIN))) + FOAM_MODULE_LIBBIN = $(FOAM_USER_LIBBIN) +endif + +EXE_INC = \ + -I$(LIB_SRC)/finiteVolume/lnInclude \ + -I$(LIB_SRC)/meshTools/lnInclude \ + -I$(LIB_SRC)/dynamicMesh/lnInclude \ + -I$(LIB_SRC)/dynamicFvMesh/lnInclude \ + -I$(LIB_ITHACA_SRC)/ITHACA_DEIM/DEIM \ + -I$(LIB_ITHACA_SRC)/thirdparty/Eigen \ + -I$(LIB_ITHACA_SRC)/thirdparty/spectra/include \ + -I$(LIB_ITHACA_SRC)/ITHACA_CORE/ITHACAutilities \ + -I$(LIB_ITHACA_SRC)/ITHACA_CORE/ITHACAstream \ + -I$(LIB_ITHACA_SRC)/ITHACA_CORE/ITHACAPOD \ + -I$(LIB_ITHACA_SRC)/ITHACA_CORE/Foam2Eigen \ + -I$(LIB_ITHACA_SRC)/ITHACA_CORE/EigenFunctions \ + -I$(LIB_ITHACA_SRC)/ITHACA_CORE/Containers \ + -Wno-comment \ + -DOFVER=$${WM_PROJECT_VERSION%.*} \ + -std=c++17 + +EXE_LIBS = \ + -lITHACA-CORE \ + -lmeshTools \ + -lsampling \ + -lfileFormats \ + -lfiniteVolume diff --git a/src/ITHACA_HR/hyperReduction.C b/src/ITHACA_HR/hyperReduction.C new file mode 100644 index 000000000..0805c9a5d --- /dev/null +++ b/src/ITHACA_HR/hyperReduction.C @@ -0,0 +1,531 @@ +/*---------------------------------------------------------------------------*\ + ██╗████████╗██╗ ██╗ █████╗ ██████╗ █████╗ ███████╗██╗ ██╗ + ██║╚══██╔══╝██║ ██║██╔══██╗██╔════╝██╔══██╗ ██╔════╝██║ ██║ + ██║ ██║ ███████║███████║██║ ███████║█████╗█████╗ ██║ ██║ + ██║ ██║ ██╔══██║██╔══██║██║ ██╔══██║╚════╝██╔══╝ ╚██╗ ██╔╝ + ██║ ██║ ██║ ██║██║ ██║╚██████╗██║ ██║ ██║ ╚████╔╝ + ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝ ╚═╝ ╚═══╝ + + * In real Time Highly Advanced Computational Applications for Finite Volumes + * Copyright (C) 2017 by the ITHACA-FV authors +------------------------------------------------------------------------------- + +License + This file is part of ITHACA-FV + + ITHACA-FV is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + ITHACA-FV is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with ITHACA-FV. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "hyperReduction.H" + +// Using the Eigen library, using the SVD decomposition method to solve the +// matrix pseudo-inverse, the default error er is 0 +Eigen::MatrixXd pinv_eigen_based(Eigen::MatrixXd &origin, const float er = 0) { + // perform svd decomposition + Eigen::JacobiSVD svd_holder(origin, Eigen::ComputeThinU | + Eigen::ComputeThinV); + // Build SVD decomposition results + Eigen::MatrixXd U = svd_holder.matrixU(); + Eigen::MatrixXd V = svd_holder.matrixV(); + Eigen::MatrixXd D = svd_holder.singularValues(); + + // Build the S matrix + Eigen::MatrixXd S(V.cols(), U.cols()); + S.setZero(); + + for (unsigned int i = 0; i < D.size(); ++i) { + + if (D(i, 0) > er) { + S(i, i) = 1 / D(i, 0); + } else { + S(i, i) = 0; + } + } + + std::cout << "Condition number of inverse: " << D(D.size()-1, 0)/D(0, 0) << std::endl; + // pinv_matrix = V * S * U^T + return V * S * U.transpose(); +} + +// Template function constructor +template +HyperReduction::HyperReduction(HyperReductionMethod hrMethod, + label maxModes, + label maxNodes, + Eigen::VectorXi initialSeeds, + word functionName, + SnapshotsLists &&...snapshotsLists) + : methodName{methodNames[int(hrMethod)]}, + vectorial_dim{compute_vectorial_dim(snapshotsLists...)}, + maxModes{maxModes}, + maxNodes{maxNodes}, + initialSeeds{initialSeeds}, + functionName{functionName}, + snapshotsTuple{std::forward_as_tuple(snapshotsLists...)} +{ + // check snapshotsLists is not empty + // check that the first snapshotsList is not empty (includes above) + + // TODO initialize vectorial_dim only from SnapshotsLists type + // vectorial_dim = std::apply(compute_vectorial_dim, snapshotsTuple); + + ITHACAparameters *para(ITHACAparameters::getInstance()); + Folder = "ITHACAoutput/" + methodName + "/" + functionName; + + nodePoints = autoPtr>(new IOList