Skip to content

Commit

Permalink
Merge pull request #240 from ngs333/cpp_dev
Browse files Browse the repository at this point in the history
Merge from ngs333 search branch - Ctest related
  • Loading branch information
ngs333 authored Aug 28, 2023
2 parents efb6474 + 84144b7 commit f33420f
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 35 deletions.
30 changes: 18 additions & 12 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,26 @@ project(fregrid LANGUAGES CXX)
set(CMAKE_VERBOSE_MAKEFILE ON)

##TODO: Upgrade to C++23 as soon as enough features are
# available in enough compilers.
# available in enough compilers that are being used.
set(CMAKE_CXX_STANDARD 20)

# These include directories below are only temporary as
# several C++23 lib features are being brought in for use
# with C++20 compilers. Some of them are purely in the "/include"
# directory, but the project source directory is also set as an
# include directory because of the way the format lib was created.
add_subdirectory(dependencies)

#Testing:
#NOTE: The inital framework are Catch2 + ApprovalTest. This should provide easy
# unit testing, acceptance testing, and micro benchmarking. However, it may be
# convenient to directly use and add cmakes Catch via "include(Catch)". E.g.
# can nccmp be directly called?"
enable_testing()
add_subdirectory(test)


# The " {CMAKE_SOURCE_DIR}/include" directories below is only temporary as
# several C++23 lib features are being brought in for use with C++20 compilers.
# Some of them are purely in the "/include" directory, but the project source
# directory is also set as an include directory because of the way the format
# lib was created. The other possibility may be to add them (temporarily) in
# the dependencies CMaleList.txt.
include_directories(${CMAKE_SOURCE_DIR})
include_directories(${CMAKE_SOURCE_DIR}/include)
#Perhaps will need to make it compiler dependent:
Expand All @@ -29,18 +41,12 @@ option(USE_NEXTAFTER "Use nextafter expanded bounding intervals" OFF)
#link_directories(/home/mzuniga/NetCdCPP/install/lib)
link_directories(/usr/lib/x86_64-linux-gnu)

##$nc-config --cflags
##-I/usr/include -I/usr/include/hdf5/serial
##$nc-config --libs
##-L/usr/lib/x86_64-linux-gnu -L/usr/lib/x86_64-linux-gnu/hdf5/serial -lnetcdf

#find_package(NetCDF REQUIRED COMPONENTS CPP)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/../bin")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/../bin")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/../bin")


add_subdirectory(common)
add_subdirectory(search)
add_subdirectory(libfrencutils)
Expand Down
42 changes: 42 additions & 0 deletions cpp/dependencies/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# https://approvaltestscpp.readthedocs.io/en/latest/generated_docs/CMakeIntegration.html#make-cmake-clone-approvaltests-cpp-and-catch2

# Needs CMake 3.14 or above
Include(FetchContent)

# Names of targets added here:
# ghc_filesystem Catch2::Catch2 ApprovalTests::ApprovalTests fmt::fmt
#TODO: update git tags to latest stables?

# -------------------------------------------------------------------
FetchContent_Declare(Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.4.0)
FetchContent_MakeAvailable(Catch2)
### v2.13.4? devel? 3.3.3 (latest of 3.)

# -------------------------------------------------------------------
FetchContent_Declare(ApprovalTests
GIT_REPOSITORY https://github.com/approvals/ApprovalTests.cpp.git
GIT_TAG master)
FetchContent_MakeAvailable(ApprovalTests)

# -------------------------------------------------------------------
## The format libray was instead brought in with the rest of the C++23 features,
## and added to the std namespace. See the toplevel CMakeLists file.
#FetchContent_Declare(fmt
# GIT_REPOSITORY https://github.com/fmtlib/fmt.git
# GIT_TAG 7.1.3)
#FetchContent_MakeAvailable(fmt)

#FetchContent_Declare(fmt
# GIT_REPOSITORY https://github.com/fmtlib/fmt.git
# GIT_TAG 10.0.0
# SYSTEM
# OVERRIDE_FIND_PACKAGE)
#FetchContent_MakeAvailable(fmt)

# -------------------------------------------------------------------
FetchContent_Declare(filesystem
GIT_REPOSITORY https://github.com/gulrak/filesystem.git
GIT_TAG v1.5.0)
FetchContent_MakeAvailable(filesystem)
4 changes: 2 additions & 2 deletions cpp/fregrid/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ include_directories(/home/mzuniga/llvm/build/install/include)
include_directories(/home/mzuniga/llvm/build/install/include/sycl)
include_directories(/home/mzuniga/llvm/build/install/include/sycl/CL)



link_directories(${CMAKE_LIBRARY_OUTPUT_DIRECTORY})

add_executable(fregrid.x fregrid.C conserve_interp.C fregrid_util.C bilinear_interp.C)
# ../test/main.C
# ../test/integration_tests.C)
#add_sycl_to_target(TARGET fregrid.x SOURCES fregrid.C)

add_executable(searchtest.x searchtest.C)
Expand Down
3 changes: 2 additions & 1 deletion cpp/fregrid/searchtest.C
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const int NY{100};
const int NZ{4};

//constexpr long long size = 1'000'000'000;
//TODO: this will be replaced by unit or functionality tests?

using namespace std;
using namespace std::chrono;
Expand All @@ -38,7 +39,7 @@ void runAndTime(const std::string& name, Func func){
const auto start = high_resolution_clock::now();
func();
//const std::chrono::duration<double> dur = std::chrono::steady_clock::now() - sta;
const auto dt = duration_cast<microseconds>( high_resolution_clock::now() - start);
const auto dt = duration_cast<microseconds>( high_resolution_clock::now() - start);
cout << "For " << name << " time is : " << (dt.count() / 1.0e6) << " sec. " << endl;
}

Expand Down
5 changes: 3 additions & 2 deletions cpp/libfrencutils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

add_library(libFrencu create_xgrid.C gradient_c2l.C interp.C mosaic_util.C
mpp.C mpp_domain.C mpp_io.C read_mosaic.C tool_util.C config.h
create_xgrid_gpu.C)
create_xgrid_gpu.C
create_xgrid_aux.h)

#add_sycl_to_target(TARGET libFrencu SOURCES create_xgrid.C ) #may need other .C files later
#TODO: dd_sycl_to_target(TARGET libFrencu SOURCES create_xgrid.C ) #may need other .C files later
add_dependencies(libFrencu libSearch)
target_link_libraries(libFrencu libSearch)
16 changes: 1 addition & 15 deletions cpp/libfrencutils/create_xgrid.C
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,6 @@ int line_intersect_2D_3D(double *a1, double *a2, double *q1, double *q2, double
double *intersect, double *u_a, double *u_q, int *inbound);


/*******************************************************************************
int get_maxxgrid
return constants MAXXGRID.
*******************************************************************************/
int get_maxxgrid(void)
{
return MAXXGRID;
}

int get_maxxgrid_(void)
{
return get_maxxgrid();
}

/*******************************************************************************
void get_grid_area(const int *nlon, const int *nlat, const double *lon, const double *lat, const double *area)
return the grid area.
Expand Down Expand Up @@ -2451,7 +2437,7 @@ size_t latlons_outside_ccd_domain(const unsigned int NV4, const double *yv, dou
* @return
*/
BBox_t getBoxForSphericalPolygon(const double lat_m[], const double lon_m[],
const array<size_t, 4> &is, bool debugf = false) {
const array<size_t, 4> &is, bool debugf) {
constexpr unsigned int NV4{4};
// xlons are the longitudes that define the X=0 and Y=0 planes(see ll2xyz function)
// where its possible to have an extrema in X or Y when an edge crosses them.
Expand Down
11 changes: 11 additions & 0 deletions cpp/libfrencutils/create_xgrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
#define MAXXGRID 1e7
#endif

#include <array>
#include "BBox3D.h"
#define MV 50
#include <vector>
#include <span>

/* this value is small compare to earth area */

double poly_ctrlon(const double lon[], const double lat[], int n, double clon);
Expand Down Expand Up @@ -74,6 +78,9 @@ int create_xgrid_great_circle(const int *nlon_in, const int *nlat_in, const int
const double *mask_in, int *i_in, int *j_in, int *i_out, int *j_out,
double *xgrid_area, double *xgrid_clon, double *xgrid_clat);

void latlon2xyz(const double lat, const double lon, std::array<double,3> & v);
nct::BBox3D getBoxForSphericalPolygon(const double lat_m[], const double lon_m[],
const std::array<size_t, 4> &is, bool debugf = false);
int search_grids(const int nlon_in, const int nlat_in, const int nlon_out, const int nlat_out,
const double *lon_in, const double *lat_in, const double *lon_out, const double *lat_out,
const double *mask_in, std::vector<std::vector<size_t>> & results1) ;
Expand All @@ -93,4 +100,8 @@ void create_xgrid_2dx2d_order2_check(const int nlon_in, const int nlat_in, const
std::vector<size_t>& i_out_r, std::vector<size_t>& j_out_r,
std::vector<double>& xgrid_area_r);

template<class T>
void printPolygon(std::ostream &os, std::span<T> lonv, std::span<T> latv) ;


#endif
48 changes: 48 additions & 0 deletions cpp/libfrencutils/create_xgrid_aux.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

#ifndef FREGRID_CREATE_XGRID_AUX_H
#define FREGRID_CREATE_XGRID_AUX_H

#include <cmath>
#include <vector>
#include <array>
#include <algorithm>
#include <span>
#include <source_location>
#include "BBox3D.h"
#include "Polygon.h"
#include "mosaic_util.h"
#include "create_xgrid.h"

bool checkBBoxViaPolySamples(std::span<double> yv, std::span<double> xv,
nct::BBox3D & box, unsigned int npoints1D = 5 ) {
bool passed = true;
std::array<double, 3> pt{};
const auto [miny_it, maxy_it] = std::minmax_element(begin(yv), end(yv));
const double miny = *miny_it;
const double maxy = *maxy_it;
const auto [minx_it, maxx_it] = std::minmax_element(begin(xv), end(xv));
const double minx = *minx_it;
const double maxx = *maxx_it;

const unsigned int NDX{npoints1D};
const unsigned int NDY{npoints1D};
const double dy = (maxy - miny) / NDY;
const double dx = (maxx - minx) / NDX;
for (int i = 0; i < NDX; ++i) {
auto ax = minx + i * dx;
for (int j = 0; j < NDY; ++j) {
latlon2xyz(miny + j * dy, ax, pt);
auto contains_point = nct::BBox3D::contains(box, pt);
if (!contains_point) {
passed = false;
//TODO: the polygon and the box can be saved to strstream
printPolygon<double>(std::cout, xv, yv);
std::cout << box << std::endl;
auto str = std::format("< {:16.10e}, {:16.10e}, {:16.10e}>", pt[0],pt[1],pt[2]);
std::cout << str <<std::endl;
}
}
}
return passed;
}
#endif //FREGRID_CREATE_XGRID_AUX_H
8 changes: 5 additions & 3 deletions cpp/search/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

add_library(libSearch INTERFACE Polygon.h Point3D.h BruteBoxQuery.h DITreeNode.h DistanceInterval.h DITree.h
Partition.h Comparators.h BoxedObj.h BBox3D.h TreePerfStats.h)
target_include_directories(libSearch INTERFACE ${CMAKE_SOURCE_DIR}/search)
add_library(libSearch INTERFACE )
target_include_directories(libSearch INTERFACE .)



14 changes: 14 additions & 0 deletions cpp/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

add_executable(bbox_tests bbox_tests.C)

include_directories(${CMAKE_SOURCE_DIR})
include_directories(${CMAKE_SOURCE_DIR}/include)
include_directories(include)

#find_package(fmt REQUIRED)

target_link_libraries(bbox_tests libFrencu Catch2WithMain tbb)
include_directories(${CMAKE_SOURCE_DIR}/libfrencutils)



47 changes: 47 additions & 0 deletions cpp/test/bbox_tests.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <array>
#include <sstream>
#include <string>
// #include <fmt/format.h>
//#include <format>

#include <catch2/catch_test_macros.hpp>
//#include "ApprovalTests.hpp"

#include "constant.h"
#include "create_xgrid.h"
#include "create_xgrid_aux.h"

TEST_CASE("TEST SIMPLE_POLY BOX")
{
std::array<size_t,4> is {0,1,2,3};
//std::vector<double> lats{88.98,88.98, 88.72, 88.72 };
// std::vector<double> lons{ 251.7, 148.3,57.81, 342.2 };
std::vector<double> lats{0,1, 0, 1 };
std::vector<double> lons{ 44, 45, 46, 45};
for(int i = 0; i<4; i++){
lats[i] = lats[i] * D2R;
lons[i] = lons[i] * D2R;
}
auto box = getBoxForSphericalPolygon(lats.data(), lons.data(), is);
bool passed = checkBBoxViaPolySamples({lats.data(),4}, {lons.data(),4}, box, 5);

REQUIRE( passed == true );
}


TEST_CASE("TEST NIKI_POLY BOX")
{
std::array<size_t,4> is {0,1,2,3};
std::vector<double> lats{88.98,88.98, 88.72, 88.72 };
std::vector<double> lons{ 251.7, 148.3,57.81, 342.2 };
for(int i = 0; i<4; i++){
lats[i] = lats[i] * D2R;
lons[i] = lons[i] * D2R;
}
auto box = getBoxForSphericalPolygon(lats.data(), lons.data(), is);
bool passed = checkBBoxViaPolySamples({lats.data(),4}, {lons.data(),4}, box, 5);

REQUIRE( passed == true );
}

//TODO: Many More Tests
2 changes: 2 additions & 0 deletions cpp/test/main.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch_all.hpp>

0 comments on commit f33420f

Please sign in to comment.