diff --git a/.gitignore b/.gitignore index 3c6ecf6ece8e10520e37bd5f305a533e28976e33..2a16a4a681b86d30940001fb2ce86dfb9d27a558 100644 --- a/.gitignore +++ b/.gitignore @@ -73,6 +73,9 @@ tests/testRiemannExact tests/testRiemannTRRS tests/testRiemannHLLC tests/testMatrixInversion +tests/testVoronoi1D +tests/testVoronoi2D +tests/testVoronoi3D tests/testDump tests/testLogger tests/benchmarkInteractions diff --git a/configure.ac b/configure.ac index 9a7f2ba702151a27f2ae702756cfb3c3bc95bcf3..ec780d1f8d1114e2d42049cea46488535c487a18 100644 --- a/configure.ac +++ b/configure.ac @@ -460,7 +460,6 @@ fi # Check for HDF5. This is required. AX_LIB_HDF5 - if test "$with_hdf5" != "yes"; then AC_MSG_ERROR([Could not find a working HDF5 library]) fi @@ -468,8 +467,6 @@ fi # We want to know if this HDF5 supports MPI and whether we should use it. # The default is to use MPI support if it is available, i.e. this is # a parallel HDF5. -# To do this need to ask the HDF5 compiler about its configuration, -# -showconfig should have yes/no. have_parallel_hdf5="no" if test "$with_hdf5" = "yes"; then AC_ARG_ENABLE([parallel-hdf5], @@ -482,7 +479,14 @@ if test "$with_hdf5" = "yes"; then if test "$enable_parallel_hdf5" = "yes"; then AC_MSG_CHECKING([for HDF5 parallel support]) - parallel=`$H5CC -showconfig | grep "Parallel HDF5:" | awk '{print $3}'` +# Check if the library is capable, the header should define H5_HAVE_PARALLEL. + + AC_COMPILE_IFELSE([AC_LANG_SOURCE([[ + #include "hdf5.h" + #ifndef H5_HAVE_PARALLEL + # error macro not defined + #endif + ]])], [parallel="yes"], [parallel="no"]) if test "$parallel" = "yes"; then have_parallel_hdf5="yes" AC_DEFINE([HAVE_PARALLEL_HDF5],1,[HDF5 library supports parallel access]) @@ -583,7 +587,7 @@ fi # Hydro scheme. AC_ARG_WITH([hydro], [AS_HELP_STRING([--with-hydro=<scheme>], - [Hydro dynamics to use @<:@gadget2, minimal, hopkins, default, gizmo default: gadget2@:>@] + [Hydro dynamics to use @<:@gadget2, minimal, hopkins, default, gizmo, shadowfax default: gadget2@:>@] )], [with_hydro="$withval"], [with_hydro="gadget2"] @@ -604,6 +608,9 @@ case "$with_hydro" in gizmo) AC_DEFINE([GIZMO_SPH], [1], [GIZMO SPH]) ;; + shadowfax) + AC_DEFINE([SHADOWFAX_SPH], [1], [Shadowfax SPH]) + ;; *) AC_MSG_ERROR([Unknown hydrodynamics scheme: $with_hydro]) diff --git a/m4/ax_lib_hdf5.m4 b/m4/ax_lib_hdf5.m4 index b0d04aa0bd64e0fa1f41ed20cd17b5957287038b..68528b15c083592cf76d4deb9a246b75cb2bb1c6 100644 --- a/m4/ax_lib_hdf5.m4 +++ b/m4/ax_lib_hdf5.m4 @@ -22,7 +22,10 @@ # yes - do check for HDF5 library in standard locations. # path - complete path to the HDF5 helper script h5cc or h5pcc. # -# SWIFT modification: HDF5 is required, so only path is described. +# SWIFT modifications: HDF5 is required, so only path is described, +# when the h5cc or h5pcc commands are not available, we check if +# HDF5 can be used anyway and no macros are defined except HAVE_HDF5 +# and with_hdf5. # # If HDF5 is successfully found, this macro calls # @@ -155,11 +158,37 @@ if test "$with_hdf5" = "yes"; then AC_MSG_CHECKING([Using provided HDF5 C wrapper]) AC_MSG_RESULT([$H5CC]) fi - AC_MSG_CHECKING([for HDF5 libraries]) if test ! -f "$H5CC" || test ! -x "$H5CC"; then - AC_MSG_RESULT([no]) - AC_MSG_WARN(m4_case(m4_normalize([$1]), - [serial], [ + +dnl Check if we already have HDF5 for C. + AC_CHECK_HEADER([hdf5.h], [ac_cv_hhdf5_h=yes], [ac_cv_hhdf5_h=no], [AC_INCLUDES_DEFAULT]) + AC_CHECK_LIB([hdf5], [H5Fcreate], [ac_cv_libhdf5=yes], + [ac_cv_libhdf5=no]) + if test "$ac_cv_hhdf5_h" = "yes" && test "$ac_cv_libhdf5" = "yes" ; then + +dnl Can compile and link, so we have a HDF5, just don't know which version. + AC_MSG_CHECKING([for HDF5 libraries]) + AC_MSG_RESULT([yes]) + with_hdf5="yes" + HDF5_VERSION="unknown" + HDF5_LIBS="-lhdf5" + AC_SUBST([HDF5_VERSION]) + AC_SUBST([HDF5_CC]) + AC_SUBST([HDF5_CFLAGS]) + AC_SUBST([HDF5_CPPFLAGS]) + AC_SUBST([HDF5_LDFLAGS]) + AC_SUBST([HDF5_LIBS]) + AC_SUBST([HDF5_FC]) + AC_SUBST([HDF5_FFLAGS]) + AC_SUBST([HDF5_FLIBS]) + AC_DEFINE([HAVE_HDF5], [1], [Defined if you have HDF5 support]) + else + +dnl Time to give up. + AC_MSG_CHECKING([for HDF5 libraries]) + AC_MSG_RESULT([no]) + AC_MSG_WARN(m4_case(m4_normalize([$1]), + [serial], [ Unable to locate serial HDF5 compilation helper script 'h5cc'. Please specify --with-hdf5=<LOCATION> as the full path to h5cc. HDF5 support is being disabled. @@ -172,9 +201,12 @@ Unable to locate HDF5 compilation helper scripts 'h5cc' or 'h5pcc'. Please specify --with-hdf5=<LOCATION> as the full path to h5cc or h5pcc. HDF5 support is being disabled. ])) - with_hdf5="no" - with_hdf5_fortran="no" + with_hdf5="no" + with_hdf5_fortran="no" + fi else + AC_MSG_CHECKING([for HDF5 libraries]) + dnl Get the h5cc output HDF5_SHOW=$(eval $H5CC -show) diff --git a/src/Makefile.am b/src/Makefile.am index dffe1861bb68fe3c59c89a7b4ee435b0b6c46576..e95a00125e3c86ec32d3ae632065fd1d071877ac 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -46,7 +46,7 @@ include_HEADERS = space.h runner.h queue.h task.h lock.h cell.h part.h const.h \ hydro_properties.h riemann.h threadpool.h cooling.h cooling_struct.h sourceterms.h \ sourceterms_struct.h statistics.h memswap.h cache.h runner_doiact_vec.h profiler.h \ dump.h logger.h active.h timeline.h xmf.h gravity_properties.h gravity_derivatives.h \ - vector_power.h + vector_power.h hydro_space.h # Common source files AM_SOURCES = space.c runner.c queue.c task.c cell.c engine.c \ @@ -56,7 +56,8 @@ AM_SOURCES = space.c runner.c queue.c task.c cell.c engine.c \ physical_constants.c potential.c hydro_properties.c \ runner_doiact_fft.c threadpool.c cooling.c sourceterms.c \ statistics.c runner_doiact_vec.c profiler.c dump.c logger.c \ - part_type.c xmf.c gravity_properties.c gravity.c + part_type.c xmf.c gravity_properties.c gravity.c \ + hydro_space.c # Include files for distribution, not installation. nobase_noinst_HEADERS = align.h approx_math.h atomic.h cycle.h error.h inline.h kernel_hydro.h kernel_gravity.h \ diff --git a/src/const.h b/src/const.h index 5ddbc3f2942c6c0887f74326f1a5ed3933448fd6..6962ee8bca32e92664e3f20cdb23e7cb6fbc4abd 100644 --- a/src/const.h +++ b/src/const.h @@ -54,6 +54,23 @@ //#define GIZMO_FIX_PARTICLES //#define GIZMO_TOTAL_ENERGY +/* Types of gradients to use for SHADOWFAX_SPH */ +/* If no option is chosen, no gradients are used (first order scheme) */ +#define SHADOWFAX_GRADIENTS + +/* SHADOWFAX_SPH slope limiters */ +#define SHADOWFAX_SLOPE_LIMITER_PER_FACE +#define SHADOWFAX_SLOPE_LIMITER_CELL_WIDE + +/* Options to control SHADOWFAX_SPH */ +/* This option disables cell movement */ +//#define SHADOWFAX_FIX_CELLS +/* This option enables cell steering, i.e. trying to keep the cells regular by + adding a correction to the cell velocities.*/ +#define SHADOWFAX_STEER_CELL_MOTION +/* This option evolves the total energy instead of the thermal energy */ +//#define SHADOWFAX_TOTAL_ENERGY + /* Source terms */ #define SOURCETERMS_NONE //#define SOURCETERMS_SN_FEEDBACK diff --git a/src/debug.c b/src/debug.c index f5f2f4974a6f2d0e8da8fce71e98233a2ed3deeb..3732ee5e769277deb393926ea2dc6f04fba93782 100644 --- a/src/debug.c +++ b/src/debug.c @@ -49,6 +49,8 @@ #include "./hydro/Default/hydro_debug.h" #elif defined(GIZMO_SPH) #include "./hydro/Gizmo/hydro_debug.h" +#elif defined(SHADOWFAX_SPH) +#include "./hydro/Shadowswift/hydro_debug.h" #else #error "Invalid choice of SPH variant" #endif diff --git a/src/hydro.h b/src/hydro.h index 3dce6df074767c15828c3a0c9eec738b32b5d7a3..abb49d35b204bbaf986f502d796883e7eb778e7f 100644 --- a/src/hydro.h +++ b/src/hydro.h @@ -47,6 +47,11 @@ #include "./hydro/Gizmo/hydro.h" #include "./hydro/Gizmo/hydro_iact.h" #define SPH_IMPLEMENTATION "GIZMO (Hopkins 2015)" +#elif defined(SHADOWFAX_SPH) +#include "./hydro/Shadowswift/hydro.h" +#include "./hydro/Shadowswift/hydro_iact.h" +#define SPH_IMPLEMENTATION \ + "Shadowfax moving mesh (Vandenbroucke and De Rijcke 2016)" #else #error "Invalid choice of SPH variant" #endif diff --git a/src/hydro/Default/hydro.h b/src/hydro/Default/hydro.h index a614d08c30b21f9e7d422bf6b6a09d10d2e89799..051c22f46b3ecdff5d3de910e0f75409b0e78f02 100644 --- a/src/hydro/Default/hydro.h +++ b/src/hydro/Default/hydro.h @@ -22,6 +22,7 @@ #include "adiabatic_index.h" #include "approx_math.h" #include "equation_of_state.h" +#include "hydro_space.h" #include "minmax.h" #include <float.h> @@ -165,9 +166,10 @@ __attribute__((always_inline)) INLINE static void hydro_timestep_extra( * the variaous density tasks * * @param p The particle to act upon + * @param hs #hydro_space containing hydro specific space information. */ __attribute__((always_inline)) INLINE static void hydro_init_part( - struct part *restrict p) { + struct part *restrict p, const struct hydro_space *hs) { p->density.wcount = 0.f; p->density.wcount_dh = 0.f; p->rho = 0.f; @@ -400,7 +402,7 @@ __attribute__((always_inline)) INLINE static void hydro_first_init_part( xp->u_full = p->u; hydro_reset_acceleration(p); - hydro_init_part(p); + hydro_init_part(p, NULL); } #endif /* SWIFT_DEFAULT_HYDRO_H */ diff --git a/src/hydro/Gadget2/hydro.h b/src/hydro/Gadget2/hydro.h index cc7b422ccbe7c678969df5779a4d4a054c65528e..747c81a8e64c18a06b04160cfab326a3521c5901 100644 --- a/src/hydro/Gadget2/hydro.h +++ b/src/hydro/Gadget2/hydro.h @@ -36,6 +36,7 @@ #include "dimension.h" #include "equation_of_state.h" #include "hydro_properties.h" +#include "hydro_space.h" #include "kernel_hydro.h" #include "minmax.h" @@ -169,9 +170,10 @@ __attribute__((always_inline)) INLINE static void hydro_timestep_extra( * the variaous density tasks * * @param p The particle to act upon + * @param hs #hydro_space containing hydro specific space information. */ __attribute__((always_inline)) INLINE static void hydro_init_part( - struct part *restrict p) { + struct part *restrict p, const struct hydro_space *hs) { p->rho = 0.f; p->density.wcount = 0.f; @@ -456,7 +458,7 @@ __attribute__((always_inline)) INLINE static void hydro_first_init_part( xp->entropy_full = p->entropy; hydro_reset_acceleration(p); - hydro_init_part(p); + hydro_init_part(p, NULL); } #endif /* SWIFT_GADGET2_HYDRO_H */ diff --git a/src/hydro/Gizmo/hydro.h b/src/hydro/Gizmo/hydro.h index 643489912a6c6b1db921e73b508910cc670d49ae..60ff8ccee0e1a1e9c3477a10293f8981ff9b837e 100644 --- a/src/hydro/Gizmo/hydro.h +++ b/src/hydro/Gizmo/hydro.h @@ -23,6 +23,7 @@ #include "approx_math.h" #include "equation_of_state.h" #include "hydro_gradients.h" +#include "hydro_space.h" #include "minmax.h" #include "riemann.h" @@ -145,9 +146,10 @@ __attribute__((always_inline)) INLINE static void hydro_first_init_part( * Simply makes sure all necessary variables are initialized to zero. * * @param p The particle to act upon + * @param hs #hydro_space containing hydro specific space information. */ __attribute__((always_inline)) INLINE static void hydro_init_part( - struct part* p) { + struct part* p, const struct hydro_space* hs) { p->density.wcount = 0.0f; p->density.wcount_dh = 0.0f; diff --git a/src/hydro/Minimal/hydro.h b/src/hydro/Minimal/hydro.h index 56078a82569fb0bc30347d5c01831e9eecfd48f4..8f216a550ae061d01a594ff23d57575e754f85dc 100644 --- a/src/hydro/Minimal/hydro.h +++ b/src/hydro/Minimal/hydro.h @@ -38,6 +38,7 @@ #include "dimension.h" #include "equation_of_state.h" #include "hydro_properties.h" +#include "hydro_space.h" #include "kernel_hydro.h" #include "minmax.h" @@ -183,9 +184,10 @@ __attribute__((always_inline)) INLINE static void hydro_timestep_extra( * density sub-structure of a particle get zeroed in here. * * @param p The particle to act upon + * @param hs #hydro_space containing hydro specific space information. */ __attribute__((always_inline)) INLINE static void hydro_init_part( - struct part *restrict p) { + struct part *restrict p, const struct hydro_space *hs) { p->density.wcount = 0.f; p->density.wcount_dh = 0.f; @@ -429,7 +431,7 @@ __attribute__((always_inline)) INLINE static void hydro_first_init_part( xp->u_full = p->u; hydro_reset_acceleration(p); - hydro_init_part(p); + hydro_init_part(p, NULL); } #endif /* SWIFT_MINIMAL_HYDRO_H */ diff --git a/src/hydro/PressureEntropy/hydro.h b/src/hydro/PressureEntropy/hydro.h index 20238896f1458d0abebacca4865968a3a671c886..4c4868cd3703e5ec5466d4878749a61284b19344 100644 --- a/src/hydro/PressureEntropy/hydro.h +++ b/src/hydro/PressureEntropy/hydro.h @@ -36,6 +36,7 @@ #include "dimension.h" #include "equation_of_state.h" #include "hydro_properties.h" +#include "hydro_space.h" #include "kernel_hydro.h" #include "minmax.h" @@ -169,9 +170,10 @@ __attribute__((always_inline)) INLINE static void hydro_timestep_extra( * the variaous density tasks * * @param p The particle to act upon + * @param hs #hydro_space containing hydro specific space information. */ __attribute__((always_inline)) INLINE static void hydro_init_part( - struct part *restrict p) { + struct part *restrict p, const struct hydro_space *hs) { p->rho = 0.f; p->rho_bar = 0.f; @@ -474,7 +476,7 @@ __attribute__((always_inline)) INLINE static void hydro_first_init_part( xp->v_full[2] = p->v[2]; hydro_reset_acceleration(p); - hydro_init_part(p); + hydro_init_part(p, NULL); } #endif /* SWIFT_PRESSURE_ENTROPY_HYDRO_H */ diff --git a/src/hydro/Shadowswift/hydro.h b/src/hydro/Shadowswift/hydro.h new file mode 100644 index 0000000000000000000000000000000000000000..0568d47ee7ed33c59790cbca943cccbf1ceda58f --- /dev/null +++ b/src/hydro/Shadowswift/hydro.h @@ -0,0 +1,591 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2016 Bert Vandenbroucke (bert.vandenbroucke@gmail.com). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +#include <float.h> +#include "adiabatic_index.h" +#include "approx_math.h" +#include "equation_of_state.h" +#include "hydro_gradients.h" +#include "hydro_space.h" +#include "voronoi_algorithm.h" + +/** + * @brief Computes the hydro time-step of a given particle + * + * @param p Pointer to the particle data. + * @param xp Pointer to the extended particle data. + * @param hydro_properties Pointer to the hydro parameters. + */ +__attribute__((always_inline)) INLINE static float hydro_compute_timestep( + const struct part* restrict p, const struct xpart* restrict xp, + const struct hydro_props* restrict hydro_properties) { + + const float CFL_condition = hydro_properties->CFL_condition; + + float R = get_radius_dimension_sphere(p->cell.volume); + + if (p->timestepvars.vmax == 0.) { + /* vmax can be zero in vacuum. We force the time step to become the maximal + time step in this case */ + return FLT_MAX; + } else { + return CFL_condition * R / fabsf(p->timestepvars.vmax); + } +} + +/** + * @brief Does some extra hydro operations once the actual physical time step + * for the particle is known. + * + * We use this to store the physical time step, since it is used for the flux + * exchange during the force loop. + * + * We also set the active flag of the particle to inactive. It will be set to + * active in hydro_init_part, which is called the next time the particle becomes + * active. + * + * @param p The particle to act upon. + * @param dt Physical time step of the particle during the next step. + */ +__attribute__((always_inline)) INLINE static void hydro_timestep_extra( + struct part* p, float dt) { + + p->force.dt = dt; + p->force.active = 0; +} + +/** + * @brief Initialises the particles for the first time + * + * This function is called only once just after the ICs have been + * read in to do some conversions. + * + * In this case, we copy the particle velocities into the corresponding + * primitive variable field. We do this because the particle velocities in GIZMO + * can be independent of the actual fluid velocity. The latter is stored as a + * primitive variable and integrated using the linear momentum, a conserved + * variable. + * + * @param p The particle to act upon + * @param xp The extended particle data to act upon + */ +__attribute__((always_inline)) INLINE static void hydro_first_init_part( + struct part* p, struct xpart* xp) { + + const float mass = p->conserved.mass; + + p->primitives.v[0] = p->v[0]; + p->primitives.v[1] = p->v[1]; + p->primitives.v[2] = p->v[2]; + + p->conserved.momentum[0] = mass * p->primitives.v[0]; + p->conserved.momentum[1] = mass * p->primitives.v[1]; + p->conserved.momentum[2] = mass * p->primitives.v[2]; + +#ifdef EOS_ISOTHERMAL_GAS + p->conserved.energy = mass * const_isothermal_internal_energy; +#else + p->conserved.energy *= mass; +#endif + +#ifdef SHADOWFAX_TOTAL_ENERGY + p->conserved.energy += 0.5f * (p->conserved.momentum[0] * p->primitives.v[0] + + p->conserved.momentum[1] * p->primitives.v[1] + + p->conserved.momentum[2] * p->primitives.v[2]); +#endif + +#if defined(SHADOWFAX_FIX_CELLS) + p->v[0] = 0.; + p->v[1] = 0.; + p->v[2] = 0.; +#endif + + /* set the initial velocity of the cells */ + xp->v_full[0] = p->v[0]; + xp->v_full[1] = p->v[1]; + xp->v_full[2] = p->v[2]; +} + +/** + * @brief Prepares a particle for the volume calculation. + * + * Simply makes sure all necessary variables are initialized to zero. + * Initializes the Voronoi cell. + * + * @param p The particle to act upon + * @param hs #hydro_space containing extra information about the space. + */ +__attribute__((always_inline)) INLINE static void hydro_init_part( + struct part* p, const struct hydro_space* hs) { + + p->density.wcount = 0.0f; + p->density.wcount_dh = 0.0f; + + voronoi_cell_init(&p->cell, p->x, hs->anchor, hs->side); + + /* Set the active flag to active. */ + p->force.active = 1; +} + +/** + * @brief Finishes the volume calculation. + * + * Calls the finalize method on the Voronoi cell, which calculates the volume + * and centroid of the cell. We use the return value of this function to set + * a new value for the smoothing length and possibly force another iteration + * of the volume calculation for this particle. We then use the volume to + * convert conserved variables into primitive variables. + * + * This method also initializes the gradient variables (if gradients are used). + * + * @param p The particle to act upon. + */ +__attribute__((always_inline)) INLINE static void hydro_end_density( + struct part* restrict p) { + + float volume; + float m, momentum[3], energy; + + hydro_gradients_init(p); + + float hnew = voronoi_cell_finalize(&p->cell); + /* Enforce hnew as new smoothing length in the iteration + This is annoyingly difficult, as we do not have access to the variables + that govern the loop... + So here's an idea: let's force in some method somewhere that makes sure + r->e->hydro_properties->target_neighbours is 1, and + r->e->hydro_properties->delta_neighbours is 0. + This way, we can accept an iteration by setting p->density.wcount to 1. + To get the right correction for h, we set wcount to something else + (say 0), and then set p->density.wcount_dh to 1/(hnew-h). */ + if (hnew < p->h) { + /* Iteration succesful: we accept, but manually set h to a smaller value + for the next time step */ + p->density.wcount = 1.0f; + p->h = 1.1f * hnew; + } else { + /* Iteration not succesful: we force h to become 1.1*hnew */ + p->density.wcount = 0.0f; + p->density.wcount_dh = 1.0f / (1.1f * hnew - p->h); + return; + } + volume = p->cell.volume; + +#ifdef SWIFT_DEBUG_CHECKS + /* the last condition checks for NaN: a NaN value always evaluates to false, + even when checked against itself */ + if (volume == 0. || volume == INFINITY || volume != volume) { + error("Invalid value for cell volume (%g)!", volume); + } +#endif + + /* compute primitive variables */ + /* eqns (3)-(5) */ + m = p->conserved.mass; + if (m > 0.) { + momentum[0] = p->conserved.momentum[0]; + momentum[1] = p->conserved.momentum[1]; + momentum[2] = p->conserved.momentum[2]; + p->primitives.rho = m / volume; + p->primitives.v[0] = momentum[0] / m; + p->primitives.v[1] = momentum[1] / m; + p->primitives.v[2] = momentum[2] / m; + + energy = p->conserved.energy; + +#ifdef SHADOWFAX_TOTAL_ENERGY + energy -= 0.5f * (momentum[0] * p->primitives.v[0] + + momentum[1] * p->primitives.v[1] + + momentum[2] * p->primitives.v[2]); +#endif + + energy /= m; + + p->primitives.P = + gas_pressure_from_internal_energy(p->primitives.rho, energy); + } else { + p->primitives.rho = 0.; + p->primitives.v[0] = 0.; + p->primitives.v[1] = 0.; + p->primitives.v[2] = 0.; + p->primitives.P = 0.; + } + +#ifdef SWIFT_DEBUG_CHECKS + if (p->primitives.rho < 0.) { + error("Negative density!"); + } + + if (p->primitives.P < 0.) { + error("Negative pressure!"); + } +#endif +} + +/** + * @brief Prepare a particle for the gradient calculation. + * + * The name of this method is confusing, as this method is really called after + * the density loop and before the gradient loop. + * + * We use it to set the physical timestep for the particle and to copy the + * actual velocities, which we need to boost our interfaces during the flux + * calculation. We also initialize the variables used for the time step + * calculation. + * + * @param p The particle to act upon. + * @param xp The extended particle data to act upon. + */ +__attribute__((always_inline)) INLINE static void hydro_prepare_force( + struct part* restrict p, struct xpart* restrict xp) { + + /* Initialize time step criterion variables */ + p->timestepvars.vmax = 0.0f; + + /* Set the actual velocity of the particle */ + p->force.v_full[0] = xp->v_full[0]; + p->force.v_full[1] = xp->v_full[1]; + p->force.v_full[2] = xp->v_full[2]; +} + +/** + * @brief Finishes the gradient calculation. + * + * Just a wrapper around hydro_gradients_finalize, which can be an empty method, + * in which case no gradients are used. + * + * @param p The particle to act upon. + */ +__attribute__((always_inline)) INLINE static void hydro_end_gradient( + struct part* p) { + + hydro_gradients_finalize(p); +} + +/** + * @brief Reset acceleration fields of a particle + * + * This is actually not necessary for Shadowswift, since we just set the + * accelerations after the flux calculation. + * + * @param p The particle to act upon. + */ +__attribute__((always_inline)) INLINE static void hydro_reset_acceleration( + struct part* p) { + + /* Reset the acceleration. */ + p->a_hydro[0] = 0.0f; + p->a_hydro[1] = 0.0f; + p->a_hydro[2] = 0.0f; + + /* Reset the time derivatives. */ + p->force.h_dt = 0.0f; +} + +/** + * @brief Sets the values to be predicted in the drifts to their values at a + * kick time + * + * @param p The particle. + * @param xp The extended data of this particle. + */ +__attribute__((always_inline)) INLINE static void hydro_reset_predicted_values( + struct part* restrict p, const struct xpart* restrict xp) {} + +/** + * @brief Converts the hydrodynamic variables from the initial condition file to + * conserved variables that can be used during the integration + * + * Requires the volume to be known. + * + * The initial condition file contains a mixture of primitive and conserved + * variables. Mass is a conserved variable, and we just copy the particle + * mass into the corresponding conserved quantity. We need the volume to + * also derive a density, which is then used to convert the internal energy + * to a pressure. However, we do not actually use these variables anymore. + * We do need to initialize the linear momentum, based on the mass and the + * velocity of the particle. + * + * @param p The particle to act upon. + * @param xp The extended particle data to act upon. + */ +__attribute__((always_inline)) INLINE static void hydro_convert_quantities( + struct part* p, struct xpart* xp) {} + +/** + * @brief Extra operations to be done during the drift + * + * Not used for Shadowswift. + * + * @param p Particle to act upon. + * @param xp The extended particle data to act upon. + * @param dt The drift time-step. + */ +__attribute__((always_inline)) INLINE static void hydro_predict_extra( + struct part* p, struct xpart* xp, float dt) {} + +/** + * @brief Set the particle acceleration after the flux loop. + * + * @param p Particle to act upon. + */ +__attribute__((always_inline)) INLINE static void hydro_end_force( + struct part* p) {} + +/** + * @brief Extra operations done during the kick + * + * Not used for Shadowswift. + * + * @param p Particle to act upon. + * @param xp Extended particle data to act upon. + * @param dt Physical time step. + */ +__attribute__((always_inline)) INLINE static void hydro_kick_extra( + struct part* p, struct xpart* xp, float dt) { + + float vcell[3]; + + /* Update the conserved variables. We do this here and not in the kick, + since we need the updated variables below. */ + p->conserved.mass += p->conserved.flux.mass; + p->conserved.momentum[0] += p->conserved.flux.momentum[0]; + p->conserved.momentum[1] += p->conserved.flux.momentum[1]; + p->conserved.momentum[2] += p->conserved.flux.momentum[2]; + p->conserved.energy += p->conserved.flux.energy; + +#ifdef EOS_ISOTHERMAL_GAS + /* reset the thermal energy */ + p->conserved.energy = p->conserved.mass * const_isothermal_internal_energy; + +#ifdef SHADOWFAX_TOTAL_ENERGY + p->conserved.energy += 0.5f * (p->conserved.momentum[0] * p->primitives.v[0] + + p->conserved.momentum[1] * p->primitives.v[1] + + p->conserved.momentum[2] * p->primitives.v[2]); +#endif + +#endif + + /* reset fluxes */ + /* we can only do this here, since we need to keep the fluxes for inactive + particles */ + p->conserved.flux.mass = 0.0f; + p->conserved.flux.momentum[0] = 0.0f; + p->conserved.flux.momentum[1] = 0.0f; + p->conserved.flux.momentum[2] = 0.0f; + p->conserved.flux.energy = 0.0f; + + if (p->conserved.mass > 0.) { + /* We want the cell velocity to be as close as possible to the fluid + velocity */ + vcell[0] = p->conserved.momentum[0] / p->conserved.mass; + vcell[1] = p->conserved.momentum[1] / p->conserved.mass; + vcell[2] = p->conserved.momentum[2] / p->conserved.mass; + } else { + vcell[0] = 0.; + vcell[1] = 0.; + vcell[2] = 0.; + } + +#ifdef SHADOWFAX_STEER_CELL_MOTION + /* To prevent stupid things like cell crossovers or generators that move + outside their cell, we steer the motion of the cell somewhat */ + if (p->primitives.rho) { + float centroid[3], d[3]; + float volume, csnd, R, vfac, fac, dnrm; + voronoi_get_centroid(&p->cell, centroid); + d[0] = centroid[0] - p->x[0]; + d[1] = centroid[1] - p->x[1]; + d[2] = centroid[2] - p->x[2]; + dnrm = sqrtf(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]); + csnd = sqrtf(hydro_gamma * p->primitives.P / p->primitives.rho); + volume = p->cell.volume; + R = get_radius_dimension_sphere(volume); + fac = 4.0f * dnrm / R; + if (fac > 0.9f) { + if (fac < 1.1f) { + vfac = csnd * (dnrm - 0.225f * R) / dnrm / (0.05f * R); + } else { + vfac = csnd / dnrm; + } + } else { + vfac = 0.0f; + } + vcell[0] += vfac * d[0]; + vcell[1] += vfac * d[1]; + vcell[2] += vfac * d[2]; + } +#endif + +#if defined(SHADOWFAX_FIX_CELLS) + xp->v_full[0] = 0.; + xp->v_full[1] = 0.; + xp->v_full[2] = 0.; + + p->v[0] = 0.; + p->v[1] = 0.; + p->v[2] = 0.; +#else + xp->v_full[0] = vcell[0]; + xp->v_full[1] = vcell[1]; + xp->v_full[2] = vcell[2]; + + p->v[0] = xp->v_full[0]; + p->v[1] = xp->v_full[1]; + p->v[2] = xp->v_full[2]; +#endif +} + +/** + * @brief Returns the internal energy of a particle + * + * @param p The particle of interest. + * @return Internal energy of the particle. + */ +__attribute__((always_inline)) INLINE static float hydro_get_internal_energy( + const struct part* restrict p) { + + if (p->primitives.rho > 0.) { + return gas_internal_energy_from_pressure(p->primitives.rho, + p->primitives.P); + } else { + return 0.; + } +} + +/** + * @brief Returns the entropy of a particle + * + * @param p The particle of interest. + * @return Entropy of the particle. + */ +__attribute__((always_inline)) INLINE static float hydro_get_entropy( + const struct part* restrict p) { + + if (p->primitives.rho > 0.) { + return gas_entropy_from_pressure(p->primitives.rho, p->primitives.P); + } else { + return 0.; + } +} + +/** + * @brief Returns the sound speed of a particle + * + * @param p The particle of interest. + * @param Sound speed of the particle. + */ +__attribute__((always_inline)) INLINE static float hydro_get_soundspeed( + const struct part* restrict p) { + + if (p->primitives.rho > 0.) { + return gas_soundspeed_from_pressure(p->primitives.rho, p->primitives.P); + } else { + return 0.; + } +} + +/** + * @brief Returns the pressure of a particle + * + * @param p The particle of interest + * @param Pressure of the particle. + */ +__attribute__((always_inline)) INLINE static float hydro_get_pressure( + const struct part* restrict p) { + + return p->primitives.P; +} + +/** + * @brief Returns the mass of a particle + * + * @param p The particle of interest + */ +__attribute__((always_inline)) INLINE static float hydro_get_mass( + const struct part* restrict p) { + + return p->conserved.mass; +} + +/** + * @brief Returns the density of a particle + * + * @param p The particle of interest + */ +__attribute__((always_inline)) INLINE static float hydro_get_density( + const struct part* restrict p) { + + return p->primitives.rho; +} + +/** + * @brief Modifies the thermal state of a particle to the imposed internal + * energy + * + * This overrides the current state of the particle but does *not* change its + * time-derivatives + * + * @param p The particle + * @param u The new internal energy + */ +__attribute__((always_inline)) INLINE static void hydro_set_internal_energy( + struct part* restrict p, float u) { + + if (p->primitives.rho > 0.) { + p->conserved.energy = u * p->conserved.mass; + +#ifdef SHADOWFAX_TOTAL_ENERGY + p->conserved.energy += + 0.5f * (p->conserved.momentum[0] * p->primitives.v[0] + + p->conserved.momentum[1] * p->primitives.v[1] + + p->conserved.momentum[2] * p->primitives.v[2]); +#endif + + p->primitives.P = gas_pressure_from_internal_energy(p->primitives.rho, u); + } +} + +/** + * @brief Modifies the thermal state of a particle to the imposed entropy + * + * This overrides the current state of the particle but does *not* change its + * time-derivatives + * + * @param p The particle + * @param S The new entropy + */ +__attribute__((always_inline)) INLINE static void hydro_set_entropy( + struct part* restrict p, float S) { + + if (p->primitives.rho > 0.) { + p->conserved.energy = + gas_internal_energy_from_entropy(p->primitives.rho, S) * + p->conserved.mass; + +#ifdef SHADOWFAX_TOTAL_ENERGY + p->conserved.energy += + 0.5f * (p->conserved.momentum[0] * p->primitives.v[0] + + p->conserved.momentum[1] * p->primitives.v[1] + + p->conserved.momentum[2] * p->primitives.v[2]); +#endif + + p->primitives.P = gas_pressure_from_entropy(p->primitives.rho, S); + } +} diff --git a/src/hydro/Shadowswift/hydro_debug.h b/src/hydro/Shadowswift/hydro_debug.h new file mode 100644 index 0000000000000000000000000000000000000000..7cd7f89c8112ebcf1930c5ca52cb389139191975 --- /dev/null +++ b/src/hydro/Shadowswift/hydro_debug.h @@ -0,0 +1,69 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2016 Matthieu Schaller (matthieu.schaller@durham.ac.uk) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +__attribute__((always_inline)) INLINE static void hydro_debug_particle( + const struct part* p, const struct xpart* xp) { + printf( + "x=[%.16e,%.16e,%.16e], " + "v=[%.3e,%.3e,%.3e], " + "a=[%.3e,%.3e,%.3e], " + "h=%.3e, " + "primitives={" + "v=[%.3e,%.3e,%.3e], " + "rho=%.3e, " + "P=%.3e, " + "gradients={" + "rho=[%.3e,%.3e,%.3e], " + "v=[[%.3e,%.3e,%.3e],[%.3e,%.3e,%.3e],[%.3e,%.3e,%.3e]], " + "P=[%.3e,%.3e,%.3e]}, " + "limiter={" + "rho=[%.3e,%.3e], " + "v=[[%.3e,%.3e],[%.3e,%.3e],[%.3e,%.3e]], " + "P=[%.3e,%.3e], " + "maxr=%.3e}}, " + "conserved={" + "momentum=[%.3e,%.3e,%.3e], " + "mass=%.3e, " + "energy=%.3e}, " + "timestepvars={" + "vmax=%.3e}, " + "density={" + "wcount_dh=%.3e, " + "wcount=%.3e}", + p->x[0], p->x[1], p->x[2], p->v[0], p->v[1], p->v[2], p->a_hydro[0], + p->a_hydro[1], p->a_hydro[2], p->h, p->primitives.v[0], + p->primitives.v[1], p->primitives.v[2], p->primitives.rho, + p->primitives.P, p->primitives.gradients.rho[0], + p->primitives.gradients.rho[1], p->primitives.gradients.rho[2], + p->primitives.gradients.v[0][0], p->primitives.gradients.v[0][1], + p->primitives.gradients.v[0][2], p->primitives.gradients.v[1][0], + p->primitives.gradients.v[1][1], p->primitives.gradients.v[1][2], + p->primitives.gradients.v[2][0], p->primitives.gradients.v[2][1], + p->primitives.gradients.v[2][2], p->primitives.gradients.P[0], + p->primitives.gradients.P[1], p->primitives.gradients.P[2], + p->primitives.limiter.rho[0], p->primitives.limiter.rho[1], + p->primitives.limiter.v[0][0], p->primitives.limiter.v[0][1], + p->primitives.limiter.v[1][0], p->primitives.limiter.v[1][1], + p->primitives.limiter.v[2][0], p->primitives.limiter.v[2][1], + p->primitives.limiter.P[0], p->primitives.limiter.P[1], + p->primitives.limiter.maxr, p->conserved.momentum[0], + p->conserved.momentum[1], p->conserved.momentum[2], p->conserved.mass, + p->conserved.energy, p->timestepvars.vmax, p->density.wcount_dh, + p->density.wcount); +} diff --git a/src/hydro/Shadowswift/hydro_gradients.h b/src/hydro/Shadowswift/hydro_gradients.h new file mode 100644 index 0000000000000000000000000000000000000000..1aea49790d998d3912a80fa1376cbd1e183f26f7 --- /dev/null +++ b/src/hydro/Shadowswift/hydro_gradients.h @@ -0,0 +1,215 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2016 Bert Vandenbroucke (bert.vandenbroucke@gmail.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +#ifndef SWIFT_HYDRO_GRADIENTS_H +#define SWIFT_HYDRO_GRADIENTS_H + +#include "hydro_slope_limiters.h" + +#if defined(SHADOWFAX_GRADIENTS) + +#define HYDRO_GRADIENT_IMPLEMENTATION "Shadowfax gradients (Springel 2010)" +#include "hydro_gradients_shadowfax.h" + +#else + +/* No gradients. Perfectly acceptable, but we have to provide empty functions */ +#define HYDRO_GRADIENT_IMPLEMENTATION "No gradients (first order scheme)" + +/** + * @brief Initialize gradient variables + * + * @param p Particle. + */ +__attribute__((always_inline)) INLINE static void hydro_gradients_init( + struct part* p) {} + +/** + * @brief Gradient calculations done during the neighbour loop + * + * @param r2 Squared distance between the two particles. + * @param dx Distance vector (pi->x - pj->x). + * @param hi Smoothing length of particle i. + * @param hj Smoothing length of particle j. + * @param pi Particle i. + * @param pj Particle j. + */ +__attribute__((always_inline)) INLINE static void hydro_gradients_collect( + float r2, float* dx, float hi, float hj, struct part* pi, struct part* pj) { +} + +/** + * @brief Gradient calculations done during the neighbour loop: non-symmetric + * version + * + * @param r2 Squared distance between the two particles. + * @param dx Distance vector (pi->x - pj->x). + * @param hi Smoothing length of particle i. + * @param hj Smoothing length of particle j. + * @param pi Particle i. + * @param pj Particle j. + */ +__attribute__((always_inline)) INLINE static void +hydro_gradients_nonsym_collect(float r2, float* dx, float hi, float hj, + struct part* pi, struct part* pj) {} + +/** + * @brief Finalize the gradient variables after all data have been collected + * + * @param p Particle. + */ +__attribute__((always_inline)) INLINE static void hydro_gradients_finalize( + struct part* p) {} + +#endif + +/** + * @brief Gradients reconstruction. Is the same for all gradient types (although + * gradients_none does nothing, since all gradients are zero -- are they?). + */ +__attribute__((always_inline)) INLINE static void hydro_gradients_predict( + struct part* pi, struct part* pj, float hi, float hj, float* dx, float r, + float* xij_i, float* Wi, float* Wj, float mindt) { + + float dWi[5], dWj[5]; + float xij_j[3]; + + /* xij_j = real_midpoint - pj->x + = xij_i + pi->x - pj->x + = xij_i + dx */ + xij_j[0] = xij_i[0] + dx[0]; + xij_j[1] = xij_i[1] + dx[1]; + xij_j[2] = xij_i[2] + dx[2]; + + dWi[0] = pi->primitives.gradients.rho[0] * xij_i[0] + + pi->primitives.gradients.rho[1] * xij_i[1] + + pi->primitives.gradients.rho[2] * xij_i[2]; + dWi[1] = pi->primitives.gradients.v[0][0] * xij_i[0] + + pi->primitives.gradients.v[0][1] * xij_i[1] + + pi->primitives.gradients.v[0][2] * xij_i[2]; + dWi[2] = pi->primitives.gradients.v[1][0] * xij_i[0] + + pi->primitives.gradients.v[1][1] * xij_i[1] + + pi->primitives.gradients.v[1][2] * xij_i[2]; + dWi[3] = pi->primitives.gradients.v[2][0] * xij_i[0] + + pi->primitives.gradients.v[2][1] * xij_i[1] + + pi->primitives.gradients.v[2][2] * xij_i[2]; + dWi[4] = pi->primitives.gradients.P[0] * xij_i[0] + + pi->primitives.gradients.P[1] * xij_i[1] + + pi->primitives.gradients.P[2] * xij_i[2]; + + dWj[0] = pj->primitives.gradients.rho[0] * xij_j[0] + + pj->primitives.gradients.rho[1] * xij_j[1] + + pj->primitives.gradients.rho[2] * xij_j[2]; + dWj[1] = pj->primitives.gradients.v[0][0] * xij_j[0] + + pj->primitives.gradients.v[0][1] * xij_j[1] + + pj->primitives.gradients.v[0][2] * xij_j[2]; + dWj[2] = pj->primitives.gradients.v[1][0] * xij_j[0] + + pj->primitives.gradients.v[1][1] * xij_j[1] + + pj->primitives.gradients.v[1][2] * xij_j[2]; + dWj[3] = pj->primitives.gradients.v[2][0] * xij_j[0] + + pj->primitives.gradients.v[2][1] * xij_j[1] + + pj->primitives.gradients.v[2][2] * xij_j[2]; + dWj[4] = pj->primitives.gradients.P[0] * xij_j[0] + + pj->primitives.gradients.P[1] * xij_j[1] + + pj->primitives.gradients.P[2] * xij_j[2]; + + hydro_slope_limit_face(Wi, Wj, dWi, dWj, xij_i, xij_j, r); + + /* time */ + dWi[0] -= 0.5 * mindt * (Wi[1] * pi->primitives.gradients.rho[0] + + Wi[2] * pi->primitives.gradients.rho[1] + + Wi[3] * pi->primitives.gradients.rho[2] + + Wi[0] * (pi->primitives.gradients.v[0][0] + + pi->primitives.gradients.v[1][1] + + pi->primitives.gradients.v[2][2])); + dWi[1] -= 0.5 * mindt * (Wi[1] * pi->primitives.gradients.v[0][0] + + Wi[2] * pi->primitives.gradients.v[0][1] + + Wi[3] * pi->primitives.gradients.v[0][2] + + pi->primitives.gradients.P[0] / Wi[0]); + dWi[2] -= 0.5 * mindt * (Wi[1] * pi->primitives.gradients.v[1][0] + + Wi[2] * pi->primitives.gradients.v[1][1] + + Wi[3] * pi->primitives.gradients.v[1][2] + + pi->primitives.gradients.P[1] / Wi[0]); + dWi[3] -= 0.5 * mindt * (Wi[1] * pi->primitives.gradients.v[2][0] + + Wi[2] * pi->primitives.gradients.v[2][1] + + Wi[3] * pi->primitives.gradients.v[2][2] + + pi->primitives.gradients.P[2] / Wi[0]); + dWi[4] -= + 0.5 * mindt * (Wi[1] * pi->primitives.gradients.P[0] + + Wi[2] * pi->primitives.gradients.P[1] + + Wi[3] * pi->primitives.gradients.P[2] + + hydro_gamma * Wi[4] * (pi->primitives.gradients.v[0][0] + + pi->primitives.gradients.v[1][1] + + pi->primitives.gradients.v[2][2])); + + dWj[0] -= 0.5 * mindt * (Wj[1] * pj->primitives.gradients.rho[0] + + Wj[2] * pj->primitives.gradients.rho[1] + + Wj[3] * pj->primitives.gradients.rho[2] + + Wj[0] * (pj->primitives.gradients.v[0][0] + + pj->primitives.gradients.v[1][1] + + pj->primitives.gradients.v[2][2])); + dWj[1] -= 0.5 * mindt * (Wj[1] * pj->primitives.gradients.v[0][0] + + Wj[2] * pj->primitives.gradients.v[0][1] + + Wj[3] * pj->primitives.gradients.v[0][2] + + pj->primitives.gradients.P[0] / Wj[0]); + dWj[2] -= 0.5 * mindt * (Wj[1] * pj->primitives.gradients.v[1][0] + + Wj[2] * pj->primitives.gradients.v[1][1] + + Wj[3] * pj->primitives.gradients.v[1][2] + + pj->primitives.gradients.P[1] / Wj[0]); + dWj[3] -= 0.5 * mindt * (Wj[1] * pj->primitives.gradients.v[2][0] + + Wj[2] * pj->primitives.gradients.v[2][1] + + Wj[3] * pj->primitives.gradients.v[2][2] + + pj->primitives.gradients.P[2] / Wj[0]); + dWj[4] -= + 0.5 * mindt * (Wj[1] * pj->primitives.gradients.P[0] + + Wj[2] * pj->primitives.gradients.P[1] + + Wj[3] * pj->primitives.gradients.P[2] + + hydro_gamma * Wj[4] * (pj->primitives.gradients.v[0][0] + + pj->primitives.gradients.v[1][1] + + pj->primitives.gradients.v[2][2])); + + Wi[0] += dWi[0]; + Wi[1] += dWi[1]; + Wi[2] += dWi[2]; + Wi[3] += dWi[3]; + Wi[4] += dWi[4]; + + Wj[0] += dWj[0]; + Wj[1] += dWj[1]; + Wj[2] += dWj[2]; + Wj[3] += dWj[3]; + Wj[4] += dWj[4]; + + /* Sanity check: if density or pressure becomes negative after the + interpolation, just reset them */ + if (Wi[0] < 0.0f) { + Wi[0] -= dWi[0]; + } + if (Wi[4] < 0.0f) { + Wi[4] -= dWi[4]; + } + if (Wj[0] < 0.0f) { + Wj[0] -= dWj[0]; + } + if (Wj[4] < 0.0f) { + Wj[4] -= dWj[4]; + } +} + +#endif // SWIFT_HYDRO_GRADIENTS_H diff --git a/src/hydro/Shadowswift/hydro_gradients_shadowfax.h b/src/hydro/Shadowswift/hydro_gradients_shadowfax.h new file mode 100644 index 0000000000000000000000000000000000000000..9ca40a604da3dc12bbb48ac033cd078f0561d8ab --- /dev/null +++ b/src/hydro/Shadowswift/hydro_gradients_shadowfax.h @@ -0,0 +1,217 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2016 Bert Vandenbroucke (bert.vandenbroucke@gmail.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +#include "voronoi_algorithm.h" + +/** + * @brief Initialize gradient variables + * + * @param p Particle. + */ +__attribute__((always_inline)) INLINE static void hydro_gradients_init( + struct part *p) { + + p->primitives.gradients.rho[0] = 0.0f; + p->primitives.gradients.rho[1] = 0.0f; + p->primitives.gradients.rho[2] = 0.0f; + + p->primitives.gradients.v[0][0] = 0.0f; + p->primitives.gradients.v[0][1] = 0.0f; + p->primitives.gradients.v[0][2] = 0.0f; + + p->primitives.gradients.v[1][0] = 0.0f; + p->primitives.gradients.v[1][1] = 0.0f; + p->primitives.gradients.v[1][2] = 0.0f; + + p->primitives.gradients.v[2][0] = 0.0f; + p->primitives.gradients.v[2][1] = 0.0f; + p->primitives.gradients.v[2][2] = 0.0f; + + p->primitives.gradients.P[0] = 0.0f; + p->primitives.gradients.P[1] = 0.0f; + p->primitives.gradients.P[2] = 0.0f; + + hydro_slope_limit_cell_init(p); +} + +/** + * @brief Add the gradient estimate for a single quantity due to a particle pair + * to the total gradient for that quantity + * + * This corresponds to one term of equation (21) in Springel (2010). + * + * @param qL Value of the quantity on the left. + * @param qR Value of the quantity on the right. + * @param cLR Vector pointing from the midpoint of the particle pair to the + * geometrical centroid of the face in between the particles. + * @param xLR Vector pointing from the right particle to the left particle. + * @param A Surface area of the face in between the particles. + * @param grad Current value of the gradient for the quantity (is updated). + */ +__attribute__((always_inline)) INLINE void hydro_gradients_single_quantity( + float qL, float qR, float *cLR, float *xLR, float rLR, float A, + float *grad) { + + grad[0] += A * ((qR - qL) * cLR[0] / rLR - 0.5f * (qL + qR) * xLR[0] / rLR); + grad[1] += A * ((qR - qL) * cLR[1] / rLR - 0.5f * (qL + qR) * xLR[1] / rLR); + grad[2] += A * ((qR - qL) * cLR[2] / rLR - 0.5f * (qL + qR) * xLR[2] / rLR); +} + +/** + * @brief Gradient calculations done during the neighbour loop + * + * @param r2 Squared distance between the two particles. + * @param dx Distance vector (pi->x - pj->x). + * @param hi Smoothing length of particle i. + * @param hj Smoothing length of particle j. + * @param pi Particle i. + * @param pj Particle j. + */ +__attribute__((always_inline)) INLINE static void hydro_gradients_collect( + float r2, float *dx, float hi, float hj, struct part *pi, struct part *pj) { + + float A, midpoint[3]; + + A = voronoi_get_face(&pi->cell, pj->id, midpoint); + if (!A) { + /* particle is not a cell neighbour: do nothing */ + return; + } + + float c[3]; + /* midpoint is relative w.r.t. pi->x, as is dx */ + /* c is supposed to be the vector pointing from the midpoint of pi and pj to + the midpoint of the face between pi and pj: + c = real_midpoint - 0.5*(pi+pj) + = midpoint + pi - 0.5*(2*pi - dx) + = midpoint + 0.5*dx */ + c[0] = midpoint[0] + 0.5f * dx[0]; + c[1] = midpoint[1] + 0.5f * dx[1]; + c[2] = midpoint[2] + 0.5f * dx[2]; + + float r = sqrtf(r2); + hydro_gradients_single_quantity(pi->primitives.rho, pj->primitives.rho, c, dx, + r, A, pi->primitives.gradients.rho); + hydro_gradients_single_quantity(pi->primitives.v[0], pj->primitives.v[0], c, + dx, r, A, pi->primitives.gradients.v[0]); + hydro_gradients_single_quantity(pi->primitives.v[1], pj->primitives.v[1], c, + dx, r, A, pi->primitives.gradients.v[1]); + hydro_gradients_single_quantity(pi->primitives.v[2], pj->primitives.v[2], c, + dx, r, A, pi->primitives.gradients.v[2]); + hydro_gradients_single_quantity(pi->primitives.P, pj->primitives.P, c, dx, r, + A, pi->primitives.gradients.P); + + hydro_slope_limit_cell_collect(pi, pj, r); + + float mindx[3]; + mindx[0] = -dx[0]; + mindx[1] = -dx[1]; + mindx[2] = -dx[2]; + hydro_gradients_single_quantity(pj->primitives.rho, pi->primitives.rho, c, + mindx, r, A, pj->primitives.gradients.rho); + hydro_gradients_single_quantity(pj->primitives.v[0], pi->primitives.v[0], c, + mindx, r, A, pj->primitives.gradients.v[0]); + hydro_gradients_single_quantity(pj->primitives.v[1], pi->primitives.v[1], c, + mindx, r, A, pj->primitives.gradients.v[1]); + hydro_gradients_single_quantity(pj->primitives.v[2], pi->primitives.v[2], c, + mindx, r, A, pj->primitives.gradients.v[2]); + hydro_gradients_single_quantity(pj->primitives.P, pi->primitives.P, c, mindx, + r, A, pj->primitives.gradients.P); + + hydro_slope_limit_cell_collect(pj, pi, r); +} + +/** + * @brief Gradient calculations done during the neighbour loop + * + * @param r2 Squared distance between the two particles. + * @param dx Distance vector (pi->x - pj->x). + * @param hi Smoothing length of particle i. + * @param hj Smoothing length of particle j. + * @param pi Particle i. + * @param pj Particle j. + */ +__attribute__((always_inline)) INLINE static void +hydro_gradients_nonsym_collect(float r2, float *dx, float hi, float hj, + struct part *pi, struct part *pj) { + + float A, midpoint[3]; + + A = voronoi_get_face(&pi->cell, pj->id, midpoint); + if (!A) { + /* particle is not a cell neighbour: do nothing */ + return; + } + + float c[3]; + /* midpoint is relative w.r.t. pi->x, as is dx */ + /* c is supposed to be the vector pointing from the midpoint of pi and pj to + the midpoint of the face between pi and pj: + c = real_midpoint - 0.5*(pi+pj) + = midpoint + pi - 0.5*(2*pi - dx) + = midpoint + 0.5*dx */ + c[0] = midpoint[0] + 0.5f * dx[0]; + c[1] = midpoint[1] + 0.5f * dx[1]; + c[2] = midpoint[2] + 0.5f * dx[2]; + + float r = sqrtf(r2); + hydro_gradients_single_quantity(pi->primitives.rho, pj->primitives.rho, c, dx, + r, A, pi->primitives.gradients.rho); + hydro_gradients_single_quantity(pi->primitives.v[0], pj->primitives.v[0], c, + dx, r, A, pi->primitives.gradients.v[0]); + hydro_gradients_single_quantity(pi->primitives.v[1], pj->primitives.v[1], c, + dx, r, A, pi->primitives.gradients.v[1]); + hydro_gradients_single_quantity(pi->primitives.v[2], pj->primitives.v[2], c, + dx, r, A, pi->primitives.gradients.v[2]); + hydro_gradients_single_quantity(pi->primitives.P, pj->primitives.P, c, dx, r, + A, pi->primitives.gradients.P); + + hydro_slope_limit_cell_collect(pi, pj, r); +} + +/** + * @brief Finalize the gradient variables after all data have been collected + * + * @param p Particle. + */ +__attribute__((always_inline)) INLINE static void hydro_gradients_finalize( + struct part *p) { + + float volume = p->cell.volume; + + p->primitives.gradients.rho[0] /= volume; + p->primitives.gradients.rho[1] /= volume; + p->primitives.gradients.rho[2] /= volume; + + p->primitives.gradients.v[0][0] /= volume; + p->primitives.gradients.v[0][1] /= volume; + p->primitives.gradients.v[0][2] /= volume; + p->primitives.gradients.v[1][0] /= volume; + p->primitives.gradients.v[1][1] /= volume; + p->primitives.gradients.v[1][2] /= volume; + p->primitives.gradients.v[2][0] /= volume; + p->primitives.gradients.v[2][1] /= volume; + p->primitives.gradients.v[2][2] /= volume; + + p->primitives.gradients.P[0] /= volume; + p->primitives.gradients.P[1] /= volume; + p->primitives.gradients.P[2] /= volume; + + hydro_slope_limit_cell(p); +} diff --git a/src/hydro/Shadowswift/hydro_iact.h b/src/hydro/Shadowswift/hydro_iact.h new file mode 100644 index 0000000000000000000000000000000000000000..63f7bdc6900c8fe9887879f3ff7e6c5eaff1b281 --- /dev/null +++ b/src/hydro/Shadowswift/hydro_iact.h @@ -0,0 +1,365 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2016 Bert Vandenbroucke (bert.vandenbroucke@gmail.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +#include "adiabatic_index.h" +#include "hydro_gradients.h" +#include "riemann.h" +#include "voronoi_algorithm.h" + +/** + * @brief Calculate the Voronoi cell by interacting particle pi and pj + * + * This method wraps around voronoi_cell_interact(). + * + * @param r2 Squared distance between particle i and particle j. + * @param dx Distance vector between the particles (dx = pi->x - pj->x). + * @param hi Smoothing length of particle i. + * @param hj Smoothing length of particle j. + * @param pi Particle i. + * @param pj Particle j. + */ +__attribute__((always_inline)) INLINE static void runner_iact_density( + float r2, float *dx, float hi, float hj, struct part *pi, struct part *pj) { + + float mindx[3]; + + voronoi_cell_interact(&pi->cell, dx, pj->id); + mindx[0] = -dx[0]; + mindx[1] = -dx[1]; + mindx[2] = -dx[2]; + voronoi_cell_interact(&pj->cell, mindx, pi->id); +} + +/** + * @brief Calculate the Voronoi cell by interacting particle pi with pj + * + * This method wraps around voronoi_cell_interact(). + * + * @param r2 Squared distance between particle i and particle j. + * @param dx Distance vector between the particles (dx = pi->x - pj->x). + * @param hi Smoothing length of particle i. + * @param hj Smoothing length of particle j. + * @param pi Particle i. + * @param pj Particle j. + */ +__attribute__((always_inline)) INLINE static void runner_iact_nonsym_density( + float r2, float *dx, float hi, float hj, struct part *pi, struct part *pj) { + + voronoi_cell_interact(&pi->cell, dx, pj->id); +} + +/** + * @brief Calculate the gradient interaction between particle i and particle j + * + * This method wraps around hydro_gradients_collect, which can be an empty + * method, in which case no gradients are used. + * + * @param r2 Squared distance between particle i and particle j. + * @param dx Distance vector between the particles (dx = pi->x - pj->x). + * @param hi Smoothing length of particle i. + * @param hj Smoothing length of particle j. + * @param pi Particle i. + * @param pj Particle j. + */ +__attribute__((always_inline)) INLINE static void runner_iact_gradient( + float r2, float *dx, float hi, float hj, struct part *pi, struct part *pj) { + + hydro_gradients_collect(r2, dx, hi, hj, pi, pj); +} + +/** + * @brief Calculate the gradient interaction between particle i and particle j: + * non-symmetric version + * + * This method wraps around hydro_gradients_nonsym_collect, which can be an + * empty method, in which case no gradients are used. + * + * @param r2 Squared distance between particle i and particle j. + * @param dx Distance vector between the particles (dx = pi->x - pj->x). + * @param hi Smoothing length of particle i. + * @param hj Smoothing length of particle j. + * @param pi Particle i. + * @param pj Particle j. + */ +__attribute__((always_inline)) INLINE static void runner_iact_nonsym_gradient( + float r2, float *dx, float hi, float hj, struct part *pi, struct part *pj) { + + hydro_gradients_nonsym_collect(r2, dx, hi, hj, pi, pj); +} + +/** + * @brief Common part of the flux calculation between particle i and j + * + * Since the only difference between the symmetric and non-symmetric version + * of the flux calculation is in the update of the conserved variables at the + * very end (which is not done for particle j if mode is 0 and particle j is + * active), both runner_iact_force and runner_iact_nonsym_force call this + * method, with an appropriate mode. + * + * This method retrieves the oriented surface area and face midpoint for the + * Voronoi face between pi and pj (if it exists). It uses the midpoint position + * to reconstruct the primitive quantities (if gradients are used) at the face + * and then uses the face quantities to estimate a flux through the face using + * a Riemann solver. + * + * This method also calculates the maximal velocity used to calculate the time + * step. + * + * @param r2 Squared distance between particle i and particle j. + * @param dx Distance vector between the particles (dx = pi->x - pj->x). + * @param hi Smoothing length of particle i. + * @param hj Smoothing length of particle j. + * @param pi Particle i. + * @param pj Particle j. + */ +__attribute__((always_inline)) INLINE static void runner_iact_fluxes_common( + float r2, float *dx, float hi, float hj, struct part *pi, struct part *pj, + int mode) { + + float r = sqrtf(r2); + int k; + float A; + float xij_i[3]; + float vmax, dvdotdx; + float vi[3], vj[3], vij[3]; + float Wi[5], Wj[5]; + float dti, dtj, mindt; + float n_unit[3]; + + A = voronoi_get_face(&pi->cell, pj->id, xij_i); + if (A == 0.0f) { + /* this neighbour does not share a face with the cell, return */ + return; + } + + /* Initialize local variables */ + for (k = 0; k < 3; k++) { + vi[k] = pi->force.v_full[k]; /* particle velocities */ + vj[k] = pj->force.v_full[k]; + } + Wi[0] = pi->primitives.rho; + Wi[1] = pi->primitives.v[0]; + Wi[2] = pi->primitives.v[1]; + Wi[3] = pi->primitives.v[2]; + Wi[4] = pi->primitives.P; + Wj[0] = pj->primitives.rho; + Wj[1] = pj->primitives.v[0]; + Wj[2] = pj->primitives.v[1]; + Wj[3] = pj->primitives.v[2]; + Wj[4] = pj->primitives.P; + + dti = pi->force.dt; + dtj = pj->force.dt; + + /* calculate the maximal signal velocity */ + vmax = 0.0f; + if (Wi[0] > 0.) { + vmax += gas_soundspeed_from_pressure(Wi[0], Wi[4]); + } + + if (Wj[0] > 0.) { + vmax += gas_soundspeed_from_pressure(Wj[0], Wj[4]); + } + + dvdotdx = (Wi[1] - Wj[1]) * dx[0] + (Wi[2] - Wj[2]) * dx[1] + + (Wi[3] - Wj[3]) * dx[2]; + if (dvdotdx > 0.) { + vmax -= dvdotdx / r; + } + + pi->timestepvars.vmax = fmaxf(pi->timestepvars.vmax, vmax); + if (mode == 1) { + pj->timestepvars.vmax = fmaxf(pj->timestepvars.vmax, vmax); + } + + /* The flux will be exchanged using the smallest time step of the two + * particles */ + mindt = fminf(dti, dtj); + + /* compute the normal vector of the interface */ + for (k = 0; k < 3; ++k) { + n_unit[k] = -dx[k] / r; + } + + /* Compute interface velocity */ + float fac = (vi[0] - vj[0]) * (xij_i[0] + 0.5f * dx[0]) + + (vi[1] - vj[1]) * (xij_i[1] + 0.5f * dx[1]) + + (vi[2] - vj[2]) * (xij_i[2] + 0.5f * dx[2]); + fac /= r; + vij[0] = 0.5f * (vi[0] + vj[0]) - fac * dx[0]; + vij[1] = 0.5f * (vi[1] + vj[1]) - fac * dx[1]; + vij[2] = 0.5f * (vi[2] + vj[2]) - fac * dx[2]; + + /* Boost the primitive variables to the frame of reference of the interface */ + /* Note that velocities are indices 1-3 in W */ + Wi[1] -= vij[0]; + Wi[2] -= vij[1]; + Wi[3] -= vij[2]; + Wj[1] -= vij[0]; + Wj[2] -= vij[1]; + Wj[3] -= vij[2]; + + hydro_gradients_predict(pi, pj, hi, hj, dx, r, xij_i, Wi, Wj, mindt); + + /* we don't need to rotate, we can use the unit vector in the Riemann problem + * itself (see GIZMO) */ + + if (Wi[0] < 0.0f || Wj[0] < 0.0f || Wi[4] < 0.0f || Wj[4] < 0.0f) { + printf("mindt: %g\n", mindt); + printf("WL: %g %g %g %g %g\n", pi->primitives.rho, pi->primitives.v[0], + pi->primitives.v[1], pi->primitives.v[2], pi->primitives.P); +#ifdef USE_GRADIENTS + printf("dWL: %g %g %g %g %g\n", dWi[0], dWi[1], dWi[2], dWi[3], dWi[4]); +#endif + printf("gradWL[0]: %g %g %g\n", pi->primitives.gradients.rho[0], + pi->primitives.gradients.rho[1], pi->primitives.gradients.rho[2]); + printf("gradWL[1]: %g %g %g\n", pi->primitives.gradients.v[0][0], + pi->primitives.gradients.v[0][1], pi->primitives.gradients.v[0][2]); + printf("gradWL[2]: %g %g %g\n", pi->primitives.gradients.v[1][0], + pi->primitives.gradients.v[1][1], pi->primitives.gradients.v[1][2]); + printf("gradWL[3]: %g %g %g\n", pi->primitives.gradients.v[2][0], + pi->primitives.gradients.v[2][1], pi->primitives.gradients.v[2][2]); + printf("gradWL[4]: %g %g %g\n", pi->primitives.gradients.P[0], + pi->primitives.gradients.P[1], pi->primitives.gradients.P[2]); + printf("WL': %g %g %g %g %g\n", Wi[0], Wi[1], Wi[2], Wi[3], Wi[4]); + printf("WR: %g %g %g %g %g\n", pj->primitives.rho, pj->primitives.v[0], + pj->primitives.v[1], pj->primitives.v[2], pj->primitives.P); +#ifdef USE_GRADIENTS + printf("dWR: %g %g %g %g %g\n", dWj[0], dWj[1], dWj[2], dWj[3], dWj[4]); +#endif + printf("gradWR[0]: %g %g %g\n", pj->primitives.gradients.rho[0], + pj->primitives.gradients.rho[1], pj->primitives.gradients.rho[2]); + printf("gradWR[1]: %g %g %g\n", pj->primitives.gradients.v[0][0], + pj->primitives.gradients.v[0][1], pj->primitives.gradients.v[0][2]); + printf("gradWR[2]: %g %g %g\n", pj->primitives.gradients.v[1][0], + pj->primitives.gradients.v[1][1], pj->primitives.gradients.v[1][2]); + printf("gradWR[3]: %g %g %g\n", pj->primitives.gradients.v[2][0], + pj->primitives.gradients.v[2][1], pj->primitives.gradients.v[2][2]); + printf("gradWR[4]: %g %g %g\n", pj->primitives.gradients.P[0], + pj->primitives.gradients.P[1], pj->primitives.gradients.P[2]); + printf("WR': %g %g %g %g %g\n", Wj[0], Wj[1], Wj[2], Wj[3], Wj[4]); + error("Negative density or pressure!\n"); + } + + float totflux[5]; + riemann_solve_for_flux(Wi, Wj, n_unit, vij, totflux); + + /* Update conserved variables */ + /* eqn. (16) */ + pi->conserved.flux.mass -= mindt * A * totflux[0]; + pi->conserved.flux.momentum[0] -= mindt * A * totflux[1]; + pi->conserved.flux.momentum[1] -= mindt * A * totflux[2]; + pi->conserved.flux.momentum[2] -= mindt * A * totflux[3]; + pi->conserved.flux.energy -= mindt * A * totflux[4]; + +#ifndef SHADOWFAX_TOTAL_ENERGY + float ekin = 0.5f * (pi->primitives.v[0] * pi->primitives.v[0] + + pi->primitives.v[1] * pi->primitives.v[1] + + pi->primitives.v[2] * pi->primitives.v[2]); + pi->conserved.flux.energy += mindt * A * totflux[1] * pi->primitives.v[0]; + pi->conserved.flux.energy += mindt * A * totflux[2] * pi->primitives.v[1]; + pi->conserved.flux.energy += mindt * A * totflux[3] * pi->primitives.v[2]; + pi->conserved.flux.energy -= mindt * A * totflux[0] * ekin; +#endif + + /* here is how it works: + Mode will only be 1 if both particles are ACTIVE and they are in the same + cell. In this case, this method IS the flux calculation for particle j, and + we HAVE TO UPDATE it. + Mode 0 can mean several things: it can mean that particle j is INACTIVE, in + which case we NEED TO UPDATE it, since otherwise the flux is lost from the + system and the conserved variable is not conserved. + It can also mean that particle j sits in another cell and is ACTIVE. In + this case, the flux exchange for particle j is done TWICE and we SHOULD NOT + UPDATE particle j. + ==> we update particle j if (MODE IS 1) OR (j IS INACTIVE) + */ + if (mode == 1 || pj->force.active == 0) { + pj->conserved.flux.mass += mindt * A * totflux[0]; + pj->conserved.flux.momentum[0] += mindt * A * totflux[1]; + pj->conserved.flux.momentum[1] += mindt * A * totflux[2]; + pj->conserved.flux.momentum[2] += mindt * A * totflux[3]; + pj->conserved.flux.energy += mindt * A * totflux[4]; + +#ifndef SHADOWFAX_TOTAL_ENERGY + ekin = 0.5f * (pj->primitives.v[0] * pj->primitives.v[0] + + pj->primitives.v[1] * pj->primitives.v[1] + + pj->primitives.v[2] * pj->primitives.v[2]); + pj->conserved.flux.energy -= mindt * A * totflux[1] * pj->primitives.v[0]; + pj->conserved.flux.energy -= mindt * A * totflux[2] * pj->primitives.v[1]; + pj->conserved.flux.energy -= mindt * A * totflux[3] * pj->primitives.v[2]; + pj->conserved.flux.energy += mindt * A * totflux[0] * ekin; +#endif + } +} + +/** + * @brief Flux calculation between particle i and particle j + * + * This method calls runner_iact_fluxes_common with mode 1. + * + * @param r2 Squared distance between particle i and particle j. + * @param dx Distance vector between the particles (dx = pi->x - pj->x). + * @param hi Smoothing length of particle i. + * @param hj Smoothing length of particle j. + * @param pi Particle i. + * @param pj Particle j. + */ +__attribute__((always_inline)) INLINE static void runner_iact_force( + float r2, float *dx, float hi, float hj, struct part *pi, struct part *pj) { + + runner_iact_fluxes_common(r2, dx, hi, hj, pi, pj, 1); +} + +/** + * @brief Flux calculation between particle i and particle j: non-symmetric + * version + * + * This method calls runner_iact_fluxes_common with mode 0. + * + * @param r2 Squared distance between particle i and particle j. + * @param dx Distance vector between the particles (dx = pi->x - pj->x). + * @param hi Smoothing length of particle i. + * @param hj Smoothing length of particle j. + * @param pi Particle i. + * @param pj Particle j. + */ +__attribute__((always_inline)) INLINE static void runner_iact_nonsym_force( + float r2, float *dx, float hi, float hj, struct part *pi, struct part *pj) { + + runner_iact_fluxes_common(r2, dx, hi, hj, pi, pj, 0); +} + +//// EMPTY VECTORIZED VERSIONS (gradients methods are missing...) + +__attribute__((always_inline)) INLINE static void runner_iact_vec_density( + float *R2, float *Dx, float *Hi, float *Hj, struct part **pi, + struct part **pj) {} + +__attribute__((always_inline)) INLINE static void +runner_iact_nonsym_vec_density(float *R2, float *Dx, float *Hi, float *Hj, + struct part **pi, struct part **pj) {} + +__attribute__((always_inline)) INLINE static void runner_iact_vec_force( + float *R2, float *Dx, float *Hi, float *Hj, struct part **pi, + struct part **pj) {} + +__attribute__((always_inline)) INLINE static void runner_iact_nonsym_vec_force( + float *R2, float *Dx, float *Hi, float *Hj, struct part **pi, + struct part **pj) {} diff --git a/src/hydro/Shadowswift/hydro_io.h b/src/hydro/Shadowswift/hydro_io.h new file mode 100644 index 0000000000000000000000000000000000000000..de45b5d68c78f96cee3030eadef4b4410e550c22 --- /dev/null +++ b/src/hydro/Shadowswift/hydro_io.h @@ -0,0 +1,183 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2016 Bert Vandenbroucke (bert.vandenbroucke@gmail.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +#include "adiabatic_index.h" +#include "equation_of_state.h" +#include "hydro_gradients.h" +#include "hydro_slope_limiters.h" +#include "io_properties.h" +#include "riemann.h" + +/** + * @brief Specifies which particle fields to read from a dataset + * + * @param parts The particle array. + * @param list The list of i/o properties to read. + * @param num_fields The number of i/o fields to read. + */ +void hydro_read_particles(struct part* parts, struct io_props* list, + int* num_fields) { + + *num_fields = 8; + + /* List what we want to read */ + list[0] = io_make_input_field("Coordinates", DOUBLE, 3, COMPULSORY, + UNIT_CONV_LENGTH, parts, x); + list[1] = io_make_input_field("Velocities", FLOAT, 3, COMPULSORY, + UNIT_CONV_SPEED, parts, v); + list[2] = io_make_input_field("Masses", FLOAT, 1, COMPULSORY, UNIT_CONV_MASS, + parts, conserved.mass); + list[3] = io_make_input_field("SmoothingLength", FLOAT, 1, COMPULSORY, + UNIT_CONV_LENGTH, parts, h); + list[4] = io_make_input_field("InternalEnergy", FLOAT, 1, COMPULSORY, + UNIT_CONV_ENERGY_PER_UNIT_MASS, parts, + conserved.energy); + list[5] = io_make_input_field("ParticleIDs", ULONGLONG, 1, COMPULSORY, + UNIT_CONV_NO_UNITS, parts, id); + list[6] = io_make_input_field("Accelerations", FLOAT, 3, OPTIONAL, + UNIT_CONV_ACCELERATION, parts, a_hydro); + list[7] = io_make_input_field("Density", FLOAT, 1, OPTIONAL, + UNIT_CONV_DENSITY, parts, primitives.rho); +} + +/** + * @brief Get the internal energy of a particle + * + * @param e #engine. + * @param p Particle. + * @return Internal energy of the particle + */ +float convert_u(struct engine* e, struct part* p) { + if (p->primitives.rho > 0.) { + return gas_internal_energy_from_pressure(p->primitives.rho, + p->primitives.P); + } else { + return 0.; + } +} + +/** + * @brief Get the entropic function of a particle + * + * @param e #engine. + * @param p Particle. + * @return Entropic function of the particle + */ +float convert_A(struct engine* e, struct part* p) { + if (p->primitives.rho > 0.) { + return gas_entropy_from_pressure(p->primitives.rho, p->primitives.P); + } else { + return 0.; + } +} + +/** + * @brief Get the total energy of a particle + * + * @param e #engine. + * @param p Particle. + * @return Total energy of the particle + */ +float convert_Etot(struct engine* e, struct part* p) { +#ifdef SHADOWFAX_TOTAL_ENERGY + return p->conserved.energy; +#else + if (p->conserved.mass > 0.) { + float momentum2; + + momentum2 = p->conserved.momentum[0] * p->conserved.momentum[0] + + p->conserved.momentum[1] * p->conserved.momentum[1] + + p->conserved.momentum[2] * p->conserved.momentum[2]; + + return p->conserved.energy + 0.5f * momentum2 / p->conserved.mass; + } else { + return 0.; + } +#endif +} + +/** + * @brief Specifies which particle fields to write to a dataset + * + * @param parts The particle array. + * @param list The list of i/o properties to write. + * @param num_fields The number of i/o fields to write. + */ +void hydro_write_particles(struct part* parts, struct io_props* list, + int* num_fields) { + + *num_fields = 13; + + /* List what we want to write */ + list[0] = io_make_output_field("Coordinates", DOUBLE, 3, UNIT_CONV_LENGTH, + parts, x); + list[1] = io_make_output_field("Velocities", FLOAT, 3, UNIT_CONV_SPEED, parts, + primitives.v); + list[2] = io_make_output_field("Masses", FLOAT, 1, UNIT_CONV_MASS, parts, + conserved.mass); + list[3] = io_make_output_field("SmoothingLength", FLOAT, 1, UNIT_CONV_LENGTH, + parts, h); + list[4] = io_make_output_field_convert_part("InternalEnergy", FLOAT, 1, + UNIT_CONV_ENERGY_PER_UNIT_MASS, + parts, primitives.P, convert_u); + list[5] = io_make_output_field("ParticleIDs", ULONGLONG, 1, + UNIT_CONV_NO_UNITS, parts, id); + list[6] = io_make_output_field("Acceleration", FLOAT, 3, + UNIT_CONV_ACCELERATION, parts, a_hydro); + list[7] = io_make_output_field("Density", FLOAT, 1, UNIT_CONV_DENSITY, parts, + primitives.rho); + list[8] = io_make_output_field("Volume", FLOAT, 1, UNIT_CONV_VOLUME, parts, + cell.volume); + list[9] = io_make_output_field("GradDensity", FLOAT, 3, UNIT_CONV_DENSITY, + parts, primitives.gradients.rho); + list[10] = io_make_output_field_convert_part( + "Entropy", FLOAT, 1, UNIT_CONV_ENTROPY, parts, primitives.P, convert_A); + list[11] = io_make_output_field("Pressure", FLOAT, 1, UNIT_CONV_PRESSURE, + parts, primitives.P); + list[12] = + io_make_output_field_convert_part("TotEnergy", FLOAT, 1, UNIT_CONV_ENERGY, + parts, conserved.energy, convert_Etot); +} + +/** + * @brief Writes the current model of SPH to the file + * @param h_grpsph The HDF5 group in which to write + */ +void writeSPHflavour(hid_t h_grpsph) { + /* Gradient information */ + io_write_attribute_s(h_grpsph, "Gradient reconstruction model", + HYDRO_GRADIENT_IMPLEMENTATION); + + /* Slope limiter information */ + io_write_attribute_s(h_grpsph, "Cell wide slope limiter model", + HYDRO_SLOPE_LIMITER_CELL_IMPLEMENTATION); + io_write_attribute_s(h_grpsph, "Piecewise slope limiter model", + HYDRO_SLOPE_LIMITER_FACE_IMPLEMENTATION); + + /* Riemann solver information */ + io_write_attribute_s(h_grpsph, "Riemann solver type", + RIEMANN_SOLVER_IMPLEMENTATION); +} + +/** + * @brief Are we writing entropy in the internal energy field ? + * + * @return 1 if entropy is in 'internal energy', 0 otherwise. + */ +int writeEntropyFlag() { return 0; } diff --git a/src/hydro/Shadowswift/hydro_part.h b/src/hydro/Shadowswift/hydro_part.h new file mode 100644 index 0000000000000000000000000000000000000000..43237d9c80a5dfa5bed2fe409281b4f89b6aa172 --- /dev/null +++ b/src/hydro/Shadowswift/hydro_part.h @@ -0,0 +1,185 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk) + * Matthieu Schaller (matthieu.schaller@durham.ac.uk) + * 2016 Bert Vandenbroucke (bert.vandenbroucke@gmail.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +#include "cooling_struct.h" +#include "voronoi_cell.h" + +/* Extra particle data not needed during the computation. */ +struct xpart { + + /* Offset between current position and position at last tree rebuild. */ + float x_diff[3]; + + /* Velocity at the last full step. */ + float v_full[3]; + + /* Additional data used to record cooling information */ + struct cooling_xpart_data cooling_data; + +} SWIFT_STRUCT_ALIGN; + +/* Data of a single particle. */ +struct part { + + /* Particle ID. */ + long long id; + + /* Associated gravitas. */ + struct gpart *gpart; + + /* Particle position. */ + double x[3]; + + /* Particle predicted velocity. */ + float v[3]; + + /* Particle acceleration. */ + float a_hydro[3]; + + /* Particle cutoff radius. */ + float h; + + /* The primitive hydrodynamical variables. */ + struct { + + /* Fluid velocity. */ + float v[3]; + + /* Density. */ + float rho; + + /* Pressure. */ + float P; + + /* Gradients of the primitive variables. */ + struct { + + /* Density gradients. */ + float rho[3]; + + /* Fluid velocity gradients. */ + float v[3][3]; + + /* Pressure gradients. */ + float P[3]; + + } gradients; + + /* Quantities needed by the slope limiter. */ + struct { + + /* Extreme values of the density among the neighbours. */ + float rho[2]; + + /* Extreme values of the fluid velocity among the neighbours. */ + float v[3][2]; + + /* Extreme values of the pressure among the neighbours. */ + float P[2]; + + /* Maximal distance to all neighbouring faces. */ + float maxr; + + } limiter; + + } primitives; + + /* The conserved hydrodynamical variables. */ + struct { + + /* Fluid momentum. */ + float momentum[3]; + + /* Fluid mass (this field already exists outside of this struct as well). */ + float mass; + + /* Fluid thermal energy (not per unit mass!). */ + float energy; + + /* Fluxes. */ + struct { + + /* Mass flux. */ + float mass; + + /* Momentum flux. */ + float momentum[3]; + + /* Energy flux. */ + float energy; + + } flux; + + } conserved; + + /* Variables used for timestep calculation (currently not used). */ + struct { + + /* Maximum fluid velocity among all neighbours. */ + float vmax; + + } timestepvars; + + /* Quantities used during the volume (=density) loop. */ + struct { + + /* Derivative of particle number density. */ + float wcount_dh; + + /* Particle number density. */ + float wcount; + + } density; + + /* Quantities used during the force loop. */ + struct { + + /* Needed to drift the primitive variables. */ + float h_dt; + + /* Physical time step of the particle. */ + float dt; + + /* Active flag. */ + char active; + + /* Actual velocity of the particle. */ + float v_full[3]; + + } force; + + /* Time-step length */ + timebin_t time_bin; + +#ifdef SWIFT_DEBUG_CHECKS + + /* Time of the last drift */ + integertime_t ti_drift; + + /* Time of the last kick */ + integertime_t ti_kick; + +#endif + + /* Voronoi cell. */ + struct voronoi_cell cell; + +} SWIFT_STRUCT_ALIGN; diff --git a/src/hydro/Shadowswift/hydro_slope_limiters.h b/src/hydro/Shadowswift/hydro_slope_limiters.h new file mode 100644 index 0000000000000000000000000000000000000000..a443007b4df3833964b7ec1780e2bc55bf02a03e --- /dev/null +++ b/src/hydro/Shadowswift/hydro_slope_limiters.h @@ -0,0 +1,94 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2016 Bert Vandenbroucke (bert.vandenbroucke@gmail.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +#ifndef SWIFT_HYDRO_SLOPE_LIMITERS_H +#define SWIFT_HYDRO_SLOPE_LIMITERS_H + +#include "dimension.h" +#include "kernel_hydro.h" + +#ifdef SHADOWFAX_SLOPE_LIMITER_PER_FACE + +#define HYDRO_SLOPE_LIMITER_FACE_IMPLEMENTATION \ + "GIZMO piecewise slope limiter (Hopkins 2015)" +#include "hydro_slope_limiters_face.h" + +#else + +#define HYDRO_SLOPE_LIMITER_FACE_IMPLEMENTATION "No piecewise slope limiter" + +/** + * @brief Slope limit the slopes at the interface between two particles + * + * @param Wi Hydrodynamic variables of particle i. + * @param Wj Hydrodynamic variables of particle j. + * @param dWi Difference between the hydrodynamic variables of particle i at the + * position of particle i and at the interface position. + * @param dWj Difference between the hydrodynamic variables of particle j at the + * position of particle j and at the interface position. + * @param xij_i Relative position vector of the interface w.r.t. particle i. + * @param xij_j Relative position vector of the interface w.r.t. partilce j. + * @param r Distance between particle i and particle j. + */ +__attribute__((always_inline)) INLINE static void hydro_slope_limit_face( + float *Wi, float *Wj, float *dWi, float *dWj, float *xij_i, float *xij_j, + float r) {} + +#endif + +#ifdef SHADOWFAX_SLOPE_LIMITER_CELL_WIDE + +#define HYDRO_SLOPE_LIMITER_CELL_IMPLEMENTATION \ + "Cell wide slope limiter (Springel 2010)" +#include "hydro_slope_limiters_cell.h" + +#else + +#define HYDRO_SLOPE_LIMITER_CELL_IMPLEMENTATION "No cell wide slope limiter" + +/** + * @brief Initialize variables for the cell wide slope limiter + * + * @param p Particle. + */ +__attribute__((always_inline)) INLINE static void hydro_slope_limit_cell_init( + struct part *p) {} + +/** + * @brief Collect information for the cell wide slope limiter during the + * neighbour loop + * + * @param pi Particle i. + * @param pj Particle j. + * @param r Distance between particle i and particle j. + */ +__attribute__((always_inline)) INLINE static void +hydro_slope_limit_cell_collect(struct part *pi, struct part *pj, float r) {} + +/** + * @brief Slope limit cell gradients + * + * @param p Particle. + */ +__attribute__((always_inline)) INLINE static void hydro_slope_limit_cell( + struct part *p) {} + +#endif + +#endif // SWIFT_HYDRO_SLOPE_LIMITERS_H diff --git a/src/hydro/Shadowswift/hydro_slope_limiters_cell.h b/src/hydro/Shadowswift/hydro_slope_limiters_cell.h new file mode 100644 index 0000000000000000000000000000000000000000..a7b3f7511fa7cd247fd9d2399cd200d8d943630e --- /dev/null +++ b/src/hydro/Shadowswift/hydro_slope_limiters_cell.h @@ -0,0 +1,141 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2016 Bert Vandenbroucke (bert.vandenbroucke@gmail.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +#include <float.h> + +/** + * @brief Initialize variables for the cell wide slope limiter + * + * @param p Particle. + */ +__attribute__((always_inline)) INLINE static void hydro_slope_limit_cell_init( + struct part* p) { + + p->primitives.limiter.rho[0] = FLT_MAX; + p->primitives.limiter.rho[1] = -FLT_MAX; + p->primitives.limiter.v[0][0] = FLT_MAX; + p->primitives.limiter.v[0][1] = -FLT_MAX; + p->primitives.limiter.v[1][0] = FLT_MAX; + p->primitives.limiter.v[1][1] = -FLT_MAX; + p->primitives.limiter.v[2][0] = FLT_MAX; + p->primitives.limiter.v[2][1] = -FLT_MAX; + p->primitives.limiter.P[0] = FLT_MAX; + p->primitives.limiter.P[1] = -FLT_MAX; + + p->primitives.limiter.maxr = -FLT_MAX; +} + +/** + * @brief Collect information for the cell wide slope limiter during the + * neighbour loop + * + * @param pi Particle i. + * @param pj Particle j. + * @param r Distance between particle i and particle j. + */ +__attribute__((always_inline)) INLINE static void +hydro_slope_limit_cell_collect(struct part* pi, struct part* pj, float r) { + + /* basic slope limiter: collect the maximal and the minimal value for the + * primitive variables among the ngbs */ + pi->primitives.limiter.rho[0] = + fmin(pj->primitives.rho, pi->primitives.limiter.rho[0]); + pi->primitives.limiter.rho[1] = + fmax(pj->primitives.rho, pi->primitives.limiter.rho[1]); + + pi->primitives.limiter.v[0][0] = + fmin(pj->primitives.v[0], pi->primitives.limiter.v[0][0]); + pi->primitives.limiter.v[0][1] = + fmax(pj->primitives.v[0], pi->primitives.limiter.v[0][1]); + pi->primitives.limiter.v[1][0] = + fmin(pj->primitives.v[1], pi->primitives.limiter.v[1][0]); + pi->primitives.limiter.v[1][1] = + fmax(pj->primitives.v[1], pi->primitives.limiter.v[1][1]); + pi->primitives.limiter.v[2][0] = + fmin(pj->primitives.v[2], pi->primitives.limiter.v[2][0]); + pi->primitives.limiter.v[2][1] = + fmax(pj->primitives.v[2], pi->primitives.limiter.v[2][1]); + + pi->primitives.limiter.P[0] = + fmin(pj->primitives.P, pi->primitives.limiter.P[0]); + pi->primitives.limiter.P[1] = + fmax(pj->primitives.P, pi->primitives.limiter.P[1]); + + pi->primitives.limiter.maxr = fmax(r, pi->primitives.limiter.maxr); +} + +/** + * @brief Apply the cell wide slope limiter to the gradient of a single quantity + * + * This corresponds to equation (B2) in Hopkins (2015). + * + * @param grad Gradient to slope limit + * @param qval Value of the quantity at the cell generator + * @param qmin Minimal value of the quantity among all cell neighbours + * @param qmax Maximal value of the quantity among all cell neighbours + * @param maxr Maximal distance between the generator and all of its neighbours + */ +__attribute__((always_inline)) INLINE static void +hydro_slope_limit_cell_quantity(float* grad, float qval, float qmin, float qmax, + float maxr) { + + float gradtrue, gradmax, gradmin, alpha; + + gradtrue = sqrtf(grad[0] * grad[0] + grad[1] * grad[1] + grad[2] * grad[2]); + if (gradtrue) { + gradtrue *= maxr; + gradmax = qmax - qval; + gradmin = qval - qmin; + alpha = fmin(1.0f, fmin(gradmax / gradtrue, gradmin / gradtrue)); + grad[0] *= alpha; + grad[1] *= alpha; + grad[2] *= alpha; + } +} + +/** + * @brief Slope limit cell gradients + * + * @param p Particle. + */ +__attribute__((always_inline)) INLINE static void hydro_slope_limit_cell( + struct part* p) { + + hydro_slope_limit_cell_quantity( + p->primitives.gradients.rho, p->primitives.rho, + p->primitives.limiter.rho[0], p->primitives.limiter.rho[1], + p->primitives.limiter.maxr); + + hydro_slope_limit_cell_quantity( + p->primitives.gradients.v[0], p->primitives.v[0], + p->primitives.limiter.v[0][0], p->primitives.limiter.v[0][1], + p->primitives.limiter.maxr); + hydro_slope_limit_cell_quantity( + p->primitives.gradients.v[1], p->primitives.v[1], + p->primitives.limiter.v[1][0], p->primitives.limiter.v[1][1], + p->primitives.limiter.maxr); + hydro_slope_limit_cell_quantity( + p->primitives.gradients.v[2], p->primitives.v[2], + p->primitives.limiter.v[2][0], p->primitives.limiter.v[2][1], + p->primitives.limiter.maxr); + + hydro_slope_limit_cell_quantity( + p->primitives.gradients.P, p->primitives.P, p->primitives.limiter.P[0], + p->primitives.limiter.P[1], p->primitives.limiter.maxr); +} diff --git a/src/hydro/Shadowswift/hydro_slope_limiters_face.h b/src/hydro/Shadowswift/hydro_slope_limiters_face.h new file mode 100644 index 0000000000000000000000000000000000000000..7ae5dd2eb073d9aae8ab6f2efffdf8df15b4bb4a --- /dev/null +++ b/src/hydro/Shadowswift/hydro_slope_limiters_face.h @@ -0,0 +1,121 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2016 Bert Vandenbroucke (bert.vandenbroucke@gmail.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +/** + * @brief Slope limit a single quantity at the interface + * + * @param phi_i Value of the quantity at the particle position. + * @param phi_j Value of the quantity at the neighbouring particle position. + * @param phi_mid0 Extrapolated value of the quantity at the interface position. + * @param xij_norm Distance between the particle position and the interface + * position. + * @param r Distance between the particle and its neighbour. + * @return The slope limited difference between the quantity at the particle + * position and the quantity at the interface position. + */ +__attribute__((always_inline)) INLINE static float +hydro_slope_limit_face_quantity(float phi_i, float phi_j, float phi_mid0, + float xij_norm, float r) { + + float delta1, delta2, phimin, phimax, phibar, phiplus, phiminus, phi_mid; + const float psi1 = 0.5f; + const float psi2 = 0.25f; + + if (phi_i == phi_j) { + return 0.0f; + } + + delta1 = psi1 * fabs(phi_i - phi_j); + delta2 = psi2 * fabs(phi_i - phi_j); + + phimin = fmin(phi_i, phi_j); + phimax = fmax(phi_i, phi_j); + + phibar = phi_i + xij_norm / r * (phi_j - phi_i); + + /* if sign(phimax+delta1) == sign(phimax) */ + if ((phimax + delta1) * phimax > 0.0f) { + phiplus = phimax + delta1; + } else { + phiplus = phimax / (1.0f + delta1 / fabs(phimax)); + } + + /* if sign(phimin-delta1) == sign(phimin) */ + if ((phimin - delta1) * phimin > 0.0f) { + phiminus = phimin - delta1; + } else { + phiminus = phimin / (1.0f + delta1 / fabs(phimin)); + } + + if (phi_i < phi_j) { + phi_mid = fmax(phiminus, fmin(phibar + delta2, phi_mid0)); + } else { + phi_mid = fmin(phiplus, fmax(phibar - delta2, phi_mid0)); + } + + return phi_mid - phi_i; +} + +/** + * @brief Slope limit the slopes at the interface between two particles + * + * @param Wi Hydrodynamic variables of particle i. + * @param Wj Hydrodynamic variables of particle j. + * @param dWi Difference between the hydrodynamic variables of particle i at the + * position of particle i and at the interface position. + * @param dWj Difference between the hydrodynamic variables of particle j at the + * position of particle j and at the interface position. + * @param xij_i Relative position vector of the interface w.r.t. particle i. + * @param xij_j Relative position vector of the interface w.r.t. partilce j. + * @param r Distance between particle i and particle j. + */ +__attribute__((always_inline)) INLINE static void hydro_slope_limit_face( + float *Wi, float *Wj, float *dWi, float *dWj, float *xij_i, float *xij_j, + float r) { + + float xij_i_norm, xij_j_norm; + + xij_i_norm = + sqrtf(xij_i[0] * xij_i[0] + xij_i[1] * xij_i[1] + xij_i[2] * xij_i[2]); + + xij_j_norm = + sqrtf(xij_j[0] * xij_j[0] + xij_j[1] * xij_j[1] + xij_j[2] * xij_j[2]); + + dWi[0] = hydro_slope_limit_face_quantity(Wi[0], Wj[0], Wi[0] + dWi[0], + xij_i_norm, r); + dWi[1] = hydro_slope_limit_face_quantity(Wi[1], Wj[1], Wi[1] + dWi[1], + xij_i_norm, r); + dWi[2] = hydro_slope_limit_face_quantity(Wi[2], Wj[2], Wi[2] + dWi[2], + xij_i_norm, r); + dWi[3] = hydro_slope_limit_face_quantity(Wi[3], Wj[3], Wi[3] + dWi[3], + xij_i_norm, r); + dWi[4] = hydro_slope_limit_face_quantity(Wi[4], Wj[4], Wi[4] + dWi[4], + xij_i_norm, r); + + dWj[0] = hydro_slope_limit_face_quantity(Wj[0], Wi[0], Wj[0] + dWj[0], + xij_j_norm, r); + dWj[1] = hydro_slope_limit_face_quantity(Wj[1], Wi[1], Wj[1] + dWj[1], + xij_j_norm, r); + dWj[2] = hydro_slope_limit_face_quantity(Wj[2], Wi[2], Wj[2] + dWj[2], + xij_j_norm, r); + dWj[3] = hydro_slope_limit_face_quantity(Wj[3], Wi[3], Wj[3] + dWj[3], + xij_j_norm, r); + dWj[4] = hydro_slope_limit_face_quantity(Wj[4], Wi[4], Wj[4] + dWj[4], + xij_j_norm, r); +} diff --git a/src/hydro/Shadowswift/voronoi1d_algorithm.h b/src/hydro/Shadowswift/voronoi1d_algorithm.h new file mode 100644 index 0000000000000000000000000000000000000000..74cc5f1dbf3a2d72df55ce73de0321b5493193a7 --- /dev/null +++ b/src/hydro/Shadowswift/voronoi1d_algorithm.h @@ -0,0 +1,192 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2016 Bert Vandenbroucke (bert.vandenbroucke@gmail.com). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +#ifndef SWIFT_VORONOIXD_ALGORITHM_H +#define SWIFT_VORONOIXD_ALGORITHM_H + +#include <math.h> +#include <stdlib.h> +#include "error.h" +#include "inline.h" +#include "voronoi1d_cell.h" + +/** + * @brief Store the extents of the simulation box in the global variables. + * + * @param anchor Corner of the simulation box with the lowest coordinate values. + * @param side Side lengths of the simulation box. + */ +__attribute__((always_inline)) INLINE static void voronoi_set_box( + const float *anchor, const float *side) {} + +/** + * @brief Initialize a 1D Voronoi cell. + * + * Sets the positions of left and right neighbours to very large values, the + * generator position to the given particle position, and all other quantities + * to zero. + * + * @param cell 1D Voronoi cell to initialize. + * @param x Position of the generator of the cell. + * @param anchor Anchor of the simulation box. + * @param side Side lengths of the simulation box. + */ +__attribute__((always_inline)) INLINE void voronoi_cell_init( + struct voronoi_cell *cell, const double *x, const double *anchor, + const double *side) { + cell->x = x[0]; + cell->xL = anchor[0] - cell->x; + cell->xR = anchor[0] + side[0] - cell->x; + cell->idL = 0; + cell->idR = 0; + cell->volume = 0.0f; + cell->centroid = 0.0f; +} + +/** + * @brief Interact a 1D Voronoi cell with a particle with given relative + * position and ID. + * + * This method checks if the given relative position is closer to the cell + * generator than the current left or right neighbour and updates neighbours + * accordingly. + * + * @param cell 1D Voronoi cell. + * @param dx Relative position of the interacting generator w.r.t. the cell + * generator (in fact: dx = generator - neighbour). + * @param id ID of the interacting neighbour. + */ +__attribute__((always_inline)) INLINE void voronoi_cell_interact( + struct voronoi_cell *cell, const float *dx, unsigned long long id) { + + /* Check for stupidity */ + if (dx[0] == 0.0f) { + error("Cannot interact a Voronoi cell generator with itself!"); + } + + if (-dx[0] < 0.0f) { + /* New left neighbour? */ + if (-dx[0] > cell->xL) { + cell->xL = -dx[0]; + cell->idL = id; + } + } else { + /* New right neighbour? */ + if (-dx[0] < cell->xR) { + cell->xR = -dx[0]; + cell->idR = id; + } + } +} + +/** + * @brief Finalize a 1D Voronoi cell. + * + * Calculates the relative positions of the midpoints of the faces (which in + * this case are just the midpoints of the segments connecting the generator + * with the two neighbours) w.r.t. the generator, and the cell volume (length) + * and centroid (midpoint of the segment connecting the midpoints of the faces). + * This function returns the maximal radius at which a particle could still + * change the structure of the cell, i.e. twice the largest distance between + * the cell generator and one of its faces. If the cell has been interacted with + * all neighbours within this radius, we know for sure that the cell is + * complete. + * + * @param cell 1D Voronoi cell. + * @return Maximal radius that could still change the structure of the cell. + */ +__attribute__((always_inline)) INLINE float voronoi_cell_finalize( + struct voronoi_cell *cell) { + + float xL, xR; + float max_radius; + + max_radius = fmax(-cell->xL, cell->xR); + cell->xL = xL = 0.5f * cell->xL; + cell->xR = xR = 0.5f * cell->xR; + + cell->volume = xR - xL; + cell->centroid = cell->x + 0.5f * (xL + xR); + + return max_radius; +} + +/** + * @brief Get the oriented surface area and midpoint of the face between a + * 1D Voronoi cell and the given neighbour. + * + * This function also checks if the given neighbour is in fact a neighbour of + * this cell. Since we perform gradient and flux calculations for all neighbour + * pairs within the smoothing length, which assumes the cell to be spherical, + * it can happen that this is not the case. It is the responsibility of the + * routine that calls this function to check for a zero return value and + * deal with it appropriately. + * + * For this specific case, we simply check if the neighbour is the left or + * right neighbour and set the surface area to 1. The midpoint is set to the + * relative position vector of the appropriate face. + * + * @param cell 1D Voronoi cell. + * @param ngb ID of a particle that is possibly a neighbour of this cell. + * @param midpoint Array to store the relative position of the face in. + * @return 0 if the given neighbour is not a neighbour, surface area 1.0f + * otherwise. + */ +__attribute__((always_inline)) INLINE float voronoi_get_face( + const struct voronoi_cell *cell, unsigned long long ngb, float *midpoint) { + + if (ngb != cell->idL && ngb != cell->idR) { + /* this is perfectly possible: we interact with all particles within the + smoothing length, and they do not need to be all neighbours. + If this happens, we return 0, so that the flux method can return */ + return 0.0f; + } + + if (ngb == cell->idL) { + /* Left face */ + midpoint[0] = cell->xL; + } else { + /* Right face */ + midpoint[0] = cell->xR; + } + /* The other components of midpoint are just zero */ + midpoint[1] = 0.0f; + midpoint[2] = 0.0f; + + return 1.0f; +} + +/** + * @brief Get the centroid of a 1D Voronoi cell. + * + * We store only the relevant coordinate of the centroid, but need to return + * a 3D vector. + * + * @param cell 1D Voronoi cell. + * @param centroid Array to store the centroid in. + */ +__attribute__((always_inline)) INLINE void voronoi_get_centroid( + const struct voronoi_cell *cell, float *centroid) { + + centroid[0] = cell->centroid; + centroid[1] = 0.0f; + centroid[2] = 0.0f; +} + +#endif // SWIFT_VORONOIXD_ALGORITHM_H diff --git a/src/hydro/Shadowswift/voronoi1d_cell.h b/src/hydro/Shadowswift/voronoi1d_cell.h new file mode 100644 index 0000000000000000000000000000000000000000..29fff097c4129b1b3ac81f6e1d4291c4efcbce90 --- /dev/null +++ b/src/hydro/Shadowswift/voronoi1d_cell.h @@ -0,0 +1,48 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2016 Bert Vandenbroucke (bert.vandenbroucke@gmail.com). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +#ifndef SWIFT_VORONOIXD_CELL_H +#define SWIFT_VORONOIXD_CELL_H + +/* 1D Voronoi cell */ +struct voronoi_cell { + + /* The position of the generator of the cell. */ + double x; + + /* The position of the left neighbour of the cell. */ + double xL; + + /* The position of the right neighbour of the cell. */ + double xR; + + /* The particle ID of the left neighbour. */ + unsigned long long idL; + + /* The particle ID of the right neighbour. */ + unsigned long long idR; + + /* The "volume" of the 1D cell. */ + float volume; + + /* The centroid of the cell. */ + float centroid; +}; + +#endif // SWIFT_VORONOIXD_CELL_H diff --git a/src/hydro/Shadowswift/voronoi2d_algorithm.h b/src/hydro/Shadowswift/voronoi2d_algorithm.h new file mode 100644 index 0000000000000000000000000000000000000000..ad4d565fbf817da7b7116a0c619a93b5aee1917c --- /dev/null +++ b/src/hydro/Shadowswift/voronoi2d_algorithm.h @@ -0,0 +1,545 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2017 Bert Vandenbroucke (bert.vandenbroucke@gmail.com). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +#ifndef SWIFT_VORONOIXD_ALGORITHM_H +#define SWIFT_VORONOIXD_ALGORITHM_H + +#include <float.h> +#include <math.h> +#include <stdlib.h> +#include "error.h" +#include "inline.h" +#include "minmax.h" +#include "voronoi2d_cell.h" + +/* Check if the number of vertices exceeds the maximal allowed number */ +#define VORONOI_CHECK_SIZE() \ + if (nvert > VORONOI2D_MAXNUMVERT) { \ + error("Too many vertices!"); \ + } + +/* IDs used to keep track of cells neighbouring walls of the simulation box + This will only work if these IDs are never used for actual particles (which + in practice means you want to have less than 2^63-4 (~9e18) particles in your + simulation) */ +#define VORONOI2D_BOX_LEFT 18446744073709551602llu +#define VORONOI2D_BOX_RIGHT 18446744073709551603llu +#define VORONOI2D_BOX_TOP 18446744073709551604llu +#define VORONOI2D_BOX_BOTTOM 18446744073709551605llu + +/** + * @brief Initialize a 2D Voronoi cell. + * + * @param cell 2D Voronoi cell to initialize. + * @param x Position of the generator of the cell. + * @param anchor Anchor of the simulation box containing all particles. + * @param side Side lengths of the simulation box containing all particles. + */ +__attribute__((always_inline)) INLINE void voronoi_cell_init( + struct voronoi_cell *cell, const double *x, const double *anchor, + const double *side) { + + /* Set the position of the generator of the cell (for reference) */ + cell->x[0] = x[0]; + cell->x[1] = x[1]; + + /* Initialize the cell as a box with the same extents as the simulation box + (note: all vertex coordinates are relative w.r.t. the cell generator) */ + cell->nvert = 4; + + cell->vertices[0][0] = anchor[0] - cell->x[0]; + cell->vertices[0][1] = anchor[1] - cell->x[1]; + + cell->vertices[1][0] = anchor[0] - cell->x[0]; + cell->vertices[1][1] = anchor[1] + side[1] - cell->x[1]; + + cell->vertices[2][0] = anchor[0] + side[0] - cell->x[0]; + cell->vertices[2][1] = anchor[1] + side[1] - cell->x[1]; + + cell->vertices[3][0] = anchor[0] + side[0] - cell->x[0]; + cell->vertices[3][1] = anchor[1] - cell->x[1]; + + /* The neighbours are ordered such that neighbour i shares the face in between + vertices i and i+1 (with last vertex + 1 = first vertex) + We walk around the cell in clockwise direction */ + cell->ngbs[0] = VORONOI2D_BOX_LEFT; + cell->ngbs[1] = VORONOI2D_BOX_TOP; + cell->ngbs[2] = VORONOI2D_BOX_RIGHT; + cell->ngbs[3] = VORONOI2D_BOX_BOTTOM; + + /* These variables are initialized to zero, we will compute them after the + neighbour iteration has finished */ + cell->volume = 0.0f; + cell->centroid[0] = 0.0f; + cell->centroid[1] = 0.0f; +} + +/** + * @brief Interact a 2D Voronoi cell with a particle with given relative + * position and ID. + * + * @param cell 2D Voronoi cell. + * @param dx Relative position of the interacting generator w.r.t. the cell + * generator (in fact: dx = generator - neighbour). + * @param id ID of the interacting neighbour. + */ +__attribute__((always_inline)) INLINE void voronoi_cell_interact( + struct voronoi_cell *cell, const float *dx, unsigned long long id) { + + /* variables used for geometrical tests */ + float half_dx[2]; + float r2; + /* variables used to store test results */ + float test, b1, b2, a1, a2; + /* general loop index */ + int i; + /* variables used to store indices of intersected edges */ + int index_above1, index_above2; + int index_below1, index_below2; + /* variable used to store directionality in edge traversal */ + int increment; + /* new number of vertices and new vertex coordinates */ + int nvert; + float vertices[VORONOI2D_MAXNUMVERT][2]; + unsigned long long ngbs[VORONOI2D_MAXNUMVERT]; + + /* The process of cutting the current cell with the midline of the generator + and the given relative neighbour position proceeds in two steps: + - first we need to locate an edge of the current cell that is intersected + by this midline. Such an edge does not necessarily exist; in this case + the given neighbour is not an actual neighbour of this cell + - Once we have an intersected edge, we create a new edge starting at the + intersection point. We follow the edges connected to the intersected + edge until we find another intersected edge, and use its intersection + point as end point of the new edge. */ + + /* First, we set up some variables that are used to check if a vertex is above + or below the midplane. */ + + /* we need a vector with half the size of the vector joining generator and + neighbour, pointing to the neighbour */ + half_dx[0] = -0.5f * dx[0]; + half_dx[1] = -0.5f * dx[1]; + + /* we need the squared length of this vector */ + r2 = half_dx[0] * half_dx[0] + half_dx[1] * half_dx[1]; + + /* a vertex v = (vx, vy) is above the midline if + vx*half_dx[0] + vy*half_dx[1] > r2 + i.e., if the length of the projected vertex position is longer than the + length of the vector pointing to the closest point on the midline (both + vectors originate at the position of the generator) + the vertex is below the midline if the projected position vector is shorter + if the projected position vector has the same length, the vertex is on the + midline */ + + /* start testing a random vertex: the first one */ + test = cell->vertices[0][0] * half_dx[0] + cell->vertices[0][1] * half_dx[1] - + r2; + if (test < 0.) { +/* vertex is below midline */ +#ifdef VORONOI_VERBOSE + message("First vertex is below midline (%g %g --> %g)!", + cell->vertices[0][0] + cell->x[0], + cell->vertices[0][1] + cell->x[1], test); +#endif + + /* store the test result; we might need it to compute the intersection + coordinates */ + b1 = test; + + /* move on until we find a vertex that is above or on the midline */ + i = 1; + test = cell->vertices[i][0] * half_dx[0] + + cell->vertices[i][1] * half_dx[1] - r2; + while (i < cell->nvert && test < 0.) { + /* make sure we always store the latest test result */ + b1 = test; + ++i; + test = cell->vertices[i][0] * half_dx[0] + + cell->vertices[i][1] * half_dx[1] - r2; + } + + /* loop finished, there are two possibilities: + - i == cell->nvert, all vertices lie below the midline and the given + neighbour is not an actual neighbour of this cell + - test >= 0., we found a vertex above (or on) the midline */ + if (i == cell->nvert) { +/* the given neighbour is not an actual neighbour: exit the routine */ +#ifdef VORONOI_VERBOSE + message("Not a neighbour!"); +#endif + return; + } + + /* we have found an intersected edge: i-1 -> i + we store the index of the vertices above and below the midline, make sure + we store the test result for later intersection computation, and set the + increment to positive, so that we look for the other intersected edge in + clockwise direction */ + index_below1 = i - 1; + index_above1 = i; + a1 = test; + increment = 1; + } else { +/* vertex is above or on midline + in the case where it is on the midline, we count that as above as well: + the vertex will be removed, and a new vertex will be created at the same + position */ +#ifdef VORONOI_VERBOSE + message("First vertex is above midline (%g %g --> %g)!", + cell->vertices[0][0] + cell->x[0], + cell->vertices[0][1] + cell->x[1], test); +#endif + + /* store the test result */ + a1 = test; + + /* move on until we find a vertex that is below the midline */ + i = 1; + test = cell->vertices[i][0] * half_dx[0] + + cell->vertices[i][1] * half_dx[1] - r2; + while (i < cell->nvert && test >= 0.) { + /* make sure we always store the most recent test result */ + a1 = test; + ++i; + test = cell->vertices[i][0] * half_dx[0] + + cell->vertices[i][1] * half_dx[1] - r2; + } + + /* loop finished, there are two possibilities: + - i == cell->nvert, all vertices lie above the midline. This should + never happen. + - test <= 0., we found a vertex below (or on) the midline */ + if (i == cell->nvert) { + /* fatal error! */ + error("Could not find a vertex below the midline!"); + } + + /* we have found an intersected edge: i-1 -> i + we store the index of the vertices above and below the midline, make sure + we store the test result for later intersection computation, and set the + increment to negative, so that we look for the other intersected edge in + counterclockwise direction */ + index_below1 = i; + index_above1 = i - 1; + increment = -1; + b1 = test; + } + +#ifdef VORONOI_VERBOSE + message("First intersected edge: %g %g --> %g %g (%i --> %i)", + cell->vertices[index_below1][0] + cell->x[0], + cell->vertices[index_below1][1] + cell->x[1], + cell->vertices[index_above1][0] + cell->x[0], + cell->vertices[index_above1][1] + cell->x[1], index_below1, + index_above1); +#endif + + /* now we need to find the second intersected edge + we start from the vertex above (or on) the midline and search in the + direction opposite to the intersected edge direction until we find a vertex + below the midline */ + + /* we make sure we store the test result for the second vertex above the + midline as well, since we need this for intersection point computations + the second vertex can be equal to the first */ + a2 = a1; + i = index_above1 + increment; + if (i < 0) { + i = cell->nvert - 1; + } + if (i == cell->nvert) { + i = 0; + } + test = cell->vertices[i][0] * half_dx[0] + cell->vertices[i][1] * half_dx[1] - + r2; + /* this loop can never deadlock, as we know there is at least 1 vertex below + the midline */ + while (test >= 0.) { + /* make sure we always store the most recent test result */ + a2 = test; + i += increment; + if (i < 0) { + i = cell->nvert - 1; + } + if (i == cell->nvert) { + i = 0; + } + test = cell->vertices[i][0] * half_dx[0] + + cell->vertices[i][1] * half_dx[1] - r2; + } + + index_below2 = i; + index_above2 = i - increment; + if (index_above2 < 0) { + index_above2 = cell->nvert - 1; + } + if (index_above2 == cell->nvert) { + index_above2 = 0; + } + /* we also store the test result for the second vertex below the midline */ + b2 = test; + + if (index_above1 == index_above2 && index_below1 == index_below2) { + /* There can be only 1 vertex above or below the midline, but we need 2 + intersected edges, so if the vertices above the midline are the same, the + ones below need to be different and vice versa */ + error("Only 1 intersected edge found!"); + } + + /* there is exactly one degenerate case we have not addressed yet: the case + where index_above1 and index_above2 are the same and are on the midline. + In this case we don't want to create 2 new vertices. Instead, we just keep + index_above1, which basically means nothing happens at all and we can just + return */ + if (index_above1 == index_above2 && a1 == 0.) { + return; + } + + /* to make the code below more clear, we make sure index_above1 always holds + the first vertex to remove, and index_above2 the last one, in clockwise + order + This means we need to interchange 1 and 2 if we were searching in counter- + clockwise direction above */ + if (increment < 0) { + i = index_below1; + index_below1 = index_below2; + index_below2 = i; + i = index_above1; + index_above1 = index_above2; + index_above2 = i; + test = b1; + b1 = b2; + b2 = test; + test = a1; + a1 = a2; + a2 = test; + } + +#ifdef VORONOI_VERBOSE + message("First vertex below: %g %g (%i, %g)", + cell->vertices[index_below1][0] + cell->x[0], + cell->vertices[index_below1][1] + cell->x[1], index_below1, b1); + message("First vertex above: %g %g (%i, %g)", + cell->vertices[index_above1][0] + cell->x[0], + cell->vertices[index_above1][1] + cell->x[1], index_above1, a1); + message("Second vertex below: %g %g (%i, %g)", + cell->vertices[index_below2][0] + cell->x[0], + cell->vertices[index_below2][1] + cell->x[1], index_below2, b2); + message("Second vertex above: %g %g (%i, %g)", + cell->vertices[index_above2][0] + cell->x[0], + cell->vertices[index_above2][1] + cell->x[1], index_above2, a2); +#endif + + if (b1 == 0. || b2 == 0.) { + error("Vertex below midline is on midline!"); + } + + /* convert the test results (which correspond to the projected distance + between the vertex and the midline) to the fractions of the intersected + edges above and below the midline */ + test = a1 / (a1 - b1); + a1 = test; + b1 = 1.0f - test; + + test = a2 / (a2 - b2); + a2 = test; + b2 = 1.0f - test; + + /* remove the vertices above the midline, and insert two new vertices, + corresponding to the intersection points of the intersected edges and the + midline + In practice, we just copy all remaining vertices, starting from the first + vertex below the midline (in clockwise order) */ + nvert = 0; + i = index_below2; + while (i != index_above1) { + vertices[nvert][0] = cell->vertices[i][0]; + vertices[nvert][1] = cell->vertices[i][1]; + ngbs[nvert] = cell->ngbs[i]; + ++nvert; + VORONOI_CHECK_SIZE(); + ++i; + if (i == cell->nvert) { + i = 0; + } + } + /* now add the new vertices, they are always last */ + vertices[nvert][0] = a1 * cell->vertices[index_below1][0] + + b1 * cell->vertices[index_above1][0]; + vertices[nvert][1] = a1 * cell->vertices[index_below1][1] + + b1 * cell->vertices[index_above1][1]; + ngbs[nvert] = id; + ++nvert; + VORONOI_CHECK_SIZE(); + vertices[nvert][0] = a2 * cell->vertices[index_below2][0] + + b2 * cell->vertices[index_above2][0]; + vertices[nvert][1] = a2 * cell->vertices[index_below2][1] + + b2 * cell->vertices[index_above2][1]; + ngbs[nvert] = cell->ngbs[index_above2]; + ++nvert; + VORONOI_CHECK_SIZE(); + + /* overwrite the original vertices */ + cell->nvert = nvert; + for (i = 0; i < cell->nvert; ++i) { + cell->vertices[i][0] = vertices[i][0]; + cell->vertices[i][1] = vertices[i][1]; + cell->ngbs[i] = ngbs[i]; + } +} + +/** + * @brief Finalize a 2D Voronoi cell. + * + * @param cell 2D Voronoi cell. + * @return Maximal radius that could still change the structure of the cell. + */ +__attribute__((always_inline)) INLINE float voronoi_cell_finalize( + struct voronoi_cell *cell) { + + int i; + float vertices[VORONOI2D_MAXNUMVERT][2]; + float A, x[2], y[2], r2, r2max; + + /* make a copy of the vertices (they are overwritten when the face midpoints + are computed */ + for (i = 0; i < cell->nvert; ++i) { + vertices[i][0] = cell->vertices[i][0]; + vertices[i][1] = cell->vertices[i][1]; + } + + r2max = 0.0f; + for (i = 0; i < cell->nvert; ++i) { + if (i < cell->nvert - 1) { + x[0] = vertices[i][0]; + y[0] = vertices[i][1]; + x[1] = vertices[i + 1][0]; + y[1] = vertices[i + 1][1]; + } else { + x[0] = vertices[i][0]; + y[0] = vertices[i][1]; + x[1] = vertices[0][0]; + y[1] = vertices[0][1]; + } + A = x[1] * y[0] - x[0] * y[1]; + cell->volume += A; + cell->centroid[0] += (x[0] + x[1]) * A; + cell->centroid[1] += (y[0] + y[1]) * A; + + /* Note that we only need the RELATIVE positions of the midpoints */ + cell->face_midpoints[i][0] = 0.5f * (x[0] + x[1]); + cell->face_midpoints[i][1] = 0.5f * (y[0] + y[1]); + + r2 = x[0] * x[0] + y[0] * y[0]; + r2max = max(r2max, r2); + + x[0] -= x[1]; + y[0] -= y[1]; + cell->face_lengths[i] = sqrtf(x[0] * x[0] + y[0] * y[0]); + } + + cell->volume *= 0.5f; + A = 6 * cell->volume; + cell->centroid[0] /= A; + cell->centroid[1] /= A; + + cell->centroid[0] += cell->x[0]; + cell->centroid[1] += cell->x[1]; + + return 2.0f * sqrtf(r2max); +} + +/** + * @brief Get the oriented surface area and midpoint of the face between a + * 2D Voronoi cell and the given neighbour. + * + * @param cell 2D Voronoi cell. + * @param ngb ID of a particle that is possibly a neighbour of this cell. + * @param midpoint Array to store the relative position of the face in. + * @return 0 if the given neighbour is not a neighbour, surface area otherwise. + */ +__attribute__((always_inline)) INLINE float voronoi_get_face( + const struct voronoi_cell *cell, unsigned long long ngb, float *midpoint) { + + /* look up the neighbour */ + int i = 0; + while (i < cell->nvert && cell->ngbs[i] != ngb) { + ++i; + } + + if (i == cell->nvert) { + /* The given cell is not a neighbour. */ + return 0.0f; + } + + midpoint[0] = cell->face_midpoints[i][0]; + midpoint[1] = cell->face_midpoints[i][1]; + midpoint[2] = 0.0f; + + return cell->face_lengths[i]; +} + +/** + * @brief Get the centroid of a 2D Voronoi cell. + * + * @param cell 2D Voronoi cell. + * @param centroid Array to store the centroid in. + */ +__attribute__((always_inline)) INLINE void voronoi_get_centroid( + const struct voronoi_cell *cell, float *centroid) { + + centroid[0] = cell->centroid[0]; + centroid[1] = cell->centroid[1]; + centroid[2] = 0.0f; +} + +/******************************************************************************* + ** EXTRA FUNCTIONS USED FOR DEBUGGING ***************************************** + ******************************************************************************/ + +/** + * @brief Print the given cell to the stdout in a format that can be plotted + * using gnuplot. + * + * @param cell voronoi_cell to print. + */ +__attribute__((always_inline)) INLINE void voronoi_print_cell( + const struct voronoi_cell *cell) { + + int i, ip1; + + /* print cell generator */ + printf("%g %g\n\n", cell->x[0], cell->x[1]); + + /* print cell vertices */ + for (i = 0; i < cell->nvert; ++i) { + ip1 = i + 1; + if (ip1 == cell->nvert) { + ip1 = 0; + } + printf("%g %g\n%g %g\n\n", cell->vertices[i][0] + cell->x[0], + cell->vertices[i][1] + cell->x[1], + cell->vertices[ip1][0] + cell->x[0], + cell->vertices[ip1][1] + cell->x[1]); + } +} + +#endif // SWIFT_VORONOIXD_ALGORITHM_H diff --git a/src/hydro/Shadowswift/voronoi2d_cell.h b/src/hydro/Shadowswift/voronoi2d_cell.h new file mode 100644 index 0000000000000000000000000000000000000000..3c54ea8d0aa9ca1c915da1f245f889ebab2073d3 --- /dev/null +++ b/src/hydro/Shadowswift/voronoi2d_cell.h @@ -0,0 +1,58 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2017 Bert Vandenbroucke (bert.vandenbroucke@gmail.com). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +#ifndef SWIFT_VORONOIXD_CELL_H +#define SWIFT_VORONOIXD_CELL_H + +/* Maximal number of vertices (and neighbours) that can be stored in a + voronoi_cell struct. */ +#define VORONOI2D_MAXNUMVERT 100 + +/* 2D Voronoi cell */ +struct voronoi_cell { + + /* The position of the generator of the cell. */ + double x[2]; + + /* The "volume" of the 2D cell. */ + float volume; + + /* The centroid of the cell. */ + float centroid[2]; + + /* Number of cell vertices (and neighbours). */ + int nvert; + + /* We only need to store one of these at the same time. */ + union { + /* The relative positions of the vertices of the cell. */ + float vertices[VORONOI2D_MAXNUMVERT][2]; + + /* The midpoints of the faces. */ + float face_midpoints[VORONOI2D_MAXNUMVERT][2]; + }; + + /* The ids of the neighbouring cells. */ + unsigned long long ngbs[VORONOI2D_MAXNUMVERT]; + + /* The lengths of the faces. */ + float face_lengths[VORONOI2D_MAXNUMVERT]; +}; + +#endif // SWIFT_VORONOIXD_CELL_H diff --git a/src/hydro/Shadowswift/voronoi3d_algorithm.h b/src/hydro/Shadowswift/voronoi3d_algorithm.h new file mode 100644 index 0000000000000000000000000000000000000000..13242ad167f1936d12714af918bce7f41ac77335 --- /dev/null +++ b/src/hydro/Shadowswift/voronoi3d_algorithm.h @@ -0,0 +1,2211 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2016 Bert Vandenbroucke (bert.vandenbroucke@gmail.com). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +#ifndef SWIFT_VORONOIXD_ALGORITHM_H +#define SWIFT_VORONOIXD_ALGORITHM_H + +#include <float.h> +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "error.h" +#include "inline.h" +#include "voronoi3d_cell.h" + +/* For debugging purposes */ +//#define LOOP_CHECK 1000 + +#ifdef LOOP_CHECK +/* We need to do the trickery below to get a unique counter for each call to the + macro. This only works if the macro is never called twice on the same line. + */ +#define MERGE(a, b) a##b +#define LOOPCOUNTER_NAME(line) MERGE(loopcount, line) + +/** + * @brief Increase the given counter variable and check if it is still valid. + * + * @param counter Counter to increase. + * @param line_number Line number where the while is called. + * @return 1 if the counter is still valid, 0 otherwise. + */ +__attribute__((always_inline)) INLINE int check_counter(int *counter, + int line_number) { + ++(*counter); + if ((*counter) == LOOP_CHECK) { + error("Number of iterations reached maximum (=%i) in while on line %i!", + LOOP_CHECK, line_number); + } + return 1; +} + +/* safewhile is a wrapper around a while that adds a unique counter variable to + the loop that is increased by 1 for each time the loop is executed, and + causes the code to crash if this number exceeds a given value. + We use this to quickly enable or disable number of iterations checks for a + large number of while loops */ +#define safewhile(condition) \ + int LOOPCOUNTER_NAME(__LINE__) = 0; \ + while (check_counter(&LOOPCOUNTER_NAME(__LINE__), __LINE__) && (condition)) + +#else /* LOOP_CHECK */ + +/* If LOOP_CHECK is not defined, safewhile and while are EXACTLY the same */ +#define safewhile(condition) while (condition) + +#endif /* LOOP_CHECK */ + +/* This flag activates a number of expensive geometrical checks that help + finding bugs. */ +//#define VORONOI3D_EXPENSIVE_CHECKS + +/* Tolerance parameter used to decide when to use more precise geometric + criteria */ +#define VORONOI3D_TOLERANCE 1.e-6f + +/* Box boundary flags used to signal cells neighbouring the box boundary + These values correspond to the top range of possible 64-bit integers, and + we make the strong assumption that there will never be a particle that has + one of these as particle ID. */ +#define VORONOI3D_BOX_FRONT 18446744073709551600llu +#define VORONOI3D_BOX_BACK 18446744073709551601llu +#define VORONOI3D_BOX_TOP 18446744073709551602llu +#define VORONOI3D_BOX_BOTTOM 18446744073709551603llu +#define VORONOI3D_BOX_LEFT 18446744073709551604llu +#define VORONOI3D_BOX_RIGHT 18446744073709551605llu + +/******************************************************************************* + * 3D specific methods + * + * Most of these methods are based on the source code of voro++: + * http://math.lbl.gov/voro++/ + ******************************************************************************/ + +/** + * @brief Print the given cell to the stderr in a format that can be easily + * plotted using gnuplot. + * + * This method prints to the stderr instead of stdout to make it possible to use + * it right before crashing the code. + * + * @param c Voronoi cell to print. + */ +__attribute__((always_inline)) INLINE void voronoi_print_gnuplot_c( + const struct voronoi_cell *c) { + + int i, j, v; + const double *x = c->x; + + fprintf(stderr, "%g\t%g\t%g\n\n", x[0], x[1], x[2]); + + for (i = 0; i < c->nvert; ++i) { + for (j = 0; j < c->orders[i]; ++j) { + v = c->edges[c->offsets[i] + j]; + if (v < 0) { + v = -v - 1; + } + fprintf(stderr, "%g\t%g\t%g\n", c->vertices[3 * i + 0] + x[0], + c->vertices[3 * i + 1] + x[1], c->vertices[3 * i + 2] + x[2]); + fprintf(stderr, "%g\t%g\t%g\n\n", c->vertices[3 * v + 0] + x[0], + c->vertices[3 * v + 1] + x[1], c->vertices[3 * v + 2] + x[2]); + } + } + fprintf(stderr, "\n"); +} + +/** + * @brief Print the contents of a 3D Voronoi cell + * + * @param cell 3D Voronoi cell + */ +__attribute__((always_inline)) INLINE void voronoi_print_cell( + const struct voronoi_cell *cell) { + + int i, j; + + fprintf(stderr, "x: %g %g %g\n", cell->x[0], cell->x[1], cell->x[2]); + fprintf(stderr, "nvert: %i\n", cell->nvert); + + for (i = 0; i < cell->nvert; ++i) { + fprintf(stderr, "%i: %g %g %g (%i)\n", i, cell->vertices[3 * i], + cell->vertices[3 * i + 1], cell->vertices[3 * i + 2], + cell->orders[i]); + for (j = 0; j < cell->orders[i]; ++j) { + fprintf(stderr, "%i (%i)\n", cell->edges[cell->offsets[i] + j], + cell->edgeindices[cell->offsets[i] + j]); + } + } + fprintf(stderr, "\n"); +} + +/** + * @brief Get the index of the vertex pointed to by the given edge of the given + * vertex. + * + * @param c 3D Voronoi cell. + * @param vertex Index of a vertex of the cell. + * @param edge Edge of that vertex. + * @return Index of the vertex on the other side of the edge. + */ +__attribute__((always_inline)) INLINE int voronoi_get_edge( + const struct voronoi_cell *c, int vertex, int edge) { + return c->edges[c->offsets[vertex] + edge]; +} + +/** + * @brief Get the index of the given edge in the edge list of the vertex on the + * other side of the edge of the given vertex. + * + * Suppose that the given vertex has edges [edge1, edge2, given_edge], and that + * the vertex on the other side of given_edge has edges [edge1, given_edge, + * edge2], then this method returns 1. + * + * @param c 3D Voronoi cell. + * @param vertex Index of a vertex of the cell. + * @param edge Edge of that vertex. + * @return Index of that edge in the edge list of the vertex on the other side + * of the edge. + */ +__attribute__((always_inline)) INLINE int voronoi_get_edgeindex( + const struct voronoi_cell *c, int vertex, int edge) { + return c->edgeindices[c->offsets[vertex] + edge]; +} + +/** + * @brief Set the index of the vertex on the other side of the given edge of the + * given vertex. + * + * @param c 3D Voronoi cell. + * @param vertex Index of a vertex of the cell. + * @param edge Edge of that vertex. + * @param value Index of the vertex on the other side of that edge. + */ +__attribute__((always_inline)) INLINE void voronoi_set_edge( + struct voronoi_cell *c, int vertex, int edge, int value) { + c->edges[c->offsets[vertex] + edge] = value; +} + +/** + * @brief Set the index of the given edge in the edge list of the vertex on the + * other side of the edge of the given vertex. + * + * Suppose that the given vertex has edges [edge1, edge2, given_edge], and we + * want to tell this method that the vertex on the other side of given_edge has + * edges [edge1, given_edge, edge2], then we need to pass on a value of 1 to + * this method. + * + * @param c 3D Voronoi cell. + * @param vertex Index of a vertex of that cell. + * @param edge Edge of that vertex. + * @param value Index of that edge in the edge list of the vertex on the other + * side of the edge. + */ +__attribute__((always_inline)) INLINE void voronoi_set_edgeindex( + struct voronoi_cell *c, int vertex, int edge, int value) { + c->edgeindices[c->offsets[vertex] + edge] = value; +} + +/** + * @brief Get the neighbour for the given edge of the given vertex. + * + * An edge is shared by two faces, and each face has a neighbour. Luckily, each + * edge also has two endpoints, so we can get away with storing only one + * neighbour per endpoint of an edge. We have complete freedom in choosing which + * neighbour to store in which endpoint, but we need to be consistent about it. + * Here we use the following convention: if we take a vector pointing away from + * the given vertex along the given edge direction, then we store the neighbour + * that corresponds to the face to the right if looking to the cell from the + * outside. This is the face that you encounter first when rotating counter- + * clockwise around that vector, starting from outside the cell. + * + * @param c 3D Voronoi cell. + * @param vertex Index of a vertex of that cell. + * @param edge Edge of that vertex. + * @return Index of the neighbour corresponding to that edge and vertex. + */ +__attribute__((always_inline)) INLINE int voronoi_get_ngb( + const struct voronoi_cell *c, int vertex, int edge) { + return c->ngbs[c->offsets[vertex] + edge]; +} + +/** + * @brief Set the neighbour for the given edge of the given vertex. + * + * An edge is shared by two faces, and each face has a neighbour. Luckily, each + * edge also has two endpoints, so we can get away with storing only one + * neighbour per endpoint of an edge. We have complete freedom in choosing which + * neighbour to store in which endpoint, but we need to be consistent about it. + * Here we use the following convention: if we take a vector pointing away from + * the given vertex along the given edge direction, then we store the neighbour + * that corresponds to the face to the right if looking to the cell from the + * outside. This is the face that you encounter first when rotating counter- + * clockwise around that vector, starting from outside the cell. + * + * @param c 3D Voronoi cell. + * @param vertex Index of a vertex of that cell. + * @param edge Edge of that vertex. + * @param value Index of the neighbour corresponding to that edge and vertex. + */ +__attribute__((always_inline)) INLINE void voronoi_set_ngb( + struct voronoi_cell *c, int vertex, int edge, int value) { + c->ngbs[c->offsets[vertex] + edge] = value; +} + +/** + * @brief Check if the 3D Voronoi cell is still consistent. + * + * A cell is consistent if its edges are consistent, i.e. if edge e of vertex v1 + * points to vertex v2, then v2 should have an edge that points to v1 as well, + * and then the edge index of vertex v1 should contain the index of that edge + * in the edge list of v2. We also check if all vertices have orders of at least + * 3, and if all vertices are actually part of the vertex list. + * Oh, and we check if the cell actually has vertices. + * + * @param cell 3D Voronoi cell to check + */ +__attribute__((always_inline)) INLINE void voronoi_check_cell_consistency( + const struct voronoi_cell *c) { + + int i, j, e, l, m; + + if (c->nvert < 4) { + error("Found cell with only %i vertices!", c->nvert); + } + + for (i = 0; i < c->nvert; ++i) { + if (c->orders[i] < 3) { + voronoi_print_cell(c); + error("Found cell with vertex of order %i!", c->orders[i]); + } + for (j = 0; j < c->orders[i]; ++j) { + e = voronoi_get_edge(c, i, j); + if (e >= c->nvert) { + voronoi_print_cell(c); + error("Found cell with edges that lead to non-existing vertices!"); + } + if (e < 0) { + continue; + } + l = voronoi_get_edgeindex(c, i, j); + m = voronoi_get_edge(c, e, l); + if (m != i) { + /* voronoi_print_gnuplot_c(c); */ + voronoi_print_cell(c); + fprintf(stderr, "i: %i, j: %i, e: %i, l: %i, m: %i\n", i, j, e, l, m); + error("Cell inconsistency!"); + } + } + } +} + +/** + * @brief Check if the given vertex is above, below or on the cutting plane + * defined by the given parameters. + * + * @param v Coordinates of a cell vertex, relative w.r.t. the position of the + * generator of the cell. + * @param dx Half of the relative distance vector between the position of the + * generator of the cell and the position of the neighbouring cell that + * generates the cutting plane, pointing from the generator position to the + * cutting plane. + * @param r2 Squared length of dx. + * @param test Variable to store the result of the geometric test in, which + * corresponds to the projected distance between the generator and the vertex + * along dx. + * @param teststack Stack to store the results of the N last tests in (for + * debugging purposes only). + * @param teststack_size Next available field in the teststack, is reset to 0 if + * the teststack is full (so the N+1th results is overwritten; for debugging + * purposes only). + * @return Result of the test: -1 if the vertex is below the cutting plane, +1 + * if it is above, and 0 if it is on the cutting plane. + */ +__attribute__((always_inline)) INLINE int voronoi_test_vertex( + const float *v, const float *dx, float r2, float *test, float *teststack, + int *teststack_size) { + + *test = v[0] * dx[0] + v[1] * dx[1] + v[2] * dx[2] - r2; + + teststack[*teststack_size] = *test; + *teststack_size = *teststack_size + 1; + if (*teststack_size == 2 * VORONOI3D_MAXNUMVERT) { + *teststack_size = 0; + } + + if (*test < -VORONOI3D_TOLERANCE) { + return -1; + } + if (*test > VORONOI3D_TOLERANCE) { + return 1; + } + return 0; +} + +/** + * @brief Initialize the cell as a cube that spans the entire simulation box. + * + * @param c 3D Voronoi cell to initialize. + * @param anchor Anchor of the simulation box. + * @param side Side lengths of the simulation box. + */ +__attribute__((always_inline)) INLINE void voronoi_initialize( + struct voronoi_cell *cell, const double *anchor, const double *side) { + + cell->nvert = 8; + + /* (0, 0, 0) -- 0 */ + cell->vertices[0] = anchor[0] - cell->x[0]; + cell->vertices[1] = anchor[1] - cell->x[1]; + cell->vertices[2] = anchor[2] - cell->x[2]; + + /* (0, 0, 1)-- 1 */ + cell->vertices[3] = anchor[0] - cell->x[0]; + cell->vertices[4] = anchor[1] - cell->x[1]; + cell->vertices[5] = anchor[2] + side[2] - cell->x[2]; + + /* (0, 1, 0) -- 2 */ + cell->vertices[6] = anchor[0] - cell->x[0]; + cell->vertices[7] = anchor[1] + side[1] - cell->x[1]; + cell->vertices[8] = anchor[2] - cell->x[2]; + + /* (0, 1, 1) -- 3 */ + cell->vertices[9] = anchor[0] - cell->x[0]; + cell->vertices[10] = anchor[1] + side[1] - cell->x[1]; + cell->vertices[11] = anchor[2] + side[2] - cell->x[2]; + + /* (1, 0, 0) -- 4 */ + cell->vertices[12] = anchor[0] + side[0] - cell->x[0]; + cell->vertices[13] = anchor[1] - cell->x[1]; + cell->vertices[14] = anchor[2] - cell->x[2]; + + /* (1, 0, 1) -- 5 */ + cell->vertices[15] = anchor[0] + side[0] - cell->x[0]; + cell->vertices[16] = anchor[1] - cell->x[1]; + cell->vertices[17] = anchor[2] + side[2] - cell->x[2]; + + /* (1, 1, 0) -- 6 */ + cell->vertices[18] = anchor[0] + side[0] - cell->x[0]; + cell->vertices[19] = anchor[1] + side[1] - cell->x[1]; + cell->vertices[20] = anchor[2] - cell->x[2]; + + /* (1, 1, 1) -- 7 */ + cell->vertices[21] = anchor[0] + side[0] - cell->x[0]; + cell->vertices[22] = anchor[1] + side[1] - cell->x[1]; + cell->vertices[23] = anchor[2] + side[2] - cell->x[2]; + + cell->orders[0] = 3; + cell->orders[1] = 3; + cell->orders[2] = 3; + cell->orders[3] = 3; + cell->orders[4] = 3; + cell->orders[5] = 3; + cell->orders[6] = 3; + cell->orders[7] = 3; + + /* edges are ordered counterclockwise w.r.t. a vector pointing from the + cell generator to the vertex + (0, 0, 0) corner */ + cell->offsets[0] = 0; + cell->edges[0] = 1; + cell->edges[1] = 2; + cell->edges[2] = 4; + cell->edgeindices[0] = 0; + cell->edgeindices[1] = 2; + cell->edgeindices[2] = 0; + + /* (0, 0, 1) corner */ + cell->offsets[1] = 3; + cell->edges[3] = 0; + cell->edges[4] = 5; + cell->edges[5] = 3; + cell->edgeindices[3] = 0; + cell->edgeindices[4] = 2; + cell->edgeindices[5] = 1; + + /* (0, 1, 0) corner */ + cell->offsets[2] = 6; + cell->edges[6] = 3; + cell->edges[7] = 6; + cell->edges[8] = 0; + cell->edgeindices[6] = 0; + cell->edgeindices[7] = 0; + cell->edgeindices[8] = 1; + + /* (0, 1, 1) corner */ + cell->offsets[3] = 9; + cell->edges[9] = 2; + cell->edges[10] = 1; + cell->edges[11] = 7; + cell->edgeindices[9] = 0; + cell->edgeindices[10] = 2; + cell->edgeindices[11] = 0; + + /* (1, 0, 0) corner */ + cell->offsets[4] = 12; + cell->edges[12] = 0; + cell->edges[13] = 6; + cell->edges[14] = 5; + cell->edgeindices[12] = 2; + cell->edgeindices[13] = 2; + cell->edgeindices[14] = 0; + + /* (1, 0, 1) corner */ + cell->offsets[5] = 15; + cell->edges[15] = 4; + cell->edges[16] = 7; + cell->edges[17] = 1; + cell->edgeindices[15] = 2; + cell->edgeindices[16] = 1; + cell->edgeindices[17] = 1; + + /* (1, 1, 0) corner */ + cell->offsets[6] = 18; + cell->edges[18] = 2; + cell->edges[19] = 7; + cell->edges[20] = 4; + cell->edgeindices[18] = 1; + cell->edgeindices[19] = 2; + cell->edgeindices[20] = 1; + + /* (1, 1, 1) corner */ + cell->offsets[7] = 21; + cell->edges[21] = 3; + cell->edges[22] = 5; + cell->edges[23] = 6; + cell->edgeindices[21] = 2; + cell->edgeindices[22] = 1; + cell->edgeindices[23] = 1; + + /* ngbs[3*i+j] is the neighbour corresponding to the plane clockwise of + edge j of vertex i (when going from edge j to vertex i) + we set them to a ridiculously large value to be able to track faces without + neighbour */ + cell->ngbs[0] = VORONOI3D_BOX_FRONT; /* (000) - (001) */ + cell->ngbs[1] = VORONOI3D_BOX_LEFT; /* (000) - (010) */ + cell->ngbs[2] = VORONOI3D_BOX_BOTTOM; /* (000) - (100) */ + + cell->ngbs[3] = VORONOI3D_BOX_LEFT; /* (001) - (000) */ + cell->ngbs[4] = VORONOI3D_BOX_FRONT; /* (001) - (101) */ + cell->ngbs[5] = VORONOI3D_BOX_TOP; /* (001) - (011) */ + + cell->ngbs[6] = VORONOI3D_BOX_LEFT; /* (010) - (011) */ + cell->ngbs[7] = VORONOI3D_BOX_BACK; /* (010) - (110) */ + cell->ngbs[8] = VORONOI3D_BOX_BOTTOM; /* (010) - (000) */ + + cell->ngbs[9] = VORONOI3D_BOX_BACK; /* (011) - (010) */ + cell->ngbs[10] = VORONOI3D_BOX_LEFT; /* (011) - (001) */ + cell->ngbs[11] = VORONOI3D_BOX_TOP; /* (011) - (111) */ + + cell->ngbs[12] = VORONOI3D_BOX_FRONT; /* (100) - (000) */ + cell->ngbs[13] = VORONOI3D_BOX_BOTTOM; /* (100) - (110) */ + cell->ngbs[14] = VORONOI3D_BOX_RIGHT; /* (100) - (101) */ + + cell->ngbs[15] = VORONOI3D_BOX_FRONT; /* (101) - (100) */ + cell->ngbs[16] = VORONOI3D_BOX_RIGHT; /* (101) - (111) */ + cell->ngbs[17] = VORONOI3D_BOX_TOP; /* (101) - (001) */ + + cell->ngbs[18] = VORONOI3D_BOX_BOTTOM; /* (110) - (010) */ + cell->ngbs[19] = VORONOI3D_BOX_BACK; /* (110) - (111) */ + cell->ngbs[20] = VORONOI3D_BOX_RIGHT; /* (110) - (100) */ + + cell->ngbs[21] = VORONOI3D_BOX_BACK; /* (111) - (011) */ + cell->ngbs[22] = VORONOI3D_BOX_TOP; /* (111) - (101) */ + cell->ngbs[23] = VORONOI3D_BOX_RIGHT; /* (111) - (110) */ +} + +/** + * @brief Find an edge of the voronoi_cell that intersects the cutting plane. + * + * There is a large number of possible paths through this method, each of which + * is covered by a separate unit test in testVoronoi3D. Paths have been numbered + * in the inline comments to help identify them. + * + * @param c 3D Voronoi cell. + * @param dx Vector pointing from pj to the midpoint of the line segment between + * pi and pj. + * @param r2 Squared length of dx. + * @param u Projected distance between the plane and the closest vertex above + * the plane, along dx. + * @param up Index of the closest vertex above the plane. + * @param us Index of the edge of vertex up that intersects the plane. + * @param uw Result of the last test_vertex call for vertex up. + * @param l Projected distance between the plane and the closest vertex below + * the plane, along dx. + * @param lp Index of the closest vertex below the plane. + * @param ls Index of the edge of vertex lp that intersects the plane. + * @param lw Result of the last test_vertex call for vertex lp. + * @param q Projected distance between the plane and a test vertex, along dx. + * @param qp Index of the test vertex. + * @param qs Index of the edge of the test vertex that is connected to up. + * @param qw Result of the last test_vertex call involving qp. + * @return A negative value if an error occurred, 0 if the plane does not + * intersect the cell, 1 if nothing special happened and 2 if we have a + * complicated setup. + */ +__attribute__((always_inline)) INLINE int voronoi_intersect_find_closest_vertex( + struct voronoi_cell *c, const float *dx, float r2, float *u, int *up, + int *us, int *uw, float *l, int *lp, int *ls, int *lw, float *q, int *qp, + int *qs, int *qw) { + + /* stack to store all vertices that have already been tested (debugging + only) */ + float teststack[2 * VORONOI3D_MAXNUMVERT]; + /* size of the used part of the stack */ + int teststack_size = 0; + /* flag signalling a complicated setup */ + int complicated; + + /* test the first vertex: uw = -1 if it is below the plane, 1 if it is above + 0 if it is very close to the plane, and things become complicated... */ + *uw = voronoi_test_vertex(&c->vertices[0], dx, r2, u, teststack, + &teststack_size); + *up = 0; + complicated = 0; + if ((*uw) == 0) { + + /* PATH 0 */ + complicated = 1; + + } else { + + /* two options: either the vertex is above or below the plane */ + + if ((*uw) == 1) { + + /* PATH 1 */ + + /* above: try to find a vertex below + we test all edges of the current vertex stored in up (vertex 0) until + we either find one below the plane or closer to the plane */ + *lp = voronoi_get_edge(c, (*up), 0); + *lw = voronoi_test_vertex(&c->vertices[3 * (*lp)], dx, r2, l, teststack, + &teststack_size); + *us = 1; + /* Not in while: PATH 1.0 */ + /* somewhere in while: PATH 1.1 */ + /* last valid option of while: PATH 1.2 */ + safewhile((*us) < c->orders[(*up)] && (*l) >= (*u)) { + *lp = voronoi_get_edge(c, (*up), (*us)); + *lw = voronoi_test_vertex(&c->vertices[3 * (*lp)], dx, r2, l, teststack, + &teststack_size); + ++(*us); + } + /* we increased us too much, correct this */ + --(*us); + if ((*l) >= (*u)) { + /* PATH 1.3 */ + /* up is the closest vertex to the plane, but is above the plane + since the entire cell is convex, up is the closest vertex of all + vertices of the cell + this means the entire cell is supposedly above the plane, which is + impossible */ + message( + "Cell completely gone! This should not happen. (l >= u, l = %g, u " + "= %g)", + (*l), (*u)); + return -1; + } + /* we know that lp is closer to the plane or below the plane + now find the index of the edge up-lp in the edge list of lp */ + *ls = voronoi_get_edgeindex(c, (*up), (*us)); + + /* if lp is also above the plane, replace up by lp and repeat the process + until lp is below the plane */ + safewhile((*lw) == 1) { + /* PATH 1.4 */ + *u = (*l); + *up = (*lp); + *us = 0; + /* no while: PATH 1.4.0 */ + /* somewhere in while: PATH 1.4.1 */ + /* last valid option of while: PATH 1.4.2 */ + safewhile((*us) < (*ls) && (*l) >= (*u)) { + *lp = voronoi_get_edge(c, (*up), (*us)); + *lw = voronoi_test_vertex(&c->vertices[3 * (*lp)], dx, r2, l, + teststack, &teststack_size); + ++(*us); + } + if ((*l) >= (*u)) { + ++(*us); + /* no while: PATH 1.4.3 */ + /* somewhere in while: PATH 1.4.4 */ + /* last valid option of while: PATH 1.4.5 */ + safewhile((*us) < c->orders[(*up)] && (*l) >= (*u)) { + *lp = voronoi_get_edge(c, (*up), (*us)); + *lw = voronoi_test_vertex(&c->vertices[3 * (*lp)], dx, r2, l, + teststack, &teststack_size); + ++(*us); + } + if ((*l) >= (*u)) { + /* PATH 1.4.6 */ + message( + "Cell completely gone! This should not happen. (l >= u, l = " + "%g, u = %g)", + (*l), (*u)); + return -1; + } + } + --(*us); + *ls = voronoi_get_edgeindex(c, (*up), (*us)); + } + /* if lp is too close to the plane, replace up by lp and proceed to + complicated setup */ + if ((*lw) == 0) { + /* PATH 1.5 */ + *up = (*lp); + complicated = 1; + } + } else { /* if(uw == 1) */ + + /* PATH 2 */ + + /* below: try to find a vertex above + we test all edges of the current vertex stored in up (vertex 0) until + we either find one above the plane or closer to the plane */ + + *qp = voronoi_get_edge(c, (*up), 0); + *qw = voronoi_test_vertex(&c->vertices[3 * (*qp)], dx, r2, q, teststack, + &teststack_size); + *us = 1; + /* not in while: PATH 2.0 */ + /* somewhere in while: PATH 2.1 */ + /* last valid option of while: PATH 2.2 */ + safewhile((*us) < c->orders[(*up)] && (*u) >= (*q)) { + *qp = voronoi_get_edge(c, (*up), (*us)); + *qw = voronoi_test_vertex(&c->vertices[3 * (*qp)], dx, r2, q, teststack, + &teststack_size); + ++(*us); + } + if ((*u) >= (*q)) { + /* PATH 2.3 */ + /* up is the closest vertex to the plane and is below the plane + since the cell is convex, up is the closest vertex of all vertices of + the cell + this means that the entire cell is below the plane + The cell is unaltered. */ + return 0; + } else { + /* the last increase in the loop pushed us too far, correct this */ + --(*us); + } + + /* repeat the above process until qp is closer or above the plane */ + safewhile((*qw) == -1) { + /* PATH 2.4 */ + *qs = voronoi_get_edgeindex(c, (*up), (*us)); + *u = (*q); + *up = (*qp); + *us = 0; + /* no while: PATH 2.4.0 */ + /* somewhere in while: PATH 2.4.1 */ + /* last valid option of while: 2.4.2 */ + safewhile((*us) < (*qs) && (*u) >= (*q)) { + *qp = voronoi_get_edge(c, (*up), (*us)); + *qw = voronoi_test_vertex(&c->vertices[3 * (*qp)], dx, r2, q, + teststack, &teststack_size); + ++(*us); + } + if ((*u) >= (*q)) { + ++(*us); + /* no while: PATH 2.4.3 */ + /* somewhere in while: PATH 2.4.4 */ + /* last valid option of while: PATH 2.4.5 */ + safewhile((*us) < c->orders[(*up)] && (*u) >= (*q)) { + *qp = voronoi_get_edge(c, (*up), (*us)); + *qw = voronoi_test_vertex(&c->vertices[3 * (*qp)], dx, r2, q, + teststack, &teststack_size); + ++(*us); + } + if ((*u) >= (*q)) { + /* PATH 2.4.6 */ + /* cell unaltered */ + return 0; + } + } + --(*us); + } + if ((*qw) == 1) { + /* qp is above the plane: initialize lp to up and replace up by qp */ + *lp = (*up); + *ls = (*us); + *l = (*u); + *up = (*qp); + *us = voronoi_get_edgeindex(c, (*lp), (*ls)); + *u = (*q); + } else { + /* PATH 2.5 */ + /* too close to call: go to complicated setup */ + *up = (*qp); + complicated = 1; + } + + } /* if(uw == 1) */ + + } /* if(uw == 0) */ + + if (complicated) { + return 2; + } else { + return 1; + } +} + +/** + * @brief Intersect the given cell with the midplane between the cell generator + * and a neighbouring cell at the given relative position and with the given ID. + * + * This method is the core of the Voronoi algorithm. If anything goes wrong + * geometrically, it most likely goes wrong somewhere within this method. + * + * @param c 3D Voronoi cell. + * @param odx The original relative distance vector between the cell generator + * and the intersecting neighbour, as it is passed on to runner_iact_density + * (remember: odx = pi->x - pj->x). + * @param ngb ID of the intersecting neighbour (pj->id in runner_iact_density). + */ +__attribute__((always_inline)) INLINE void voronoi_intersect( + struct voronoi_cell *c, const float *odx, unsigned long long ngb) { + + /* vector pointing from pi to the midpoint of the line segment between pi and + pj. This corresponds to -0.5*odx */ + float dx[3]; + /* squared norm of dx */ + float r2; + /* u: distance between the plane and the closest vertex above the plane (up) + l: distance between the plane and the closest vertex below the plane (lp) + q: distance between the plane and the vertex that is currently being + tested (qp) */ + float u = 0.0f, l = 0.0f, q = 0.0f; + /* up: index of the closest vertex above the plane + us: index of the edge of vertex up that intersects the plane + uw: result of the last orientation test involving vertex u + same naming used for vertex l and vertex q */ + int up = -1, us = -1, uw = -1, lp = -1, ls = -1, lw = -1, qp = -1, qs = -1, + qw = -1; + /* auxiliary flag used to capture degeneracies */ + int complicated = -1; + + /* stack to store all vertices that have already been tested (debugging + only) */ + float teststack[2 * VORONOI3D_MAXNUMVERT]; + /* size of the used part of the stack */ + int teststack_size = 0; + +#ifdef VORONOI3D_EXPENSIVE_CHECKS + voronoi_check_cell_consistency(c); +#endif + + /* initialize dx and r2 */ + dx[0] = -0.5f * odx[0]; + dx[1] = -0.5f * odx[1]; + dx[2] = -0.5f * odx[2]; + r2 = dx[0] * dx[0] + dx[1] * dx[1] + dx[2] * dx[2]; + + /* find an intersected edge of the cell */ + int result = voronoi_intersect_find_closest_vertex( + c, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + if (result < 0) { + /* the closest_vertex test only found vertices above the intersecting plane + this would mean that the entire cell lies above the midplane of the line + segment connecting a point inside the cell (the generator) and a point + that could be inside or outside the cell (the neighbour). This is + geometrically absurd and should NEVER happen. */ + voronoi_print_gnuplot_c(c); + error("Error while searching intersected edge!"); + } + if (result == 0) { + /* no intersection */ + return; + } + if (result == 2) { + complicated = 1; + } else { + complicated = 0; + } + + /* At this point: + up contains a vertex above the plane + lp contains a vertex below the plane + us and ls contain the index of the edge that connects up and lp, this edge + is intersected by the midplane + u and l contain the projected distances of up and lp to the midplane, + along dx + IF complicated is 1, up contains a vertex that is considered to be on the + plane. All other variables can be considered to be uninitialized in this + case. */ + + int vindex = -1; + int visitflags[VORONOI3D_MAXNUMVERT]; + int dstack[2 * VORONOI3D_MAXNUMVERT]; + int dstack_size = 0; + float r = 0.0f; + int cs = -1, rp = -1; + int double_edge = 0; + int i = -1, j = -1, k = -1; + + /* initialize visitflags */ + for (i = 0; i < VORONOI3D_MAXNUMVERT; ++i) { + visitflags[i] = 0; + } + + if (complicated) { + + /* We've entered the complicated setup, which means that somewhere along the + way we found a vertex that is on or very close to the midplane. The index + of that vertex is stored in up, all other variables are meaningless at + this point. */ + + /* first of all, we need to find a vertex which has edges that extend below + the plane (since the remainder of our algorithm depends on that). This is + not necessarily the case: in principle a vertex can only have edges that + extend inside or above the plane. + we create a stack of vertices to test (we use dstack for this), and add + vertex up. For each vertex on the stack, we then traverse its edges. If + the edge extends above the plane, we ignore it. If it extends below, we + stop. If the edge lies in the plane, we add the vertex on the other end + to the stack. + We make sure that up contains the index of a vertex extending beyond the + plane on exit. */ + dstack[dstack_size] = up; + ++dstack_size; + lw = 0; + j = 0; + safewhile(j < dstack_size && lw != -1) { + up = dstack[j]; + for (i = 0; i < c->orders[up]; ++i) { + lp = voronoi_get_edge(c, up, i); + lw = voronoi_test_vertex(&c->vertices[3 * lp], dx, r2, &l, teststack, + &teststack_size); + if (lw == -1) { + /* jump out of the for loop */ + break; + } + if (lw == 0) { + /* only add each vertex to the stack once */ + k = 0; + safewhile(k < dstack_size && dstack[k] != lp) { ++k; } + if (k == dstack_size) { + dstack[dstack_size] = lp; + ++dstack_size; + } + } + } + ++j; + } + + /* we increased j after lw was calculated, so only the value of lw should be + used to determine whether or not the loop was successful */ + if (lw != -1) { + /* we did not find an edge that extends below the plane. There are two + possible reasons for this: either all vertices of the cell lie above + or inside the midplane of the segment connecting a point inside the + cell (the generator) with a point inside or outside the cell (the + neighbour). This is geometrically absurd. + Another reason might be that somehow all vertices in the midplane only + have edges that extend outwards. This is contradictory to the fact that + a Voronoi cell is convex, and therefore also unacceptable. + We conclude that we should NEVER end up here. */ + voronoi_print_cell(c); + error("Unable to find a vertex below the midplane!"); + } + /* reset the delete stack, we need it later on */ + dstack_size = 0; + + /* the search routine detected a vertex very close to or in the midplane + the index of this vertex is stored in up + we proceed by checking the edges of this vertex */ + + lp = voronoi_get_edge(c, up, 0); + lw = voronoi_test_vertex(&c->vertices[3 * lp], dx, r2, &l, teststack, + &teststack_size); + + /* the first edge can be below, above or on the plane */ + if (lw != -1) { + + /* above or on the plane: we try to find one below the plane */ + + rp = lw; + i = 1; + lp = voronoi_get_edge(c, up, i); + lw = voronoi_test_vertex(&c->vertices[3 * lp], dx, r2, &l, teststack, + &teststack_size); + safewhile(lw != -1) { + ++i; + if (i == c->orders[up]) { + /* none of the edges of up is below the plane. Since the cell is + supposed to be convex, this means the entire cell is above or on + the plane. This should not happen... + Furthermore, we should really NEVER end up here, as in this case + an error should already have be thrown above. */ + voronoi_print_gnuplot_c(c); + error( + "Cell completely gone! This should not happen. (i == " + "c->order[up], i = %d, c->orders[up] = %d, up = %d)\n" + "dx: [%g %g %g]\nv[up]: [%g %g %g]\nx: [%g %g %g]", + i, c->orders[up], up, dx[0], dx[1], dx[2], c->vertices[3 * up], + c->vertices[3 * up + 1], c->vertices[3 * up + 2], c->x[0], + c->x[1], c->x[2]); + } + lp = voronoi_get_edge(c, up, i); + lw = voronoi_test_vertex(&c->vertices[3 * lp], dx, r2, &l, teststack, + &teststack_size); + } + + /* lp, l and lw now contain values corresponding to an edge below the + plane + rp contains the result of test_vertex for the first edge of up, for + reference */ + + /* we go on to the next edge of up, and see if we can find an edge that + does not extend below the plane */ + + j = i + 1; + safewhile(j < c->orders[up] && lw == -1) { + lp = voronoi_get_edge(c, up, j); + lw = voronoi_test_vertex(&c->vertices[3 * lp], dx, r2, &l, teststack, + &teststack_size); + ++j; + } + + if (lw != -1) { + /* the last iteration increased j by 1 too many, correct this */ + --j; + } + + /* j-i now contains the number of edges below the plane. We will replace + up by a new vertex of order this number + 2 (since 2 new edges will be + created inside the plane) + however, we do not do this if there is exactly one edge that lies in + the plane, and all other edges lie below, because in this case we can + just keep vertex up as is */ + + if (j == c->orders[up] && i == 1 && rp == 0) { + /* keep the order of up, and flag this event for later reference */ + k = c->orders[up]; + double_edge = 1; + } else { + /* general case: keep all edges below the plane, and create 2 new ones + in the plane */ + k = j - i + 2; + } + + /* create new order k vertex */ + vindex = c->nvert; + ++c->nvert; + if (c->nvert == VORONOI3D_MAXNUMVERT) { + error("Too many vertices!"); + } + c->orders[vindex] = k; + c->offsets[vindex] = c->offsets[vindex - 1] + c->orders[vindex - 1]; + if (c->offsets[vindex] + k >= VORONOI3D_MAXNUMEDGE) { + error("Too many edges!"); + } + + visitflags[vindex] = -vindex; + /* the new vertex adopts the coordinates of the old vertex */ + c->vertices[3 * vindex + 0] = c->vertices[3 * up + 0]; + c->vertices[3 * vindex + 1] = c->vertices[3 * up + 1]; + c->vertices[3 * vindex + 2] = c->vertices[3 * up + 2]; + + /* us contains the index of the last edge NOT below the plane + note that i is at least 1, so there is no need to wrap in this case */ + us = i - 1; + + /* copy all edges of up below the plane into the new vertex, starting from + edge 1 (edge 0 is reserved to connect to a newly created vertex + below) */ + k = 1; + safewhile(i < j) { + qp = voronoi_get_edge(c, up, i); + qs = voronoi_get_edgeindex(c, up, i); + voronoi_set_ngb(c, vindex, k, voronoi_get_ngb(c, up, i)); + voronoi_set_edge(c, vindex, k, qp); + voronoi_set_edgeindex(c, vindex, k, qs); + voronoi_set_edge(c, qp, qs, vindex); + voronoi_set_edgeindex(c, qp, qs, k); + /* disconnect up, since this vertex will be removed */ + voronoi_set_edge(c, up, i, -1); + ++i; + ++k; + } + + /* store the index of the first edge not below the plane */ + if (i == c->orders[up]) { + qs = 0; + } else { + qs = i; + } + } else { /* if(lw != -1) */ + + /* the first edge lies below the plane, try to find one that does not */ + + /* we first do a reverse search */ + i = c->orders[up] - 1; + lp = voronoi_get_edge(c, up, i); + lw = voronoi_test_vertex(&c->vertices[3 * lp], dx, r2, &l, teststack, + &teststack_size); + safewhile(lw == -1) { + --i; + if (i == 0) { + /* No edge above or in the plane found: the cell is unaltered */ + return; + } + lp = voronoi_get_edge(c, up, i); + lw = voronoi_test_vertex(&c->vertices[3 * lp], dx, r2, &l, teststack, + &teststack_size); + } + + /* now we do a forward search */ + j = 1; + qp = voronoi_get_edge(c, up, j); + qw = voronoi_test_vertex(&c->vertices[3 * qp], dx, r2, &q, teststack, + &teststack_size); + safewhile(qw == -1) { + ++j; + qp = voronoi_get_edge(c, up, j); + qw = voronoi_test_vertex(&c->vertices[3 * qp], dx, r2, &l, teststack, + &teststack_size); + } + + /* at this point j contains the index of the first edge not below the + plane, i the index of the last edge not below the plane + we use this to compute the number of edges below the plane. up is + replaced by a new vertex that has that number + 2 edges (since 2 new + edges are created inside the plane). We again capture the special event + where there is only one edge not below the plane, which lies inside the + plane. In this case up is copied as is. */ + + if (i == j && qw == 0) { + /* we keep up as is, and flag this event */ + double_edge = 1; + k = c->orders[up]; + } else { + /* (c->orders[up]-1 - i) + j is the number of edges below the plane */ + k = c->orders[up] - i + j + 1; + } + + /* create new order k vertex */ + vindex = c->nvert; + ++c->nvert; + if (c->nvert == VORONOI3D_MAXNUMVERT) { + error("Too many vertices!"); + } + c->orders[vindex] = k; + c->offsets[vindex] = c->offsets[vindex - 1] + c->orders[vindex - 1]; + if (c->offsets[vindex] + k >= VORONOI3D_MAXNUMEDGE) { + error("Too many edges!"); + } + + visitflags[vindex] = -vindex; + /* the new vertex is just a copy of vertex up */ + c->vertices[3 * vindex + 0] = c->vertices[3 * up + 0]; + c->vertices[3 * vindex + 1] = c->vertices[3 * up + 1]; + c->vertices[3 * vindex + 2] = c->vertices[3 * up + 2]; + + /* as above, us stores the index of the last edge NOT below the plane */ + us = i; + + /* copy all edges below the plane into the new vertex, starting from edge + 1 (edge 0 will be connected to a newly created vertex below) + We have to do this in two steps: first we copy the high index edges of + up, then the low index ones (since the edges below the plane are not a + continuous block of indices in this case) */ + k = 1; + ++i; + safewhile(i < c->orders[up]) { + qp = voronoi_get_edge(c, up, i); + qs = voronoi_get_edgeindex(c, up, i); + voronoi_set_ngb(c, vindex, k, voronoi_get_ngb(c, up, i)); + voronoi_set_edge(c, vindex, k, qp); + voronoi_set_edgeindex(c, vindex, k, qs); + voronoi_set_edge(c, qp, qs, vindex); + voronoi_set_edgeindex(c, qp, qs, k); + /* disconnect up, it will be removed */ + voronoi_set_edge(c, up, i, -1); + ++i; + ++k; + } + i = 0; + safewhile(i < j) { + qp = voronoi_get_edge(c, up, i); + qs = voronoi_get_edgeindex(c, up, i); + voronoi_set_ngb(c, vindex, k, voronoi_get_ngb(c, up, i)); + voronoi_set_edge(c, vindex, k, qp); + voronoi_set_edgeindex(c, vindex, k, qs); + voronoi_set_edge(c, qp, qs, vindex); + voronoi_set_edgeindex(c, qp, qs, k); + voronoi_set_edge(c, up, i, -1); + ++i; + ++k; + } + /* qs stores the index of the first edge not below the plane */ + qs = j; + } + + /* at this point, we have created a new vertex that contains all edges of up + below the plane, and two dangling edges: 0 and k + Furthermore, us stores the index of the last edge not below the plane, + qs the index of the first edge not below the plane */ + + /* now set the neighbours for the dangling edge(s) */ + if (!double_edge) { + /* the last edge has the same neighbour as the first edge not below the + plane */ + voronoi_set_ngb(c, vindex, k, voronoi_get_ngb(c, up, qs)); + /* the first edge has the new neighbour as neighbour */ + voronoi_set_ngb(c, vindex, 0, ngb); + } else { + /* up is copied as is, so we also copy its last remaining neighbour */ + voronoi_set_ngb(c, vindex, 0, voronoi_get_ngb(c, up, qs)); + } + + /* add up to the delete stack */ + dstack[dstack_size] = up; + ++dstack_size; + + /* make sure the variables below have the same meaning as they would have + if we had the non complicated setup: + cs contains the index of the last dangling edge of the new vertex + qp and q correspond to the last vertex that has been deleted + qs corresponds to the first edge not below the plane + up and us correspond to the last edge not below the plane, i.e. the edge + that will be the last one to connect to the new vertex + note that the value of i is ignored below, it is just used to temporary + store the new value of up */ + cs = k; + qp = up; + q = u; + i = voronoi_get_edge(c, up, us); + us = voronoi_get_edgeindex(c, up, us); + up = i; + /* we store the index of the newly created vertex in the visitflags of the + last deleted vertex */ + visitflags[qp] = vindex; + } else { /* if(complicated) */ + + if (u == l) { + error("Upper and lower vertex are the same!"); + } + + /* the line joining up and lp has general (vector) equation + x = lp + (up-lp)*t, + with t a parameter ranging from 0 to 1 + we can rewrite this as + x = lp*(1-t) + up*t + the value for t corresponding to the intersection of the line and the + midplane can be found as the ratio of the projected distance between one + of the vertices and the midplane, and the total projected distance + between the two vertices: u-l (remember that u > 0 and l < 0) */ + r = u / (u - l); + l = 1.0f - r; + + if (r > FLT_MAX || r < -FLT_MAX || l > FLT_MAX || l < -FLT_MAX) { + error("Value overflow (r: %g, l: %g)", r, l); + } + + /* create a new order 3 vertex */ + vindex = c->nvert; + ++c->nvert; + if (c->nvert == VORONOI3D_MAXNUMVERT) { + error("Too many vertices!"); + } + c->orders[vindex] = 3; + c->offsets[vindex] = c->offsets[vindex - 1] + c->orders[vindex - 1]; + if (c->offsets[vindex] + 3 >= VORONOI3D_MAXNUMEDGE) { + error("Too many edges!"); + } + + visitflags[vindex] = -vindex; + c->vertices[3 * vindex + 0] = + c->vertices[3 * lp + 0] * r + c->vertices[3 * up + 0] * l; + c->vertices[3 * vindex + 1] = + c->vertices[3 * lp + 1] * r + c->vertices[3 * up + 1] * l; + c->vertices[3 * vindex + 2] = + c->vertices[3 * lp + 2] * r + c->vertices[3 * up + 2] * l; + + /* add vertex up to the delete stack */ + dstack[dstack_size] = up; + ++dstack_size; + + /* connect the new vertex to lp (and update lp as well) */ + voronoi_set_edge(c, vindex, 1, lp); + voronoi_set_edgeindex(c, vindex, 1, ls); + voronoi_set_edge(c, lp, ls, vindex); + voronoi_set_edgeindex(c, lp, ls, 1); + /* disconnect vertex up, it will be deleted */ + voronoi_set_edge(c, up, us, -1); + /* note that we do not connect edges 0 and 2: edge 2 will be connected to + the next new vertex that we created, while edge 0 will be connected to + the last new vertex */ + + /* set neighbour relations for the new vertex: + - edge 0 will be connected to the next intersection point (below), and + hence has pj as ngb + - edge 1 is connected to lp and has the original neighbour of the + intersected edge corresponding to up as neighbour + - edge 2 has the neighbour on the other side of the original intersected + edge as neighbour, which is the same as the neighbour of the edge + corresponding to lp */ + voronoi_set_ngb(c, vindex, 0, ngb); + voronoi_set_ngb(c, vindex, 1, voronoi_get_ngb(c, up, us)); + voronoi_set_ngb(c, vindex, 2, voronoi_get_ngb(c, lp, ls)); + + qs = us + 1; + if (qs == c->orders[up]) { + qs = 0; + } + qp = up; + q = u; + + cs = 2; + + } /* if(complicated) */ + + /* at this point: + qp corresponds to the last vertex that has been deleted + up corresponds to the last vertex that should be used to connect a new + vertex to the newly created vertex above. In the normal case, qp and up + are the same vertex, but qp and up can be different if the newly created + vertex lies in the midplane + qs contains the index of the edge of qp that is next in line to be tested: + the edge that comes after the intersected edge that was deleted above + us corresponds to the edge of up that was connected to the vertex that is + now connected to the newly created vertex above + q contains the projected distance between qp and the midplane, along dx + cs contains the index of the last dangling edge of the last vertex that + was created above; we still need to connect this edge to a vertex below */ + + /* we have found one intersected edge (or at least an edge that lies inside + the midplane) and created one new vertex that lies in the midplane, with + dangling edges. We now try to find other intersected edges and create other + new vertices that will be connected to the first new vertex. */ + + int cp = -1; + int iqs = -1; + int new_double_edge = -1; + + /* cp and rp both contain the index of the last vertex that was created + cp will be updated if we add more vertices, rp will be kept, as we need it + to link the last new vertex to the first new vertex in the end */ + cp = vindex; + rp = vindex; + /* we traverse connections of the first removed vertex, until we arrive at an + edge that links to this vertex (or its equivalent in the degenerate + case) */ + safewhile(qp != up || qs != us) { + /* test the next edge of qp */ + lp = voronoi_get_edge(c, qp, qs); + lw = voronoi_test_vertex(&c->vertices[3 * lp], dx, r2, &l, teststack, + &teststack_size); + if (lw == 0) { + + /* degenerate case: next vertex lies inside the plane */ + + k = 2; + if (double_edge) { + k = 1; + } + /* store the vertex and edge on the other side of the edge in qp and qs */ + qs = voronoi_get_edgeindex(c, qp, qs); + qp = lp; + + /* move on to the next edge of qp and keep the original edge for + reference */ + iqs = qs; + ++qs; + if (qs == c->orders[qp]) { + qs = 0; + } + + /* test the next edges, and try to find one that does NOT lie below the + plane */ + lp = voronoi_get_edge(c, qp, qs); + lw = voronoi_test_vertex(&c->vertices[3 * lp], dx, r2, &l, teststack, + &teststack_size); + safewhile(lw == -1) { + ++k; + ++qs; + if (qs == c->orders[qp]) { + qs = 0; + } + lp = voronoi_get_edge(c, qp, qs); + lw = voronoi_test_vertex(&c->vertices[3 * lp], dx, r2, &l, teststack, + &teststack_size); + } + + /* qs now contains the next edge NOT below the plane + k contains the order of the new vertex to create: the number of edges + below the plane + 2 (+1 if we have a double edge) */ + + /* if qp (the vertex in the plane) was already visited before, visitflags + will contain the index of the newly created vertex that replaces it */ + j = visitflags[qp]; + + /* we need to find out what the order of the new vertex will be, and if we + are dealing with a new double edge or not */ + if (qp == up && qs == us) { + new_double_edge = 0; + if (j > 0) { + k += c->orders[j]; + } + } else { + if (j > 0) { + k += c->orders[j]; + if (lw == 0) { + i = -visitflags[lp]; + if (i > 0) { + if (voronoi_get_edge(c, i, c->orders[i] - 1) == j) { + new_double_edge = 1; + --k; + } else { + new_double_edge = 0; + } + } else { + if (j == rp && lp == up && voronoi_get_edge(c, qp, qs) == us) { + new_double_edge = 1; + --k; + } else { + new_double_edge = 0; + } + } + } else { + new_double_edge = 0; + } + } else { + if (lw == 0) { + i = -visitflags[lp]; + if (i == cp) { + new_double_edge = 1; + --k; + } else { + new_double_edge = 0; + } + } else { + new_double_edge = 0; + } + } + } + + // if (j > 0) { + // error("Case not handled!"); + // } + + /* create new order k vertex */ + vindex = c->nvert; + ++c->nvert; + if (c->nvert == VORONOI3D_MAXNUMVERT) { + error("Too many vertices!"); + } + c->orders[vindex] = k; + c->offsets[vindex] = c->offsets[vindex - 1] + c->orders[vindex - 1]; + if (c->offsets[vindex] + k >= VORONOI3D_MAXNUMEDGE) { + error("Too many edges!"); + } + + visitflags[vindex] = -vindex; + c->vertices[3 * vindex + 0] = c->vertices[3 * qp + 0]; + c->vertices[3 * vindex + 1] = c->vertices[3 * qp + 1]; + c->vertices[3 * vindex + 2] = c->vertices[3 * qp + 2]; + + visitflags[qp] = vindex; + dstack[dstack_size] = qp; + ++dstack_size; + j = vindex; + i = 0; + + if (!double_edge) { + voronoi_set_ngb(c, j, i, ngb); + voronoi_set_edge(c, j, i, cp); + voronoi_set_edgeindex(c, j, i, cs); + voronoi_set_edge(c, cp, cs, j); + voronoi_set_edgeindex(c, cp, cs, i); + ++i; + } + + qs = iqs; + iqs = k - 1; + if (new_double_edge) { + iqs = k; + } + safewhile(i < iqs) { + ++qs; + if (qs == c->orders[qp]) { + qs = 0; + } + lp = voronoi_get_edge(c, qp, qs); + ls = voronoi_get_edgeindex(c, qp, qs); + voronoi_set_ngb(c, j, i, voronoi_get_ngb(c, qp, qs)); + voronoi_set_edge(c, j, i, lp); + voronoi_set_edgeindex(c, j, i, ls); + voronoi_set_edge(c, lp, ls, j); + voronoi_set_edgeindex(c, lp, ls, i); + voronoi_set_edge(c, qp, qs, -1); + ++i; + } + ++qs; + if (qs == c->orders[qp]) { + qs = 0; + } + cs = i; + cp = j; + + if (new_double_edge) { + voronoi_set_ngb(c, j, 0, voronoi_get_ngb(c, qp, qs)); + } else { + voronoi_set_ngb(c, j, cs, voronoi_get_ngb(c, qp, qs)); + } + + double_edge = new_double_edge; + } else { /* if(lw == 0) */ + + /* normal case: next vertex lies below or above the plane */ + + if (lw == 1) { + + /* vertex lies above the plane */ + + /* we just delete the vertex and continue with the next edge of this + vertex */ + + qs = voronoi_get_edgeindex(c, qp, qs) + 1; + if (qs == c->orders[lp]) { + qs = 0; + } + qp = lp; + q = l; + dstack[dstack_size] = qp; + ++dstack_size; + } else { + + /* vertex lies below the plane */ + + /* we have found our next intersected edge: create a new vertex and link + it to the other vertices */ + + if (q == l) { + error("Upper and lower vertex are the same!"); + } + + r = q / (q - l); + l = 1.0f - r; + + if (r > FLT_MAX || r < -FLT_MAX || l > FLT_MAX || l < -FLT_MAX) { + error("Value out of bounds (r: %g, l: %g)!", r, l); + } + + /* create new order 3 vertex */ + vindex = c->nvert; + ++c->nvert; + if (c->nvert == VORONOI3D_MAXNUMVERT) { + error("Too many vertices!"); + } + visitflags[vindex] = -vindex; + c->orders[vindex] = 3; + c->offsets[vindex] = c->offsets[vindex - 1] + c->orders[vindex - 1]; + if (c->offsets[vindex] + 3 >= VORONOI3D_MAXNUMEDGE) { + error("Too many edges!"); + } + + c->vertices[3 * vindex + 0] = + c->vertices[3 * lp + 0] * r + c->vertices[3 * qp + 0] * l; + c->vertices[3 * vindex + 1] = + c->vertices[3 * lp + 1] * r + c->vertices[3 * qp + 1] * l; + c->vertices[3 * vindex + 2] = + c->vertices[3 * lp + 2] * r + c->vertices[3 * qp + 2] * l; + + /* link the edges: + the first edge is connected to the last edge of the previous new + vertex. The last edge will be connected to the next new vertex, and + is left open for the moment */ + ls = voronoi_get_edgeindex(c, qp, qs); + voronoi_set_edge(c, vindex, 0, cp); + voronoi_set_edge(c, vindex, 1, lp); + voronoi_set_edgeindex(c, vindex, 0, cs); + voronoi_set_edgeindex(c, vindex, 1, ls); + voronoi_set_edge(c, lp, ls, vindex); + voronoi_set_edgeindex(c, lp, ls, 1); + voronoi_set_edge(c, cp, cs, vindex); + voronoi_set_edgeindex(c, cp, cs, 0); + voronoi_set_edge(c, qp, qs, -1); + + voronoi_set_ngb(c, vindex, 0, ngb); + voronoi_set_ngb(c, vindex, 1, voronoi_get_ngb(c, qp, qs)); + voronoi_set_ngb(c, vindex, 2, voronoi_get_ngb(c, lp, ls)); + + /* continue with the next edge of qp (the last vertex above the + midplane */ + ++qs; + if (qs == c->orders[qp]) { + qs = 0; + } + /* store the last newly created vertex and its dangling edge for the + next iteration */ + cp = vindex; + cs = 2; + } /* if(lw == 1) */ + + } /* if(lw == 0) */ + + } /* while() */ + + /* we finished adding new vertices. Now connect the last dangling edge of the + last newly created vertex to the first dangling edge of the first newly + created vertex */ + voronoi_set_edge(c, cp, cs, rp); + voronoi_set_edge(c, rp, 0, cp); + voronoi_set_edgeindex(c, cp, cs, 0); + voronoi_set_edgeindex(c, rp, 0, cs); + + /* now remove the vertices in the delete stack */ + + /* the algorithm above did not necessarily visit all vertices above the plane. + here we scan for vertices that are linked to vertices that are to be + removed and add them to the delete stack if necessary + this only works because we made sure that all deleted vertices no longer + have edges that connect them to vertices that need to stay */ + for (i = 0; i < dstack_size; ++i) { + for (j = 0; j < c->orders[dstack[i]]; ++j) { + if (voronoi_get_edge(c, dstack[i], j) >= 0) { + dstack[dstack_size] = voronoi_get_edge(c, dstack[i], j); + ++dstack_size; + voronoi_set_edge(c, dstack[i], j, -1); + voronoi_set_edgeindex(c, dstack[i], j, -1); + } + } + } + + /* collapse order 1 and 2 vertices: vertices with only 1 edge or 2 edges that + can be created during the plane intersection routine */ + /* first flag them */ + int low_order_stack[VORONOI3D_MAXNUMVERT]; + int low_order_index = 0; + for (i = 0; i < c->nvert; ++i) { + if (voronoi_get_edge(c, i, 0) >= 0 && c->orders[i] < 3) { + low_order_stack[low_order_index] = i; + ++low_order_index; + } + } + + /* now remove them */ + safewhile(low_order_index) { + int v = low_order_stack[low_order_index - 1]; + /* the vertex might already have been deleted by a previous operation */ + if (voronoi_get_edge(c, v, 0) < 0) { + --low_order_index; + continue; + } + if (c->orders[v] == 2) { + int j = voronoi_get_edge(c, v, 0); + int k = voronoi_get_edge(c, v, 1); + int b = voronoi_get_edgeindex(c, v, 1); + int l = 0; + safewhile(l < c->orders[j] && voronoi_get_edge(c, j, l) != k) { ++l; } + if (l == c->orders[j]) { + int a = voronoi_get_edgeindex(c, v, 0); + /* j and k are not joined together. Replace their edges pointing to v + with a new edge pointing from j to k */ + voronoi_set_edge(c, j, a, k); + voronoi_set_edgeindex(c, j, a, b); + voronoi_set_edge(c, k, b, j); + voronoi_set_edgeindex(c, k, b, a); + /* no new elements added to the stack: decrease the counter */ + --low_order_index; + } else { + /* just remove the edges from j to v and from k to v: create two new + vertices */ + /* vertex j */ + vindex = c->nvert; + ++c->nvert; + c->vertices[3 * vindex] = c->vertices[3 * j]; + c->vertices[3 * vindex + 1] = c->vertices[3 * j + 1]; + c->vertices[3 * vindex + 2] = c->vertices[3 * j + 2]; + c->orders[vindex] = c->orders[j] - 1; + c->offsets[vindex] = c->offsets[vindex - 1] + c->orders[vindex - 1]; + int m = 0; + for (int n = 0; n < c->orders[j]; ++n) { + int l = voronoi_get_edge(c, j, n); + if (l != v) { + /* make a new edge */ + voronoi_set_edge(c, vindex, m, l); + voronoi_set_edgeindex(c, vindex, m, voronoi_get_edgeindex(c, j, n)); + /* update the other vertex */ + voronoi_set_edge(c, l, voronoi_get_edgeindex(c, j, n), vindex); + voronoi_set_edgeindex(c, l, voronoi_get_edgeindex(c, j, n), m); + /* copy ngb information */ + voronoi_set_ngb(c, vindex, m, voronoi_get_ngb(c, j, n)); + ++m; + } + /* remove the old vertex */ + voronoi_set_edge(c, j, n, -1); + voronoi_set_edgeindex(c, j, n, -1); + } + /* vertex k */ + vindex = c->nvert; + ++c->nvert; + c->vertices[3 * vindex] = c->vertices[3 * k]; + c->vertices[3 * vindex + 1] = c->vertices[3 * k + 1]; + c->vertices[3 * vindex + 2] = c->vertices[3 * k + 2]; + c->orders[vindex] = c->orders[k] - 1; + c->offsets[vindex] = c->offsets[vindex - 1] + c->orders[vindex - 1]; + m = 0; + for (int n = 0; n < c->orders[k]; ++n) { + int l = voronoi_get_edge(c, k, n); + if (l != v) { + /* make a new edge */ + voronoi_set_edge(c, vindex, m, l); + voronoi_set_edgeindex(c, vindex, m, voronoi_get_edgeindex(c, k, n)); + /* update the other vertex */ + voronoi_set_edge(c, l, voronoi_get_edgeindex(c, k, n), vindex); + voronoi_set_edgeindex(c, l, voronoi_get_edgeindex(c, k, n), m); + /* copy ngb information */ + /* this one is special: we copy the ngb corresponding to the + deleted edge and skip the one after that */ + if (n == b + 1) { + voronoi_set_ngb(c, vindex, m, voronoi_get_ngb(c, k, b)); + } else { + voronoi_set_ngb(c, vindex, m, voronoi_get_ngb(c, k, n)); + } + ++m; + } + /* remove the old vertex */ + voronoi_set_edge(c, k, n, -1); + voronoi_set_edgeindex(c, k, n, -1); + } + /* check if j or k has become an order 2 vertex */ + /* if they have become an order 1 vertex, they were already an order 2 + vertex, and they should already be in the list... */ + if (c->orders[vindex] == 2) { + if (c->orders[vindex - 1] == 2) { + low_order_stack[low_order_index] = vindex - 1; + ++low_order_index; + low_order_stack[low_order_index] = vindex; + /* we do not increase the index here: we want this element to be the + next element that is processed */ + } else { + low_order_stack[low_order_index] = vindex; + } + } else { + if (c->orders[vindex - 1] == 2) { + low_order_stack[low_order_index] = vindex - 1; + } else { + /* no new vertices added to the stack: decrease the counter */ + --low_order_index; + } + } + } + /* Remove the vertex */ + voronoi_set_edge(c, v, 0, -1); + voronoi_set_edgeindex(c, v, 0, -1); + voronoi_set_edge(c, v, 1, -1); + voronoi_set_edgeindex(c, v, 1, -1); + } else if (c->orders[v] == 1) { + int j = voronoi_get_edge(c, v, 0); + /* we have to remove the edge between j and v. We create a new vertex */ + vindex = c->nvert; + ++c->nvert; + c->vertices[3 * vindex] = c->vertices[3 * j]; + c->vertices[3 * vindex + 1] = c->vertices[3 * j + 1]; + c->vertices[3 * vindex + 2] = c->vertices[3 * j + 2]; + c->orders[vindex] = c->orders[j] - 1; + c->offsets[vindex] = c->offsets[vindex - 1] + c->orders[vindex - 1]; + int m = 0; + for (int k = 0; k < c->orders[j]; ++k) { + int l = voronoi_get_edge(c, j, k); + if (l != v) { + /* make a new edge */ + voronoi_set_edge(c, vindex, m, l); + voronoi_set_edgeindex(c, vindex, m, voronoi_get_edgeindex(c, j, k)); + /* update the other vertex */ + voronoi_set_edge(c, l, voronoi_get_edgeindex(c, j, k), vindex); + voronoi_set_edgeindex(c, l, voronoi_get_edgeindex(c, j, k), m); + /* copy ngb information */ + voronoi_set_ngb(c, vindex, m, voronoi_get_ngb(c, j, k)); + ++m; + } + /* remove the old vertex */ + voronoi_set_edge(c, j, k, -1); + voronoi_set_edgeindex(c, j, k, -1); + } + /* if the new vertex is a new order 2 vertex, add it to the stack */ + if (c->orders[vindex] == 2) { + low_order_stack[low_order_index - 1] = vindex; + } else { + --low_order_index; + } + /* remove the order 1 vertex */ + voronoi_set_edge(c, v, 0, -1); + voronoi_set_edgeindex(c, v, 0, -1); + } else { + error("Vertex with order %i. This should not happen!", c->orders[v]); + } + } + + /* remove deleted vertices from all arrays */ + struct voronoi_cell new_cell; + /* make sure the contents of the new cell are the same as for the old cell */ + memcpy(&new_cell, c, sizeof(struct voronoi_cell)); + int m, n; + for (vindex = 0; vindex < c->nvert; ++vindex) { + j = vindex; + /* find next edge that is not deleted */ + safewhile(j < c->nvert && voronoi_get_edge(c, j, 0) < 0) { ++j; } + + if (j == c->nvert) { + /* ready */ + break; + } + + /* copy vertices */ + new_cell.vertices[3 * vindex + 0] = c->vertices[3 * j + 0]; + new_cell.vertices[3 * vindex + 1] = c->vertices[3 * j + 1]; + new_cell.vertices[3 * vindex + 2] = c->vertices[3 * j + 2]; + + /* copy order */ + new_cell.orders[vindex] = c->orders[j]; + + /* set offset */ + if (vindex) { + new_cell.offsets[vindex] = + new_cell.offsets[vindex - 1] + new_cell.orders[vindex - 1]; + } else { + new_cell.offsets[vindex] = 0; + } + + /* copy edges, edgeindices and ngbs */ + for (k = 0; k < c->orders[j]; ++k) { + voronoi_set_edge(&new_cell, vindex, k, voronoi_get_edge(c, j, k)); + voronoi_set_edgeindex(&new_cell, vindex, k, + voronoi_get_edgeindex(c, j, k)); + voronoi_set_ngb(&new_cell, vindex, k, voronoi_get_ngb(c, j, k)); + } + + /* update other edges */ + for (k = 0; k < c->orders[j]; ++k) { + m = voronoi_get_edge(c, j, k); + n = voronoi_get_edgeindex(c, j, k); + if (m < vindex) { + voronoi_set_edge(&new_cell, m, n, vindex); + } else { + voronoi_set_edge(c, m, n, vindex); + } + } + + /* deactivate edge */ + voronoi_set_edge(c, j, 0, -1); + } + new_cell.nvert = vindex; + + new_cell.x[0] = c->x[0]; + new_cell.x[1] = c->x[1]; + new_cell.x[2] = c->x[2]; + new_cell.centroid[0] = c->centroid[0]; + new_cell.centroid[1] = c->centroid[1]; + new_cell.centroid[2] = c->centroid[2]; + new_cell.volume = c->volume; + new_cell.nface = c->nface; + + /* Update the cell values. */ + voronoi3d_cell_copy(&new_cell, c); + +#ifdef VORONOI3D_EXPENSIVE_CHECKS + voronoi_check_cell_consistency(c); +#endif +} + +/** + * @brief Get the volume of the tetrahedron made up by the four given vertices. + * + * The vertices are not expected to be oriented in a specific way. If the input + * happens to be coplanar or colinear, the returned volume will just be zero. + * + * @param v1 First vertex. + * @param v2 Second vertex. + * @param v3 Third vertex. + * @param v4 Fourth vertex. + * @return Volume of the tetrahedron. + */ +__attribute__((always_inline)) INLINE float voronoi_volume_tetrahedron( + const float *v1, const float *v2, const float *v3, const float *v4) { + + float V; + float r1[3], r2[3], r3[3]; + + r1[0] = v2[0] - v1[0]; + r1[1] = v2[1] - v1[1]; + r1[2] = v2[2] - v1[2]; + r2[0] = v3[0] - v1[0]; + r2[1] = v3[1] - v1[1]; + r2[2] = v3[2] - v1[2]; + r3[0] = v4[0] - v1[0]; + r3[1] = v4[1] - v1[1]; + r3[2] = v4[2] - v1[2]; + V = fabs(r1[0] * r2[1] * r3[2] + r1[1] * r2[2] * r3[0] + + r1[2] * r2[0] * r3[1] - r1[2] * r2[1] * r3[0] - + r2[2] * r3[1] * r1[0] - r3[2] * r1[1] * r2[0]); + V /= 6.; + return V; +} + +/** + * @brief Get the centroid of the tetrahedron made up by the four given + * vertices. + * + * The centroid is just the average of four vertex coordinates. + * + * @param centroid Array to store the centroid in. + * @param v1 First vertex. + * @param v2 Second vertex. + * @param v3 Third vertex. + * @param v4 Fourth vertex. + */ +__attribute__((always_inline)) INLINE void voronoi_centroid_tetrahedron( + float *centroid, const float *v1, const float *v2, const float *v3, + const float *v4) { + + centroid[0] = 0.25f * (v1[0] + v2[0] + v3[0] + v4[0]); + centroid[1] = 0.25f * (v1[1] + v2[1] + v3[1] + v4[1]); + centroid[2] = 0.25f * (v1[2] + v2[2] + v3[2] + v4[2]); +} + +/** + * @brief Calculate the volume and centroid of a 3D Voronoi cell. + * + * @param cell 3D Voronoi cell. + */ +__attribute__((always_inline)) INLINE void voronoi_calculate_cell( + struct voronoi_cell *cell) { + + float v1[3], v2[3], v3[3], v4[3]; + int i, j, k, l, m, n; + float tcentroid[3]; + float tvol; + + /* we need to calculate the volume of the tetrahedra formed by the first + vertex and the triangles that make up the other faces + since we do not store faces explicitly, this means keeping track of the + edges that have been processed somehow + we follow the method used in voro++ and "flip" processed edges to + negative values + this also means that we need to process all triangles corresponding to + an edge at once */ + cell->volume = 0.0f; + v1[0] = cell->vertices[0]; + v1[1] = cell->vertices[1]; + v1[2] = cell->vertices[2]; + cell->centroid[0] = 0.0f; + cell->centroid[1] = 0.0f; + cell->centroid[2] = 0.0f; + + /* loop over all vertices (except the first one) */ + for (i = 1; i < cell->nvert; ++i) { + + v2[0] = cell->vertices[3 * i + 0]; + v2[1] = cell->vertices[3 * i + 1]; + v2[2] = cell->vertices[3 * i + 2]; + + /* loop over the edges of the vertex*/ + for (j = 0; j < cell->orders[i]; ++j) { + + k = voronoi_get_edge(cell, i, j); + + if (k >= 0) { + + /* mark the edge as processed */ + voronoi_set_edge(cell, i, j, -k - 1); + + l = voronoi_get_edgeindex(cell, i, j) + 1; + if (l == cell->orders[k]) { + l = 0; + } + v3[0] = cell->vertices[3 * k + 0]; + v3[1] = cell->vertices[3 * k + 1]; + v3[2] = cell->vertices[3 * k + 2]; + m = voronoi_get_edge(cell, k, l); + voronoi_set_edge(cell, k, l, -1 - m); + + int loopcount = 0; + safewhile(m != i) { + if (loopcount == 999) { + voronoi_print_cell(cell); + voronoi_print_gnuplot_c(cell); + } + ++loopcount; + n = voronoi_get_edgeindex(cell, k, l) + 1; + if (n == cell->orders[m]) { + n = 0; + } + v4[0] = cell->vertices[3 * m + 0]; + v4[1] = cell->vertices[3 * m + 1]; + v4[2] = cell->vertices[3 * m + 2]; + tvol = voronoi_volume_tetrahedron(v1, v2, v3, v4); + cell->volume += tvol; + voronoi_centroid_tetrahedron(tcentroid, v1, v2, v3, v4); + cell->centroid[0] += tcentroid[0] * tvol; + cell->centroid[1] += tcentroid[1] * tvol; + cell->centroid[2] += tcentroid[2] * tvol; + k = m; + l = n; + v3[0] = v4[0]; + v3[1] = v4[1]; + v3[2] = v4[2]; + m = voronoi_get_edge(cell, k, l); + voronoi_set_edge(cell, k, l, -1 - m); + } /* while() */ + + } /* if(k >= 0) */ + + } /* for(j) */ + + } /* for(i) */ + + cell->centroid[0] /= cell->volume; + cell->centroid[1] /= cell->volume; + cell->centroid[2] /= cell->volume; + + /* centroid was calculated relative w.r.t. particle position */ + cell->centroid[0] += cell->x[0]; + cell->centroid[1] += cell->x[1]; + cell->centroid[2] += cell->x[2]; + + /* Reset the edges: we still need them for the face calculation */ + for (i = 0; i < VORONOI3D_MAXNUMEDGE; ++i) { + if (cell->edges[i] < 0) { + cell->edges[i] = -1 - cell->edges[i]; + } + } +} + +/** + * @brief Calculate the faces for a 3D Voronoi cell. This reorganizes the + * internal variables of the cell, so no new neighbours can be added after + * this method has been called! + * + * Note that the face midpoints are calculated relative w.r.t. the cell + * generator! + * + * @param cell 3D Voronoi cell. + */ +__attribute__((always_inline)) INLINE void voronoi_calculate_faces( + struct voronoi_cell *cell) { + + int i, j, k, l, m, n; + float area; + float midpoint[3]; + float u[3], v[3], w[3]; + float loc_area; + unsigned long long newngbs[VORONOI3D_MAXNUMEDGE]; + + cell->nface = 0; + for (i = 0; i < cell->nvert; ++i) { + + for (j = 0; j < cell->orders[i]; ++j) { + + k = voronoi_get_edge(cell, i, j); + + if (k >= 0) { + + newngbs[cell->nface] = voronoi_get_ngb(cell, i, j); + area = 0.; + midpoint[0] = 0.; + midpoint[1] = 0.; + midpoint[2] = 0.; + voronoi_set_edge(cell, i, j, -1 - k); + l = voronoi_get_edgeindex(cell, i, j) + 1; + if (l == cell->orders[k]) { + l = 0; + } + m = voronoi_get_edge(cell, k, l); + voronoi_set_edge(cell, k, l, -1 - m); + + safewhile(m != i) { + n = voronoi_get_edgeindex(cell, k, l) + 1; + if (n == cell->orders[m]) { + n = 0; + } + u[0] = cell->vertices[3 * k + 0] - cell->vertices[3 * i + 0]; + u[1] = cell->vertices[3 * k + 1] - cell->vertices[3 * i + 1]; + u[2] = cell->vertices[3 * k + 2] - cell->vertices[3 * i + 2]; + v[0] = cell->vertices[3 * m + 0] - cell->vertices[3 * i + 0]; + v[1] = cell->vertices[3 * m + 1] - cell->vertices[3 * i + 1]; + v[2] = cell->vertices[3 * m + 2] - cell->vertices[3 * i + 2]; + w[0] = u[1] * v[2] - u[2] * v[1]; + w[1] = u[2] * v[0] - u[0] * v[2]; + w[2] = u[0] * v[1] - u[1] * v[0]; + loc_area = sqrtf(w[0] * w[0] + w[1] * w[1] + w[2] * w[2]); + area += loc_area; + midpoint[0] += loc_area * (cell->vertices[3 * k + 0] + + cell->vertices[3 * i + 0] + + cell->vertices[3 * m + 0]); + midpoint[1] += loc_area * (cell->vertices[3 * k + 1] + + cell->vertices[3 * i + 1] + + cell->vertices[3 * m + 1]); + midpoint[2] += loc_area * (cell->vertices[3 * k + 2] + + cell->vertices[3 * i + 2] + + cell->vertices[3 * m + 2]); + k = m; + l = n; + m = voronoi_get_edge(cell, k, l); + voronoi_set_edge(cell, k, l, -1 - m); + } + + cell->face_areas[cell->nface] = 0.5f * area; + cell->face_midpoints[cell->nface][0] = midpoint[0] / area / 3.0f; + cell->face_midpoints[cell->nface][1] = midpoint[1] / area / 3.0f; + cell->face_midpoints[cell->nface][2] = midpoint[2] / area / 3.0f; + ++cell->nface; + + if (cell->nface == VORONOI3D_MAXFACE) { + error("Too many faces!"); + } + + } /* if(k >= 0) */ + + } /* for(j) */ + + } /* for(i) */ + + /* Overwrite the old neighbour array. */ + for (i = 0; i < cell->nface; ++i) { + cell->ngbs[i] = newngbs[i]; + } +} + +/******************************************************************************* + * voronoi_algorithm interface implementations + * + * If you change any function parameters below, you also have to change them in + * the 1D and 2D algorithm! + ******************************************************************************/ + +/** + * @brief Initialize a 3D Voronoi cell. + * + * @param cell 3D Voronoi cell to initialize. + * @param x Position of the generator of the cell. + * @param anchor Anchor of the simulation box. + * @param side Side lengths of the simulation box. + */ +__attribute__((always_inline)) INLINE void voronoi_cell_init( + struct voronoi_cell *cell, const double *x, const double *anchor, + const double *side) { + + cell->x[0] = x[0]; + cell->x[1] = x[1]; + cell->x[2] = x[2]; + + voronoi_initialize(cell, anchor, side); + + cell->volume = 0.0f; + cell->centroid[0] = 0.0f; + cell->centroid[1] = 0.0f; + cell->centroid[2] = 0.0f; + cell->nface = 0; +} + +/** + * @brief Interact a 3D Voronoi cell with a particle with given relative + * position and ID. + * + * @param cell 3D Voronoi cell. + * @param dx Relative position of the interacting generator w.r.t. the cell + * generator (in fact: dx = generator - neighbour). + * @param id ID of the interacting neighbour. + */ +__attribute__((always_inline)) INLINE void voronoi_cell_interact( + struct voronoi_cell *cell, const float *dx, unsigned long long id) { + + voronoi_intersect(cell, dx, id); +} + +/** + * @brief Finalize a 3D Voronoi cell. + * + * @param cell 3D Voronoi cell. + * @return Maximal radius that could still change the structure of the cell. + */ +__attribute__((always_inline)) INLINE float voronoi_cell_finalize( + struct voronoi_cell *cell) { + + int i; + float max_radius, v[3], v2; + + /* Calculate the volume and centroid of the cell. */ + voronoi_calculate_cell(cell); + /* Calculate the faces. */ + voronoi_calculate_faces(cell); + + /* Loop over the vertices and calculate the maximum radius. */ + max_radius = 0.0f; + for (i = 0; i < cell->nvert; ++i) { + v[0] = cell->vertices[3 * i]; + v[1] = cell->vertices[3 * i + 1]; + v[2] = cell->vertices[3 * i + 2]; + v2 = v[0] * v[0] + v[1] * v[1] + v[2] * v[2]; + max_radius = fmaxf(max_radius, v2); + } + max_radius = sqrtf(max_radius); + + return 2.0f * max_radius; +} + +/** + * @brief Get the surface area and midpoint of the face between a 3D Voronoi + * cell and the given neighbour. + * + * @param cell 3D Voronoi cell. + * @param ngb ID of a particle that is possibly a neighbour of this cell. + * @param midpoint Array to store the relative position of the face in. + * @return 0 if the given neighbour is not a neighbour, the surface area of + * the face otherwise. + */ +__attribute__((always_inline)) INLINE float voronoi_get_face( + const struct voronoi_cell *cell, unsigned long long ngb, float *midpoint) { + + int i = 0; + while (i < cell->nface && cell->ngbs[i] != ngb) { + ++i; + } + if (i == cell->nface) { + /* Ngb not found */ + return 0.0f; + } + + midpoint[0] = cell->face_midpoints[i][0]; + midpoint[1] = cell->face_midpoints[i][1]; + midpoint[2] = cell->face_midpoints[i][2]; + + return cell->face_areas[i]; +} + +/** + * @brief Get the centroid of a 3D Voronoi cell. + * + * @param cell 3D Voronoi cell. + * @param centroid Array to store the centroid in. + */ +__attribute__((always_inline)) INLINE void voronoi_get_centroid( + const struct voronoi_cell *cell, float *centroid) { + + centroid[0] = cell->centroid[0]; + centroid[1] = cell->centroid[1]; + centroid[2] = cell->centroid[2]; +} + +#endif // SWIFT_VORONOIXD_ALGORITHM_H diff --git a/src/hydro/Shadowswift/voronoi3d_cell.h b/src/hydro/Shadowswift/voronoi3d_cell.h new file mode 100644 index 0000000000000000000000000000000000000000..ef43eff1745f48219af14aec2455aaa5e5b0d47a --- /dev/null +++ b/src/hydro/Shadowswift/voronoi3d_cell.h @@ -0,0 +1,143 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2016 Bert Vandenbroucke (bert.vandenbroucke@gmail.com). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +#ifndef SWIFT_VORONOIXD_CELL_H +#define SWIFT_VORONOIXD_CELL_H + +/* Maximal number of neighbours that can be stored in a voronoi_cell struct */ +#define VORONOI3D_MAXNUMNGB 100 +/* Maximal number of vertices that can be stored in a voronoi_cell struct */ +#define VORONOI3D_MAXNUMVERT 500 +/* Maximal number of edges that can be stored in a voronoi_cell struct */ +#define VORONOI3D_MAXNUMEDGE 1500 +/* Maximal number of faces that can be stored in a voronoi_cell struct */ +#define VORONOI3D_MAXFACE 100 + +/* 3D Voronoi cell */ +struct voronoi_cell { + + /* The position of the generator of the cell. */ + double x[3]; + + /* The volume of the 3D cell. */ + float volume; + + /* The centroid of the cell. */ + float centroid[3]; + + /* Number of cell vertices. */ + int nvert; + + /* Vertex coordinates. */ + float vertices[3 * VORONOI3D_MAXNUMVERT]; + + /* Number of edges for every vertex. */ + char orders[VORONOI3D_MAXNUMVERT]; + + /* Offsets of the edges, edgeindices and neighbours corresponding to a + particular vertex in the internal arrays */ + int offsets[VORONOI3D_MAXNUMVERT]; + + /* Edge information. Edges are ordered counterclockwise w.r.t. a vector + pointing from the cell generator to the vertex. */ + int edges[VORONOI3D_MAXNUMEDGE]; + + /* Additional edge information. */ + char edgeindices[VORONOI3D_MAXNUMEDGE]; + + /* Neighbour information. This field is used differently depending on where we + are in the algorithm. During cell construction, it contains, for every edge + of every vertex, the index of the neighbour that generates the face + counterclockwise of the edge w.r.t. a vector pointing from the vertex along + the edge. After cell finalization, it contains a neighbour for every face, + in the same order as the face_areas and face_midpoints arrays. */ + unsigned long long ngbs[VORONOI3D_MAXNUMEDGE]; + + /* Number of faces of the cell. */ + unsigned char nface; + + /* Surface areas of the cell faces. */ + float face_areas[VORONOI3D_MAXFACE]; + + /* Midpoints of the cell faces. */ + float face_midpoints[VORONOI3D_MAXFACE][3]; +}; + +/** + * @brief Copy the contents of the 3D Voronoi cell pointed to by source into the + * 3D Voronoi cell pointed to by destination + * + * @param source Pointer to a 3D Voronoi cell to read from. + * @param destination Pointer to a 3D Voronoi cell to write to. + */ +__attribute__((always_inline)) INLINE void voronoi3d_cell_copy( + struct voronoi_cell *source, struct voronoi_cell *destination) { + + /* Copy the position of the generator of the cell. */ + destination->x[0] = source->x[0]; + destination->x[1] = source->x[1]; + destination->x[2] = source->x[2]; + + /* Copy the volume of the 3D cell. */ + destination->volume = source->volume; + + /* Copy the centroid of the cell. */ + destination->centroid[0] = source->centroid[0]; + destination->centroid[1] = source->centroid[1]; + destination->centroid[2] = source->centroid[2]; + + /* Copy the number of cell vertices. */ + destination->nvert = source->nvert; + + /* Copy the vertex coordinates. We only copy the 3*nvert first coordinates. */ + for (int i = 0; i < 3 * source->nvert; ++i) { + destination->vertices[i] = source->vertices[i]; + } + + /* Copy the number of edges for every vertex. Again, we only copy the nvert + first values. */ + for (int i = 0; i < source->nvert; ++i) { + destination->orders[i] = source->orders[i]; + } + + /* Copy the nvert first values of the offsets. */ + for (int i = 0; i < source->nvert; ++i) { + destination->offsets[i] = source->offsets[i]; + } + + /* Copy the edge information. No idea how many edges we have, so we copy + everything. */ + for (int i = 0; i < VORONOI3D_MAXNUMEDGE; ++i) { + destination->edges[i] = source->edges[i]; + } + + /* Copy all additional edge information. */ + for (int i = 0; i < VORONOI3D_MAXNUMEDGE; ++i) { + destination->edgeindices[i] = source->edgeindices[i]; + } + + /* Copy neighbour information. Since neighbours are stored per edge, the total + number of neighbours in this list is larger than numngb and we copy + everything. */ + for (int i = 0; i < VORONOI3D_MAXNUMEDGE; ++i) { + destination->ngbs[i] = source->ngbs[i]; + } +} + +#endif // SWIFT_VORONOIXD_CELL_H diff --git a/src/hydro/Shadowswift/voronoi_algorithm.h b/src/hydro/Shadowswift/voronoi_algorithm.h new file mode 100644 index 0000000000000000000000000000000000000000..19ecc723741e6c91b19e85f6457311a80c6faf34 --- /dev/null +++ b/src/hydro/Shadowswift/voronoi_algorithm.h @@ -0,0 +1,33 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2016 Bert Vandenbroucke (bert.vandenbroucke@gmail.com). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +#ifndef SWIFT_VORONOI_ALGORITHM_H +#define SWIFT_VORONOI_ALGORITHM_H + +#if defined(HYDRO_DIMENSION_1D) +#include "voronoi1d_algorithm.h" +#elif defined(HYDRO_DIMENSION_2D) +#include "voronoi2d_algorithm.h" +#elif defined(HYDRO_DIMENSION_3D) +#include "voronoi3d_algorithm.h" +#else +#error "You have to select a dimension for the hydro!" +#endif + +#endif // SWIFT_VORONOI_ALGORITHM_H diff --git a/src/hydro/Shadowswift/voronoi_cell.h b/src/hydro/Shadowswift/voronoi_cell.h new file mode 100644 index 0000000000000000000000000000000000000000..30d3e17fdfa76448773c0e45834ddf732989a3a4 --- /dev/null +++ b/src/hydro/Shadowswift/voronoi_cell.h @@ -0,0 +1,33 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2016 Bert Vandenbroucke (bert.vandenbroucke@gmail.com). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +#ifndef SWIFT_VORONOI_CELL_H +#define SWIFT_VORONOI_CELL_H + +#if defined(HYDRO_DIMENSION_1D) +#include "voronoi1d_cell.h" +#elif defined(HYDRO_DIMENSION_2D) +#include "voronoi2d_cell.h" +#elif defined(HYDRO_DIMENSION_3D) +#include "voronoi3d_cell.h" +#else +#error "You have to select a dimension for the hydro!" +#endif + +#endif // SWIFT_VORONOI_CELL_H diff --git a/src/hydro_io.h b/src/hydro_io.h index 05ae94ade7b103ff1b584dc2447cbab40479d1fc..202d724f821b570b427210bb48b7070563513458 100644 --- a/src/hydro_io.h +++ b/src/hydro_io.h @@ -32,6 +32,8 @@ #include "./hydro/Default/hydro_io.h" #elif defined(GIZMO_SPH) #include "./hydro/Gizmo/hydro_io.h" +#elif defined(SHADOWFAX_SPH) +#include "./hydro/Shadowswift/hydro_io.h" #else #error "Invalid choice of SPH variant" #endif diff --git a/src/hydro_properties.c b/src/hydro_properties.c index 46785b4b2d5b958f6db3bd9813d139575217d6fe..818c1b6349192ed73b28cd4c3ae771f89a3754cd 100644 --- a/src/hydro_properties.c +++ b/src/hydro_properties.c @@ -44,6 +44,12 @@ void hydro_props_init(struct hydro_props *p, p->target_neighbours = pow_dimension(p->eta_neighbours) * kernel_norm; p->delta_neighbours = parser_get_param_float(params, "SPH:delta_neighbours"); +#ifdef SHADOWFAX_SPH + /* change the meaning of target_neighbours and delta_neighbours */ + p->target_neighbours = 1.0f; + p->delta_neighbours = 0.0f; +#endif + /* Maximal smoothing length */ p->h_max = parser_get_opt_param_float(params, "SPH:h_max", hydro_props_default_h_max); diff --git a/src/hydro_space.c b/src/hydro_space.c new file mode 100644 index 0000000000000000000000000000000000000000..c4a6f9c1495a44050c5477ebfdf0bb76a64bfc51 --- /dev/null +++ b/src/hydro_space.c @@ -0,0 +1,52 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2017 Bert Vandenbroucke (bert.vandenbroucke@gmail.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +#include "hydro_space.h" +#include "space.h" + +/** + * @brief Initialize the extra space information needed for some hydro schemes. + * + * @param hs #hydro_space to initialize. + * @param s #space containing the hydro space. + */ +#ifdef SHADOWFAX_SPH +__attribute__((always_inline)) INLINE void hydro_space_init( + struct hydro_space *hs, const struct space *s) { + + if (s->periodic) { + hs->anchor[0] = -0.5f * s->dim[0]; + hs->anchor[1] = -0.5f * s->dim[1]; + hs->anchor[2] = -0.5f * s->dim[2]; + hs->side[0] = 2.0f * s->dim[0]; + hs->side[1] = 2.0f * s->dim[1]; + hs->side[2] = 2.0f * s->dim[2]; + } else { + hs->anchor[0] = 0.0f; + hs->anchor[1] = 0.0f; + hs->anchor[2] = 0.0f; + hs->side[0] = s->dim[0]; + hs->side[1] = s->dim[1]; + hs->side[2] = s->dim[2]; + } +} +#else +__attribute__((always_inline)) INLINE void hydro_space_init( + struct hydro_space *hs, const struct space *s) {} +#endif diff --git a/src/hydro_space.h b/src/hydro_space.h new file mode 100644 index 0000000000000000000000000000000000000000..e64b532d16a5b78526dfed22d99558a768d07400 --- /dev/null +++ b/src/hydro_space.h @@ -0,0 +1,43 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2017 Bert Vandenbroucke (bert.vandenbroucke@gmail.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ +#ifndef SWIFT_HYDRO_SPACE_H +#define SWIFT_HYDRO_SPACE_H + +#include "../config.h" + +struct space; + +/** + * @brief Extra space information that is needed for some hydro schemes. + */ +#ifdef SHADOWFAX_SPH +struct hydro_space { + /*! Anchor of the simulation space. */ + double anchor[3]; + + /*! Side lengths of the simulation space. */ + double side[3]; +}; +#else +struct hydro_space {}; +#endif + +void hydro_space_init(struct hydro_space *hs, const struct space *s); + +#endif /* SWIFT_HYDRO_SPACE_H */ diff --git a/src/parallel_io.c b/src/parallel_io.c index dbd029569e0947cd5d5884ad392bedc81ab04468..b857fd76a53738b19e5b26b8717881e71c424b6e 100644 --- a/src/parallel_io.c +++ b/src/parallel_io.c @@ -441,6 +441,7 @@ void read_ic_parallel(char* fileName, const struct unit_system* internal_units, N_total[ptype] = (numParticles[ptype]) + (numParticles_highWord[ptype] << 32); + /* Get the box size if not cubic */ dim[0] = boxSize[0]; dim[1] = (boxSize[1] < 0) ? boxSize[0] : boxSize[1]; dim[2] = (boxSize[2] < 0) ? boxSize[0] : boxSize[2]; diff --git a/src/part.h b/src/part.h index e9a151f5b6dc6c2670ced942d195e108555c5a4b..1b40aee0db3deb4790e07e3da9807060900d0c55 100644 --- a/src/part.h +++ b/src/part.h @@ -58,6 +58,10 @@ #include "./hydro/Gizmo/hydro_part.h" #define hydro_need_extra_init_loop 0 #define EXTRA_HYDRO_LOOP +#elif defined(SHADOWFAX_SPH) +#include "./hydro/Shadowswift/hydro_part.h" +#define hydro_need_extra_init_loop 0 +#define EXTRA_HYDRO_LOOP #else #error "Invalid choice of SPH variant" #endif diff --git a/src/partition.c b/src/partition.c index a265f015f44d0ab39106e4fa2b302ec11c23a556..49dbf883e0dea3f00c93ca33ec8cb0248bbfbfaa 100644 --- a/src/partition.c +++ b/src/partition.c @@ -995,7 +995,7 @@ void partition_init(struct partition *partition, /* Defaults make use of METIS if available */ #ifdef HAVE_METIS - const char *default_repart = "both"; + const char *default_repart = "task_weights"; const char *default_part = "simple_metis"; #else const char *default_repart = "none"; diff --git a/src/runner.c b/src/runner.c index 4d8c839549e3057c3a4128626a635f33ee77650e..91893c8c10d62cb1fd86f1fe70894fcdbfe0c8e3 100644 --- a/src/runner.c +++ b/src/runner.c @@ -489,6 +489,7 @@ void runner_do_init(struct runner *r, struct cell *c, int timer) { const int count = c->count; const int gcount = c->gcount; const struct engine *e = r->e; + const struct space *s = e->s; TIMER_TIC; @@ -514,7 +515,7 @@ void runner_do_init(struct runner *r, struct cell *c, int timer) { if (part_is_active(p, e)) { /* Get ready for a density calculation */ - hydro_init_part(p); + hydro_init_part(p, &s->hs); } } @@ -596,6 +597,7 @@ void runner_do_ghost(struct runner *r, struct cell *c, int timer) { struct part *restrict parts = c->parts; struct xpart *restrict xparts = c->xparts; const struct engine *e = r->e; + const struct space *s = e->s; const float hydro_h_max = e->hydro_properties->h_max; const float target_wcount = e->hydro_properties->target_neighbours; const float max_wcount = @@ -676,7 +678,7 @@ void runner_do_ghost(struct runner *r, struct cell *c, int timer) { redo += 1; /* Re-initialise everything */ - hydro_init_part(p); + hydro_init_part(p, &s->hs); /* Off we go ! */ continue; diff --git a/src/space.c b/src/space.c index fec47f28b26e0bf22265cb765525b913acd7a638..64a9ab15c960e7664afdf6be4293bbad3176fc76 100644 --- a/src/space.c +++ b/src/space.c @@ -2692,6 +2692,8 @@ void space_init(struct space *s, const struct swift_params *params, bzero(s->xparts, Npart * sizeof(struct xpart)); } + hydro_space_init(&s->hs, s); + /* Set the particles in a state where they are ready for a run */ space_init_parts(s); space_init_xparts(s); diff --git a/src/space.h b/src/space.h index 435e3200d0aa9d03dfaa21cacd4ff3e65aad712c..73bd50da928c55890a91415f6e07c5100a7b71e7 100644 --- a/src/space.h +++ b/src/space.h @@ -31,6 +31,7 @@ /* Includes. */ #include "cell.h" +#include "hydro_space.h" #include "lock.h" #include "parser.h" #include "part.h" @@ -69,6 +70,9 @@ struct space { /*! Is the space periodic? */ int periodic; + /*! Extra space information needed for some hydro schemes. */ + struct hydro_space hs; + /*! Are we doing gravity? */ int gravity; diff --git a/src/tools.c b/src/tools.c index 89ac286fb435c01b361bdea66e62dd2d7f41ee24..73684c82662870d368f7dd360c84635654f06434 100644 --- a/src/tools.c +++ b/src/tools.c @@ -144,7 +144,7 @@ void pairs_single_density(double *dim, long long int pid, p = parts[k]; printf("pairs_single: part[%i].id == %lli.\n", k, pid); - hydro_init_part(&p); + hydro_init_part(&p, NULL); /* Loop over all particle pairs. */ for (k = 0; k < N; k++) { @@ -459,7 +459,7 @@ void engine_single_density(double *dim, long long int pid, p = parts[k]; /* Clear accumulators. */ - hydro_init_part(&p); + hydro_init_part(&p, NULL); /* Loop over all particle pairs (force). */ for (k = 0; k < N; k++) { diff --git a/tests/Makefile.am b/tests/Makefile.am index 0db5c2544433012dcd7f451f535391aa81b1f802..4fb1e3492f95eea4b6019524f939bdc3be2634e7 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -25,7 +25,9 @@ TESTS = testGreetings testMaths testReading.sh testSingle testKernel testSymmetr testPair.sh testPairPerturbed.sh test27cells.sh test27cellsPerturbed.sh \ testParser.sh testSPHStep test125cells.sh testKernelGrav testFFT \ testAdiabaticIndex testRiemannExact testRiemannTRRS testRiemannHLLC \ - testMatrixInversion testThreadpool testDump testLogger + testMatrixInversion testThreadpool testDump testLogger + +#testVoronoi1D testVoronoi2D testVoronoi3D # List of test programs to compile check_PROGRAMS = testGreetings testReading testSingle testTimeIntegration \ @@ -33,7 +35,9 @@ check_PROGRAMS = testGreetings testReading testSingle testTimeIntegration \ testKernel testKernelGrav testFFT testInteractions testMaths \ testSymmetry testThreadpool benchmarkInteractions \ testAdiabaticIndex testRiemannExact testRiemannTRRS \ - testRiemannHLLC testMatrixInversion testDump testLogger + testRiemannHLLC testMatrixInversion testDump testLogger + +#testVoronoi1D testVoronoi2D testVoronoi3D # Sources for the individual programs testGreetings_SOURCES = testGreetings.c @@ -78,6 +82,12 @@ testRiemannHLLC_SOURCES = testRiemannHLLC.c testMatrixInversion_SOURCES = testMatrixInversion.c +#testVoronoi1D_SOURCES = testVoronoi1D.c + +#testVoronoi2D_SOURCES = testVoronoi2D.c + +#testVoronoi3D_SOURCES = testVoronoi3D.c + testThreadpool_SOURCES = testThreadpool.c testDump_SOURCES = testDump.c diff --git a/tests/benchmarkInteractions.c b/tests/benchmarkInteractions.c index e3f558f88dffbab252bf7c06f9e943ff568b6fff..0f5b3d2eb294c13e3035885b511c702e6f0cd540 100644 --- a/tests/benchmarkInteractions.c +++ b/tests/benchmarkInteractions.c @@ -92,7 +92,7 @@ struct part *make_particles(size_t count, double *offset, double spacing, p->h = h; p->id = ++(*partId); -#if !defined(GIZMO_SPH) +#if !defined(GIZMO_SPH) && !defined(SHADOWFAX_SPH) p->mass = 1.0f; #endif @@ -113,7 +113,7 @@ struct part *make_particles(size_t count, double *offset, double spacing, p->h = h; p->id = ++(*partId); -#if !defined(GIZMO_SPH) +#if !defined(GIZMO_SPH) && !defined(SHADOWFAX_SPH) p->mass = 1.0f; #endif } @@ -125,7 +125,7 @@ struct part *make_particles(size_t count, double *offset, double spacing, */ void prepare_force(struct part *parts, size_t count) { -#if !defined(GIZMO_SPH) +#if !defined(GIZMO_SPH) && !defined(SHADOWFAX_SPH) struct part *p; for (size_t i = 0; i < count; ++i) { p = &parts[i]; @@ -152,18 +152,18 @@ void dump_indv_particle_fields(char *fileName, struct part *p) { "%13e %13e %13e\n", p->id, p->x[0], p->x[1], p->x[2], p->v[0], p->v[1], p->v[2], p->a_hydro[0], p->a_hydro[1], p->a_hydro[2], -#if defined(GIZMO_SPH) +#if defined(GIZMO_SPH) || defined(SHADOWFAX_SPH) 0., 0., #else p->rho, p->density.rho_dh, #endif p->density.wcount, p->density.wcount_dh, p->force.h_dt, -#if defined(GIZMO_SPH) +#if defined(GIZMO_SPH) || defined(SHADOWFAX_SPH) 0., #else p->force.v_sig, #endif -#if defined(MINIMAL_SPH) || defined(GIZMO_SPH) +#if defined(MINIMAL_SPH) || defined(GIZMO_SPH) || defined(SHADOWFAX_SPH) 0., 0., 0., 0. #else p->density.div_v, p->density.rot_v[0], p->density.rot_v[1], diff --git a/tests/test125cells.c b/tests/test125cells.c index b3895ffa9fc0e4c147bfbf58dc1b5a6301b02763..21fc3f5407f90d9b75485d08bf1077e0a20e88b2 100644 --- a/tests/test125cells.c +++ b/tests/test125cells.c @@ -101,7 +101,7 @@ void set_energy_state(struct part *part, enum pressure_field press, float size, part->u = pressure / (hydro_gamma_minus_one * density); #elif defined(MINIMAL_SPH) part->u = pressure / (hydro_gamma_minus_one * density); -#elif defined(GIZMO_SPH) +#elif defined(GIZMO_SPH) || defined(SHADOWFAX_SPH) part->primitives.P = pressure; #else error("Need to define pressure here !"); @@ -198,8 +198,13 @@ void reset_particles(struct cell *c, enum velocity_field vel, set_velocity(p, vel, size); set_energy_state(p, press, size, density); +#if defined(GIZMO_SPH) || defined(SHADOWFAX_SPH) + float volume = p->conserved.mass / density; #if defined(GIZMO_SPH) - p->geometry.volume = p->conserved.mass / density; + p->geometry.volume = volume; +#else + p->cell.volume = volume; +#endif p->primitives.rho = density; p->primitives.v[0] = p->v[0]; p->primitives.v[1] = p->v[1]; @@ -208,7 +213,7 @@ void reset_particles(struct cell *c, enum velocity_field vel, p->conserved.momentum[1] = p->conserved.mass * p->v[1]; p->conserved.momentum[2] = p->conserved.mass * p->v[2]; p->conserved.energy = - p->primitives.P / hydro_gamma_minus_one * p->geometry.volume + + p->primitives.P / hydro_gamma_minus_one * volume + 0.5f * (p->conserved.momentum[0] * p->conserved.momentum[0] + p->conserved.momentum[1] * p->conserved.momentum[1] + p->conserved.momentum[2] * p->conserved.momentum[2]) / @@ -260,7 +265,7 @@ struct cell *make_cell(size_t n, const double offset[3], double size, double h, part->x[2] = offset[2] + size * (z + 0.5) / (float)n; part->h = size * h / (float)n; -#ifdef GIZMO_SPH +#if defined(GIZMO_SPH) || defined(SHADOWFAX_SPH) part->conserved.mass = density * volume / count; #else part->mass = density * volume / count; @@ -284,7 +289,7 @@ struct cell *make_cell(size_t n, const double offset[3], double size, double h, part->conserved.momentum[1] = part->conserved.mass * part->v[1]; part->conserved.momentum[2] = part->conserved.mass * part->v[2]; part->conserved.energy = - part->primitives.P / hydro_gamma_minus_one * part->geometry.volume + + part->primitives.P / hydro_gamma_minus_one * volume + 0.5f * (part->conserved.momentum[0] * part->conserved.momentum[0] + part->conserved.momentum[1] * part->conserved.momentum[1] + part->conserved.momentum[2] * part->conserved.momentum[2]) / @@ -363,7 +368,7 @@ void dump_particle_fields(char *fileName, struct cell *main_cell, main_cell->parts[pid].v[0], main_cell->parts[pid].v[1], main_cell->parts[pid].v[2], main_cell->parts[pid].h, hydro_get_density(&main_cell->parts[pid]), -#ifdef MINIMAL_SPH +#if defined(MINIMAL_SPH) || defined(SHADOWFAX_SPH) 0.f, #else main_cell->parts[pid].density.div_v, diff --git a/tests/test27cells.c b/tests/test27cells.c index 599c8713a4b2077444b10f85ed37ee76f5219c5a..2390ae8aeac465c49831984c0d24817b50d757b3 100644 --- a/tests/test27cells.c +++ b/tests/test27cells.c @@ -117,8 +117,15 @@ struct cell *make_cell(size_t n, double *offset, double size, double h, part->h = size * h / (float)n; part->id = ++(*partId); -#ifdef GIZMO_SPH +#if defined(GIZMO_SPH) || defined(SHADOWFAX_SPH) part->conserved.mass = density * volume / count; + +#ifdef SHADOWFAX_SPH + double anchor[3] = {0., 0., 0.}; + double side[3] = {1., 1., 1.}; + voronoi_cell_init(&part->cell, part->x, anchor, side); +#endif + #else part->mass = density * volume / count; #endif @@ -176,7 +183,7 @@ void clean_up(struct cell *ci) { */ void zero_particle_fields(struct cell *c) { for (int pid = 0; pid < c->count; pid++) { - hydro_init_part(&c->parts[pid]); + hydro_init_part(&c->parts[pid], NULL); } } @@ -215,7 +222,7 @@ void dump_particle_fields(char *fileName, struct cell *main_cell, main_cell->parts[pid].v[0], main_cell->parts[pid].v[1], main_cell->parts[pid].v[2], hydro_get_density(&main_cell->parts[pid]), -#if defined(GIZMO_SPH) +#if defined(GIZMO_SPH) || defined(SHADOWFAX_SPH) 0.f, #else main_cell->parts[pid].density.rho_dh, @@ -252,7 +259,7 @@ void dump_particle_fields(char *fileName, struct cell *main_cell, cj->parts[pjd].id, cj->parts[pjd].x[0], cj->parts[pjd].x[1], cj->parts[pjd].x[2], cj->parts[pjd].v[0], cj->parts[pjd].v[1], cj->parts[pjd].v[2], hydro_get_density(&cj->parts[pjd]), -#if defined(GIZMO_SPH) +#if defined(GIZMO_SPH) || defined(SHADOWFAX_SPH) 0.f, #else main_cell->parts[pjd].density.rho_dh, diff --git a/tests/testPair.c b/tests/testPair.c index c734424bca58b7a8ce05bba2c3392ca5c600fa9e..c2533b63b902e3bdc7e7cae6fcbcf50c87dee4af 100644 --- a/tests/testPair.c +++ b/tests/testPair.c @@ -63,7 +63,7 @@ struct cell *make_cell(size_t n, double *offset, double size, double h, part->v[2] = random_uniform(-0.05, 0.05); part->h = size * h / (float)n; part->id = ++(*partId); -#ifdef GIZMO_SPH +#if defined(GIZMO_SPH) || defined(SHADOWFAX_SPH) part->conserved.mass = density * volume / count; #else part->mass = density * volume / count; @@ -116,7 +116,7 @@ void clean_up(struct cell *ci) { */ void zero_particle_fields(struct cell *c) { for (int pid = 0; pid < c->count; pid++) { - hydro_init_part(&c->parts[pid]); + hydro_init_part(&c->parts[pid], NULL); } } @@ -142,7 +142,7 @@ void dump_particle_fields(char *fileName, struct cell *ci, struct cell *cj) { ci->parts[pid].id, ci->parts[pid].x[0], ci->parts[pid].x[1], ci->parts[pid].x[2], ci->parts[pid].v[0], ci->parts[pid].v[1], ci->parts[pid].v[2], hydro_get_density(&ci->parts[pid]), -#if defined(GIZMO_SPH) +#if defined(GIZMO_SPH) || defined(SHADOWFAX_SPH) 0.f, #else ci->parts[pid].density.rho_dh, @@ -166,7 +166,7 @@ void dump_particle_fields(char *fileName, struct cell *ci, struct cell *cj) { cj->parts[pjd].id, cj->parts[pjd].x[0], cj->parts[pjd].x[1], cj->parts[pjd].x[2], cj->parts[pjd].v[0], cj->parts[pjd].v[1], cj->parts[pjd].v[2], hydro_get_density(&cj->parts[pjd]), -#if defined(GIZMO_SPH) +#if defined(GIZMO_SPH) || defined(SHADOWFAX_SPH) 0.f, #else cj->parts[pjd].density.rho_dh, diff --git a/tests/testSymmetry.c b/tests/testSymmetry.c index 6469d314fb8b1438cc2c9737669c1a13a97bd803..73c5708a6add174b88f26cc716a716fa2ad81709 100644 --- a/tests/testSymmetry.c +++ b/tests/testSymmetry.c @@ -31,6 +31,13 @@ int main(int argc, char *argv[]) { /* Choke if need be */ feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW); +#if defined(SHADOWFAX_SPH) + /* Initialize the Voronoi simulation box */ + double box_anchor[3] = {-2.0f, -2.0f, -2.0f}; + double box_side[3] = {6.0f, 6.0f, 6.0f}; +/* voronoi_set_box(box_anchor, box_side);*/ +#endif + /* Create two random particles (don't do this at home !) */ struct part pi, pj; for (size_t i = 0; i < sizeof(struct part) / sizeof(float); ++i) { @@ -46,7 +53,7 @@ int main(int argc, char *argv[]) { pi.id = 1; pj.id = 2; -#if defined(GIZMO_SPH) +#if defined(GIZMO_SPH) || defined(SHADOWFAX_SPH) /* Give the primitive variables sensible values, since the Riemann solver does not like negative densities and pressures */ pi.primitives.rho = random_uniform(0.1f, 1.0f); @@ -93,6 +100,12 @@ int main(int argc, char *argv[]) { /* set time step to reasonable value */ pi.force.dt = 0.001; pj.force.dt = 0.001; + +#ifdef SHADOWFAX_SPH + voronoi_cell_init(&pi.cell, pi.x, box_anchor, box_side); + voronoi_cell_init(&pj.cell, pj.x, box_anchor, box_side); +#endif + #endif /* Make an xpart companion */ diff --git a/tests/testVoronoi1D.c b/tests/testVoronoi1D.c new file mode 100644 index 0000000000000000000000000000000000000000..d16a36d9449d7bfdb2c74408efad61b219b1d7e3 --- /dev/null +++ b/tests/testVoronoi1D.c @@ -0,0 +1,79 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (C) 2016 Bert Vandenbroucke (bert.vandenbroucke@gmail.com). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +#include "hydro/Shadowswift/voronoi1d_algorithm.h" + +int main() { + + double box_anchor[1] = {-0.5}; + double box_side[1] = {2.}; + + /* Create a Voronoi cell */ + double x[1] = {0.5f}; + struct voronoi_cell cell; + voronoi_cell_init(&cell, x, box_anchor, box_side); + + /* Interact with a left and right neighbour */ + float xL[1] = {0.5f}; + float xR[1] = {-0.5f}; + voronoi_cell_interact(&cell, xL, 1); + voronoi_cell_interact(&cell, xR, 2); + + /* Interact with some more neighbours to check if they are properly ignored */ + float x0[1] = {0.6f}; + float x1[1] = {-0.7f}; + voronoi_cell_interact(&cell, x0, 3); + voronoi_cell_interact(&cell, x1, 4); + + /* Finalize cell and check results */ + voronoi_cell_finalize(&cell); + + if (cell.volume != 0.5f) { + error("Wrong volume: %g!", cell.volume); + } + if (cell.centroid != 0.5f) { + error("Wrong centroid: %g!", cell.centroid); + } + if (cell.idL != 1) { + error("Wrong left neighbour: %llu!", cell.idL); + } + if (cell.idR != 2) { + error("Wrong right neighbour: %llu!", cell.idR); + } + + /* Check face method */ + float A; + float midpoint[3]; + A = voronoi_get_face(&cell, 1, midpoint); + if (A != 1.0f) { + error("Wrong surface area returned for left neighbour!"); + } + if (midpoint[0] != -0.25f) { + error("Wrong midpoint returned for left neighbour!"); + } + A = voronoi_get_face(&cell, 2, midpoint); + if (A != 1.0f) { + error("Wrong surface area returned for right neighbour!"); + } + if (midpoint[0] != 0.25f) { + error("Wrong midpoint returned for right neighbour!"); + } + + return 0; +} diff --git a/tests/testVoronoi2D.c b/tests/testVoronoi2D.c new file mode 100644 index 0000000000000000000000000000000000000000..8e2c875ee1b41828a0b39aa896f02dff0ccbac88 --- /dev/null +++ b/tests/testVoronoi2D.c @@ -0,0 +1,211 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (C) 2017 Bert Vandenbroucke (bert.vandenbroucke@gmail.com). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +#include "hydro/Shadowswift/voronoi2d_algorithm.h" +#include "tools.h" + +/* Number of cells used to test the 2D interaction algorithm */ +#define TESTVORONOI2D_NUMCELL 100 + +int main() { + + /* initialize simulation box */ + double anchor[3] = {-0.5f, -0.5f, -0.5f}; + double side[3] = {2.0f, 2.0f, 2.0f}; + + /* test initialization and finalization algorithms */ + { + message("Testing initialization and finalization algorithm..."); + + struct voronoi_cell cell; + double x[3] = {0.5, 0.5, 0.5}; + + voronoi_cell_init(&cell, x, anchor, side); + + float maxradius = voronoi_cell_finalize(&cell); + + assert(maxradius == 2.0f * sqrtf(2.0f)); + + assert(cell.volume == 4.0f); + + assert(cell.centroid[0] == 0.5f); + assert(cell.centroid[1] == 0.5f); + + message("Done."); + } + + /* test interaction algorithm: normal case */ + { + message("Testing %i cell grid with random positions...", + TESTVORONOI2D_NUMCELL); + + /* create 100 cells with random positions in [0,1]x[0,1] */ + struct voronoi_cell cells[TESTVORONOI2D_NUMCELL]; + double x[2]; + float dx[2]; + int i, j; + float Atot; + struct voronoi_cell *cell_i, *cell_j; + + for (i = 0; i < TESTVORONOI2D_NUMCELL; ++i) { + x[0] = random_uniform(0., 1.); + x[1] = random_uniform(0., 1.); + voronoi_cell_init(&cells[i], x, anchor, side); +#ifdef VORONOI_VERBOSE + message("cell[%i]: %g %g", i, x[0], x[1]); +#endif + } + + /* interact the cells (with periodic boundaries) */ + for (i = 0; i < TESTVORONOI2D_NUMCELL; ++i) { + cell_i = &cells[i]; + for (j = 0; j < TESTVORONOI2D_NUMCELL; ++j) { + if (i != j) { + cell_j = &cells[j]; + dx[0] = cell_i->x[0] - cell_j->x[0]; + dx[1] = cell_i->x[1] - cell_j->x[1]; + /* periodic boundaries */ + if (dx[0] >= 0.5f) { + dx[0] -= 1.0f; + } + if (dx[0] < -0.5f) { + dx[0] += 1.0f; + } + if (dx[1] >= 0.5f) { + dx[1] -= 1.0f; + } + if (dx[1] < -0.5f) { + dx[1] += 1.0f; + } +#ifdef VORONOI_VERBOSE + message("Cell %i before:", i); + voronoi_print_cell(&cells[i]); + message("Interacting cell %i with cell %i (%g %g, %g %g", i, j, + cells[i].x[0], cells[i].x[1], cells[j].x[0], cells[j].x[1]); +#endif + voronoi_cell_interact(cell_i, dx, j); + } + } + } + + Atot = 0.0f; + /* print the cells to the stdout */ + for (i = 0; i < TESTVORONOI2D_NUMCELL; ++i) { + printf("Cell %i:\n", i); + voronoi_print_cell(&cells[i]); + voronoi_cell_finalize(&cells[i]); + Atot += cells[i].volume; + } + + /* Check the total surface area */ + assert(fabs(Atot - 1.0f) < 1.e-6); + + /* Check the neighbour relations for an arbitrary cell: cell 44 + We plotted the grid and manually found the correct neighbours and their + order. */ + assert(cells[44].nvert == 7); + assert(cells[44].ngbs[0] == 26); + assert(cells[44].ngbs[1] == 38); + assert(cells[44].ngbs[2] == 3); + assert(cells[44].ngbs[3] == 33); + assert(cells[44].ngbs[4] == 5); + assert(cells[44].ngbs[5] == 90); + assert(cells[44].ngbs[6] == 4); + + message("Done."); + } + + /* test interaction algorithm */ + { + message("Testing 100 cell grid with Cartesian mesh positions..."); + + struct voronoi_cell cells[100]; + double x[2]; + float dx[2]; + int i, j; + float Atot; + struct voronoi_cell *cell_i, *cell_j; + + for (i = 0; i < 10; ++i) { + for (j = 0; j < 10; ++j) { + x[0] = (i + 0.5f) * 0.1; + x[1] = (j + 0.5f) * 0.1; + voronoi_cell_init(&cells[10 * i + j], x, anchor, side); + } + } + + /* interact the cells (with periodic boundaries) */ + for (i = 0; i < 100; ++i) { + cell_i = &cells[i]; + for (j = 0; j < 100; ++j) { + if (i != j) { + cell_j = &cells[j]; + dx[0] = cell_i->x[0] - cell_j->x[0]; + dx[1] = cell_i->x[1] - cell_j->x[1]; + /* periodic boundaries */ + if (dx[0] >= 0.5f) { + dx[0] -= 1.0f; + } + if (dx[0] < -0.5f) { + dx[0] += 1.0f; + } + if (dx[1] >= 0.5f) { + dx[1] -= 1.0f; + } + if (dx[1] < -0.5f) { + dx[1] += 1.0f; + } +#ifdef VORONOI_VERBOSE + message("Cell %i before:", i); + voronoi_print_cell(&cells[i]); + message("Interacting cell %i with cell %i (%g %g, %g %g", i, j, + cells[i].x[0], cells[i].x[1], cells[j].x[0], cells[j].x[1]); +#endif + voronoi_cell_interact(cell_i, dx, j); + } + } + } + + Atot = 0.0f; + /* print the cells to the stdout */ + for (i = 0; i < 100; ++i) { + printf("Cell %i:\n", i); + voronoi_print_cell(&cells[i]); + voronoi_cell_finalize(&cells[i]); + Atot += cells[i].volume; + } + + /* Check the total surface area */ + assert(fabs(Atot - 1.0f) < 1.e-6); + + /* Check the neighbour relations for an arbitrary cell: cell 44 + We plotted the grid and manually found the correct neighbours and their + order. */ + assert(cells[44].nvert == 5); + assert(cells[44].ngbs[0] == 34); + assert(cells[44].ngbs[1] == 35); + assert(cells[44].ngbs[2] == 45); + assert(cells[44].ngbs[3] == 54); + assert(cells[44].ngbs[4] == 43); + + message("Done."); + } + + return 0; +} diff --git a/tests/testVoronoi3D.c b/tests/testVoronoi3D.c new file mode 100644 index 0000000000000000000000000000000000000000..b4f219a41368bb3ce4e8111ae44c43e7fa1f7441 --- /dev/null +++ b/tests/testVoronoi3D.c @@ -0,0 +1,1518 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (C) 2016 Bert Vandenbroucke (bert.vandenbroucke@gmail.com). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + ******************************************************************************/ + +#include <stdlib.h> +#include "error.h" +#include "hydro/Shadowswift/voronoi3d_algorithm.h" +#include "part.h" +#include "tools.h" + +/* Number of random generators to use in the first grid build test */ +#define TESTVORONOI3D_NUMCELL_RANDOM 100 + +/* Number of cartesian generators to use (in one coordinate direction) for the + second grid build test. The total number of generators is the third power of + this number (so be careful with large numbers) */ +#define TESTVORONOI3D_NUMCELL_CARTESIAN_1D 5 + +/* Total number of generators in the second grid build test. Do not change this + value, but change the 1D value above instead. */ +#define TESTVORONOI3D_NUMCELL_CARTESIAN_3D \ + (TESTVORONOI3D_NUMCELL_CARTESIAN_1D * TESTVORONOI3D_NUMCELL_CARTESIAN_1D * \ + TESTVORONOI3D_NUMCELL_CARTESIAN_1D) + +/* Bottom front left corner and side lengths of the large box that contains all + particles and is used as initial cell at the start of the construction */ +#define VORONOI3D_BOX_ANCHOR_X 0.0f +#define VORONOI3D_BOX_ANCHOR_Y 0.0f +#define VORONOI3D_BOX_ANCHOR_Z 0.0f +#define VORONOI3D_BOX_SIDE_X 1.0f +#define VORONOI3D_BOX_SIDE_Y 1.0f +#define VORONOI3D_BOX_SIDE_Z 1.0f + +/** + * @brief Get the volume of the simulation box stored in the global variables. + * + * This method is only used for debugging purposes. + * + * @return Volume of the simulation box as it is stored in the global variables. + */ +float voronoi_get_box_volume() { + return VORONOI3D_BOX_SIDE_X * VORONOI3D_BOX_SIDE_Y * VORONOI3D_BOX_SIDE_Z; +} + +/** + * @brief Get the centroid of the simulation box stored in the global variables. + * + * This method is only used for debugging purposes. + * + * @param box_centroid Array to store the resulting coordinates in. + */ +void voronoi_get_box_centroid(float *box_centroid) { + box_centroid[0] = 0.5f * VORONOI3D_BOX_SIDE_X + VORONOI3D_BOX_ANCHOR_X; + box_centroid[1] = 0.5f * VORONOI3D_BOX_SIDE_Y + VORONOI3D_BOX_ANCHOR_Y; + box_centroid[2] = 0.5f * VORONOI3D_BOX_SIDE_Z + VORONOI3D_BOX_ANCHOR_Z; +} + +/** + * @brief Get the surface area and coordinates of the face midpoint for the + * face of the simulation box with the given unique ID. + * + * This method is only used for debugging purposes. + * + * @param id Unique ID of one of the 6 faces of the simulation box. + * @param face_midpoint Array to store the coordinates of the requested + * midpoint in. + * @return Surface area of the face, or 0 if the given ID does not correspond to + * a known face of the simulation box. + */ +float voronoi_get_box_face(unsigned long long id, float *face_midpoint) { + + if (id == VORONOI3D_BOX_FRONT) { + face_midpoint[0] = 0.5f * VORONOI3D_BOX_SIDE_X + VORONOI3D_BOX_ANCHOR_X; + face_midpoint[1] = VORONOI3D_BOX_ANCHOR_Y; + face_midpoint[2] = 0.5f * VORONOI3D_BOX_SIDE_Z + VORONOI3D_BOX_ANCHOR_Z; + return VORONOI3D_BOX_SIDE_X * VORONOI3D_BOX_SIDE_Z; + } + if (id == VORONOI3D_BOX_BACK) { + face_midpoint[0] = 0.5f * VORONOI3D_BOX_SIDE_X + VORONOI3D_BOX_ANCHOR_X; + face_midpoint[1] = VORONOI3D_BOX_ANCHOR_Y + VORONOI3D_BOX_SIDE_Y; + face_midpoint[2] = 0.5f * VORONOI3D_BOX_SIDE_Z + VORONOI3D_BOX_ANCHOR_Z; + return VORONOI3D_BOX_SIDE_X * VORONOI3D_BOX_SIDE_Z; + } + + if (id == VORONOI3D_BOX_BOTTOM) { + face_midpoint[0] = 0.5f * VORONOI3D_BOX_SIDE_X + VORONOI3D_BOX_ANCHOR_X; + face_midpoint[1] = 0.5f * VORONOI3D_BOX_SIDE_Y + VORONOI3D_BOX_ANCHOR_Y; + face_midpoint[2] = VORONOI3D_BOX_ANCHOR_Z; + return VORONOI3D_BOX_SIDE_X * VORONOI3D_BOX_SIDE_Y; + } + if (id == VORONOI3D_BOX_TOP) { + face_midpoint[0] = 0.5f * VORONOI3D_BOX_SIDE_X + VORONOI3D_BOX_ANCHOR_X; + face_midpoint[1] = 0.5f * VORONOI3D_BOX_SIDE_Y + VORONOI3D_BOX_ANCHOR_Y; + face_midpoint[2] = VORONOI3D_BOX_ANCHOR_Z + VORONOI3D_BOX_SIDE_Z; + return VORONOI3D_BOX_SIDE_X * VORONOI3D_BOX_SIDE_Y; + } + + if (id == VORONOI3D_BOX_LEFT) { + face_midpoint[0] = VORONOI3D_BOX_ANCHOR_X; + face_midpoint[1] = 0.5f * VORONOI3D_BOX_SIDE_Y + VORONOI3D_BOX_ANCHOR_Y; + face_midpoint[2] = 0.5f * VORONOI3D_BOX_SIDE_Z + VORONOI3D_BOX_ANCHOR_Z; + return VORONOI3D_BOX_SIDE_X * VORONOI3D_BOX_SIDE_Y; + } + if (id == VORONOI3D_BOX_RIGHT) { + face_midpoint[0] = VORONOI3D_BOX_ANCHOR_X + VORONOI3D_BOX_SIDE_X; + face_midpoint[1] = 0.5f * VORONOI3D_BOX_SIDE_Y + VORONOI3D_BOX_ANCHOR_Y; + face_midpoint[2] = 0.5f * VORONOI3D_BOX_SIDE_Z + VORONOI3D_BOX_ANCHOR_Z; + return VORONOI3D_BOX_SIDE_X * VORONOI3D_BOX_SIDE_Y; + } + + return 0.0f; +} + +/** + * @brief Check if voronoi_volume_tetrahedron() works + */ +void test_voronoi_volume_tetrahedron() { + float v1[3] = {0., 0., 0.}; + float v2[3] = {0., 0., 1.}; + float v3[3] = {0., 1., 0.}; + float v4[3] = {1., 0., 0.}; + + float V = voronoi_volume_tetrahedron(v1, v2, v3, v4); + assert(V == 1.0f / 6.0f); +} + +/** + * @brief Check if voronoi_centroid_tetrahedron() works + */ +void test_voronoi_centroid_tetrahedron() { + float v1[3] = {0., 0., 0.}; + float v2[3] = {0., 0., 1.}; + float v3[3] = {0., 1., 0.}; + float v4[3] = {1., 0., 0.}; + + float centroid[3]; + voronoi_centroid_tetrahedron(centroid, v1, v2, v3, v4); + assert(centroid[0] == 0.25f); + assert(centroid[1] == 0.25f); + assert(centroid[2] == 0.25f); +} + +/** + * @brief Check if voronoi_calculate_cell() works + */ +void test_calculate_cell() { + + double box_anchor[3] = {VORONOI3D_BOX_ANCHOR_X, VORONOI3D_BOX_ANCHOR_Y, + VORONOI3D_BOX_ANCHOR_Z}; + double box_side[3] = {VORONOI3D_BOX_SIDE_X, VORONOI3D_BOX_SIDE_Y, + VORONOI3D_BOX_SIDE_Z}; + + struct voronoi_cell cell; + + cell.x[0] = 0.5f; + cell.x[1] = 0.5f; + cell.x[2] = 0.5f; + + /* Initialize the cell to a large cube. */ + voronoi_initialize(&cell, box_anchor, box_side); + /* Calculate the volume and centroid of the large cube. */ + voronoi_calculate_cell(&cell); + /* Calculate the faces. */ + voronoi_calculate_faces(&cell); + + /* Update these values if you ever change to another large cube! */ + assert(cell.volume == voronoi_get_box_volume()); + float box_centroid[3]; + voronoi_get_box_centroid(box_centroid); + assert(cell.centroid[0] = box_centroid[0]); + assert(cell.centroid[1] = box_centroid[1]); + assert(cell.centroid[2] = box_centroid[2]); + + /* Check cell neighbours. */ + assert(cell.nface == 6); + assert(cell.ngbs[0] == VORONOI3D_BOX_FRONT); + assert(cell.ngbs[1] == VORONOI3D_BOX_LEFT); + assert(cell.ngbs[2] == VORONOI3D_BOX_BOTTOM); + assert(cell.ngbs[3] == VORONOI3D_BOX_TOP); + assert(cell.ngbs[4] == VORONOI3D_BOX_BACK); + assert(cell.ngbs[5] == VORONOI3D_BOX_RIGHT); + + /* Check cell faces */ + float face_midpoint[3], face_area; + face_area = voronoi_get_box_face(VORONOI3D_BOX_FRONT, face_midpoint); + assert(cell.face_areas[0] == face_area); + assert(cell.face_midpoints[0][0] == face_midpoint[0] - cell.x[0]); + assert(cell.face_midpoints[0][1] == face_midpoint[1] - cell.x[1]); + assert(cell.face_midpoints[0][2] == face_midpoint[2] - cell.x[2]); + + face_area = voronoi_get_box_face(VORONOI3D_BOX_LEFT, face_midpoint); + assert(cell.face_areas[1] == face_area); + assert(cell.face_midpoints[1][0] == face_midpoint[0] - cell.x[0]); + assert(cell.face_midpoints[1][1] == face_midpoint[1] - cell.x[1]); + assert(cell.face_midpoints[1][2] == face_midpoint[2] - cell.x[2]); + + face_area = voronoi_get_box_face(VORONOI3D_BOX_BOTTOM, face_midpoint); + assert(cell.face_areas[2] == face_area); + assert(cell.face_midpoints[2][0] == face_midpoint[0] - cell.x[0]); + assert(cell.face_midpoints[2][1] == face_midpoint[1] - cell.x[1]); + assert(cell.face_midpoints[2][2] == face_midpoint[2] - cell.x[2]); + + face_area = voronoi_get_box_face(VORONOI3D_BOX_TOP, face_midpoint); + assert(cell.face_areas[3] == face_area); + assert(cell.face_midpoints[3][0] == face_midpoint[0] - cell.x[0]); + assert(cell.face_midpoints[3][1] == face_midpoint[1] - cell.x[1]); + assert(cell.face_midpoints[3][2] == face_midpoint[2] - cell.x[2]); + + face_area = voronoi_get_box_face(VORONOI3D_BOX_BACK, face_midpoint); + assert(cell.face_areas[4] == face_area); + assert(cell.face_midpoints[4][0] == face_midpoint[0] - cell.x[0]); + assert(cell.face_midpoints[4][1] == face_midpoint[1] - cell.x[1]); + assert(cell.face_midpoints[4][2] == face_midpoint[2] - cell.x[2]); + + face_area = voronoi_get_box_face(VORONOI3D_BOX_RIGHT, face_midpoint); + assert(cell.face_areas[5] == face_area); + assert(cell.face_midpoints[5][0] == face_midpoint[0] - cell.x[0]); + assert(cell.face_midpoints[5][1] == face_midpoint[1] - cell.x[1]); + assert(cell.face_midpoints[5][2] == face_midpoint[2] - cell.x[2]); +} + +void test_paths() { + float u, l, q; + int up, us, uw, lp, ls, lw, qp, qs, qw; + float r2, dx[3]; + struct voronoi_cell cell; + + /* PATH 1.0 */ + // the first vertex is above the cutting plane and its first edge is below the + // plane + { + cell.vertices[0] = 1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = -1.0f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.nvert = 2; + cell.orders[0] = 3; + cell.orders[1] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 3; + cell.edges[0] = 1; + cell.edgeindices[0] = 0; + cell.edges[3] = 0; + cell.edgeindices[3] = 0; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 1); + assert(up == 0); + assert(us == 0); + assert(uw == 1); + assert(u == 0.25f); + assert(lp == 1); + assert(ls == 0); + assert(lw == -1); + assert(l == -0.75f); + } + + /* PATH 1.1 */ + // the first vertex is above the cutting plane and its second edge is below + // the plane + { + cell.vertices[0] = 1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = 2.0f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.vertices[6] = -1.0f; + cell.vertices[7] = 0.0f; + cell.vertices[8] = 0.0f; + cell.nvert = 3; + cell.orders[0] = 3; + cell.orders[1] = 3; + cell.orders[2] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 3; + cell.offsets[2] = 6; + cell.edges[0] = 1; + cell.edges[1] = 2; + cell.edges[6] = 0; + cell.edgeindices[1] = 0; + cell.edgeindices[6] = 1; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 1); + assert(up == 0); + assert(us == 1); + assert(uw == 1); + assert(u == 0.25f); + assert(lp == 2); + assert(ls == 0); + assert(lw == -1); + assert(l == -0.75f); + } + + /* PATH 1.2 */ + // the first vertex is above the cutting plane and its second and last edge + // is below the plane + { + cell.vertices[0] = 1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = 2.0f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.vertices[6] = -1.0f; + cell.vertices[7] = 0.0f; + cell.vertices[8] = 0.0f; + cell.nvert = 3; + cell.orders[0] = 2; + cell.orders[1] = 3; + cell.orders[2] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 2; + cell.offsets[2] = 5; + cell.edges[0] = 1; + cell.edges[1] = 2; + cell.edges[6] = 0; + cell.edgeindices[1] = 0; + cell.edgeindices[5] = 1; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 1); + assert(up == 0); + assert(us == 1); + assert(uw == 1); + assert(u == 0.25f); + assert(lp == 2); + assert(ls == 0); + assert(lw == -1); + assert(l == -0.75f); + } + + /* PATH 1.3 */ + // the first vertex is above the cutting plane and is the closest vertex to + // the plane. The code should crash. + { + cell.vertices[0] = 1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = 2.0f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.vertices[6] = 2.0f; + cell.vertices[7] = 0.0f; + cell.vertices[8] = 0.0f; + cell.vertices[9] = 2.0f; + cell.vertices[10] = 0.0f; + cell.vertices[11] = 0.0f; + cell.nvert = 4; + cell.orders[0] = 3; + cell.offsets[0] = 0; + cell.edges[0] = 1; + cell.edges[1] = 2; + cell.edges[2] = 3; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == -1); + } + + /* PATH 1.4.0 */ + // first vertex is above the plane, second vertex is closer and third vertex + // lies below + { + cell.vertices[0] = 1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = 0.75f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.vertices[6] = -1.0f; + cell.vertices[7] = 0.0f; + cell.vertices[8] = 0.0f; + cell.nvert = 3; + cell.orders[0] = 3; + cell.orders[1] = 3; + cell.orders[2] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 3; + cell.offsets[2] = 6; + cell.edges[0] = 1; + cell.edges[3] = 2; + cell.edges[6] = 1; + cell.edgeindices[0] = 2; + cell.edgeindices[3] = 0; + cell.edgeindices[6] = 0; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 1); + assert(up == 1); + assert(us == 0); + assert(u == 0.125f); + assert(lp == 2); + assert(ls == 0); + assert(l == -0.75f); + } + + /* PATH 1.4.1 */ + // first vertex is above the plane, second vertex is closer and fourth vertex + // is below + { + cell.vertices[0] = 1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = 0.75f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.vertices[6] = 2.0f; + cell.vertices[7] = 0.0f; + cell.vertices[8] = 0.0f; + cell.vertices[9] = -1.0f; + cell.vertices[10] = 0.0f; + cell.vertices[11] = 0.0f; + cell.nvert = 4; + cell.orders[0] = 3; + cell.orders[1] = 3; + cell.orders[2] = 3; + cell.orders[3] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 3; + cell.offsets[2] = 6; + cell.offsets[3] = 9; + cell.edges[0] = 1; + cell.edges[3] = 2; + cell.edges[4] = 3; + cell.edges[5] = 0; + cell.edges[6] = 1; + cell.edges[9] = 1; + // this is the only difference between PATH 1.4.1 and PATH 1.4.2 + cell.edgeindices[0] = 3; + cell.edgeindices[3] = 0; + cell.edgeindices[4] = 0; + cell.edgeindices[9] = 0; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 1); + assert(up == 1); + assert(us == 1); + assert(u == 0.125f); + assert(lp == 3); + assert(ls == 0); + assert(l == -0.75f); + } + + /* PATH 1.4.2 */ + // first vertex is above the plane, second is closer, fourth is below + { + cell.vertices[0] = 1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = 0.75f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.vertices[6] = 2.0f; + cell.vertices[7] = 0.0f; + cell.vertices[8] = 0.0f; + cell.vertices[9] = -1.0f; + cell.vertices[10] = 0.0f; + cell.vertices[11] = 0.0f; + cell.nvert = 4; + cell.orders[0] = 3; + cell.orders[1] = 3; + cell.orders[2] = 3; + cell.orders[3] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 3; + cell.offsets[2] = 6; + cell.offsets[3] = 9; + cell.edges[0] = 1; + cell.edges[3] = 2; + cell.edges[4] = 3; + cell.edges[5] = 0; + cell.edges[6] = 1; + cell.edges[9] = 1; + // this is the only difference between PATH 1.4.1 and PATH 1.4.2 + cell.edgeindices[0] = 2; + cell.edgeindices[3] = 1; + cell.edgeindices[4] = 0; + cell.edgeindices[9] = 0; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 1); + assert(up == 1); + assert(us == 1); + assert(u == 0.125f); + assert(lp == 3); + assert(ls == 0); + assert(l == -0.75f); + } + + /* PATH 1.4.3 */ + // first vertex is above the plane, second is closer, third is below + { + cell.vertices[0] = 1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = 0.75f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.vertices[6] = -1.0f; + cell.vertices[7] = 0.0f; + cell.vertices[8] = 0.0f; + cell.nvert = 3; + cell.orders[0] = 3; + cell.orders[1] = 3; + cell.orders[2] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 3; + cell.offsets[2] = 6; + cell.edges[0] = 1; + // this is the difference between PATH 1.4.0 and this path + cell.edges[3] = 0; + cell.edges[4] = 2; + cell.edges[6] = 1; + cell.edgeindices[0] = 0; + cell.edgeindices[3] = 0; + cell.edgeindices[6] = 1; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 1); + assert(up == 1); + assert(us == 1); + assert(u == 0.125f); + assert(lp == 2); + assert(ls == 0); + assert(l == -0.75f); + } + + /* PATH 1.4.4 */ + // first vertex is above the plane, second is closer, fourth is below + { + cell.vertices[0] = 1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = 0.75f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.vertices[6] = 2.0f; + cell.vertices[7] = 0.0f; + cell.vertices[8] = 0.0f; + cell.vertices[9] = -1.0f; + cell.vertices[10] = 0.0f; + cell.vertices[11] = 0.0f; + cell.nvert = 4; + cell.orders[0] = 3; + cell.orders[1] = 4; + cell.orders[2] = 3; + cell.orders[3] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 3; + cell.offsets[2] = 7; + cell.offsets[3] = 10; + cell.edges[0] = 1; + cell.edges[3] = 0; + cell.edges[4] = 2; + cell.edges[5] = 3; + cell.edges[7] = 1; + cell.edges[10] = 1; + cell.edgeindices[0] = 0; + cell.edgeindices[3] = 0; + cell.edgeindices[5] = 0; + cell.edgeindices[10] = 0; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 1); + assert(up == 1); + assert(us == 2); + assert(u == 0.125f); + assert(lp == 3); + assert(ls == 0); + assert(l == -0.75f); + } + + /* PATH 1.4.5 */ + // same as 1.4.4, but with an order 3 second vertex + { + cell.vertices[0] = 1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = 0.75f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.vertices[6] = 2.0f; + cell.vertices[7] = 0.0f; + cell.vertices[8] = 0.0f; + cell.vertices[9] = -1.0f; + cell.vertices[10] = 0.0f; + cell.vertices[11] = 0.0f; + cell.nvert = 4; + cell.orders[0] = 3; + cell.orders[1] = 3; + cell.orders[2] = 3; + cell.orders[3] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 3; + cell.offsets[2] = 6; + cell.offsets[3] = 9; + cell.edges[0] = 1; + cell.edges[3] = 0; + cell.edges[4] = 2; + cell.edges[5] = 3; + cell.edges[6] = 1; + cell.edges[9] = 1; + cell.edgeindices[0] = 0; + cell.edgeindices[3] = 0; + cell.edgeindices[5] = 0; + cell.edgeindices[9] = 0; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 1); + assert(up == 1); + assert(us == 2); + assert(u == 0.125f); + assert(lp == 3); + assert(ls == 0); + assert(l == -0.75f); + } + + /* PATH 1.4.6 */ + // first vertex is above the plane, second is closer and is the closest + { + cell.vertices[0] = 1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = 0.75f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.vertices[6] = 2.0f; + cell.vertices[7] = 0.0f; + cell.vertices[8] = 0.0f; + cell.nvert = 3; + cell.orders[0] = 3; + cell.orders[1] = 2; + cell.orders[2] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 3; + cell.offsets[2] = 5; + cell.edges[0] = 1; + cell.edges[3] = 0; + cell.edges[4] = 2; + cell.edgeindices[0] = 0; + cell.edgeindices[3] = 0; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == -1); + } + + /* PATH 1.5 */ + // first vertex is above the plane, second vertex is too close to call + { + cell.vertices[0] = 1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = 0.5f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.nvert = 2; + cell.orders[0] = 3; + cell.orders[1] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 3; + cell.edges[0] = 1; + cell.edges[3] = 0; + cell.edgeindices[0] = 0; + cell.edgeindices[3] = 0; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 2); + assert(up == 1); + } + + /* PATH 2.0 */ + // the first vertex is below the plane and its first edge is above the plane + { + cell.vertices[0] = -1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = 1.0f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.nvert = 2; + cell.orders[0] = 3; + cell.orders[1] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 3; + cell.edges[0] = 1; + cell.edgeindices[0] = 0; + cell.edges[3] = 0; + cell.edgeindices[3] = 0; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 1); + assert(up == 1); + assert(us == 0); + assert(uw == -1); + assert(u == 0.25f); + assert(lp == 0); + assert(ls == 0); + assert(qw == 1); + assert(l == -0.75f); + } + + /* PATH 2.1 */ + // the first vertex is below the plane and its second edge is above the plane + { + cell.vertices[0] = -1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = -2.0f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.vertices[6] = 1.0f; + cell.vertices[7] = 0.0f; + cell.vertices[8] = 0.0f; + cell.nvert = 3; + cell.orders[0] = 3; + cell.orders[1] = 3; + cell.orders[2] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 3; + cell.offsets[2] = 6; + cell.edges[0] = 1; + cell.edges[1] = 2; + cell.edges[6] = 0; + cell.edgeindices[1] = 0; + cell.edgeindices[6] = 1; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 1); + assert(up == 2); + assert(us == 0); + assert(uw == -1); + assert(u == 0.25f); + assert(lp == 0); + assert(ls == 1); + assert(qw == 1); + assert(l == -0.75f); + } + + /* PATH 2.2 */ + { + cell.vertices[0] = -1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = -2.0f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.vertices[6] = 1.0f; + cell.vertices[7] = 0.0f; + cell.vertices[8] = 0.0f; + cell.nvert = 3; + cell.orders[0] = 2; + cell.orders[1] = 3; + cell.orders[2] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 2; + cell.offsets[2] = 5; + cell.edges[0] = 1; + cell.edges[1] = 2; + cell.edges[5] = 0; + cell.edgeindices[1] = 0; + cell.edgeindices[5] = 1; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 1); + assert(up == 2); + assert(us == 0); + assert(uw == -1); + assert(u == 0.25f); + assert(lp == 0); + assert(ls == 1); + assert(qw == 1); + assert(l == -0.75f); + } + + /* PATH 2.3 */ + // the first vertex is below the plane and is the closest vertex to the plane + { + cell.vertices[0] = -1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = -2.0f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.vertices[6] = -2.0f; + cell.vertices[7] = 0.0f; + cell.vertices[8] = 0.0f; + cell.vertices[9] = -2.0f; + cell.vertices[10] = 0.0f; + cell.vertices[11] = 0.0f; + cell.nvert = 4; + cell.orders[0] = 3; + cell.offsets[0] = 0; + cell.edges[0] = 1; + cell.edges[1] = 2; + cell.edges[2] = 3; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 0); + } + + /* PATH 2.4.0 */ + // first vertex is below the plane, second is closer and third is above + { + cell.vertices[0] = -1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = -0.5f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.vertices[6] = 1.0f; + cell.vertices[7] = 0.0f; + cell.vertices[8] = 0.0f; + cell.nvert = 3; + cell.orders[0] = 3; + cell.orders[1] = 3; + cell.orders[2] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 3; + cell.offsets[2] = 6; + cell.edges[0] = 1; + cell.edges[3] = 2; + cell.edges[5] = 0; + cell.edges[6] = 1; + cell.edgeindices[0] = 2; + cell.edgeindices[3] = 0; + cell.edgeindices[5] = 0; + cell.edgeindices[6] = 0; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 1); + assert(up == 2); + assert(u == 0.25f); + assert(lp == 1); + assert(l == -0.5f); + } + + /* PATH 2.4.1 */ + // first vertex is below, second is closer and fourth is above + { + cell.vertices[0] = -1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = -0.5f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.vertices[6] = -2.0f; + cell.vertices[7] = 0.0f; + cell.vertices[8] = 0.0f; + cell.vertices[9] = 1.0f; + cell.vertices[10] = 0.0f; + cell.vertices[11] = 0.0f; + cell.nvert = 4; + cell.orders[0] = 3; + cell.orders[1] = 4; + cell.orders[2] = 3; + cell.orders[3] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 3; + cell.offsets[2] = 7; + cell.offsets[3] = 10; + cell.edges[0] = 1; + cell.edges[3] = 2; + cell.edges[4] = 3; + cell.edges[6] = 0; + cell.edges[10] = 1; + cell.edgeindices[0] = 3; + cell.edgeindices[3] = 0; + cell.edgeindices[4] = 0; + cell.edgeindices[6] = 0; + cell.edgeindices[10] = 1; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 1); + assert(up == 3); + assert(us == 0); + assert(u == 0.25f); + assert(lp == 1); + assert(ls == 1); + assert(l == -0.5f); + } + + /* PATH 2.4.2 */ + // first vertex is below, second is closer and fourth is above + // same as 2.4.1, but with order 3 second vertex + { + cell.vertices[0] = -1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = -0.5f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.vertices[6] = -2.0f; + cell.vertices[7] = 0.0f; + cell.vertices[8] = 0.0f; + cell.vertices[9] = 1.0f; + cell.vertices[10] = 0.0f; + cell.vertices[11] = 0.0f; + cell.nvert = 4; + cell.orders[0] = 3; + cell.orders[1] = 3; + cell.orders[2] = 3; + cell.orders[3] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 3; + cell.offsets[2] = 6; + cell.offsets[3] = 9; + cell.edges[0] = 1; + cell.edges[3] = 2; + cell.edges[4] = 3; + cell.edges[5] = 0; + cell.edges[9] = 1; + cell.edgeindices[0] = 3; + cell.edgeindices[3] = 0; + cell.edgeindices[4] = 0; + cell.edgeindices[5] = 0; + cell.edgeindices[9] = 1; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 1); + assert(up == 3); + assert(us == 0); + assert(u == 0.25f); + assert(lp == 1); + assert(ls == 1); + assert(l == -0.5f); + } + + /* PATH 2.4.3 */ + // first vertex is below, second is closer, third is above + // first vertex is first edge of second + { + cell.vertices[0] = -1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = -0.5f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.vertices[6] = 1.0f; + cell.vertices[7] = 0.0f; + cell.vertices[8] = 0.0f; + cell.nvert = 3; + cell.orders[0] = 3; + cell.orders[1] = 3; + cell.orders[2] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 3; + cell.offsets[2] = 6; + cell.edges[0] = 1; + cell.edges[3] = 0; + cell.edges[4] = 2; + cell.edges[6] = 1; + cell.edgeindices[0] = 0; + cell.edgeindices[3] = 0; + cell.edgeindices[4] = 0; + cell.edgeindices[6] = 1; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 1); + assert(up == 2); + assert(us == 0); + assert(u == 0.25f); + assert(lp == 1); + assert(ls == 1); + assert(l == -0.5f); + } + + /* PATH 2.4.4 */ + // first vertex is below, second is closer, fourth is above + // first vertex is first edge of second + { + cell.vertices[0] = -1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = -0.5f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.vertices[6] = -2.0f; + cell.vertices[7] = 0.0f; + cell.vertices[8] = 0.0f; + cell.vertices[9] = 1.0f; + cell.vertices[10] = 0.0f; + cell.vertices[11] = 0.0f; + cell.nvert = 4; + cell.orders[0] = 3; + cell.orders[1] = 4; + cell.orders[2] = 3; + cell.orders[3] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 3; + cell.offsets[2] = 7; + cell.offsets[3] = 10; + cell.edges[0] = 1; + cell.edges[3] = 0; + cell.edges[4] = 2; + cell.edges[5] = 3; + cell.edges[10] = 1; + cell.edgeindices[0] = 0; + cell.edgeindices[3] = 0; + cell.edgeindices[5] = 0; + cell.edgeindices[10] = 2; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 1); + assert(up == 3); + assert(us == 0); + assert(u == 0.25f); + assert(lp == 1); + assert(ls == 2); + assert(l == -0.5f); + } + + /* PATH 2.4.5 */ + // first vertex is below, second is closer, fourth is above + // first vertex is first edge of second + // second vertex is order 3 vertex (and not order 4 like 2.4.4) + { + cell.vertices[0] = -1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = -0.5f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.vertices[6] = -2.0f; + cell.vertices[7] = 0.0f; + cell.vertices[8] = 0.0f; + cell.vertices[9] = 1.0f; + cell.vertices[10] = 0.0f; + cell.vertices[11] = 0.0f; + cell.nvert = 4; + cell.orders[0] = 3; + cell.orders[1] = 3; + cell.orders[2] = 3; + cell.orders[3] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 3; + cell.offsets[2] = 6; + cell.offsets[3] = 9; + cell.edges[0] = 1; + cell.edges[3] = 0; + cell.edges[4] = 2; + cell.edges[5] = 3; + cell.edges[9] = 1; + cell.edgeindices[0] = 0; + cell.edgeindices[3] = 0; + cell.edgeindices[5] = 0; + cell.edgeindices[9] = 2; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 1); + assert(up == 3); + assert(us == 0); + assert(u == 0.25f); + assert(lp == 1); + assert(ls == 2); + assert(l == -0.5f); + } + + /* PATH 2.4.6 */ + // first vertex is below, second is closer and is closest + { + cell.vertices[0] = -1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = -0.5f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.vertices[6] = -2.0f; + cell.vertices[7] = 0.0f; + cell.vertices[8] = 0.0f; + cell.nvert = 3; + cell.orders[0] = 3; + cell.orders[1] = 2; + cell.orders[2] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 3; + cell.offsets[2] = 5; + cell.edges[0] = 1; + cell.edges[3] = 0; + cell.edges[4] = 2; + cell.edgeindices[0] = 0; + cell.edgeindices[3] = 0; + cell.edgeindices[4] = 0; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 0); + } + + /* PATH 2.5 */ + // first vertex is below, second is too close to call + { + cell.vertices[0] = -1.0f; + cell.vertices[1] = 0.0f; + cell.vertices[2] = 0.0f; + cell.vertices[3] = 0.5f; + cell.vertices[4] = 0.0f; + cell.vertices[5] = 0.0f; + cell.nvert = 2; + cell.orders[0] = 3; + cell.orders[1] = 3; + cell.offsets[0] = 0; + cell.offsets[1] = 3; + cell.edges[0] = 1; + cell.edges[3] = 0; + cell.edgeindices[0] = 0; + cell.edgeindices[3] = 0; + dx[0] = 0.5f; + dx[1] = 0.0f; + dx[2] = 0.0f; + r2 = 0.25f; + int result = voronoi_intersect_find_closest_vertex( + &cell, dx, r2, &u, &up, &us, &uw, &l, &lp, &ls, &lw, &q, &qp, &qs, &qw); + assert(result == 2); + } +} + +#ifdef SHADOWFAX_SPH +void set_coordinates(struct part *p, double x, double y, double z, + unsigned int id) { + + double box_anchor[3] = {VORONOI3D_BOX_ANCHOR_X, VORONOI3D_BOX_ANCHOR_Y, + VORONOI3D_BOX_ANCHOR_Z}; + double box_side[3] = {VORONOI3D_BOX_SIDE_X, VORONOI3D_BOX_SIDE_Y, + VORONOI3D_BOX_SIDE_Z}; + + p->x[0] = x; + p->x[1] = y; + p->x[2] = z; + p->id = id; + voronoi_cell_init(&p->cell, p->x, box_anchor, box_side); +} +#endif + +void test_degeneracies() { +#ifdef SHADOWFAX_SPH + int idx = 0; + /* make a small cube */ + struct part particles[100]; + set_coordinates(&particles[idx], 0.1, 0.1, 0.1, idx); + idx++; + set_coordinates(&particles[idx], 0.2, 0.1, 0.1, idx); + idx++; + set_coordinates(&particles[idx], 0.1, 0.2, 0.1, idx); + idx++; + set_coordinates(&particles[idx], 0.1, 0.1, 0.2, idx); + idx++; + /* corner on cutting plane */ + set_coordinates(&particles[idx], 0.2, 0.2, 0.2, idx); + idx++; + /* edge on cutting plane */ + set_coordinates(&particles[idx], 0.2, 0.1, 0.2, idx); + idx++; + set_coordinates(&particles[idx], 0.2, 0.2, 0.1, idx); + idx++; + /* cutting plane is diagonal */ + set_coordinates(&particles[idx], 0.05, 0.1, 0.05, idx); + idx++; + /* order 4 vertex (found after an impressive display of analytical geometry + of which I'm rather proud) */ + float t = 0.5 / 0.0475; + set_coordinates(&particles[idx], 0.0075 * t + 0.1, 0.0075 * t + 0.1, + 0.1 - 0.0025 * t, idx); + idx++; + /* order 4 vertex with float edge */ + t = 0.35 / 0.06125; + set_coordinates(&particles[idx], 0.0075 * t + 0.1, 0.015 * t + 0.1, + 0.1 - 0.005 * t, idx); + idx++; + /* plane that was already encountered */ + t = 0.5 / 0.0475; + set_coordinates(&particles[idx], 0.0075 * t + 0.1, 0.0075 * t + 0.1, + 0.1 - 0.0025 * t, idx); + idx++; + /* no intersection (just to cover all code) */ + set_coordinates(&particles[idx], 0.3, 0.3, 0.3, idx); + idx++; + set_coordinates(&particles[idx], 0.3, 0.1, 0.3, idx); + idx++; + /* order 5 vertex */ + t = 0.04 / 0.0175; + set_coordinates(&particles[idx], 0.1 - 0.0075 * t, 0.1 + 0.00375 * t, + 0.1 + 0.00625 * t, idx); + idx++; + /* plane with order 5 vertex */ + set_coordinates(&particles[idx], 0.1, 0.2, 0.1, idx); + idx++; + /* edge with order 5 vertex that looses an edge */ + t = -0.1 / 0.095; + set_coordinates(&particles[idx], 0.1 - 0.015 * t, 0.1 + 0.015 * t, + 0.1 - 0.005 * t, idx); + idx++; + for (int i = 1; i < idx; i++) { + float dx[3]; + dx[0] = particles[0].x[0] - particles[i].x[0]; + dx[1] = particles[0].x[1] - particles[i].x[1]; + dx[2] = particles[0].x[2] - particles[i].x[2]; + voronoi_cell_interact(&particles[0].cell, dx, particles[i].id); + } +#endif +} + +int main() { + + /* Set the all enclosing simulation box dimensions */ + double box_anchor[3] = {VORONOI3D_BOX_ANCHOR_X, VORONOI3D_BOX_ANCHOR_Y, + VORONOI3D_BOX_ANCHOR_Z}; + double box_side[3] = {VORONOI3D_BOX_SIDE_X, VORONOI3D_BOX_SIDE_Y, + VORONOI3D_BOX_SIDE_Z}; + + /* Check basic Voronoi cell functions */ + test_voronoi_volume_tetrahedron(); + test_voronoi_centroid_tetrahedron(); + test_calculate_cell(); + + /* Test the different paths */ + test_paths(); + + /* Test the interaction and geometry algorithms */ + { + /* Create a Voronoi cell */ + double x[3] = {0.5f, 0.5f, 0.5f}; + struct voronoi_cell cell; + voronoi_cell_init(&cell, x, box_anchor, box_side); + + /* Interact with neighbours */ + float x0[3] = {0.5f, 0.0f, 0.0f}; + float x1[3] = {-0.5f, 0.0f, 0.0f}; + float x2[3] = {0.0f, 0.5f, 0.0f}; + float x3[3] = {0.0f, -0.5f, 0.0f}; + float x4[3] = {0.0f, 0.0f, 0.5f}; + float x5[3] = {0.0f, 0.0f, -0.5f}; + voronoi_cell_interact(&cell, x0, 1); + voronoi_cell_interact(&cell, x1, 2); + voronoi_cell_interact(&cell, x2, 3); + voronoi_cell_interact(&cell, x3, 4); + voronoi_cell_interact(&cell, x4, 5); + voronoi_cell_interact(&cell, x5, 6); + float expected_midpoints[6][3], expected_areas[6]; + expected_areas[0] = 0.25f; + expected_midpoints[0][0] = 0.25f; + expected_midpoints[0][1] = 0.5f; + expected_midpoints[0][2] = 0.5f; + expected_areas[1] = 0.25f; + expected_midpoints[1][0] = 0.75f; + expected_midpoints[1][1] = 0.5f; + expected_midpoints[1][2] = 0.5f; + expected_areas[2] = 0.25f; + expected_midpoints[2][0] = 0.5f; + expected_midpoints[2][1] = 0.25f; + expected_midpoints[2][2] = 0.5f; + expected_areas[3] = 0.25f; + expected_midpoints[3][0] = 0.5f; + expected_midpoints[3][1] = 0.75f; + expected_midpoints[3][2] = 0.5f; + expected_areas[4] = 0.25f; + expected_midpoints[4][0] = 0.5f; + expected_midpoints[4][1] = 0.5f; + expected_midpoints[4][2] = 0.25f; + expected_areas[5] = 0.25f; + expected_midpoints[5][0] = 0.5f; + expected_midpoints[5][1] = 0.5f; + expected_midpoints[5][2] = 0.75f; + + /* Interact with some more neighbours to check if they are properly + ignored */ + float xE0[3] = {0.6f, 0.0f, 0.1f}; + float xE1[3] = {-0.7f, 0.2f, 0.04f}; + voronoi_cell_interact(&cell, xE0, 7); + voronoi_cell_interact(&cell, xE1, 8); + + /* Finalize cell and check results */ + voronoi_cell_finalize(&cell); + + if (fabs(cell.volume - 0.125f) > 1.e-5) { + error("Wrong volume: %g!", cell.volume); + } + if (fabs(cell.centroid[0] - 0.5f) > 1.e-5f || + fabs(cell.centroid[1] - 0.5f) > 1.e-5f || + fabs(cell.centroid[2] - 0.5f) > 1.e-5f) { + error("Wrong centroid: %g %g %g!", cell.centroid[0], cell.centroid[1], + cell.centroid[2]); + } + + /* Check faces. */ + float A, midpoint[3]; + for (int i = 0; i < 6; ++i) { + A = voronoi_get_face(&cell, i + 1, midpoint); + if (A) { + if (fabs(A - expected_areas[i]) > 1.e-5) { + error("Wrong surface area: %g!", A); + } + if (fabs(midpoint[0] - expected_midpoints[i][0] + cell.x[0]) > 1.e-5 || + fabs(midpoint[1] - expected_midpoints[i][1] + cell.x[1]) > 1.e-5 || + fabs(midpoint[2] - expected_midpoints[i][2] + cell.x[2]) > 1.e-5) { + error("Wrong face midpoint: %g %g %g (should be %g %g %g)!", + midpoint[0], midpoint[1], midpoint[2], expected_midpoints[i][0], + expected_midpoints[i][1], expected_midpoints[i][2]); + } + } else { + error("Neighbour %i not found!", i); + } + } + } + + /* Test degenerate cases */ + test_degeneracies(); + + /* Construct a small random grid */ + { + message("Constructing a small random grid..."); + + int i, j; + double x[3]; + float dx[3]; + float Vtot; + struct voronoi_cell cells[TESTVORONOI3D_NUMCELL_RANDOM]; + struct voronoi_cell *cell_i, *cell_j; + + /* initialize cells with random generator locations */ + for (i = 0; i < TESTVORONOI3D_NUMCELL_RANDOM; ++i) { + x[0] = random_uniform(0., 1.); + x[1] = random_uniform(0., 1.); + x[2] = random_uniform(0., 1.); + voronoi_cell_init(&cells[i], x, box_anchor, box_side); + } + + /* interact the cells */ + for (i = 0; i < TESTVORONOI3D_NUMCELL_RANDOM; ++i) { + cell_i = &cells[i]; + for (j = 0; j < TESTVORONOI3D_NUMCELL_RANDOM; ++j) { + if (i != j) { + cell_j = &cells[j]; + dx[0] = cell_i->x[0] - cell_j->x[0]; + dx[1] = cell_i->x[1] - cell_j->x[1]; + dx[2] = cell_i->x[2] - cell_j->x[2]; + voronoi_cell_interact(cell_i, dx, j); + } + } + } + + Vtot = 0.0f; + /* print the cells to the stdout */ + for (i = 0; i < TESTVORONOI3D_NUMCELL_RANDOM; ++i) { + /* voronoi_print_gnuplot_c(&cells[i]);*/ + voronoi_cell_finalize(&cells[i]); + Vtot += cells[i].volume; + } + + assert(fabs(Vtot - 1.0f) < 1.e-6); + + message("Done."); + } + + /* Construct a small Cartesian grid full of degeneracies */ + { + message("Constructing a Cartesian grid..."); + + int i, j, k; + double x[3]; + float dx[3]; + float Vtot; + struct voronoi_cell cells[TESTVORONOI3D_NUMCELL_CARTESIAN_3D]; + struct voronoi_cell *cell_i, *cell_j; + + /* initialize cells with Cartesian generator locations */ + for (i = 0; i < TESTVORONOI3D_NUMCELL_CARTESIAN_1D; ++i) { + for (j = 0; j < TESTVORONOI3D_NUMCELL_CARTESIAN_1D; ++j) { + for (k = 0; k < TESTVORONOI3D_NUMCELL_CARTESIAN_1D; ++k) { + x[0] = (i + 0.5f) * 1.0 / TESTVORONOI3D_NUMCELL_CARTESIAN_1D; + x[1] = (j + 0.5f) * 1.0 / TESTVORONOI3D_NUMCELL_CARTESIAN_1D; + x[2] = (k + 0.5f) * 1.0 / TESTVORONOI3D_NUMCELL_CARTESIAN_1D; + voronoi_cell_init(&cells[TESTVORONOI3D_NUMCELL_CARTESIAN_1D * + TESTVORONOI3D_NUMCELL_CARTESIAN_1D * i + + TESTVORONOI3D_NUMCELL_CARTESIAN_1D * j + k], + x, box_anchor, box_side); + } + } + } + + /* interact the cells */ + for (i = 0; i < TESTVORONOI3D_NUMCELL_CARTESIAN_3D; ++i) { + cell_i = &cells[i]; + for (j = 0; j < TESTVORONOI3D_NUMCELL_CARTESIAN_3D; ++j) { + if (i != j) { + cell_j = &cells[j]; + dx[0] = cell_i->x[0] - cell_j->x[0]; + dx[1] = cell_i->x[1] - cell_j->x[1]; + dx[2] = cell_i->x[2] - cell_j->x[2]; + voronoi_cell_interact(cell_i, dx, j); + } + } + } + + Vtot = 0.0f; + /* print the cells to the stdout */ + for (i = 0; i < TESTVORONOI3D_NUMCELL_CARTESIAN_3D; ++i) { + /* voronoi_print_gnuplot_c(&cells[i]);*/ + voronoi_cell_finalize(&cells[i]); + Vtot += cells[i].volume; + } + + message("Vtot: %g (Vtot-1.0f: %g)", Vtot, (Vtot - 1.0f)); + assert(fabs(Vtot - 1.0f) < 2.e-6); + + message("Done."); + } + + return 0; +}