Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_Cholmod_TypeMap.hpp
00001 // @HEADER
00002 //
00003 // ***********************************************************************
00004 //
00005 //           Amesos2: Templated Direct Sparse Solver Package 
00006 //                  Copyright 2011 Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00039 //
00040 // ***********************************************************************
00041 //
00042 // @HEADER
00043 
00044 
00056 #ifndef AMESOS2_CHOLMOD_TYPEMAP_HPP
00057 #define AMESOS2_CHOLMOD_TYPEMAP_HPP
00058 
00059 #include <functional>
00060 #ifdef HAVE_TEUCHOS_COMPLEX
00061 #include <complex>
00062 #endif
00063 
00064 #include <Teuchos_as.hpp>
00065 #ifdef HAVE_TEUCHOS_COMPLEX
00066 #include <Teuchos_SerializationTraits.hpp>
00067 #endif
00068 
00069 #include "Amesos2_TypeMap.hpp"
00070 
00071 namespace Amesos2{
00072   namespace CHOL {
00073     
00074     struct complex
00075     {
00076       double complexpair[2];
00077     };
00078 
00079     #   include "cholmod.h"
00080     //#   include <mkl_types.h>
00081   } // end namespace PMKL
00082 } // end namespace Amesos2
00083 
00084 
00085 /* ==================== Conversion ====================
00086  *
00087  * Define here, in the Teuchos namespace, any conversions between
00088  * commonly used date types and the solver-specific data types.  Use
00089  * template specializations of the Teuchos::ValueTypeConversionTraits
00090  * class.
00091  */
00092 namespace Teuchos {
00093 
00094   template <typename TypeFrom>
00095   class ValueTypeConversionTraits<Amesos2::CHOL::complex, TypeFrom>
00096   {
00097   public:
00098     static Amesos2::CHOL::complex convert( const TypeFrom t )
00099     {                           // adapt conversion as necessary
00100       Amesos2::CHOL::complex ret;
00101       //ret.r = Teuchos::as<float>(t.real());
00102       //ret.i = Teuchos::as<float>(t.imag());
00103       ret.complexpair[0] = Teuchos::as<float>(t.real());
00104       ret.complexpair[1] = Teuchos::as<float>(t.imag());
00105       return( ret );
00106     }
00107 
00108     static Amesos2::CHOL::complex safeConvert( const TypeFrom t )
00109     {                           // adapt conversion as necessary
00110       Amesos2::CHOL::complex ret;
00111       //ret.r = Teuchos::as<float>(t.real());
00112       //ret.i = Teuchos::as<float>(t.imag());
00113       ret.complexpair[0] = Teuchos::as<float>(t.real());
00114       ret.complexpair[1] = Teuchos::as<float>(t.imag());
00115       return( ret );
00116     }
00117   };
00118 
00119   // Also convert *from* New_Solver types
00120   template <typename TypeTo>
00121   class ValueTypeConversionTraits<TypeTo, Amesos2::CHOL::complex>
00122   {
00123   public:
00124     static TypeTo convert( const Amesos2::CHOL::complex t )
00125     {                           // adapt conversion as necessary
00126       typedef typename TypeTo::value_type value_type;
00127       //value_type ret_r = Teuchos::as<value_type>( t.real );
00128       //value_type ret_i = Teuchos::as<value_type>( t.imag );
00129       value_type ret_r = Teuchos::as<value_type>(t.complexpair[0]);
00130       value_type ret_i = Teuchos::as<value_type>(t.complexpair[1]);
00131       return ( TypeTo( ret_r, ret_i ) );
00132     }
00133 
00134     static TypeTo safeConvert( const Amesos2::CHOL::complex t )
00135     {                           // adapt conversion as necessary
00136       typedef typename TypeTo::value_type value_type;
00137       //value_type ret_r = Teuchos::as<value_type>( t.real );
00138       //value_type ret_i = Teuchos::as<value_type>( t.imag );
00139       value_type ret_r = Teuchos::as<value_type>(t.complexpair[0]);
00140       value_type ret_i = Teuchos::as<value_type>(t.complexpair[1]);
00141       return ( TypeTo( ret_r, ret_i ) );
00142     }
00143   };
00144 
00146 
00147 } // end namespace Teuchos
00148 
00149 
00150 namespace Amesos2 {
00151 
00152   // forward declaration due to circular reference
00153   template <class, class> class Cholmod;
00154 
00155   template <>
00156   struct TypeMap<Cholmod,float>
00157   {
00158     typedef float type;
00159     typedef float magnitude_type;
00160   };
00161 
00162   template <>
00163   struct TypeMap<Cholmod,double>
00164   {
00165     typedef double type;
00166     typedef double magnitude_type;
00167   };
00168 
00169 #ifdef HAVE_TEUCHOS_COMPLEX
00170 
00171   template <>
00172   struct TypeMap<Cholmod,std::complex<double> >
00173   {
00174     typedef CHOL::complex type;
00175     typedef double magnitude_type;
00176   };
00177 
00178 #endif  // HAVE_TEUCHOS_COMPLEX
00179 
00180   /* \endcond Choldmod_type_specializations */
00181 
00182 } // end namespace Amesos
00183 
00184 #endif  // AMESOS2_CHOLMOD_TYPEMAP_HPP