<commit_before>/****************************************************************************
 * Copyright (c) 2012-2018 by the DataTransferKit authors                   *
 * All rights reserved.                                                     *
 *                                                                          *
 * This file is part of the DataTransferKit library. DataTransferKit is     *
 * distributed under a BSD 3-clause license. For the licensing terms see    *
 * the LICENSE file in the top-level directory.                             *
 *                                                                          *
 * SPDX-License-Identifier: BSD-3-Clause                                    *
 ****************************************************************************/

#include <Teuchos_UnitTestHarness.hpp>

#include <DTK_DBC.hpp> // DataTransferKitException
#include <DTK_NearestNeighborOperator.hpp>
#include <Kokkos_Core.hpp>
#include <Teuchos_DefaultComm.hpp>
#include <Tpetra_CrsMatrix.hpp>
#include <Tpetra_Distributor.hpp>
#include <Tpetra_Map.hpp>

#include <array>
#include <numeric>
#include <random>
#include <vector>

std::vector<std::array<double, 3>>
makeStructuredCloud( double Lx, double Ly, double Lz, int nx, int ny, int nz,
                     double ox = 0., double oy = 0., double oz = 0. )
{
    std::vector<std::array<double, 3>> cloud( nx * ny * nz );
    std::function<int( int, int, int )> ind = [nx, ny]( int i, int j, int k ) {
        return i + j * nx + k * ( nx * ny );
    };
    double x, y, z;
    for ( int i = 0; i < nx; ++i )
        for ( int j = 0; j < ny; ++j )
            for ( int k = 0; k < nz; ++k )
            {
                x = ox + i * Lx / nx;
                y = oy + j * Ly / ny;
                z = oz + k * Lz / nz;
                cloud[ind( i, j, k )] = {{x, y, z}};
            }
    return cloud;
}

std::vector<std::array<double, 3>>
makeRandomCloud( double Lx, double Ly, double Lz, int n, double seed = 0. )
{
    std::vector<std::array<double, 3>> cloud( n );
    std::default_random_engine generator( seed );
    std::uniform_real_distribution<double> distributionx( 0.0, Lx );
    std::uniform_real_distribution<double> distributiony( 0.0, Ly );
    std::uniform_real_distribution<double> distributionz( 0.0, Lz );
    for ( int i = 0; i < n; ++i )
    {
        double x = distributionx( generator );
        double y = distributiony( generator );
        double z = distributionz( generator );
        cloud[i] = {{x, y, z}};
    }
    return cloud;
}

template <typename DeviceType>
void copyPointsFromCloud( std::vector<std::array<double, 3>> const &cloud,
                          Kokkos::View<double **, DeviceType> &points )
{
    int const n_points = cloud.size();
    int const spatial_dim = 3;
    Kokkos::realloc( points, n_points, spatial_dim );
    auto points_host = Kokkos::create_mirror_view( points );
    for ( int i = 0; i < n_points; ++i )
        for ( int d = 0; d < spatial_dim; ++d )
            points_host( i, d ) = cloud[i][d];
    Kokkos::deep_copy( points, points_host );
}

TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( NearestNeighborOperator, unique_source_point,
                                   DeviceType )
{
    Teuchos::RCP<const Teuchos::Comm<int>> comm =
        Teuchos::DefaultComm<int>::getComm();
    int const comm_size = comm->getSize();
    int const comm_rank = comm->getRank();

    int const space_dim = 3;

    // Build structured cloud of points for the source and random cloud for the
    // target.
    Kokkos::View<double **, DeviceType> source_points( "source" );

    Kokkos::View<double **, DeviceType> target_points( "target", 1, space_dim );

    TEST_THROW( DataTransferKit::NearestNeighborOperator<DeviceType>(
                    comm, source_points, target_points ),
                DataTransferKit::DataTransferKitException );
<commit_msg>Comment on contract violation in NNOp constructor test<commit_after>/****************************************************************************
 * Copyright (c) 2012-2018 by the DataTransferKit authors                   *
 * All rights reserved.                                                     *
 *                                                                          *
 * This file is part of the DataTransferKit library. DataTransferKit is     *
 * distributed under a BSD 3-clause license. For the licensing terms see    *
 * the LICENSE file in the top-level directory.                             *
 *                                                                          *
 * SPDX-License-Identifier: BSD-3-Clause                                    *
 ****************************************************************************/

#include <Teuchos_UnitTestHarness.hpp>

#include <DTK_DBC.hpp> // DataTransferKitException
#include <DTK_NearestNeighborOperator.hpp>
#include <Kokkos_Core.hpp>
#include <Teuchos_DefaultComm.hpp>
#include <Tpetra_CrsMatrix.hpp>
#include <Tpetra_Distributor.hpp>
#include <Tpetra_Map.hpp>

#include <array>
#include <numeric>
#include <random>
#include <vector>

std::vector<std::array<double, 3>>
makeStructuredCloud( double Lx, double Ly, double Lz, int nx, int ny, int nz,
                     double ox = 0., double oy = 0., double oz = 0. )
{
    std::vector<std::array<double, 3>> cloud( nx * ny * nz );
    std::function<int( int, int, int )> ind = [nx, ny]( int i, int j, int k ) {
        return i + j * nx + k * ( nx * ny );
    };
    double x, y, z;
    for ( int i = 0; i < nx; ++i )
        for ( int j = 0; j < ny; ++j )
            for ( int k = 0; k < nz; ++k )
            {
                x = ox + i * Lx / nx;
                y = oy + j * Ly / ny;
                z = oz + k * Lz / nz;
                cloud[ind( i, j, k )] = {{x, y, z}};
            }
    return cloud;
}

std::vector<std::array<double, 3>>
makeRandomCloud( double Lx, double Ly, double Lz, int n, double seed = 0. )
{
    std::vector<std::array<double, 3>> cloud( n );
    std::default_random_engine generator( seed );
    std::uniform_real_distribution<double> distributionx( 0.0, Lx );
    std::uniform_real_distribution<double> distributiony( 0.0, Ly );
    std::uniform_real_distribution<double> distributionz( 0.0, Lz );
    for ( int i = 0; i < n; ++i )
    {
        double x = distributionx( generator );
        double y = distributiony( generator );
        double z = distributionz( generator );
        cloud[i] = {{x, y, z}};
    }
    return cloud;
}

template <typename DeviceType>
void copyPointsFromCloud( std::vector<std::array<double, 3>> const &cloud,
                          Kokkos::View<double **, DeviceType> &points )
{
    int const n_points = cloud.size();
    int const spatial_dim = 3;
    Kokkos::realloc( points, n_points, spatial_dim );
    auto points_host = Kokkos::create_mirror_view( points );
    for ( int i = 0; i < n_points; ++i )
        for ( int d = 0; d < spatial_dim; ++d )
            points_host( i, d ) = cloud[i][d];
    Kokkos::deep_copy( points, points_host );
}

TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( NearestNeighborOperator, unique_source_point,
                                   DeviceType )
{
    Teuchos::RCP<const Teuchos::Comm<int>> comm =
        Teuchos::DefaultComm<int>::getComm();
    int const comm_size = comm->getSize();
    int const comm_rank = comm->getRank();

    int const space_dim = 3;

    // Build structured cloud of points for the source and random cloud for the
    // target.
    Kokkos::View<double **, DeviceType> source_points( "source" );

    Kokkos::View<double **, DeviceType> target_points( "target", 1, space_dim );

    // violate pre condition that source cloud is not empty
    TEST_THROW( DataTransferKit::NearestNeighborOperator<DeviceType>(
                    comm, source_points, target_points ),
                DataTransferKit::DataTransferKitException );

{'fraction_non_alphanumeric': 0.15599239061509196, 'fraction_numerical': 0.008877615726062143, 'mean_word_length': 2.2372742200328406, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5522297', 'n_tokens_mistral': 2435, 'n_tokens_neox': 2321, 'n_words': 660}