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
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 #undef PRINT_RESULTS_TO_FILES
00064
00065
00066 #include "NOX.H"
00067 #include "NOX_Epetra.H"
00068
00069
00070 #ifdef HAVE_MPI
00071 #include "Epetra_MpiComm.h"
00072 #else
00073 #include "Epetra_SerialComm.h"
00074 #endif
00075 #include "Epetra_Map.h"
00076 #include "Epetra_Vector.h"
00077 #include "Epetra_RowMatrix.h"
00078 #include "Epetra_CrsMatrix.h"
00079 #include "Epetra_Map.h"
00080 #include "Epetra_LinearProblem.h"
00081
00082
00083
00084 #include "1DfemInterface.H"
00085
00086 #include "Teuchos_dyn_cast.hpp"
00087
00088 using namespace std;
00089
00090
00091
00092
00093 class TransientInterface : public Interface
00094 {
00095
00096 public:
00097
00098
00099
00100
00101 TransientInterface(int NumGlobalElements, Epetra_Comm& Comm,
00102 double xmin = 0.0, double xmax = 1.0) :
00103 Interface(NumGlobalElements, Comm, xmin, xmax),
00104 oldSolution(0),
00105 exactSolution(0)
00106 {
00107
00108 initializeSoln();
00109
00110 oldSolution = new Epetra_Vector(*initialSolution);
00111 };
00112
00113
00114
00115
00116 virtual ~TransientInterface()
00117 {
00118 delete oldSolution;
00119 if (exactSolution) delete exactSolution;
00120 };
00121
00122
00123
00124
00125 bool evaluate(NOX::Epetra::Interface::Required::FillType flag,
00126 const Epetra_Vector* soln,
00127 const Epetra_Vector* solndot,
00128 double alpha,
00129 double beta,
00130 Epetra_Vector* tmp_rhs,
00131 Epetra_RowMatrix* tmp_matrix)
00132 {
00133
00134 Epetra_CrsMatrix* jacout;
00135
00136
00137 bool fillF = false;
00138 bool fillMatrix = false;
00139 if (tmp_rhs != 0) {
00140 fillF = true;
00141 rhs = tmp_rhs;
00142 }
00143 else {
00144 fillMatrix = true;
00145 jacout = &Teuchos::dyn_cast<Epetra_CrsMatrix>(*tmp_matrix);
00146 }
00147
00148
00149
00150
00151 if (flag == NOX::Epetra::Interface::Required::Residual) {
00152
00153 }
00154 else if (flag == NOX::Epetra::Interface::Required::Jac) {
00155
00156 }
00157 else if (flag == NOX::Epetra::Interface::Required::Prec) {
00158
00159 }
00160 else if (flag == NOX::Epetra::Interface::Required::User) {
00161
00162 }
00163
00164
00165
00166 Epetra_Vector u(*OverlapMap);
00167 Epetra_Vector uold(*OverlapMap);
00168 Epetra_Vector xvec(*OverlapMap);
00169 Epetra_Vector udot(*OverlapMap);
00170
00171
00172 u.Import(*soln, *Importer, Insert);
00173 uold.Import(*oldSolution, *Importer, Insert);
00174 xvec.Import(*xptr, *Importer, Insert);
00175 udot.Import(*solndot, *Importer, Insert);
00176
00177
00178 int ierr;
00179 int OverlapNumMyElements = OverlapMap->NumMyElements();
00180
00181 int OverlapMinMyGID;
00182 if (MyPID == 0) OverlapMinMyGID = StandardMap->MinMyGID();
00183 else OverlapMinMyGID = StandardMap->MinMyGID()-1;
00184
00185 int row, column;
00186 double jac;
00187 double xx[2];
00188 double uu[2];
00189 double uuold[2];
00190 double uudot[2];
00191 Basis basis;
00192
00193
00194 if (fillF)
00195 rhs->PutScalar(0.0);
00196 if (fillMatrix)
00197 jacout->PutScalar(0.0);
00198
00199
00200 for (int ne=0; ne < OverlapNumMyElements-1; ne++) {
00201
00202
00203 for(int gp=0; gp < 2; gp++) {
00204
00205 xx[0]=xvec[ne];
00206 xx[1]=xvec[ne+1];
00207 uu[0]=u[ne];
00208 uu[1]=u[ne+1];
00209 uuold[0]=uold[ne];
00210 uuold[1]=uold[ne+1];
00211 uudot[0] = udot[ne];
00212 uudot[1] = udot[ne+1];
00213
00214
00215 basis.computeBasis(gp, xx, uu, uudot);
00216
00217
00218 for (int i=0; i< 2; i++) {
00219 row=OverlapMap->GID(ne+i);
00220
00221
00222 if (StandardMap->MyGID(row)) {
00223 if (fillF) {
00224 (*rhs)[StandardMap->LID(OverlapMap->GID(ne+i))]+=
00225 +basis.wt*basis.dx*(
00226
00227 basis.uuold*basis.phi[i] +
00228 (1.0/(basis.dx*basis.dx))*basis.duu*basis.dphide[i] -
00229 8.0/factor/factor*basis.uu*basis.uu*
00230 (1.0-basis.uu)*basis.phi[i]);
00231 }
00232 }
00233
00234 if (fillMatrix) {
00235 for(int j=0;j < 2; j++) {
00236 if (StandardMap->MyGID(row)) {
00237 column=OverlapMap->GID(ne+j);
00238 jac=basis.wt*basis.dx*(
00239
00240 basis.phi[j]*alpha*basis.phi[i] +
00241 beta*(
00242 (1.0/(basis.dx*basis.dx))*
00243 basis.dphide[j]*basis.dphide[i] -
00244 8.0/factor/factor*
00245 (2.0*basis.uu-3.0*basis.uu*basis.uu)*
00246 basis.phi[j]*basis.phi[i]));
00247 ierr=jacout->SumIntoGlobalValues(row, 1, &jac, &column);
00248 }
00249 }
00250 }
00251 }
00252 }
00253 }
00254
00255
00256
00257 if (MyPID==0) {
00258 if (fillF)
00259 (*rhs)[0]= (*soln)[0] - 1.0;
00260 if (fillMatrix) {
00261 column=0;
00262 jac=1.0;
00263 jacout->ReplaceGlobalValues(0, 1, &jac, &column);
00264 column=1;
00265 jac=0.0;
00266 jacout->ReplaceGlobalValues(0, 1, &jac, &column);
00267 }
00268 }
00269
00270
00271
00272 if (MyPID==NumProc-1) {
00273 if (fillF)
00274 (*rhs)[NumMyElements-1]= (*soln)[OverlapNumMyElements-1] - 0.0;
00275 if (fillMatrix) {
00276 row=NumGlobalElements-1;
00277 column=row;
00278 jac=1.0;
00279 jacout->ReplaceGlobalValues(row, 1, &jac, &column);
00280 column--;
00281 jac=0.0;
00282 jacout->ReplaceGlobalValues(row, 1, &jac, &column);
00283 }
00284 }
00285
00286
00287 Comm->Barrier();
00288
00289 if (fillMatrix)
00290 jacout->FillComplete();
00291
00292 return true;
00293 }
00294
00295
00296
00297
00298 bool initializeSoln()
00299 {
00300 Epetra_Vector& x = *xptr;
00301
00302 double arg;
00303 for(int i=0; i<NumMyElements; i++) {
00304 arg = x[i]/factor;
00305 (*initialSolution)[i] = (1.0 - ( exp(arg) - exp(-arg) ) /
00306 ( exp(arg) + exp(-arg) )) / 2.0;
00307 }
00308 return true;
00309 }
00310
00311
00312
00313 bool reset(const Epetra_Vector& x)
00314 { *oldSolution = x; return true; };
00315
00316
00317 Epetra_Vector& getOldSoln()
00318 { return *oldSolution; };
00319
00320
00321
00322
00323
00324 Epetra_Vector& getExactSoln(double time)
00325 {
00326
00327
00328 if( !exactSolution )
00329 exactSolution = new Epetra_Vector(*xptr);
00330
00331 Epetra_Vector& x = *xptr;
00332
00333 for(int i=0; i<NumMyElements; i++)
00334 (*exactSolution)[i] =
00335 (1.0 - tanh( (x[i]-2.0*time/factor)/factor )) / 2.0;
00336
00337 return *exactSolution;
00338 }
00339
00340
00341 void setdt( double dt_ ) { dt = dt_; }
00342
00343
00344 double getdt() { return dt; }
00345
00346 private:
00347
00348 double dt;
00349
00350 Epetra_Vector *oldSolution;
00351 Epetra_Vector *exactSolution;
00352
00353 };
00354