Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include "Albany_SaveEigenData.hpp"
00008 #include "Albany_EigendataInfoStruct.hpp"
00009 #include "NOX_Abstract_MultiVector.H"
00010 #include "NOX_Epetra_MultiVector.H"
00011 #include "Epetra_Import.h"
00012 #include "Epetra_Vector.h"
00013 #include <string>
00014
00015 Albany::SaveEigenData::
00016 SaveEigenData(Teuchos::ParameterList& locaParams, Teuchos::RCP<NOX::Epetra::Observer> observer, Albany::StateManager* pStateMgr)
00017 : nsave(0),
00018 nSaveAsStates(0)
00019 {
00020 bool doEig = locaParams.sublist("Stepper").get("Compute Eigenvalues", false);
00021 if (doEig) {
00022 nsave = locaParams.sublist("Stepper").
00023 sublist("Eigensolver").get("Save Eigenvectors",0);
00024
00025 nSaveAsStates = nsave;
00026 }
00027
00028 std::cout << "\nSaveEigenData: Will save up to "
00029 << nsave << " eigenvectors, and output "
00030 << nSaveAsStates << " as states." << std::endl;
00031
00032 noxObserver = observer;
00033 pAlbStateMgr = pStateMgr;
00034 }
00035
00036 Albany::SaveEigenData::~SaveEigenData()
00037 {
00038 }
00039
00040 NOX::Abstract::Group::ReturnType
00041 Albany::SaveEigenData::save(
00042 Teuchos::RCP< std::vector<double> >& evals_r,
00043 Teuchos::RCP< std::vector<double> >& evals_i,
00044 Teuchos::RCP< NOX::Abstract::MultiVector >& evecs_r,
00045 Teuchos::RCP< NOX::Abstract::MultiVector >& evecs_i)
00046 {
00047 using namespace std;
00048
00049 if (nsave==0) return NOX::Abstract::Group::Ok;
00050
00051 Teuchos::RCP<NOX::Epetra::MultiVector> ne_r =
00052 Teuchos::rcp_dynamic_cast<NOX::Epetra::MultiVector>(evecs_r);
00053 Teuchos::RCP<NOX::Epetra::MultiVector> ne_i =
00054 Teuchos::rcp_dynamic_cast<NOX::Epetra::MultiVector>(evecs_i);
00055 Epetra_MultiVector& e_r = ne_r->getEpetraMultiVector();
00056 Epetra_MultiVector& e_i = ne_i->getEpetraMultiVector();
00057
00058 char buf[100];
00059
00060 int ns = std::min(nsave, evecs_r->numVectors());
00061
00062
00063 Teuchos::RCP<EigendataStruct> eigenData = Teuchos::rcp( new EigendataStruct );
00064
00065 Teuchos::RCP<Albany::AbstractDiscretization> disc =
00066 pAlbStateMgr->getDiscretization();
00067
00068 eigenData->eigenvalueRe = evals_r;
00069 eigenData->eigenvalueIm = evals_i;
00070
00071 eigenData->eigenvectorRe =
00072 Teuchos::rcp(new Epetra_MultiVector(*(disc->getOverlapMap()), ns));
00073 eigenData->eigenvectorIm =
00074 Teuchos::rcp(new Epetra_MultiVector(*(disc->getOverlapMap()), ns));
00075
00076
00077 Teuchos::RCP<Epetra_Import> importer =
00078 Teuchos::rcp(new Epetra_Import(*(disc->getOverlapMap()), *(disc->getMap())));
00079
00080
00081 for(int i=0; i<ns; i++) {
00082 (*(eigenData->eigenvectorRe))(i)->Import( *(e_r(i)), *importer, Insert );
00083 (*(eigenData->eigenvectorIm))(i)->Import( *(e_i(i)), *importer, Insert );
00084 }
00085
00086 pAlbStateMgr->setEigenData(eigenData);
00087
00088
00089
00090 std::fstream evecFile;
00091 std::fstream evalFile;
00092 evalFile.open ("evals.txtdump", std::fstream::out);
00093 evalFile << "# Eigenvalues: index, Re, Im" << std::endl;
00094 for (int i=0; i<ns; i++) {
00095 evalFile << i << " " << (*evals_r)[i] << " " << (*evals_i)[i] << std::endl;
00096
00097 if ( fabs((*evals_i)[i]) == 0 ) {
00098
00099
00100
00101
00102
00103
00104
00105 sprintf(buf,"evec%d.csv",i);
00106 evecFile.open (buf, std::fstream::out);
00107 evecFile << std::setprecision(8)
00108 << "# Eigenvalue " << i << " with value: " << (*evals_r)[i]
00109 << "\n# Has Eigenvector: \n" << *(e_r(i)) << "\n" << std::endl;
00110 evecFile.close();
00111
00112 double norm; e_r(i)->Norm2(&norm);
00113 std::cout << "Saved to " << buf << " (norm = " << norm << ")" << std::endl;
00114
00115
00116 noxObserver->observeSolution( *(e_r(i)) , (*evals_r)[i]);
00117 }
00118 else {
00119
00120
00121
00122
00123
00124
00125
00126
00127 sprintf(buf,"evec%d.csv",i);
00128 evecFile.open (buf, std::fstream::out);
00129 evecFile << std::setprecision(8)
00130 << "# Eigenvalue " << i << " with value: "
00131 << (*evals_r)[i] <<" + " << (*evals_i)[i] << "\n"
00132 << "# Has Eigenvector Re,Im: \n"
00133 << *(e_r(i)) << "\n" << *(e_i(i)) << "\n" << std::endl;
00134 evecFile.close();
00135 std::cout << "Saved Re, Im to " << buf << std::endl;
00136
00137
00138 noxObserver->observeSolution( *(e_r(i)), (*evals_r)[i] );
00139 noxObserver->observeSolution( *(e_i(i)), (*evals_i)[i] );
00140 }
00141 }
00142 evalFile.close();
00143
00144 return NOX::Abstract::Group::Ok;
00145 }