NumericIntegrationTest.cpp 8.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
//======================================================================================================================
//
//  This file is part of waLBerla. waLBerla 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 3 of
//  the License, or (at your option) any later version.
//
//  waLBerla is distributed in the hope that it will be useful, but WITHOUT
//  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
//  for more details.
//
//  You should have received a copy of the GNU General Public License along
//  with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
//
//! \file NumericItegrationTest.cpp
//! \ingroup mesh
//! \author Christian Godenschwager <christian.godenschwager@fau.de>
//
//======================================================================================================================

#include "core/debug/TestSubsystem.h"
#include "core/grid_generator/SCIterator.h"
#include "core/logging/Logging.h"
#include "core/math/AABB.h"
#include "core/math/KahanSummation.h"
#include "core/mpi/Environment.h"


#include "geometry/containment_octree/ContainmentOctree.h"
#include "geometry/mesh/TriangleMesh.h"
#include "geometry/mesh/TriangleMeshIO.h"

#include "mesh/MeshOperations.h"
#include "mesh/TriangleMeshes.h"
#include "mesh/DistanceComputations.h"
#include "mesh/MeshIO.h"
#include "mesh/distance_octree/DistanceOctree.h"


namespace walberla {
namespace mesh {

template< typename ContainmentT >
real_t volumeNumeric( const ContainmentT & body, const AABB & aabb, const real_t spacing )
{
   Vector3<real_t> pointOfReference = aabb.min() + Vector3<real_t>( real_t(0.5) * spacing );
   
   uint_t volume = 0;
   
   for(grid_generator::SCIterator it( aabb, pointOfReference, spacing ); it != grid_generator::SCIterator(); ++it)
   {
      if( body.contains( ContainmentT::toPoint( *it ) ) )
         ++volume;
   }

   return real_t(volume) * spacing * spacing * spacing;
}


template< typename ContainmentT >
Vector3<real_t> centroidNumeric( const ContainmentT & body, const AABB & aabb, const real_t spacing )
{
   Vector3<real_t> pointOfReference = aabb.min() + Vector3<real_t>( real_t(0.5) * spacing );

   math::KahanAccumulator<real_t> centroid[3];
   uint_t numPoints = 0;

   for(grid_generator::SCIterator it( aabb, pointOfReference, spacing ); it != grid_generator::SCIterator(); ++it)
   {
      if(body.contains( ContainmentT::toPoint( *it ) ))
      {
         centroid[0] += (*it)[0];
         centroid[1] += (*it)[1];
         centroid[2] += (*it)[2];
         ++numPoints;
      }
   }

   return Vector3<real_t>( centroid[0].get(), centroid[1].get(), centroid[2].get() ) / real_t(numPoints);
}


template< typename ContainmentT >
Matrix3<real_t> inertiaTensorNumeric( const ContainmentT & body, const AABB & aabb, const real_t spacing )
{
   Vector3<real_t> pointOfReference = aabb.min() + Vector3<real_t>( real_t(0.5) * spacing );

   math::KahanAccumulator<real_t> intertiaTensor[6];
   uint_t numPoints = 0;

   for(grid_generator::SCIterator it( aabb, pointOfReference, spacing ); it != grid_generator::SCIterator(); ++it)
   {
      if(body.contains( ContainmentT::toPoint( *it ) ))
      {
96 97 98 99
         const Vector3<real_t> p = *it;
         const real_t & x = p[0];
         const real_t & y = p[1];
         const real_t & z = p[2];
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

         intertiaTensor[0] += y*y + z*z;
         intertiaTensor[1] += -x*y;
         intertiaTensor[2] += -x*z;
         intertiaTensor[3] += x*x + z*z;
         intertiaTensor[4] += -y*z;
         intertiaTensor[5] += x*x + y*y;
         ++numPoints;
      }
   }

   return Matrix3<real_t>( intertiaTensor[0].get(), intertiaTensor[1].get(), intertiaTensor[2].get(),
                           intertiaTensor[1].get(), intertiaTensor[3].get(), intertiaTensor[4].get(),
                           intertiaTensor[2].get(), intertiaTensor[4].get(), intertiaTensor[5].get() ) * (spacing * spacing * spacing);
}


template< typename MeshType >
void testNumeric( const shared_ptr<MeshType> & mesh )
{
   auto triDist = make_shared< mesh::TriangleDistance<mesh::TriangleMesh> >( mesh );
   auto distanceOctree = make_shared< mesh::DistanceOctree<mesh::TriangleMesh> >( triDist );
   auto containmentOctree = make_shared< geometry::ContainmentOctree< mesh::DistanceOctree<mesh::TriangleMesh> > >( distanceOctree );

   AABB aabb = computeAABB(*mesh);
   uint_t numPoints = 1000000;
   real_t spacing = std::pow( aabb.volume() / real_t(numPoints), real_t(1) / real_t(3) );

   WALBERLA_LOG_INFO("Computing numeric volume");
   real_t numericVolume = volumeNumeric(*containmentOctree, aabb, spacing );
   WALBERLA_LOG_INFO("Computing geometrical volume");
   real_t geometricalVolume = computeVolume(*mesh);
   WALBERLA_LOG_INFO("Numerical volume:   " << numericVolume << "\n" <<
                     "Geometrical volume: " << geometricalVolume << "\n" <<
                     "Difference:         " << numericVolume - geometricalVolume );
   WALBERLA_CHECK( std::fabs( numericVolume - geometricalVolume ) < real_t(0.001) || std::fabs( real_t(1) - numericVolume / geometricalVolume ) < real_t(0.001) );


   WALBERLA_LOG_INFO("Computing numeric centroid");
   Vector3<real_t> numericCentroid = centroidNumeric(*containmentOctree, aabb, spacing );
   WALBERLA_LOG_INFO("Computing geometrical centroid");
   Vector3<real_t> geometricalCentroid = toWalberla( computeCentroid(*mesh) );
   WALBERLA_LOG_INFO("Numerical centroid:   " << numericCentroid << "\n" <<
                     "Geometrical centroid: " << geometricalCentroid << "\n" <<
                     "Difference:           " << numericCentroid - geometricalCentroid );

   WALBERLA_CHECK( std::fabs( numericCentroid[0] - geometricalCentroid[0] ) < real_t(0.001) || std::fabs( real_t(1) - numericCentroid[0] / geometricalCentroid[0] ) < real_t(0.001) );
   WALBERLA_CHECK( std::fabs( numericCentroid[0] - geometricalCentroid[0] ) < real_t(0.001) || std::fabs( real_t(1) - numericCentroid[1] / geometricalCentroid[1] ) < real_t(0.001) );
   WALBERLA_CHECK( std::fabs( numericCentroid[0] - geometricalCentroid[0] ) < real_t(0.001) || std::fabs( real_t(1) - numericCentroid[2] / geometricalCentroid[2] ) < real_t(0.001) );

   WALBERLA_LOG_INFO("Computing numeric intertia tensor");
   Matrix3<real_t> numericTensor = inertiaTensorNumeric(*containmentOctree, aabb, spacing );
   WALBERLA_LOG_INFO("Computing geometrical intertia tensor");
   Matrix3<real_t> geometricalTensor = computeIntertiaTensor(*mesh);
   WALBERLA_LOG_INFO("Numerical tensor:\n"   << numericTensor << "\n" <<
                     "Geometrical tensor:\n" << geometricalTensor << "\n" <<
                     "Difference:\n"         << numericTensor - geometricalTensor );

   WALBERLA_CHECK( std::fabs( numericTensor[0] - geometricalTensor[0] ) < real_t(0.001) || std::fabs( real_t(1) - numericTensor[0] / geometricalTensor[0] ) < real_t(0.001) );
   WALBERLA_CHECK( std::fabs( numericTensor[1] - geometricalTensor[1] ) < real_t(0.001) || std::fabs( real_t(1) - numericTensor[1] / geometricalTensor[1] ) < real_t(0.001) );
   WALBERLA_CHECK( std::fabs( numericTensor[2] - geometricalTensor[2] ) < real_t(0.001) || std::fabs( real_t(1) - numericTensor[2] / geometricalTensor[2] ) < real_t(0.001) );
   WALBERLA_CHECK( std::fabs( numericTensor[3] - geometricalTensor[3] ) < real_t(0.001) || std::fabs( real_t(1) - numericTensor[3] / geometricalTensor[3] ) < real_t(0.001) );
   WALBERLA_CHECK( std::fabs( numericTensor[4] - geometricalTensor[4] ) < real_t(0.001) || std::fabs( real_t(1) - numericTensor[4] / geometricalTensor[4] ) < real_t(0.001) );
   WALBERLA_CHECK( std::fabs( numericTensor[5] - geometricalTensor[5] ) < real_t(0.001) || std::fabs( real_t(1) - numericTensor[5] / geometricalTensor[5] ) < real_t(0.001) );
   WALBERLA_CHECK( std::fabs( numericTensor[6] - geometricalTensor[6] ) < real_t(0.001) || std::fabs( real_t(1) - numericTensor[6] / geometricalTensor[6] ) < real_t(0.001) );
   WALBERLA_CHECK( std::fabs( numericTensor[7] - geometricalTensor[7] ) < real_t(0.001) || std::fabs( real_t(1) - numericTensor[7] / geometricalTensor[7] ) < real_t(0.001) );
   WALBERLA_CHECK( std::fabs( numericTensor[8] - geometricalTensor[8] ) < real_t(0.001) || std::fabs( real_t(1) - numericTensor[8] / geometricalTensor[8] ) < real_t(0.001) );
}


int main( int argc, char * argv[] )
{
   debug::enterTestMode();
   mpi::Environment mpiEnv( argc, argv );
   mpi::MPIManager::instance()->useWorldComm();

   std::vector<std::string> args( argv, argv + argc );
   if(args.size() != size_t( 2 ))
   {
      WALBERLA_ABORT_NO_DEBUG_INFO( "USAGE:\n" << args[0] << "<MeshFileName>" );
   }

   auto mesh = make_shared< mesh::TriangleMesh >();
   mesh::readAndBroadcast(args[1], *mesh);

   testNumeric(mesh);

   return EXIT_SUCCESS;
}


} // namespace mesh
} // namespace walberla

int main( int argc, char * argv[] )
{
   return walberla::mesh::main( argc, argv );
197
}