TSF_NVector.cpp
Go to the documentation of this file.
00001 #include "TSF_NVector.hpp"
00002 #include "TSFLinearCombinationDecl.hpp"
00003 #include "Thyra_SUNDIALS_Ops.hpp"
00004 
00005 #ifndef HAVE_TEUCHOS_EXPLICIT_INSTANTIATION
00006 #include "TSFVectorImpl.hpp"
00007 #include "TSFLinearCombinationImpl.hpp"
00008 #endif
00009 
00010 using namespace TSFExtended;
00011 using namespace TSFExtendedOps;
00012 
00013 #ifdef TRILINOS_6
00014 
00015 #ifdef HAVE_SUNDIALS
00016 #include "sundialsmath.h"
00017 #include "sundialstypes.h"
00018 
00019 extern "C"
00020 {
00021 
00022   /*
00023    * -----------------------------------------------------------------
00024    * exported functions
00025    * -----------------------------------------------------------------
00026    */
00027 
00028   /* ----------------------------------------------------------------------------
00029    * Function to create a new Trilinos vector
00030    */
00031 
00032   N_Vector N_VNew_Trilinos(const VectorSpace<realtype>& space)
00033   {
00034     /* Create vector */
00035     N_Vector v = new _generic_N_Vector(); 
00036     if (v == NULL) return(NULL);
00037 
00038     /* Create vector operation structure */
00039     N_Vector_Ops ops = new _generic_N_Vector_Ops();
00040     if (ops == NULL) 
00041       {
00042         delete v; 
00043         return(NULL);
00044       }
00045 
00046 
00047     ops->nvclone           = N_VClone_Trilinos;
00048     ops->nvcloneempty      = 0;//N_VCloneEmpty_Trilinos;
00049     ops->nvdestroy         = N_VDestroy_Trilinos;
00050     ops->nvspace           = N_VSpace_Trilinos;
00051     ops->nvgetarraypointer = 0;//N_VGetArrayPointer_Trilinos;
00052     ops->nvsetarraypointer = 0;//N_VSetArrayPointer_Trilinos;
00053     ops->nvlinearsum       = N_VLinearSum_Trilinos;
00054     ops->nvconst           = N_VConst_Trilinos;
00055     ops->nvprod            = N_VProd_Trilinos;
00056     ops->nvdiv             = N_VDiv_Trilinos;
00057     ops->nvscale           = N_VScale_Trilinos;
00058     ops->nvabs             = N_VAbs_Trilinos;
00059     ops->nvinv             = N_VInv_Trilinos;
00060     ops->nvaddconst        = N_VAddConst_Trilinos;
00061     ops->nvdotprod         = N_VDotProd_Trilinos;
00062     ops->nvmaxnorm         = N_VMaxNorm_Trilinos;
00063     ops->nvwrmsnormmask    = N_VWrmsNormMask_Trilinos;
00064     ops->nvwrmsnorm        = N_VWrmsNorm_Trilinos;
00065     ops->nvmin             = N_VMin_Trilinos;
00066     ops->nvwl2norm         = N_VWL2Norm_Trilinos;
00067     ops->nvl1norm          = N_VL1Norm_Trilinos;
00068     ops->nvcompare         = N_VCompare_Trilinos;
00069     ops->nvinvtest         = N_VInvTest_Trilinos;
00070     ops->nvconstrmask      = N_VConstrMask_Trilinos;
00071     ops->nvminquotient     = N_VMinQuotient_Trilinos;
00072 
00073     /* Create content */
00074     TrilinosVectorContent content = new _TrilinosVectorContent();
00075     if (content == NULL) 
00076       {
00077         delete ops; 
00078         delete v; 
00079         return(NULL);
00080       }
00081 
00082     content->data = space.createMember();
00083 
00084     /* Attach content and ops */
00085     v->content = content;
00086     v->ops = ops;
00087 
00088     return(v);
00089   }
00090 
00091 
00092 
00093   void N_VPrint_Trilinos(N_Vector x)
00094   {
00095     cout << toTrilinos(x) << std::endl;
00096   }
00097 
00098   /*
00099    * -----------------------------------------------------------------
00100    * implementation of vector operations
00101    * -----------------------------------------------------------------
00102    */
00103 
00104 
00105   N_Vector N_VClone_Trilinos(N_Vector w)
00106   {
00107     N_Vector v;
00108 
00109     Vector<realtype>& original = toTrilinos(w);
00110     v = N_VNew_Trilinos(original.space());
00111     toTrilinos(v).acceptCopyOf(original);
00112 
00113     return(v);
00114   }
00115 
00116   void N_VDestroy_Trilinos(N_Vector v)
00117   {
00118     TrilinosVectorContent xPtr = reinterpret_cast<TrilinosVectorContent>(v->content);
00119     delete xPtr;
00120     delete v->ops;
00121     delete v;
00122   }
00123 
00124   void N_VSpace_Trilinos(N_Vector v, long int *lrw, long int *liw)
00125   {
00126     *lrw = toTrilinos(v).space().dim();
00127     *liw = 1;
00128   }
00129 
00130 
00131   void N_VLinearSum_Trilinos(realtype a, N_Vector x, 
00132                              realtype b, N_Vector y, N_Vector z)
00133   {
00134     if (z==x)
00135       {
00136         toTrilinos(z).update(b, toTrilinos(y), a);
00137       }
00138     else if (z==y)
00139       {
00140         toTrilinos(z).update(a, toTrilinos(x), b);
00141       }
00142     else
00143       {
00144         toTrilinos(z).update(a, toTrilinos(x), 
00145                              b, toTrilinos(y), 0.0);
00146       }
00147   }
00148 
00149   void N_VConst_Trilinos(realtype c, N_Vector z)
00150   {
00151     toTrilinos(z).setToConstant(c);
00152   }
00153 
00154   void N_VProd_Trilinos(N_Vector x, N_Vector y, N_Vector z)
00155   {
00156     Thyra::VProd(*(toTrilinos(x).ptr()),
00157                  *(toTrilinos(y).ptr()),
00158                  toTrilinos(z).ptr().get());
00159   }
00160 
00161   void N_VDiv_Trilinos(N_Vector x, N_Vector y, N_Vector z)
00162   {
00163     Thyra::VDiv(*(toTrilinos(x).ptr()),
00164                 *(toTrilinos(y).ptr()),
00165                 toTrilinos(z).ptr().get());
00166   }
00167 
00168   void N_VScale_Trilinos(realtype c, N_Vector x, N_Vector z)
00169   {
00170     Thyra::VScale(c, *(toTrilinos(x).ptr()), toTrilinos(z).ptr().get());
00171   }
00172 
00173   void N_VAbs_Trilinos(N_Vector x, N_Vector z)
00174   {
00175     /* notice that the argument order is reversed. This is not a typo. */
00176     Thyra::abs(toTrilinos(z).ptr().get(), *(toTrilinos(x).ptr()));
00177   }
00178 
00179   void N_VInv_Trilinos(N_Vector x, N_Vector z)
00180   {
00181     /* notice that the argument order is reversed. This is not a typo. */
00182     Thyra::reciprocal(toTrilinos(z).ptr().get(), *(toTrilinos(x).ptr()));
00183   }
00184 
00185   void N_VAddConst_Trilinos(N_Vector x, realtype b, N_Vector z)
00186   {
00187     Thyra::VAddConst(b, *(toTrilinos(x).ptr()), toTrilinos(z).ptr().get());
00188   }
00189 
00190   realtype N_VDotProd_Trilinos(N_Vector x, N_Vector y)
00191   {
00192     realtype rtn = toTrilinos(x) * toTrilinos(y);
00193     return rtn;
00194   }
00195 
00196   realtype N_VMaxNorm_Trilinos(N_Vector x)
00197   {
00198     realtype rtn = toTrilinos(x).normInf();
00199     return rtn;
00200   }
00201 
00202   realtype N_VWrmsNorm_Trilinos(N_Vector x, N_Vector w)
00203   {
00204     realtype rtn = Thyra::VWrmsNorm(*(toTrilinos(x).ptr()),
00205                                     *(toTrilinos(w).ptr()));
00206     return rtn;
00207   }
00208 
00209   realtype N_VWrmsNormMask_Trilinos(N_Vector x, N_Vector w, N_Vector id)
00210   {
00211     realtype rtn = Thyra::VWrmsMaskNorm(*(toTrilinos(x).ptr()),
00212                                         *(toTrilinos(w).ptr()),
00213                                         *(toTrilinos(id).ptr()));
00214     return rtn;
00215   }
00216 
00217   realtype N_VMin_Trilinos(N_Vector x)
00218   {
00219     realtype rtn = toTrilinos(x).min();
00220     return rtn;
00221   }
00222 
00223   realtype N_VWL2Norm_Trilinos(N_Vector x, N_Vector w)
00224   {
00225     realtype rtn = Thyra::VWL2Norm(*(toTrilinos(x).ptr()),
00226                                    *(toTrilinos(w).ptr()));
00227     return rtn;
00228   }
00229 
00230   realtype N_VL1Norm_Trilinos(N_Vector x)
00231   {
00232     realtype rtn = toTrilinos(x).norm1();
00233     return rtn;
00234   }
00235 
00236   void N_VCompare_Trilinos(realtype c, N_Vector x, N_Vector z)
00237   {
00238     Thyra::VCompare(c, *(toTrilinos(x).ptr()),
00239                     toTrilinos(z).ptr().get());
00240   }
00241 
00242   booleantype N_VInvTest_Trilinos(N_Vector x, N_Vector z)
00243   {
00244     booleantype rtn = Thyra::VInvTest(*(toTrilinos(x).ptr()),
00245                                       toTrilinos(z).ptr().get());
00246     return rtn;
00247   }
00248 
00249   booleantype N_VConstrMask_Trilinos(N_Vector c, N_Vector x, N_Vector m)
00250   {
00251     booleantype rtn = Thyra::VConstrMask(*(toTrilinos(x).ptr()),
00252                                          *(toTrilinos(c).ptr()),
00253                                          toTrilinos(m).ptr().get());
00254     return rtn;
00255   }
00256 
00257   realtype N_VMinQuotient_Trilinos(N_Vector x, N_Vector y)
00258   {
00259     realtype rtn = Thyra::VMinQuotient(*(toTrilinos(x).ptr()),
00260                                        *(toTrilinos(y).ptr()));
00261     return rtn;
00262   }
00263 
00264 }
00265 
00266 #endif
00267 #endif

Site Contact