preCICE
Loading...
Searching...
No Matches
BaseQNAcceleration.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <Eigen/Core>
4#include <algorithm>
5#include <deque>
6#include <fstream>
7#include <map>
8#include <sstream>
9#include <string>
10#include <vector>
14#include "logging/Logger.hpp"
15#include "time/TimeGrids.hpp"
16
17/* ****************************************************************************
18 *
19 * A few comments concerning the present design choice.
20 *
21 * All the functions from the base class BAseQNAcceleration are specialized in
22 * the sub classes as needed. This is done vi overwriting the base functions in
23 * the specialized sub classes and calling the respective base function after
24 * performing the specialized stuff (in order to perform the common, generalized
25 * computations i.e. handling of V,W matrices etc.)
26 * However, for the performAcceleration Method we decided (for better readability)
27 * to have this method only in the base class, while introducing a function
28 * performPPSecondaryData that handles all the specialized stuff concerning acceleration
29 * processing for the secondary data in the sub classes.
30 *
31 * Another possibility would have been to introduce a bunch of functions like
32 * initializeSpecialized(), removeMatrixColumnSpecialized(),
33 * iterationsConvergedSpecialized(), etc
34 * and call those function from the base class top down to the sub classes.
35 *
36 * The third possibility was to separate the approximation of the Jacobian from
37 * the common stuff like handling V,W matrices in the acceleration.
38 * Here, we have a class QNAcceleration that handles the V,W stuff and the basic
39 * scheme of the QN update. Furthermore we have a base class (or rather interface)
40 * JacobianApproximation with sub classes IQNIMVJAPX and IQNAPX that handle all the
41 * specialized stuff like Jacobian approximation, handling of secondary data etc.
42 * However, this approach is not feasible, as we have to call the function
43 * removeMatrixColumn() down in the specialized sub classes IQNIMVJApx and IQNApx.
44 * This is not possible as the function works on the V, W matrices that are
45 * completely treated by QNAcceleration.
46 *
47 * ****************************************************************************
48 */
49
50// ----------------------------------------------------------- CLASS DEFINITION
51
52namespace precice {
53namespace io {
54class TXTReader;
55class TXTWriter;
56} // namespace io
57
58namespace acceleration {
59
65public:
67 double initialRelaxation,
68 bool forceInitialRelaxation,
69 int maxIterationsUsed,
70 int timeWindowsReused,
71 int filter,
72 double singularityLimit,
73 std::vector<int> dataIDs,
74 OnBoundViolation onBoundViolation,
75 impl::PtrPreconditioner preconditioner,
76 bool reducedTimeGrid);
77
82 {
83 // not necessary for user, only for developer, if needed, this should be configurable
84 // if (utils::IntraComm::isPrimary() || !utils::IntraComm::isParallel()) {
85 // _infostream.open("precice-accelerationInfo.log", std::ios_base::out);
86 // _infostream << std::setprecision(16);
87 // _infostream << _infostringstream.str();
88 // }
89 }
90
94 std::vector<int> getPrimaryDataIDs() const final override
95 {
96 return _primaryDataIDs;
97 }
98
102 void initialize(const DataMap &cplData) final override;
103
109 void performAcceleration(DataMap &cplData, double windowStart, double windowEnd) final override;
113 std::vector<DataID> checkBoundViolation(Eigen::VectorXd &data, DataMap &cplData) const;
117 void onBoundViolationsClamp(Eigen::VectorXd &data, DataMap &cplData, const std::vector<DataID> &violatingIDs);
121 void onBoundViolationsScale(Eigen::VectorXd &data, DataMap &cplData, const std::vector<DataID> &violatingIDs, Eigen::VectorXd &xUpdate);
125 void clampToBounds(Eigen::Map<Eigen::MatrixXd> &data, const std::vector<std::optional<double>> &lowerBound, const std::vector<std::optional<double>> &upperBound);
129 double computeShorteningFactor(Eigen::Map<Eigen::MatrixXd> &data, Eigen::Map<Eigen::MatrixXd> &update, const std::vector<std::optional<double>> &lowerBound, const std::vector<std::optional<double>> &upperBound);
136 void iterationsConverged(const DataMap &cplData, double windowStart) final override;
137
141 void exportState(io::TXTWriter &writer) final override;
142
148 void importState(io::TXTReader &reader) final override;
149
151 int getDeletedColumns() const;
152
154 int getDroppedColumns() const;
155
163 int getLSSystemCols() const;
164
168 int getMaxUsedIterations() const;
169
173 int getMaxUsedTimeWindows() const;
174
176 void addLogEntries(io::TXTTableWriter &writer) const override;
177
179 void writeLogEntries(io::TXTTableWriter &writer) const override;
180
181protected:
182 logging::Logger _log{"acceleration::BaseQNAcceleration"};
183
186
188 const double _initialRelaxation;
189
192
195
197 const std::vector<int> _primaryDataIDs;
198
200 std::vector<int> _dataIDs;
201
203 std::vector<DataID> _idsWithBounds;
204
206 bool _firstIteration = true;
207
208 /* @brief Indicates the first time window, where constant relaxation is used
209 * later, we replace the constant relaxation by a qN-update from last time window.
210 */
211 bool _firstTimeWindow = true;
212
213 /*
214 * @brief True if this process has nodes at the coupling interface
215 */
217
219
220 /* @brief If true, the QN-scheme always performs a underrelaxation in the first iteration of
221 * a new time window. Otherwise, the LS system from the previous time window is used in the
222 * first iteration.
223 */
225
229 bool _resetLS = false;
230
232 Eigen::VectorXd _oldPrimaryXTilde;
233
235 Eigen::VectorXd _oldXTilde;
236
238 Eigen::VectorXd _primaryResiduals;
239
241 Eigen::VectorXd _residuals; // @todo is this member still needed? Potential refactoring.
242
244 Eigen::MatrixXd _matrixV;
245
247 Eigen::MatrixXd _matrixW;
248
251
254
258 const int _filter;
259
266 const double _singularityLimit;
267
275 std::deque<int> _matrixCols;
276
280 std::vector<int> _dimOffsets;
281
284 std::vector<int> _dimOffsetsPrimary;
285
287 std::ostringstream _infostringstream;
288 std::fstream _infostream;
289
290 int getLSSystemRows() const;
291 int getPrimaryLSSystemRows() const;
292
299 virtual void specializedIterationsConverged(const DataMap &cplData) = 0;
300
302 virtual void updateDifferenceMatrices(const DataMap &cplData);
303
305 virtual void updateCouplingData(const DataMap &cplData, double windowStart);
306
308 virtual void applyFilter();
309
311 virtual void computeQNUpdate(Eigen::VectorXd &xUpdate) = 0;
312
314 virtual void removeMatrixColumn(int columnIndex);
315
317 void writeInfo(const std::string &s, bool allProcs = false);
318
319 int its = 0, tWindows = 0;
320
321private:
324 void initializeVectorsAndPreconditioner(const DataMap &cplData, double windowStart);
325
332
334 void concatenateCouplingData(Eigen::VectorXd &data, Eigen::VectorXd &oldData, const DataMap &cplData, std::vector<int> dataIDs, precice::time::TimeGrids timeGrids, double windowStart) const;
335
337 std::optional<time::TimeGrids> _timeGrids;
338
341 std::optional<time::TimeGrids> _primaryTimeGrids;
342
344 Eigen::VectorXd _primaryValues;
345
347 Eigen::VectorXd _oldPrimaryValues;
348
350 Eigen::VectorXd _oldPrimaryResiduals;
351
353 Eigen::VectorXd _values;
354
356 Eigen::VectorXd _oldValues;
357
362 Eigen::MatrixXd _matrixVBackup;
363 Eigen::MatrixXd _matrixWBackup;
364 std::deque<int> _matrixColsBackup;
365
367 int _nbDelCols = 0;
368
370 int _nbDropCols = 0;
371};
372} // namespace acceleration
373} // namespace precice
std::map< int, PtrCouplingData > DataMap
OnBoundViolation
Options for handling bound violations.
std::map< int, cplscheme::PtrCouplingData > DataMap
Map from data ID to data values.
virtual void updateDifferenceMatrices(const DataMap &cplData)
Updates the V, W matrices (as well as the matrices for the secondary data)
double computeShorteningFactor(Eigen::Map< Eigen::MatrixXd > &data, Eigen::Map< Eigen::MatrixXd > &update, const std::vector< std::optional< double > > &lowerBound, const std::vector< std::optional< double > > &upperBound)
calculate the shortening factor to fit the violating data to the bounds
void onBoundViolationsScale(Eigen::VectorXd &data, DataMap &cplData, const std::vector< DataID > &violatingIDs, Eigen::VectorXd &xUpdate)
Handles bound violations after QN update by scaling the QN step.
const double _initialRelaxation
Constant relaxation factor used for first iteration.
virtual void computeQNUpdate(Eigen::VectorXd &xUpdate)=0
Computes the quasi-Newton update using the specified pp scheme (IQNIMVJ, IQNILS)
std::vector< int > _dimOffsetsPrimary
Stores the local dimensions regarding primary data,.
Eigen::VectorXd _oldPrimaryValues
Concatenation of all old primary data involved in the QN system.
std::vector< int > getPrimaryDataIDs() const final override
Returns all IQN involved data IDs.
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.
void concatenateCouplingData(Eigen::VectorXd &data, Eigen::VectorXd &oldData, const DataMap &cplData, std::vector< int > dataIDs, precice::time::TimeGrids timeGrids, double windowStart) const
Samples and concatenates the data and old data in cplData into a long vector.
void onBoundViolationsClamp(Eigen::VectorXd &data, DataMap &cplData, const std::vector< DataID > &violatingIDs)
Handles bound violations after QN update by clamping.
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.
Eigen::VectorXd _oldValues
Concatenation of all old primary and secondary data involved in the QN system.
std::vector< DataID > _idsWithBounds
Data IDs of all coupling data that have bounds defined.
int getMaxUsedTimeWindows() const
Get the maximum number of reused time windows.
~BaseQNAcceleration() override
Destructor, empty.
int getMaxUsedIterations() const
Get the maximum number of reused iterations.
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.
Eigen::VectorXd _primaryResiduals
Current iteration residuals of primary IQN data. Temporary.
std::ostringstream _infostringstream
write some debug/acceleration info to file
virtual void updateCouplingData(const DataMap &cplData, double windowStart)
Splits up QN system vector back into the waveforms in coupling data.
bool _resetLS
If true, the LS system has been modified (reset or recomputed) in such a way, that mere updating of m...
void importState(io::TXTReader &reader) final override
Imports the last exported state of the acceleration from file.
Eigen::VectorXd _primaryValues
Concatenation of all primary data involved in the QN system.
int _nbDropCols
Number of dropped columns in this time window (old time window out of scope)
int getDroppedColumns() const
how many QN columns were dropped (went out of scope) in this time window
std::vector< DataID > checkBoundViolation(Eigen::VectorXd &data, DataMap &cplData) const
Check bound violations caused by performing QN update.
const int _filter
filter method that is used to maintain good conditioning of the least-squares system Either of two ty...
Eigen::MatrixXd _matrixV
Stores residual deltas.
std::vector< int > _dimOffsets
Stores the local dimensions, i.e., the offsets in _invJacobian for all processors.
const double _singularityLimit
Determines sensitivity when two matrix columns are considered equal.
const int _maxIterationsUsed
Maximum number of old data iterations kept.
void writeLogEntries(io::TXTTableWriter &writer) const override
Writes QN-specific values to the iteration log columns.
void exportState(io::TXTWriter &writer) final override
Exports the current state of the acceleration to a file.
Eigen::VectorXd _residuals
Current iteration residuals of IQN data. Temporary.
std::optional< time::TimeGrids > _primaryTimeGrids
Stores the time grids to which the primary data involved in the QN system will be interpolated to....
virtual void specializedInitializeVectorsAndPreconditioner(const DataMap &cplData)=0
handles the initialization of matrices and vectors in the sub-classes
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 primary and secondary data involved in the QN system.
std::optional< time::TimeGrids > _timeGrids
Stores the time grids to which the primary and secondary data involved in the QN system will be inter...
void initialize(const DataMap &cplData) final override
Initializes the acceleration.
void addLogEntries(io::TXTTableWriter &writer) const override
Adds QN-specific columns to the iteration log file.
const int _timeWindowsReused
Maximum number of old time windows (with data values) kept.
const std::vector< int > _primaryDataIDs
Data IDs of primary data to be involved in the IQN coefficient computation.
void writeInfo(const std::string &s, bool allProcs=false)
Writes 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.
int getDeletedColumns() const
how many QN columns were deleted in this time window
const bool _reducedTimeGrid
if _reducedTimeGrid = false uses the full QN-WI and if _reducedTimeGrid = true uses rQN-WI form the p...
impl::PtrPreconditioner _preconditioner
Preconditioner for least-squares system if vectorial system is used.
void clampToBounds(Eigen::Map< Eigen::MatrixXd > &data, const std::vector< std::optional< double > > &lowerBound, const std::vector< std::optional< double > > &upperBound)
Clamp data to their bounds.
impl::QRFactorization _qrV
Stores the current QR decomposition ov _matrixV, can be updated via deletion/insertion of columns.
std::vector< int > _dataIDs
Data IDs of all coupling data.
void iterationsConverged(const DataMap &cplData, double windowStart) final override
Marks a iteration sequence as converged.
void performAcceleration(DataMap &cplData, double windowStart, double windowEnd) final override
Performs one acceleration step.
void initializeVectorsAndPreconditioner(const DataMap &cplData, double windowStart)
Initializes the vectors, matrices and preconditioner This has to be done after the first iteration of...
Eigen::VectorXd _oldPrimaryResiduals
Difference between solver input and output from last time window regarding primary data.
BaseQNAcceleration(double initialRelaxation, bool forceInitialRelaxation, int maxIterationsUsed, int timeWindowsReused, int filter, double singularityLimit, std::vector< int > dataIDs, OnBoundViolation onBoundViolation, impl::PtrPreconditioner preconditioner, bool reducedTimeGrid)
Eigen::VectorXd _oldPrimaryXTilde
Solver output regarding primary data from last iteration.
Class that provides functionality for a dynamic QR-decomposition, that can be updated in O(mn) flops ...
File reader for matrix/vector in Matlab V7 ASCII format.
Definition TXTReader.hpp:15
File writer for table-data in text-format.
File writer for matrix in Matlab V7 ASCII format.
Definition TXTWriter.hpp:13
This class provides a lightweight logger.
Definition Logger.hpp:17
Interface for storing the time grids in the Quasi-Newton and Aitken methods. A time grid is a ordered...
Definition TimeGrids.hpp:21
std::shared_ptr< Preconditioner > PtrPreconditioner
contains implementations of acceleration schemes.
provides Import and Export of the coupling mesh and data.
Main namespace of the precice library.