00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include "Teuchos_ParameterList.hpp"
00043
00044 #include "LOCA_GlobalData.H"
00045 #include "LOCA_Factory.H"
00046 #include "LOCA_ErrorCheck.H"
00047 #include "NOX_Utils.H"
00048 #include "LOCA_Parameter_SublistParser.H"
00049
00050 #include "LOCA_Eigensolver_DGGEVStrategy.H"
00051 #include "LOCA_EigenvalueSort_Strategies.H"
00052
00053 #include "Teuchos_LAPACK_wrappers.hpp"
00054 #include "LOCA_LAPACK_Group.H"
00055
00056
00057 LOCA::Eigensolver::DGGEVStrategy::DGGEVStrategy(
00058 const Teuchos::RCP<LOCA::GlobalData>& global_data,
00059 const Teuchos::RCP<LOCA::Parameter::SublistParser>& tpParams,
00060 const Teuchos::RCP<Teuchos::ParameterList>& eigParams) :
00061 globalData(global_data),
00062 topParams(tpParams),
00063 eigenParams(eigParams),
00064 nev(4),
00065 which("LM")
00066 {
00067 nev = eigenParams->get("Num Eigenvalues", 4);
00068 which = eigenParams->get("Sorting Order","LM");
00069 }
00070
00071 LOCA::Eigensolver::DGGEVStrategy::~DGGEVStrategy()
00072 {
00073 }
00074
00075 NOX::Abstract::Group::ReturnType
00076 LOCA::Eigensolver::DGGEVStrategy::computeEigenvalues(
00077 NOX::Abstract::Group& group,
00078 Teuchos::RCP< std::vector<double> >& evals_r,
00079 Teuchos::RCP< std::vector<double> >& evals_i,
00080 Teuchos::RCP< NOX::Abstract::MultiVector >& evecs_r,
00081 Teuchos::RCP< NOX::Abstract::MultiVector >& evecs_i)
00082 {
00083
00084
00085 LOCA::LAPACK::Group* grp =
00086 dynamic_cast<LOCA::LAPACK::Group*>(&group);
00087
00088 bool hasMassMatrix = true;
00089
00090
00091
00092 #if defined(HAVE_LAPACK_GGEV) || defined(HAVE_LAPACK_GEGV)
00093
00094 #else
00095 if (hasMassMatrix) {
00096 if (globalData->locaUtils->isPrintType(NOX::Utils::StepperIteration)) {
00097 globalData->locaErrorCheck->printWarning(
00098 "LOCA::Eigensolver::DGGEVStrategy::computeEigenvalues",
00099 "LAPACK Generalized eigensolver (dggev) requested but not available!");
00100 }
00101 return LOCA::Abstract::Group::Ok;
00102 }
00103
00104 #endif
00105
00106 if (globalData->locaUtils->isPrintType(NOX::Utils::StepperIteration)) {
00107 globalData->locaUtils->out()
00108 << std::endl << globalData->locaUtils->fill(64,'=') << std::endl
00109 << "LAPACK ";
00110 if (hasMassMatrix)
00111 globalData->locaUtils->out() << "DGGEV ";
00112 else
00113 globalData->locaUtils->out() << "DGEEV ";
00114 globalData->locaUtils->out() << "Eigensolver starting."
00115 << std::endl << std::endl;;
00116 }
00117
00118
00119 grp->computeJacobian();
00120 if (hasMassMatrix)
00121 grp->computeShiftedMatrix(0.0, 1.0);
00122
00123
00124 NOX::LAPACK::Matrix<double>& jacobianMatrix = grp->getJacobianMatrix();
00125 NOX::LAPACK::Matrix<double>& massMatrix = grp->getShiftedMatrix();
00126
00127
00128 int n = jacobianMatrix.numRows();
00129 int lda = jacobianMatrix.numRows();
00130 int ldb = massMatrix.numRows();
00131
00132
00133 double *vr = new double[n*n];
00134
00135
00136 double *alphar = new double[n];
00137 double *alphai = new double[n];
00138 double *beta = new double[n];
00139
00140
00141 int lwork = -1;
00142
00143
00144 double work0;
00145
00146
00147 double *work;
00148
00149
00150 int info;
00151
00152
00153 NOX::LAPACK::Matrix<double> J(jacobianMatrix);
00154
00155 NOX::LAPACK::Matrix<double> M;
00156
00157
00158 if (hasMassMatrix) {
00159
00160
00161 M = massMatrix;
00162
00163 #if defined(HAVE_LAPACK_GGEV) || defined(HAVE_LAPACK_GEGV)
00164 DGGEV_F77("N", "V", &n, &J(0,0), &lda, &M(0,0), &ldb, alphar, alphai, beta,
00165 vr, &n, vr, &n, &work0, &lwork, &info);
00166 #endif
00167 }
00168 else {
00169 DGEEV_F77("N", "V", &n, &J(0,0), &lda, alphar, alphai,
00170 vr, &n, vr, &n, &work0, &lwork, &info);
00171 }
00172
00173
00174 lwork = (int) work0;
00175 work = new double[lwork];
00176
00177
00178 if (hasMassMatrix) {
00179 #if defined(HAVE_LAPACK_GGEV) || defined(HAVE_LAPACK_GEGV)
00180 DGGEV_F77("N", "V", &n, &J(0,0), &lda, &M(0,0), &ldb, alphar, alphai, beta,
00181 vr, &n, vr, &n, work, &lwork, &info);
00182 #endif
00183 }
00184 else {
00185 DGEEV_F77("N", "V", &n, &J(0,0), &lda, alphar, alphai,
00186 vr, &n, vr, &n, work, &lwork, &info);
00187 }
00188
00189
00190 if (info != 0)
00191 return NOX::Abstract::Group::Failed;
00192
00193
00194 std::vector<double> evals_r_tmp(n);
00195 std::vector<double> evals_i_tmp(n);
00196 Teuchos::RCP<NOX::Abstract::MultiVector> evecs_r_tmp =
00197 group.getX().createMultiVector(n, NOX::ShapeCopy);
00198 Teuchos::RCP<NOX::Abstract::MultiVector>evecs_i_tmp =
00199 group.getX().createMultiVector(n, NOX::ShapeCopy);
00200 NOX::LAPACK::Vector* tmpr;
00201 NOX::LAPACK::Vector* tmpi;
00202 double rnext;
00203 double inext;
00204 bool isComplexEval = false;
00205 bool isPrevComplexEval = false;
00206 for (int j=0; j<n; j++) {
00207
00208
00209 if (hasMassMatrix) {
00210 evals_r_tmp[j] = alphar[j]/beta[j];
00211 evals_i_tmp[j] = alphai[j]/beta[j];
00212 }
00213 else {
00214 evals_r_tmp[j] = alphar[j];
00215 evals_i_tmp[j] = alphai[j];
00216 }
00217
00218
00219 if (!isPrevComplexEval && j < n-1) {
00220 if (hasMassMatrix) {
00221 rnext = alphar[j+1]/beta[j+1];
00222 inext = alphai[j+1]/beta[j+1];
00223 }
00224 else {
00225 rnext = alphar[j+1];
00226 inext = alphai[j+1];
00227 }
00228
00229
00230 if (fabs(evals_r_tmp[j] - rnext) < 1.0e-14*fabs(1.0+evals_r_tmp[j]) &&
00231 fabs(evals_i_tmp[j] + inext) < 1.0e-14*fabs(1.0+evals_i_tmp[j]))
00232 isComplexEval = true;
00233 else
00234 isComplexEval = false;
00235 }
00236 else if (!isPrevComplexEval && j == n-1)
00237 isComplexEval = false;
00238
00239 tmpr = dynamic_cast<NOX::LAPACK::Vector*>(&((*evecs_r_tmp)[j]));
00240 tmpi = dynamic_cast<NOX::LAPACK::Vector*>(&((*evecs_i_tmp)[j]));
00241
00242 if (isComplexEval)
00243 for (int i=0; i<n; i++) {
00244 (*tmpr)(i) = vr[i+j*n];
00245 (*tmpi)(i) = vr[i+(j+1)*n];
00246 }
00247 else if (isPrevComplexEval)
00248 for (int i=0; i<n; i++) {
00249 (*tmpr)(i) = vr[i+(j-1)*n];
00250 (*tmpi)(i) = -vr[i+j*n];
00251 }
00252 else
00253 for (int i=0; i<n; i++) {
00254 (*tmpr)(i) = vr[i+j*n];
00255 (*tmpi)(i) = 0.0;;
00256 }
00257
00258 if (isPrevComplexEval) {
00259 isPrevComplexEval = false;
00260 isComplexEval = false;
00261 }
00262 if (isComplexEval) {
00263 isPrevComplexEval = true;
00264 isComplexEval = false;
00265 }
00266
00267 }
00268
00269
00270 Teuchos::RCP<LOCA::EigenvalueSort::AbstractStrategy> evalSort =
00271 globalData->locaFactory->createEigenvalueSortStrategy(topParams,
00272 eigenParams);
00273
00274
00275 std::vector<int> perm(n);
00276
00277
00278 evalSort->sort(n, &evals_r_tmp[0], &evals_i_tmp[0], &perm);
00279
00280
00281 std::vector<int> perm_short(perm.begin(), perm.begin()+nev);
00282
00283
00284 evals_r = Teuchos::rcp(new std::vector<double>(evals_r_tmp.begin(),
00285 evals_r_tmp.begin()+nev));
00286 evals_i = Teuchos::rcp(new std::vector<double>(evals_i_tmp.begin(),
00287 evals_i_tmp.begin()+nev));
00288 evecs_r = evecs_r_tmp->subCopy(perm_short);
00289 evecs_i = evecs_i_tmp->subCopy(perm_short);
00290
00291
00292 if (globalData->locaUtils->isPrintType(NOX::Utils::StepperIteration)) {
00293 for (int i=0; i<nev; i++)
00294 globalData->locaUtils->out()
00295 << "Eigenvalue " << i << " : "
00296 << globalData->locaUtils->sciformat((*evals_r)[i]) << " "
00297 << globalData->locaUtils->sciformat((*evals_i)[i]) << " i"
00298 << std::endl;
00299 }
00300
00301 if (globalData->locaUtils->isPrintType(NOX::Utils::StepperIteration))
00302 globalData->locaUtils->out()
00303 << "\nLAPACK Eigensolver finished.\n"
00304 << globalData->locaUtils->fill(64,'=') << "\n" << std::endl;
00305
00306 delete [] alphar;
00307 delete [] alphai;
00308 delete [] beta;
00309 delete [] vr;
00310 delete [] work;
00311
00312 return NOX::Abstract::Group::Ok;
00313 }