00001
00002
00003
00004
00005
00006 #include <Teuchos_UnitTestHarness.hpp>
00007 #include <LocalNonlinearSolver.hpp>
00008 #include <Sacado.hpp>
00009 #include "PHAL_AlbanyTraits.hpp"
00010
00011 using namespace std;
00012
00013 namespace
00014 {
00015
00016 TEUCHOS_UNIT_TEST( LocalNonlinearSolver, Instantiation )
00017 {
00018 typedef PHAL::AlbanyTraits Traits;
00019 typedef PHAL::AlbanyTraits::Residual EvalT;
00020 typedef PHAL::AlbanyTraits::Residual::ScalarT ScalarT;
00021
00022 int numLocalVars(2);
00023
00024 std::vector<ScalarT> F(numLocalVars);
00025 std::vector<ScalarT> dFdX(numLocalVars * numLocalVars);
00026 std::vector<ScalarT> X(numLocalVars);
00027 LCM::LocalNonlinearSolver<EvalT, Traits> solver;
00028
00029 const int n = 2;
00030 const int nrhs = 1;
00031 RealType A[] =
00032 { 1.1, 0.1, .01, 0.9 };
00033 const int lda = 2;
00034 int IPIV[] =
00035 { 0, 0 };
00036 RealType B[] =
00037 { 0.1, 0.2 };
00038 const int ldb = 2;
00039 int info(0);
00040
00041 const RealType refX[] =
00042 { 0.088978766430738, 0.212335692618807 };
00043
00044
00045 solver.lapack.GESV(n, nrhs, &A[0], lda, &IPIV[0], &B[0], ldb, &info);
00046
00047 TEST_COMPARE(fabs(B[0] - refX[0]), <=, 1.0e-15);
00048 TEST_COMPARE(fabs(B[1] - refX[1]), <=, 1.0e-15);
00049 }
00050
00051 TEUCHOS_UNIT_TEST( LocalNonlinearSolver, Residual )
00052 {
00053 typedef PHAL::AlbanyTraits Traits;
00054 typedef PHAL::AlbanyTraits::Residual EvalT;
00055 typedef PHAL::AlbanyTraits::Residual::ScalarT ScalarT;
00056
00057
00058 int numLocalVars(1);
00059 std::vector<ScalarT> F(numLocalVars);
00060 std::vector<ScalarT> dFdX(numLocalVars * numLocalVars);
00061 std::vector<ScalarT> X(numLocalVars);
00062 LCM::LocalNonlinearSolver<EvalT, Traits> solver;
00063
00064
00065 X[0] = 1.0;
00066
00067 int count(0);
00068 bool converged = false;
00069 while (!converged && count < 10)
00070 {
00071
00072 F[0] = X[0] * X[0] - 2.0;
00073 dFdX[0] = 2.0 * X[0];
00074
00075 solver.solve(dFdX, X, F);
00076
00077 if (fabs(F[0]) <= 1.0E-15)
00078 converged = true;
00079
00080 count++;
00081 }
00082 F[0] = X[0] * X[0] - 2.0;
00083 std::vector<ScalarT> sol(numLocalVars);
00084 solver.computeFadInfo(dFdX, X, F);
00085
00086 const RealType refX[] =
00087 { std::sqrt(2) };
00088 TEST_COMPARE(fabs(X[0] - refX[0]), <=, 1.0e-15);
00089
00090 }
00091
00092 TEUCHOS_UNIT_TEST( LocalNonlinearSolver, Jacobian )
00093 {
00094 typedef PHAL::AlbanyTraits Traits;
00095 typedef PHAL::AlbanyTraits::Jacobian EvalT;
00096 typedef PHAL::AlbanyTraits::Jacobian::ScalarT ScalarT;
00097
00098
00099 int numLocalVars(1);
00100 std::vector<ScalarT> F(numLocalVars);
00101 std::vector<ScalarT> dFdX(numLocalVars * numLocalVars);
00102 std::vector<ScalarT> X(numLocalVars);
00103 LCM::LocalNonlinearSolver<EvalT, Traits> solver;
00104
00105
00106 X[0] = 1.0;
00107
00108 ScalarT two(1, 0, 2.0);
00109 int count(0);
00110 bool converged = false;
00111 while (!converged && count < 10)
00112 {
00113
00114 F[0] = X[0] * X[0] - two;
00115 dFdX[0] = 2.0 * X[0];
00116
00117 solver.solve(dFdX, X, F);
00118
00119 if (fabs(F[0]) <= 1.0E-15)
00120 converged = true;
00121
00122 count++;
00123 }
00124
00125 F[0] = X[0] * X[0] - two;
00126 solver.computeFadInfo(dFdX, X, F);
00127
00128 const RealType refX[] =
00129 { std::sqrt(2) };
00130 TEST_COMPARE(fabs(X[0].val() - refX[0]), <=, 1.0e-15);
00131
00132 }
00133 TEUCHOS_UNIT_TEST( LocalNonlinearSolver, Tangent )
00134 {
00135 typedef PHAL::AlbanyTraits Traits;
00136 typedef PHAL::AlbanyTraits::Tangent EvalT;
00137 typedef PHAL::AlbanyTraits::Tangent::ScalarT ScalarT;
00138
00139
00140 int numLocalVars(1);
00141 std::vector<ScalarT> F(numLocalVars);
00142 std::vector<ScalarT> dFdX(numLocalVars * numLocalVars);
00143 std::vector<ScalarT> X(numLocalVars);
00144 LCM::LocalNonlinearSolver<EvalT, Traits> solver;
00145
00146
00147 X[0] = 1.0;
00148
00149 ScalarT two(1, 0, 2.0);
00150
00151 int count(0);
00152 bool converged = false;
00153 while (!converged && count < 10)
00154 {
00155
00156 F[0] = X[0] * X[0] - two;
00157 dFdX[0] = 2.0 * X[0];
00158
00159 solver.solve(dFdX, X, F);
00160
00161 if (fabs(F[0]) <= 1.0E-15)
00162 converged = true;
00163
00164 count++;
00165 }
00166
00167 F[0] = X[0] * X[0] - two;
00168 solver.computeFadInfo(dFdX, X, F);
00169
00170 const RealType refX[] =
00171 { std::sqrt(2) };
00172 TEST_COMPARE(fabs(X[0].val() - refX[0]), <=, 1.0e-15);
00173 }
00174 }