3#include <boost/range/adaptor/map.hpp>
27namespace acceleration {
34 double initialRelaxation,
35 bool forceInitialRelaxation,
36 int maxIterationsUsed,
37 int timeWindowsReused,
39 double singularityLimit,
42 : _preconditioner(
std::move(preconditioner)),
43 _initialRelaxation(initialRelaxation),
44 _maxIterationsUsed(maxIterationsUsed),
45 _timeWindowsReused(timeWindowsReused),
46 _dataIDs(
std::move(dataIDs)),
47 _forceInitialRelaxation(forceInitialRelaxation),
50 _singularityLimit(singularityLimit),
51 _infostringstream(
std::ostringstream::ate)
54 "Initial relaxation factor for QN acceleration has to "
55 "be larger than zero and smaller or equal than one. "
56 "Current initial relaxation is {}",
59 "Maximum number of iterations used in the quasi-Newton acceleration "
60 "scheme has to be larger than zero. "
61 "Current maximum reused iterations is {}",
64 "Number of previous time windows to be reused for "
65 "quasi-Newton acceleration has to be larger than or equal to zero. "
66 "Current number of time windows reused is {}",
81 for (
const DataMap::value_type &pair : cplData) {
82 PRECICE_ASSERT(pair.second->getSize() == pair.second->getPreviousIterationSize(),
"current and previousIteration have to be initialized and of identical size.",
83 pair.second->getSize(), pair.second->getPreviousIterationSize());
88 "Gradient data, which is required by at least one of the configured data mappings, is not yet compatible with quasi-Newton acceleration. This combination might lead to numerical issues. "
89 "Consider switching to a different acceleration scheme or a different data mapping scheme.");
93 for (
const auto &data : cplData | boost::adaptors::map_values) {
95 "Quasi-Newton acceleration does not yet support using data from all substeps. Please set substeps=\"false\" in the exchange tag of data \"{}\".", data->getDataName());
102 entries += cplData.
at(elem)->getSize();
103 subVectorSizes.
push_back(cplData.
at(elem)->getSize());
115 _values = Eigen::VectorXd::Zero(entries);
139 int accumulatedNumberOfUnknowns = 0;
141 const auto &offsets = cplData.
at(elem)->getVertexOffsets();
142 accumulatedNumberOfUnknowns += offsets[i] * cplData.
at(elem)->getDimensions();
162 for (
const DataMap::value_type &pair : cplData) {
165 int secondaryEntries = pair.second->getSize();
190 "The coupling residual equals almost zero. There is maybe something wrong in your adapter. "
191 "Maybe you always write the same data or you call advance without "
192 "providing new data first or you do not use available read data. "
193 "Or you just converge much further than actually necessary.");
208 "The number of columns in the least squares system exceeded half the number of unknowns at the interface. "
209 "The system will probably become bad or ill-conditioned and the quasi-Newton acceleration may not "
210 "converge. Maybe the number of allowed columns (\"max-used-iterations\") should be limited.");
215 Eigen::VectorXd deltaXTilde =
_values;
225 "Adding a vector with a two-norm of {} to the quasi-Newton V matrix, which will lead to "
226 "ill-conditioning. A filter might delete the column again. Still, this could mean that you are "
227 "converging too tightly, that you reached steady-state, or that you are giving by mistake identical "
228 "data to preCICE in two consecutive iterations.",
233 if (not columnLimitReached && overdetermined) {
317 PRECICE_DEBUG(
" Last time window converged after one iteration. Need to restore the matrices from backup.");
358 applyingFilter.
stop();
368 Eigen::VectorXd xUpdate = Eigen::VectorXd::Zero(
_residuals.size());
403 "The quasi-Newton update contains NaN values. This means that the quasi-Newton acceleration failed to converge. "
404 "When writing your own adapter this could indicate that you give wrong information to preCICE, such as identical "
405 "data in succeeding iterations. Or you do not properly save and reload checkpoints. "
406 "If you give the correct data this could also mean that the coupled problem is too hard to solve. Try to use a QR "
407 "filter or increase its threshold (larger epsilon).");
428 for (
int i = delIndices.
size() - 1; i >= 0; i--) {
432 PRECICE_DEBUG(
" Filter: removing column with index {} in iteration {} of time window: {}", delIndices[i],
its,
tWindows);
445 int size = cplData.
at(
id)->getSize();
446 auto &valuesPart = cplData.
at(
id)->values();
447 for (
int i = 0; i < size; i++) {
448 valuesPart(i) =
_values(i + offset);
486 stream <<
"Matrix column counters: ";
488 stream << cols <<
", ";
533 for (
int i = 0; i < toRemove; i++) {
568 if (cols > columnIndex) {
#define PRECICE_WARN_IF(condition,...)
#define PRECICE_DEBUG(...)
#define PRECICE_TRACE(...)
#define PRECICE_CHECK(check,...)
#define PRECICE_ASSERT(...)
void checkDataIDs(const DataMap &cplData) const
Checks if all dataIDs are contained in cplData.
static const int QR2FILTER
void concatenateCouplingData(const DataMap &cplData, const std::vector< DataID > &dataIDs, Eigen::VectorXd &targetValues, Eigen::VectorXd &targetOldValues) const
Concatenates all coupling data involved into a single vector.
static const int NOFILTER
virtual void updateDifferenceMatrices(const DataMap &cplData)
Updates the V, W matrices (as well as the matrices for the secondary data)
Eigen::VectorXd _oldResiduals
Difference between solver input and output from last time window.
std::deque< int > _matrixColsBackup
int _filter
filter method that is used to maintain good conditioning of the least-squares system Either of two ty...
Eigen::MatrixXd _matrixWBackup
double _initialRelaxation
Constant relaxation factor used for first iteration.
int _timeWindowsReused
Maximum number of old time windows (with data values) kept.
virtual int getLSSystemCols() const
: computes number of cols in least squares system, i.e, number of cols in _matrixV,...
int _nbDelCols
Number of filtered out columns in this time window.
virtual void specializedIterationsConverged(const DataMap &cplData)=0
Marks a iteration sequence as converged.
virtual void applyFilter()
Applies the filter method for the least-squares system, defined in the configuration.
virtual void iterationsConverged(const DataMap &cplData)
Marks a iteration sequence as converged.
Eigen::VectorXd _oldValues
Concatenation of all (old) coupling data involved in the QN system.
Eigen::MatrixXd _matrixVBackup
backup of the V,W and matrixCols data structures. Needed for the skipping of initial relaxation,...
Eigen::VectorXd _oldXTilde
Solver output from last iteration.
std::ostringstream _infostringstream
write some debug/acceleration info to file
bool _resetLS
If true, the LS system has been modified (reset or recomputed) in such a way, that mere updating of m...
bool _hasNodesOnInterface
BaseQNAcceleration(double initialRelaxation, bool forceInitialRelaxation, int maxIterationsUsed, int timeWindowsReused, int filter, double singularityLimit, std::vector< int > dataIDs, impl::PtrPreconditioner preconditioner)
int _nbDropCols
Number of dropped columns in this time window (old time window out of scope)
virtual int getDroppedColumns() const
how many QN columns were dropped (went out of scope) in this time window
virtual void computeQNUpdate(const DataMap &cplData, Eigen::VectorXd &xUpdate)=0
Computes the quasi-Newton update using the specified pp scheme (IQNIMVJ, IQNILS)
Eigen::MatrixXd _matrixV
Stores residual deltas.
std::vector< int > _dimOffsets
Stores the local dimensions, i.e., the offsets in _invJacobian for all processors.
virtual void exportState(io::TXTWriter &writer)
Exports the current state of the acceleration to a file.
Eigen::VectorXd _residuals
Current iteration residuals of IQN data. Temporary.
virtual void importState(io::TXTReader &reader)
Imports the last exported state of the acceleration from file.
virtual void removeMatrixColumn(int columnIndex)
Removes one iteration from V,W matrices and adapts _matrixCols.
Eigen::MatrixXd _matrixW
Stores x tilde deltas, where x tilde are values computed by solvers.
Eigen::VectorXd _values
Concatenation of all coupling data involved in the QN system.
virtual void splitCouplingData(const DataMap &cplData)
Splits up QN system vector back into the coupling data.
virtual void performAcceleration(DataMap &cplData)
Performs one acceleration step.
double _singularityLimit
Determines sensitivity when two matrix columns are considered equal.
void writeInfo(const std::string &s, bool allProcs=false)
Wwrites info to the _infostream (also in parallel)
std::deque< int > _matrixCols
Indices (of columns in W, V matrices) of 1st iterations of time windows.
bool _firstIteration
Indicates the first iteration, where constant relaxation is used.
virtual int getDeletedColumns() const
how many QN columns were deleted in this time window
std::map< int, Eigen::VectorXd > _secondaryResiduals
Current iteration residuals of secondary data.
impl::PtrPreconditioner _preconditioner
Preconditioner for least-squares system if vectorial system is used.
impl::QRFactorization _qrV
Stores the current QR decomposition ov _matrixV, can be updated via deletion/insertion of columns.
int _maxIterationsUsed
Maximum number of old data iterations kept.
virtual void computeUnderrelaxationSecondaryData(const DataMap &cplData)=0
Computes underrelaxation for the secondary data.
std::vector< int > _secondaryDataIDs
Data IDs of data not involved in IQN coefficient computation.
std::vector< int > _dataIDs
Data IDs of data to be involved in the IQN algorithm.
bool _forceInitialRelaxation
virtual void initialize(const DataMap &cplData)
Initializes the acceleration.
void setGlobalRows(int gr)
void applyFilter(double singularityLimit, std::vector< int > &delIndices, Eigen::MatrixXd &V)
filters the least squares system, i.e., the decomposition Q*R = V according to the defined filter tec...
void reset()
resets the QR factorization zo zero Q(0:0, 0:0)R(0:0, 0:0)
void popBack()
deletes the column at position _cols-1, i.e., deletes the last column and updates the QR factorizatio...
void pushFront(const Eigen::VectorXd &v)
inserts a new column at position 0, i.e., shifts right and inserts at first position and updates the ...
File reader for matrix/vector in Matlab V7 ASCII format.
File writer for matrix in Matlab V7 ASCII format.
void stop()
Stops a running event.
static int getSize()
Number of ranks. This includes ranks from both participants, e.g. minimal size is 2.
static double l2norm(const Eigen::VectorXd &vec)
The l2 norm of a vector is calculated on distributed data.
static Rank getRank()
Current rank.
static bool isPrimary()
True if this process is running the primary rank.
static bool isParallel()
True if this process is running in parallel.
static com::PtrCommunication & getCommunication()
Intra-participant communication.
constexpr bool equals(const Eigen::MatrixBase< DerivedA > &A, const Eigen::MatrixBase< DerivedB > &B, double tolerance=NUMERICAL_ZERO_DIFFERENCE)
Compares two Eigen::MatrixBase for equality up to tolerance.
static constexpr SynchronizeTag Synchronize
Convenience instance of the SynchronizeTag.
void removeColumnFromMatrix(Eigen::MatrixXd &A, int col)
bool contained(const ELEMENT_T &element, const std::vector< ELEMENT_T > &vec)
Returns true, if given element is in vector, otherwise false.
void appendFront(Eigen::MatrixXd &A, Eigen::VectorXd &v)
void shiftSetFirst(Eigen::MatrixXd &A, const Eigen::VectorXd &v)
Main namespace of the precice library.