Skip to content

Commit

Permalink
Merge pull request #892 from DLR-AMR/feature-c-api-for-geometry_analytic
Browse files Browse the repository at this point in the history
Feature: C-API for Geometry Analytic
  • Loading branch information
sandro-elsweijer authored Feb 26, 2024
2 parents fa6ae93 + 12769e1 commit 786dd1c
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 46 deletions.
39 changes: 34 additions & 5 deletions example/geometry/t8_example_geometries.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
#include <t8_forest/t8_forest_general.h>
#include <t8_forest/t8_forest_io.h>
#include <t8_geometry/t8_geometry_base.hxx>
#include <t8_geometry/t8_geometry_implementations/t8_geometry_analytic.hxx>
#include <t8_geometry/t8_geometry_implementations/t8_geometry_analytic.h>
#include <t8_geometry/t8_geometry_implementations/t8_geometry_linear.hxx>
#include <t8_geometry/t8_geometry_implementations/t8_geometry_cad.hxx>
#include <t8_geometry/t8_geometry_implementations/t8_geometry_analytic.hxx>
#include <t8_geometry/t8_geometry_helpers.h>
#include <t8_cmesh/t8_cmesh_examples.h>

Expand Down Expand Up @@ -63,6 +64,7 @@ typedef enum {
T8_GEOM_CIRCLE,
T8_GEOM_3D,
T8_GEOM_MOVING,
T8_GEOM_ANALYTIC_QUAD_TO_SPHERE,
T8_GEOM_CAD_TRIANGLE,
T8_GEOM_CAD_CURVE_CUBE,
T8_GEOM_CAD_SURFACE_CUBES,
Expand Down Expand Up @@ -510,6 +512,23 @@ t8_geom_adapt_boundary (t8_forest_t forest, t8_forest_t forest_from, t8_locidx_t
return 0;
}

void
quad_to_sphere_callback (t8_cmesh_t cmesh, t8_gloidx_t gtreeid, const double *ref_coords, const size_t num_coords,
double *out_coords, const void *tree_data, const void *user_data)
{
for (size_t i_coord = 0; i_coord < num_coords; i_coord++) {
const size_t offset = 3 * i_coord;

const double radius = 1.0;
const double latitude = 2 * M_PI * ref_coords[offset + 0];
const double longitude = ref_coords[offset + 1] * M_PI;

out_coords[offset + 0] = radius * sin (longitude) * cos (latitude);
out_coords[offset + 1] = radius * sin (longitude) * sin (latitude);
out_coords[offset + 2] = radius * cos (longitude);
}
}

static void
t8_analytic_geom (int level, t8_example_geom_type geom_type)
{
Expand Down Expand Up @@ -595,6 +614,15 @@ t8_analytic_geom (int level, t8_example_geom_type geom_type)
t8_cmesh_set_tree_class (cmesh, 0, T8_ECLASS_QUAD);
snprintf (vtuname, BUFSIZ, "forest_moving_lvl_%i", level);
break;
case T8_GEOM_ANALYTIC_QUAD_TO_SPHERE:
t8_global_productionf ("Wrapping a quad around a sphere.\n");

geometry = new t8_geometry_analytic (3, "geom_quad_to_sphere", quad_to_sphere_callback, NULL, NULL, NULL);
t8_cmesh_set_tree_class (cmesh, 0, T8_ECLASS_QUAD);
t8_cmesh_set_join (cmesh, 0, 0, 1, 0, 0);

snprintf (vtuname, BUFSIZ, "forest_quad_to_sphere");
break;
case T8_GEOM_CAD_TRIANGLE: {
#if T8_WITH_OCC
t8_global_productionf ("Creating uniform level %i forests with an cad triangle geometry.\n", level);
Expand Down Expand Up @@ -1066,10 +1094,11 @@ main (int argc, char **argv)
"\t\t The mesh will not be uniform. Instead it is refined at the domain boundary.\n"
"\t\t5 - A cube that is distorted in z-direction with one 3D cube tree.\n"
"\t\t6 - A moving mesh consisting of a single 2D quad tree.\n"
"\t\t7 - A curved triangle with an cad curve.\n"
"\t\t8 - A cube with two cad curves as edges.\n"
"\t\t9 - Two cubes with one cad surface as face.\n"
"\t\t10 - A hollow cylinder with a cad surface on the in- and outside.\n");
"\t\t7 - A quad morphed into a sphere.\n"
"\t\t8 - A curved triangle with an cad curve.\n"
"\t\t9 - A cube with two cad curves as edges.\n"
"\t\t10 - Two cubes with one cad surface as face.\n"
"\t\t11 - A hollow cylinder with a cad surface on the in- and outside.\n");

parsed = sc_options_parse (t8_get_package_id (), SC_LP_ERROR, opt, argc, argv);
if (helpme) {
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ libt8_installed_headers_geometry_impl = \
src/t8_geometry/t8_geometry_implementations/t8_geometry_linear.h \
src/t8_geometry/t8_geometry_implementations/t8_geometry_linear_axis_aligned.h \
src/t8_geometry/t8_geometry_implementations/t8_geometry_analytic.hxx \
src/t8_geometry/t8_geometry_implementations/t8_geometry_analytic.h \
src/t8_geometry/t8_geometry_implementations/t8_geometry_examples.h \
src/t8_geometry/t8_geometry_implementations/t8_geometry_cad.h \
src/t8_geometry/t8_geometry_implementations/t8_geometry_cad.hxx \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,32 @@ t8_geometry_analytic::t8_geom_load_tree_data (t8_cmesh_t cmesh, t8_gloidx_t gtre
}
}

T8_EXTERN_C_BEGIN ();

void
t8_geometry_analytic_destroy (t8_geometry_c **geom)
{
T8_ASSERT (geom != NULL);

delete *geom;
*geom = NULL;
}

t8_geometry_c *
t8_geometry_analytic_new (int dim, const char *name, t8_geom_analytic_fn analytical,
t8_geom_analytic_jacobian_fn jacobian, t8_geom_load_tree_data_fn load_tree_data,
const void *user_data)
{
t8_geometry_analytic *geom = new t8_geometry_analytic (dim, name, analytical, jacobian, load_tree_data, user_data);
return (t8_geometry_c *) geom;
}

void
t8_geom_load_tree_data_vertices (t8_cmesh_t cmesh, t8_gloidx_t gtreeid, const void **vertices_out)
t8_geom_load_tree_data_vertices (t8_cmesh_t cmesh, t8_gloidx_t gtreeid, const void **user_data)
{
T8_ASSERT (t8_cmesh_is_committed (cmesh));
t8_locidx_t ltreeid = t8_cmesh_get_local_id (cmesh, gtreeid);
*vertices_out = t8_cmesh_get_tree_vertices (cmesh, ltreeid);
*user_data = t8_cmesh_get_tree_vertices (cmesh, ltreeid);
}

T8_EXTERN_C_END ();
96 changes: 96 additions & 0 deletions src/t8_geometry/t8_geometry_implementations/t8_geometry_analytic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
This file is part of t8code.
t8code is a C library to manage a collection (a forest) of multiple
connected adaptive space-trees of general element classes in parallel.
Copyright (C) 2024 the developers
t8code is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
t8code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with t8code; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

/** \file t8_geometry_analytic.h
* This header provides the C interface to create an analytical geometry.
*/

#ifndef T8_GEOMETRY_ANALYTIC_H
#define T8_GEOMETRY_ANALYTIC_H

/**
* Definition of an analytic geometry function.
* This function maps reference coordinates to physical
* coordinates.
* \param [in] cmesh The cmesh.
* \param [in] gtreeid The global tree (of the cmesh) in which the reference point is.
* \param [in] ref_coords Array of dimension x \a num_coords many entries, specifying a point in \f$ [0,1]^\mathrm{dim} \f$.
* \param [in] num_coords
* \param [out] out_coords The mapped coordinates in physical space of \a ref_coords. The length is \a num_coords * 3.
* \param [in] tree_data The data of the current tree as loaded by a \ref t8_geom_load_tree_data_fn.
* \param [in] user_data The user data pointer stored in the geometry.
*/
typedef void (*t8_geom_analytic_fn) (t8_cmesh_t cmesh, t8_gloidx_t gtreeid, const double *ref_coords,
const size_t num_coords, double *out_coords, const void *tree_data,
const void *user_data);

/**
* Definition for the jacobian of an analytic geometry function.
* \param [in] cmesh The cmesh.
* \param [in] gtreeid The global tree (of the cmesh) in which the reference point is.
* \param [in] ref_coords Array of \a dimension x \a num_coords many entries, specifying points in \f$ [0,1]^\mathrm{dim} \f$.
* \param [in] num_coords Amount of points of /f$ \mathrm{dim} /f$ to map.
* \param [out] jacobian The jacobian at \a ref_coords. Array of size \f$ \mathrm{dim} \cdot 3 \f$ x \a num_coords. Indices \f$ 3 \cdot i\f$ , \f$ 3 \cdot i+1 \f$ , \f$ 3 \cdot i+2 \f$
* correspond to the \f$ i \f$-th column of the jacobian (Entry \f$ 3 \cdot i + j \f$ is \f$ \frac{\partial f_j}{\partial x_i} \f$).
* \param [in] tree_data The data of the current tree as loaded by a \ref t8_geom_load_tree_data_fn.
* \param [in] user_data The user data pointer stored in the geometry.
*/
typedef void (*t8_geom_analytic_jacobian_fn) (t8_cmesh_t cmesh, t8_gloidx_t gtreeid, const double *ref_coords,
const size_t num_coords, double *jacobian, const void *tree_data,
const void *user_data);

/**
* Definition for the load tree data function.
* \param [in] cmesh The cmesh.
* \param [in] gtreeid The global tree (of the cmesh) in which the reference point is.
* \param [in] tree_data The data of the trees.
*/
typedef void (*t8_geom_load_tree_data_fn) (t8_cmesh_t cmesh, t8_gloidx_t gtreeid, const void **tree_data);

T8_EXTERN_C_BEGIN ();

/** Destroy a geometry analytic object.
* \param [in,out] geom A pointer to a geometry object. Set to NULL on output.
*/
void
t8_geometry_analytic_destroy (t8_geometry_c **geom);

/** Create a new analytical geometry.
* \return A pointer to an allocated geometry struct.
*/
t8_geometry_c *
t8_geometry_analytic_new (int dim, const char *name, t8_geom_analytic_fn analytical,
t8_geom_analytic_jacobian_fn jacobian, t8_geom_load_tree_data_fn load_tree_data,
const void *user_data);

/**
* Load vertex data from given tree.
* \param [in] cmesh The cmesh.
* \param [in] gtreeid The global tree id (in the cmesh).
* \param [out] vertex_out The load tree vertices.
*/
void
t8_geom_load_tree_data_vertices (t8_cmesh_t cmesh, t8_gloidx_t gtreeid, const void **user_data);

T8_EXTERN_C_END ();

#endif /* T8_GEOMETRY_ANALYTIC_H */
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
*/

/** \file t8_geometry_analytic.hxx
* TODO: Add description
* This geometry implements analytic geometries. It provides an interface to
* define custom functions for evaluation, Jacobians and the loading of the
* tree data.
*/

#ifndef T8_GEOMETRY_ANALYTIC_HXX
Expand All @@ -30,40 +32,7 @@
#include <t8.h>
#include <t8_geometry/t8_geometry_with_vertices.hxx>
#include <t8_geometry/t8_geometry_with_vertices.h>

/**
* Definition of an analytic geometry function.
* This function maps reference coordinates to physical
* coordinates.
* \param [in] cmesh The cmesh.
* \param [in] gtreeid The global tree (of the cmesh) in which the reference point is.
* \param [in] ref_coords Array of dimension x \a num_coords many entries, specifying a point in \f$ [0,1]^\mathrm{dim} \f$.
* \param [in] num_coords
* \param [out] out_coords The mapped coordinates in physical space of \a ref_coords. The length is \a num_coords * 3.
* \param [in] tree_data The data of the current tree as loaded by a \ref t8_geom_load_tree_data_fn.
* \param [in] user_data The user data pointer stored in the geometry.
*/
typedef void (*t8_geom_analytic_fn) (t8_cmesh_t cmesh, t8_gloidx_t gtreeid, const double *ref_coords,
const size_t num_coords, double *out_coords, const void *tree_data,
const void *user_data);

/**
* Definition for the jacobian of an analytic geometry function.
* \param [in] cmesh The cmesh.
* \param [in] gtreeid The global tree (of the cmesh) in which the reference point is.
* \param [in] ref_coords Array of \a dimension x \a num_coords many entries, specifying points in \f$ [0,1]^\mathrm{dim} \f$.
* \param [in] num_coords Amount of points of /f$ \mathrm{dim} /f$ to map.
* \param [out] jacobian The jacobian at \a ref_coords. Array of size \f$ \mathrm{dim} \cdot 3 \f$ x \a num_coords. Indices \f$ 3 \cdot i\f$ , \f$ 3 \cdot i+1 \f$ , \f$ 3 \cdot i+2 \f$
* correspond to the \f$ i \f$-th column of the jacobian (Entry \f$ 3 \cdot i + j \f$ is \f$ \frac{\partial f_j}{\partial x_i} \f$).
* \param [in] tree_data The data of the current tree as loaded by a \ref t8_geom_load_tree_data_fn.
* \param [in] user_data The user data pointer stored in the geometry.
*/
typedef void (*t8_geom_analytic_jacobian_fn) (t8_cmesh_t cmesh, t8_gloidx_t gtreeid, const double *ref_coords,
const size_t num_coords, double *jacobian, const void *tree_data,
const void *user_data);

/* TODO: Document. */
typedef void (*t8_geom_load_tree_data_fn) (t8_cmesh_t cmesh, t8_gloidx_t gtreeid, const void **tree_data);
#include <t8_geometry/t8_geometry_implementations/t8_geometry_analytic.h>

struct t8_geometry_analytic: public t8_geometry
{
Expand Down Expand Up @@ -176,8 +145,4 @@ struct t8_geometry_analytic: public t8_geometry
* and modified via \ref t8_geom_analytic_get_user_data. */
};

/* TODO: Document */
void
t8_geom_load_tree_data_vertices (t8_cmesh_t cmesh, t8_gloidx_t gtreeid, const void **vertices_out);

#endif /* !T8_GEOMETRY_ANALYTICAL_HXX! */

0 comments on commit 786dd1c

Please sign in to comment.