/usr/include/deal.II/lac/eigen.h is in libdeal.ii-dev 8.1.0-6ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | // ---------------------------------------------------------------------
// $Id: eigen.h 31349 2013-10-20 19:07:06Z maier $
//
// Copyright (C) 2000 - 2013 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, 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 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE at
// the top level of the deal.II distribution.
//
// ---------------------------------------------------------------------
#ifndef __deal2__eigen_h
#define __deal2__eigen_h
#include <deal.II/base/config.h>
#include <deal.II/lac/shifted_matrix.h>
#include <deal.II/lac/solver.h>
#include <deal.II/lac/solver_control.h>
#include <deal.II/lac/solver_cg.h>
#include <deal.II/lac/solver_gmres.h>
#include <deal.II/lac/solver_minres.h>
#include <deal.II/lac/vector_memory.h>
#include <deal.II/lac/precondition.h>
#include <cmath>
DEAL_II_NAMESPACE_OPEN
/*!@addtogroup Solvers */
/*@{*/
/**
* Power method (von Mises) for eigenvalue computations.
*
* This method determines the largest eigenvalue of a matrix by
* applying increasing powers of this matrix to a vector. If there is
* an eigenvalue $l$ with dominant absolute value, the iteration vectors
* will become aligned to its eigenspace and $Ax = lx$.
*
* A shift parameter allows to shift the spectrum, so it is possible
* to compute the smallest eigenvalue, too.
*
* Convergence of this method is known to be slow.
*
* @author Guido Kanschat, 2000
*/
template <class VECTOR = Vector<double> >
class EigenPower : private Solver<VECTOR>
{
public:
/**
* Declare type of container size.
*/
typedef types::global_dof_index size_type;
/**
* Standardized data struct to
* pipe additional data to the
* solver.
*/
struct AdditionalData
{
/**
* Shift parameter. This
* parameter allows to shift
* the spectrum to compute a
* different eigenvalue.
*/
double shift;
/**
* Constructor. Set the shift parameter.
*/
AdditionalData (const double shift = 0.)
:
shift(shift)
{}
};
/**
* Constructor.
*/
EigenPower (SolverControl &cn,
VectorMemory<VECTOR> &mem,
const AdditionalData &data=AdditionalData());
/**
* Virtual destructor.
*/
virtual ~EigenPower ();
/**
* Power method. @p x is the
* (not necessarily normalized,
* but nonzero) start vector for
* the power method. After the
* iteration, @p value is the
* approximated eigenvalue and
* @p x is the corresponding
* eigenvector, normalized with
* respect to the l2-norm.
*/
template <class MATRIX>
void
solve (double &value,
const MATRIX &A,
VECTOR &x);
protected:
/**
* Shift parameter.
*/
AdditionalData additional_data;
};
/**
* Inverse iteration (Wieland) for eigenvalue computations.
*
* This class implements an adaptive version of the inverse iteration by Wieland.
*
* There are two choices for the stopping criterion: by default, the
* norm of the residual $A x - l x$ is computed. Since this might not
* converge to zero for non-symmetric matrices with non-trivial Jordan
* blocks, it can be replaced by checking the difference of successive
* eigenvalues. Use AdditionalData::use_residual for switching
* this option.
*
* Usually, the initial guess entering this method is updated after
* each step, replacing it with the new approximation of the
* eigenvalue. Using a parameter AdditionalData::relaxation
* between 0 and 1, this update can be damped. With relaxation
* parameter 0, no update is performed. This damping allows for slower
* adaption of the shift value to make sure that the method converges
* to the eigenvalue closest to the initial guess. This can be aided
* by the parameter AdditionalData::start_adaption, which
* indicates the first iteration step in which the shift value should
* be adapted.
*
* @author Guido Kanschat, 2000, 2003
*/
template <class VECTOR = Vector<double> >
class EigenInverse : private Solver<VECTOR>
{
public:
/**
* Declare type of container size.
*/
typedef types::global_dof_index size_type;
/**
* Standardized data struct to
* pipe additional data to the
* solver.
*/
struct AdditionalData
{
/**
* Damping of the updated shift value.
*/
double relaxation;
/**
* Start step of adaptive
* shift parameter.
*/
unsigned int start_adaption;
/**
* Flag for the stopping criterion.
*/
bool use_residual;
/**
* Constructor.
*/
AdditionalData (double relaxation = 1.,
unsigned int start_adaption = 6,
bool use_residual = true)
:
relaxation(relaxation),
start_adaption(start_adaption),
use_residual(use_residual)
{}
};
/**
* Constructor.
*/
EigenInverse (SolverControl &cn,
VectorMemory<VECTOR> &mem,
const AdditionalData &data=AdditionalData());
/**
* Virtual destructor.
*/
virtual ~EigenInverse ();
/**
* Inverse method. @p value is
* the start guess for the
* eigenvalue and @p x is the
* (not necessarily normalized,
* but nonzero) start vector for
* the power method. After the
* iteration, @p value is the
* approximated eigenvalue and
* @p x is the corresponding
* eigenvector, normalized with
* respect to the l2-norm.
*/
template <class MATRIX>
void
solve (double &value,
const MATRIX &A,
VECTOR &x);
protected:
/**
* Flags for execution.
*/
AdditionalData additional_data;
};
/*@}*/
//---------------------------------------------------------------------------
template <class VECTOR>
EigenPower<VECTOR>::EigenPower (SolverControl &cn,
VectorMemory<VECTOR> &mem,
const AdditionalData &data)
:
Solver<VECTOR>(cn, mem),
additional_data(data)
{}
template <class VECTOR>
EigenPower<VECTOR>::~EigenPower ()
{}
template <class VECTOR>
template <class MATRIX>
void
EigenPower<VECTOR>::solve (double &value,
const MATRIX &A,
VECTOR &x)
{
SolverControl::State conv=SolverControl::iterate;
deallog.push("Power method");
VECTOR *Vy = this->memory.alloc ();
VECTOR &y = *Vy;
y.reinit (x);
VECTOR *Vr = this->memory.alloc ();
VECTOR &r = *Vr;
r.reinit (x);
double length = x.l2_norm ();
double old_length = 0.;
x.scale(1./length);
A.vmult (y,x);
// Main loop
for (int iter=0; conv==SolverControl::iterate; iter++)
{
y.add(additional_data.shift, x);
// Compute absolute value of eigenvalue
old_length = length;
length = y.l2_norm ();
// do a little trick to compute the sign
// with not too much effect of round-off errors.
double entry = 0.;
size_type i = 0;
double thresh = length/x.size();
do
{
Assert (i<x.size(), ExcInternalError());
entry = y (i++);
}
while (std::fabs(entry) < thresh);
--i;
// Compute unshifted eigenvalue
value = (entry * x (i) < 0.) ? -length : length;
value -= additional_data.shift;
// Update normalized eigenvector
x.equ (1/length, y);
// Compute residual
A.vmult (y,x);
// Check the change of the eigenvalue
// Brrr, this is not really a good criterion
conv = this->control().check (iter, std::fabs(1./length-1./old_length));
}
this->memory.free(Vy);
this->memory.free(Vr);
deallog.pop();
// in case of failure: throw exception
if (this->control().last_check() != SolverControl::success)
AssertThrow(false, SolverControl::NoConvergence (this->control().last_step(),
this->control().last_value()));
// otherwise exit as normal
}
//---------------------------------------------------------------------------
template <class VECTOR>
EigenInverse<VECTOR>::EigenInverse (SolverControl &cn,
VectorMemory<VECTOR> &mem,
const AdditionalData &data)
:
Solver<VECTOR>(cn, mem),
additional_data(data)
{}
template <class VECTOR>
EigenInverse<VECTOR>::~EigenInverse ()
{}
template <class VECTOR>
template <class MATRIX>
void
EigenInverse<VECTOR>::solve (double &value,
const MATRIX &A,
VECTOR &x)
{
deallog.push("Wielandt");
SolverControl::State conv=SolverControl::iterate;
// Prepare matrix for solver
ShiftedMatrix <MATRIX> A_s(A, -value);
// Define solver
ReductionControl inner_control (5000, 1.e-16, 1.e-5, false, false);
PreconditionIdentity prec;
SolverGMRES<VECTOR>
solver(inner_control, this->memory);
// Next step for recomputing the shift
unsigned int goal = additional_data.start_adaption;
// Auxiliary vector
VECTOR *Vy = this->memory.alloc ();
VECTOR &y = *Vy;
y.reinit (x);
VECTOR *Vr = this->memory.alloc ();
VECTOR &r = *Vr;
r.reinit (x);
double length = x.l2_norm ();
double old_value = value;
x.scale(1./length);
// Main loop
for (size_type iter=0; conv==SolverControl::iterate; iter++)
{
solver.solve (A_s, y, x, prec);
// Compute absolute value of eigenvalue
length = y.l2_norm ();
// do a little trick to compute the sign
// with not too much effect of round-off errors.
double entry = 0.;
size_type i = 0;
double thresh = length/x.size();
do
{
Assert (i<x.size(), ExcInternalError());
entry = y (i++);
}
while (std::fabs(entry) < thresh);
--i;
// Compute unshifted eigenvalue
value = (entry * x (i) < 0.) ? -length : length;
value = 1./value;
value -= A_s.shift ();
if (iter==goal)
{
const double new_shift = - additional_data.relaxation * value
+ (1.-additional_data.relaxation) * A_s.shift();
A_s.shift(new_shift);
++goal;
}
// Update normalized eigenvector
x.equ (1./length, y);
// Compute residual
if (additional_data.use_residual)
{
y.equ (value, x);
A.vmult(r,x);
r.sadd(-1., value, x);
double res = r.l2_norm();
// Check the residual
conv = this->control().check (iter, res);
}
else
{
conv = this->control().check (iter, std::fabs(1./value-1./old_value));
}
old_value = value;
}
this->memory.free(Vy);
this->memory.free(Vr);
deallog.pop();
// in case of failure: throw
// exception
if (this->control().last_check() != SolverControl::success)
throw SolverControl::NoConvergence (this->control().last_step(),
this->control().last_value());
// otherwise exit as normal
}
DEAL_II_NAMESPACE_CLOSE
#endif
|