Commit f343ff94 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber
Browse files

Implementing operator functions.

parent 98fce90e
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
/***************************************************************************
                          tnlBoundaryOperatorFunction.h  -  description
                             -------------------
    begin                : Jan 6, 2016
    copyright            : (C) 2016 by oberhuber
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef TNLBOUNDARYOPERATORFUNCTION_H
#define	TNLBOUNDARYOPERATORFUNCTION_H



#endif	/* TNLBOUNDARYOPERATORFUNCTION_H */
+43 −24
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@

#include <mesh/tnlGrid.h>
#include <functions/tnlMeshFunction.h>
#include <functions/tnlOperatorFunction.h>

template< typename OutMeshFunction,
          typename InFunction >
@@ -87,36 +88,54 @@ class tnlMeshFunctionEvaluator
      };
}; 

/*template< int Dimensions,
          typename MeshReal1,
          typename MeshReal2,
          typename MeshDevice1,
          typename MeshDevice2,
          typename MeshIndex1,
          typename MeshIndex2,
          int MeshEntityDimensions,
          typename Real >
class tnlMeshFunctionEvaluator< tnlMeshFunction< tnlGrid< Dimensions, MeshReal1, MeshDevice1, MeshIndex1 >, MeshEntityDimensions, Real >,
                                tnlMeshFunction< tnlGrid< Dimensions, MeshReal2, MeshDevice2, MeshIndex2 >, MeshEntityDimensions, Real > >
template< typename OutMeshFunction,
          typename Operator,
          typename Function >
class tnlMeshFunctionEvaluator< OutMeshFunction, tnlOperatorFunction< Operator, Function > >
{
   public:
      
      typedef tnlGrid< Dimensions, MeshReal1, MeshDevice1, MeshIndex1 > Mesh1;
      typedef tnlGrid< Dimensions, MeshReal2, MeshDevice2, MeshIndex2 > Mesh2;
      typedef tnlMeshFunction< Mesh1, MeshEntityDimensions, Real > OutMeshFunction;
      typedef tnlMeshFunction< Mesh2, MeshEntityDimensions, Real > InFunction;
      typedef typename OutMeshFunction::MeshType MeshType;
      typedef typename MeshType::RealType MeshRealType;
      typedef typename MeshType::DeviceType MeshDeviceType;
      typedef typename MeshType::IndexType MeshIndexType;
      typedef typename OutMeshFunction::Real RealType;
      typedef tnlOperatorFunction< Operator, Function > OperatorFunctionType;
      
      static void assign( OutMeshFunction& f1,
                          const InFunction& f2 )
      static void evaluateAllEntities( OutMeshFunction& meshFunction,
                                       const OperatorFunctionType& function,                          
                                       const RealType& time = 0.0,
                                       const RealType& outFunctionMultiplicator = 0.0,
                                       const RealType& inFunctionMultiplicator = 1.0 )
      {
         if( f1.getMesh().getDimensions() == f2.getMesh().getDimensions() )
            f1.getData() = f2.getData();
         else
         evaluateInteriorEntities( meshFunction, function, time, outFunctionMultiplicator, inFunctionMultiplicator );
      };
      
      static void evaluateInteriorEntities( OutMeshFunction& meshFunction,
                                            const OperatorFunctionType& function,                          
                                            const RealType& time = 0.0,
                                            const RealType& outFunctionMultiplicator = 0.0,
                                            const RealType& inFunctionMultiplicator = 1.0 );
      
      class TraverserUserData
      {
            //TODO: Interpolace
         }
         public:
            TraverserUserData( const OperatorFunctionType* operatorFunction,              
                               const RealType* time,
                               OutMeshFunction* meshFunction,
                               const RealType* outFunctionMultiplicator,
                               const RealType* inFunctionMultiplicator )
            : meshFunction( meshFunction ), operatorFunction( operatorFunction ),time( time ), 
              outFunctionMultiplicator( outFunctionMultiplicator ),
              inFunctionMultiplicator( inFunctionMultiplicator ){}

         protected:
            OutMeshFunction* meshFunction;            
            const OperatorFunctionType* operatorFunction;
            const RealType *time, *outFunctionMultiplicator, *inFunctionMultiplicator;
            
      };
};*/

};
#endif	/* TNLMESHFUNCTIONEVALUATOR_H */
+83 −11
Original line number Diff line number Diff line
@@ -18,6 +18,9 @@
#ifndef TNLMESHFUNCTIONEVALUATOR_IMPL_H
#define	TNLMESHFUNCTIONEVALUATOR_IMPL_H

#include <functions/tnlMeshFunctionEvaluator.h>
#include <mesh/tnlTraverser.h>

template< typename OutMeshFunction,
          typename InFunction >
void
@@ -63,12 +66,12 @@ template< typename OutMeshFunction,
          typename InFunction >
void
tnlMeshFunctionEvaluator< OutMeshFunction, InFunction >::
assign( OutMeshFunction& meshFunction,
evaluateEntities( OutMeshFunction& meshFunction,
                  const InFunction& function,
                  const RealType& time,
                  const RealType& outFunctionMultiplicator,
                  const RealType& inFunctionMultiplicator,
        EntitisType entitiesType )
                  EntitiesType entitiesType )
{
   typedef typename MeshType::template MeshEntities< meshEntityDimensions > MeshEntityType;
   
@@ -82,10 +85,9 @@ assign( OutMeshFunction& meshFunction,
      {
         typedef tnlFunctionAdapter< MeshType, InFunction > FunctionAdapter;
         ( *userData.meshFunction )( entity ) = 
               FunctionAdapter::getValue(
                  *userData.function,
                  entity,
                  *userData.time );
            *userData.outFunctionMultiplicator * ( *userData.meshFunction )( entity ) +
            *userData.inFunctionMultiplicator *
            FunctionAdapter::getValue( *userData.function, entity, *userData.time );
      }
   };

@@ -160,6 +162,76 @@ assign( OutMeshFunction& meshFunction,
}


template< typename OutMeshFunction,
          typename Function,
          typename Operator >
void
tnlMeshFunctionEvaluator< OutMeshFunction, tnlOperatorFunction< Operator, Function> >::
evaluateEntities( OutMeshFunction& meshFunction,
                  const OperatorFunctionType& operatorFunction,
                  const RealType& time,
                  const RealType& outFunctionMultiplicator,
                  const RealType& inFunctionMultiplicator )
{
   typedef typename MeshType::template MeshEntities< meshEntityDimensions > MeshEntityType;
   
   class AssignEntitiesProcessor
   {
      template< typename EntityType >
      __cuda_callable__
      static inline void processEntity( const MeshType& mesh,
                                        TraverserUserData& userData,
                                        const EntityType& entity )
      {
         typedef tnlFunctionAdapter< MeshType, InFunction > FunctionAdapter;
         ( *userData.meshFunction )( entity ) = 
            *userData.outFunctionMultiplicator * ( *userData.meshFunction )( entity ) +
            *userData.inFunctionMultiplicator *
            FunctionAdapter::getValue( *userData.function, entity, *userData.time );
      }
   };

   if( std::is_same< MeshDeviceType, tnlHost >::value )
   {
      TraverserUserData userData( &function, &time, &meshFunction, &outFunctionMultiplicator, &inFunctionMultiplicator );
      tnlTraverser< MeshType, MeshEntityType > meshTraverser;
      meshTraverser.template processInterirorEntities< TraverserUserData, AssignEntitiesProcessor >
         ( meshFunction.getMesh(),
           userData );
      
   }
   if( std::is_same< MeshDeviceType, tnlCuda >::value )
   {      
      OutMeshFunction* kernelMeshFunction = tnlCuda::passToDevice( meshFunction );
      Function* kernelFunction = tnlCuda::passToDevice( *operatorFunction.function );
      Operator* kernelOperator = tnlCuda::passToDevice( *operatorFunction.operator_ );
      OperatorFunctionType auxOperatorFunction( *kernelOperator, *kernelFunction );
      OperatorFunctionType* kernelOperatorFunction = tnlCuda::passToDevice( auxOperatorFunction );
      RealType* kernelTime = tnlCuda::passToDevice( time );
      RealType* kernelOutFunctionMultiplicator = tnlCuda::passToDevice( outFunctionMultiplicator );
      RealType* kernelInFunctionMultiplicator = tnlCuda::passToDevice( inFunctionMultiplicator );
      
      TraverserUserData userData( kernelOperatorFunction, kernelTime, kernelMeshFunction, kernelOutFunctionMultiplicator, kernelInFunctionMultiplicator );
      checkCudaDevice;
      tnlTraverser< MeshType, MeshEntityType > meshTraverser;
      meshTraverser.template processInteriorEntities< TraverserUserData, AssignEntitiesProcessor >
         ( meshFunction.getMesh(),
           userData );


      checkCudaDevice;      
      tnlCuda::freeFromDevice( kernelMeshFunction );
      tnlCuda::freeFromDevice( kernelFunction );
      tnlCuda::freeFromDevice( kernelOperator );
      tnlCuda::freeFromDevice( kernelOperatorFunction );
      tnlCuda::freeFromDevice( kernelTime );
      tnlCuda::freeFromDevice( kernelOutFunctionMultiplicator );
      tnlCuda::freeFromDevice( kernelInFunctionMultiplicator );
            
      checkCudaDevice;
   }
}


#endif	/* TNLMESHFUNCTIONEVALUATOR_IMPL_H */
+3 −38
Original line number Diff line number Diff line
@@ -218,18 +218,7 @@ tnlMeshFunction< Mesh, MeshEntityDimensions, Real >&
tnlMeshFunction< Mesh, MeshEntityDimensions, Real >::
operator = ( const Function& f )
{
   if( std::is_void< typename Function::DeviceType >::value ||
       std::is_same< typename Function::DeviceType, DeviceType >::value )
   {
      tnlFunctionEvaluator< ThisType, Function >  evaluator;
      evaluator.assignment( f, *this );
      return *this;
   }
   if( Function::getFunctionType() == MeshFunction )
   {
      tnlMeshFunctionEvaluator< ThisType, Function >::assign( f, *this );
      return *this;
   }
   tnlMeshFunctionEvaluator< ThisType, Function >::evaluateAllEntities( *this, f );
   return *this;
}

@@ -241,18 +230,7 @@ tnlMeshFunction< Mesh, MeshEntityDimensions, Real >&
tnlMeshFunction< Mesh, MeshEntityDimensions, Real >::
operator += ( const Function& f )
{
   if( std::is_void< typename Function::DeviceType >::value ||
       std::is_same< typename Function::DeviceType, DeviceType >::value )
   {
      tnlFunctionEvaluator< ThisType, Function >  evaluator;
      evaluator.assignment( f, *this, 1.0, 1.0 );
      return *this;
   }
   if( Function::getFunctionType() == MeshFunction )
   {
      tnlMeshFunctionEvaluator< ThisType, Function >::assign( f, *this );
      return *this;
   }
   tnlMeshFunctionEvaluator< ThisType, Function >::evaluateAllEntities( *this, f, 1.0, 1.0 );
   return *this;
}

@@ -264,23 +242,10 @@ tnlMeshFunction< Mesh, MeshEntityDimensions, Real >&
tnlMeshFunction< Mesh, MeshEntityDimensions, Real >::
operator -= ( const Function& f )
{
   if( std::is_void< typename Function::DeviceType >::value ||
       std::is_same< typename Function::DeviceType, DeviceType >::value )
   {
      tnlFunctionEvaluator< ThisType, Function >  evaluator;
      evaluator.assignment( f, *this );
      return *this;
   }
   if( Function::getFunctionType() == MeshFunction )
   {
      tnlMeshFunctionEvaluator< ThisType, Function >::assign( f, *this, 1.0, -1.0 );
      return *this;
   }
   tnlMeshFunctionEvaluator< ThisType, Function >::evaluateAllEntities( *this, f, 1.0, -1.0 );
   return *this;
}



template< typename Mesh,
          int MeshEntityDimensions,
          typename Real >
+9 −18
Original line number Diff line number Diff line
@@ -21,29 +21,25 @@
#include <type_traits>
#include <core/tnlCuda.h>

/***
 * This class evaluates given operator on given function. The image is defined only
 * for the interior mesh entities.
 */
template< typename Operator,
          typename Function,
          typename BoundaryConditions = Operator >
          typename Function >
class tnlOperatorFunction
{   
   //static_assert( std::is_same< typename Operator::MeshType, typename MeshFunction::MeshType >::value,
   //               "Operator and MeshFunction have different mesh type." );
   static_assert( std::is_same< typename Operator::MeshType, typename BoundaryConditions::MeshType >::value,
                  "Operator and BoundaryConditions have different mesh type." );
   
   public:
      
      typedef Operator OperatorType;
      typedef Function FunctionType;
      typedef BoundaryConditions BoundaryConditionsType;
      typedef typename OperatorType::RealType RealType;
      typedef typename OperatorType::DeviceType DeviceType;
      typedef typename OperatorType::IndexType IndexType;
      
      tnlOperatorFunction(
         const OperatorType& operator_,
         const FunctionType& function,
         const BoundaryConditionsType& boundaryConditions );
         const FunctionType& function );
      
      template< typename MeshEntity >
      __cuda_callable__
@@ -53,14 +49,9 @@ class tnlOperatorFunction
      
   protected:
      
      const Operator& operator_;
      
      const FunctionType& function;
      
      const BoundaryConditions& boundaryConditions;
      const Operator* operator_;
      
      const FunctionType* function;         
};


#endif	/* TNLOPERATORFUNCTION_H */
Loading