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 #ifndef RYTHMOS_RK_BUTCHER_TABLEAU_HELPERS_HPP
00031 #define RYTHMOS_RK_BUTCHER_TABLEAU_HELPERS_HPP
00032
00033 #include "Rythmos_Types.hpp"
00034
00035 #include "Rythmos_RKButcherTableauBase.hpp"
00036 #include "Teuchos_Assert.hpp"
00037 #include "Thyra_ProductVectorBase.hpp"
00038
00039 namespace Rythmos {
00040
00041
00042 template<class Scalar>
00043 void assembleIRKState(
00044 const int stageIndex,
00045 const Teuchos::SerialDenseMatrix<int,Scalar> &A_in,
00046 const Scalar dt,
00047 const Thyra::VectorBase<Scalar> &x_base,
00048 const Thyra::ProductVectorBase<Scalar> &x_stage_bar,
00049 Teuchos::Ptr<Thyra::VectorBase<Scalar> > x_out_ptr
00050 )
00051 {
00052
00053 typedef ScalarTraits<Scalar> ST;
00054
00055 const int numStages_in = A_in.numRows();
00056 TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE( stageIndex, 0, numStages_in );
00057 TEUCHOS_ASSERT_EQUALITY( A_in.numRows(), numStages_in );
00058 TEUCHOS_ASSERT_EQUALITY( A_in.numCols(), numStages_in );
00059 TEUCHOS_ASSERT_EQUALITY( x_stage_bar.productSpace()->numBlocks(), numStages_in );
00060 Thyra::VectorBase<Scalar>& x_out = *x_out_ptr;
00061
00062 V_V( outArg(x_out), x_base );
00063 for ( int j = 0; j < numStages_in; ++j ) {
00064 Vp_StV( outArg(x_out), dt * A_in(stageIndex,j), *x_stage_bar.getVectorBlock(j) );
00065 }
00066
00067 }
00068
00069
00070
00071 template<class Scalar>
00072 void assembleIRKSolution(
00073 const Teuchos::SerialDenseVector<int,Scalar> &b_in,
00074 const Scalar dt,
00075 const Thyra::VectorBase<Scalar> &x_base,
00076 const Thyra::ProductVectorBase<Scalar> &x_stage_bar,
00077 Teuchos::Ptr<Thyra::VectorBase<Scalar> > x_out_ptr
00078 )
00079 {
00080
00081 typedef ScalarTraits<Scalar> ST;
00082
00083 const int numStages_in = b_in.length();
00084 TEUCHOS_ASSERT_EQUALITY( b_in.length(), numStages_in );
00085 TEUCHOS_ASSERT_EQUALITY( x_stage_bar.productSpace()->numBlocks(), numStages_in );
00086 Thyra::VectorBase<Scalar>& x_out = *x_out_ptr;
00087
00088 V_V( outArg(x_out), x_base );
00089 for ( int j = 0; j < numStages_in; ++j ) {
00090 Vp_StV( outArg(x_out), dt * b_in(j), *x_stage_bar.getVectorBlock(j) );
00091 }
00092
00093 }
00094
00095
00096 template<class Scalar>
00097 void assembleERKState(
00098 const int stageIndex,
00099 const Teuchos::SerialDenseMatrix<int,Scalar> &A_in,
00100 const Scalar dt,
00101 const Thyra::VectorBase<Scalar> &x_base,
00102 const Thyra::VectorBase<Scalar> &x_stage_bar,
00103 Teuchos::Ptr<Thyra::VectorBase<Scalar> > x_out_ptr
00104 )
00105 {
00106 TEST_FOR_EXCEPT(true);
00107 }
00108
00109
00110 template<class Scalar>
00111 void assembleERKSolution(
00112 const Teuchos::SerialDenseVector<int,Scalar> &b_in,
00113 const Scalar dt,
00114 const Thyra::VectorBase<Scalar> &x_base,
00115 const Thyra::VectorBase<Scalar> &x_stage_bar,
00116 Teuchos::Ptr<Thyra::VectorBase<Scalar> > x_out_ptr
00117 )
00118 {
00119 TEST_FOR_EXCEPT(true);
00120 }
00121
00122 template<class Scalar>
00123 bool isEmptyRKButcherTableau( const RKButcherTableauBase<Scalar>& rkbt ) {
00124 typedef ScalarTraits<Scalar> ST;
00125
00126
00127 if (rkbt.numStages() == 0) {
00128 return true;
00129 }
00130
00131
00132 int numNonZero = 0;
00133 int numStages_local = rkbt.numStages();
00134 const Teuchos::SerialDenseVector<int,Scalar> b_local = rkbt.b();
00135 for (int i=0 ; i<numStages_local ; ++i) {
00136 if (b_local(i) != ST::zero()) {
00137 numNonZero++;
00138 }
00139 }
00140 if (numNonZero == 0) {
00141 return true;
00142 }
00143
00144
00145 return false;
00146 }
00147
00148
00149 template<class Scalar>
00150 void assertNonEmptyRKButcherTableau( const RKButcherTableauBase<Scalar>& rkbt )
00151 {
00152 TEST_FOR_EXCEPTION( isEmptyRKButcherTableau(rkbt), std::logic_error,
00153 "Error, this RKButcherTableau is either empty or the b vector is all zeros!\n"
00154 );
00155 }
00156
00157 template<class Scalar>
00158 bool isDIRKButcherTableau( const RKButcherTableauBase<Scalar>& rkbt )
00159 {
00160 if (isEmptyRKButcherTableau(rkbt)) {
00161 return false;
00162 }
00163 typedef ScalarTraits<Scalar> ST;
00164 int numStages_local = rkbt.numStages();
00165 const Teuchos::SerialDenseMatrix<int,Scalar> A_local = rkbt.A();
00166 for (int i=0 ; i<numStages_local ; ++i) {
00167 for (int j=0 ; j<numStages_local ; ++j) {
00168 if ((j>i) && (A_local(i,j) != ST::zero())) {
00169 return false;
00170 }
00171 }
00172 }
00173 return true;
00174 }
00175
00176 template<class Scalar>
00177 bool isIRKButcherTableau( const RKButcherTableauBase<Scalar>& rkbt )
00178 {
00179 if (isEmptyRKButcherTableau(rkbt)) {
00180 return false;
00181 }
00182 return true;
00183 }
00184
00185 template<class Scalar>
00186 void validateIRKButcherTableau( const RKButcherTableauBase<Scalar>& rkbt )
00187 {
00188 TEST_FOR_EXCEPTION( !isIRKButcherTableau(rkbt), std::logic_error,
00189 "Error! This implicit RK Butcher Tableau is empty!\n"
00190 );
00191 }
00192
00193 template<class Scalar>
00194 void validateDIRKButcherTableau( const RKButcherTableauBase<Scalar>& rkbt )
00195 {
00196 TEST_FOR_EXCEPTION( !isDIRKButcherTableau(rkbt), std::logic_error,
00197 "Error! This Diagonal Implicit RK Butcher Tableau has non-zeros in the upper triangular part!\n"
00198 );
00199 }
00200
00201 template<class Scalar>
00202 bool isSDIRKButcherTableau( const RKButcherTableauBase<Scalar>& rkbt )
00203 {
00204 if (isEmptyRKButcherTableau(rkbt)) {
00205 return false;
00206 }
00207 if (!isDIRKButcherTableau(rkbt)) {
00208 return false;
00209 }
00210
00211 typedef ScalarTraits<Scalar> ST;
00212 int numStages_local = rkbt.numStages();
00213 const Teuchos::SerialDenseMatrix<int,Scalar> A_local = rkbt.A();
00214 Scalar val = A_local(0,0);
00215 for (int i=0 ; i<numStages_local ; ++i) {
00216 if (A_local(i,i) != val) {
00217 return false;
00218 }
00219 }
00220 return true;
00221 }
00222
00223 template<class Scalar>
00224 void validateSDIRKButcherTableau( const RKButcherTableauBase<Scalar>& rkbt )
00225 {
00226 TEST_FOR_EXCEPTION( !isSDIRKButcherTableau(rkbt), std::logic_error,
00227 "Error! This Singly Diagonal Implicit RK Butcher Tableau does not have equal diagonal entries!\n"
00228 );
00229 }
00230
00231 template<class Scalar>
00232 bool isERKButcherTableau( const RKButcherTableauBase<Scalar>& rkbt)
00233 {
00234 if (isEmptyRKButcherTableau(rkbt)) {
00235 return false;
00236 }
00237
00238 typedef ScalarTraits<Scalar> ST;
00239 int numStages_local = rkbt.numStages();
00240 const Teuchos::SerialDenseMatrix<int,Scalar> A_local = rkbt.A();
00241 for (int i=0 ; i<numStages_local ; ++i) {
00242 for (int j=0 ; j<numStages_local ; ++j) {
00243 if ((j>=i) && ((A_local(i,j) != ST::zero()))) {
00244 return false;
00245 }
00246 }
00247 }
00248 const Teuchos::SerialDenseVector<int,Scalar> c_local = rkbt.c();
00249 if( c_local(0) != ST::zero() ) {
00250 return false;
00251 }
00252
00253 return true;
00254 }
00255
00256
00257
00258 template<class Scalar>
00259 void validateERKButcherTableau( const RKButcherTableauBase<Scalar>& rkbt )
00260 {
00261 TEST_FOR_EXCEPTION( !isERKButcherTableau(rkbt), std::logic_error,
00262 "Error! This ERK Butcher Tableau is not lower triangular or c(0) is not zero!\n"
00263 );
00264 }
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328 enum E_RKButcherTableauTypes {
00329 RYTHMOS_RK_BUTCHER_TABLEAU_TYPE_INVALID,
00330 RYTHMOS_RK_BUTCHER_TABLEAU_TYPE_ERK,
00331 RYTHMOS_RK_BUTCHER_TABLEAU_TYPE_IRK,
00332 RYTHMOS_RK_BUTCHER_TABLEAU_TYPE_DIRK,
00333 RYTHMOS_RK_BUTCHER_TABLEAU_TYPE_SDIRK
00334 };
00335
00336 template<class Scalar>
00337 E_RKButcherTableauTypes determineRKBTType(const RKButcherTableauBase<Scalar>& rkbt) {
00338 if (isEmptyRKButcherTableau(rkbt)) {
00339 return RYTHMOS_RK_BUTCHER_TABLEAU_TYPE_INVALID;
00340 }
00341 if (isERKButcherTableau(rkbt)) {
00342 return RYTHMOS_RK_BUTCHER_TABLEAU_TYPE_ERK;
00343 }
00344 if (rkbt.numStages() == 1) {
00345
00346 return RYTHMOS_RK_BUTCHER_TABLEAU_TYPE_IRK;
00347 }
00348 if (isSDIRKButcherTableau(rkbt)) {
00349 return RYTHMOS_RK_BUTCHER_TABLEAU_TYPE_SDIRK;
00350 }
00351 if (isDIRKButcherTableau(rkbt)) {
00352 return RYTHMOS_RK_BUTCHER_TABLEAU_TYPE_DIRK;
00353 }
00354 if (isIRKButcherTableau(rkbt)) {
00355 return RYTHMOS_RK_BUTCHER_TABLEAU_TYPE_IRK;
00356 }
00357 return RYTHMOS_RK_BUTCHER_TABLEAU_TYPE_INVALID;
00358 }
00359
00360
00361
00362 }
00363
00364
00365 #endif // RYTHMOS_RK_BUTCHER_TABLEAU_HELPERS_HPP