Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
DLR-AMR
GitHub Repository: DLR-AMR/t8code
Path: blob/main/api/t8_fortran_interface/t8_fortran_interface.c
504 views
1
/*
2
This file is part of t8code.
3
t8code is a C library to manage a collection (a forest) of multiple
4
connected adaptive space-trees of general element classes in parallel.
5
6
Copyright (C) 2025 the developers
7
8
t8code is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
12
13
t8code is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
17
18
You should have received a copy of the GNU General Public License
19
along with t8code; if not, write to the Free Software Foundation, Inc.,
20
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
*/
22
23
#include <t8_fortran_interface.h>
24
#include <t8_forest/t8_forest_general.h>
25
#include <t8_forest/t8_forest_geometrical.h>
26
#include <t8_cmesh/t8_cmesh_examples.h>
27
#include <t8_cmesh/t8_cmesh_helpers.h>
28
#include <t8_schemes/t8_scheme.h>
29
#include <t8_schemes/t8_default/t8_default_c_interface.h>
30
31
void
32
t8_fortran_init_all_ (sc_MPI_Comm *comm)
33
{
34
T8_ASSERT (comm != NULL);
35
/* Initialize sc */
36
sc_init (*comm, 1, 1, NULL, SC_LP_DEFAULT);
37
/* Initialize t8code */
38
t8_init (SC_LP_DEFAULT);
39
}
40
41
/* Wrapper around sc_finalize */
42
void
43
t8_fortran_finalize ()
44
{
45
sc_finalize ();
46
}
47
48
void
49
t8_fortran_cmesh_commit (t8_cmesh_t cmesh, sc_MPI_Comm *comm)
50
{
51
t8_cmesh_commit (cmesh, *comm);
52
}
53
54
void
55
t8_fortran_cmesh_set_join_by_stash_noConn (t8_cmesh_t cmesh, const int do_both_directions)
56
{
57
t8_cmesh_set_join_by_stash (cmesh, NULL, do_both_directions);
58
}
59
60
void
61
t8_fortran_init_all (sc_MPI_Comm *comm)
62
{
63
int rank;
64
65
t8_fortran_init_all_ (comm);
66
if (*comm != sc_MPI_COMM_NULL) {
67
sc_MPI_Comm_rank (*comm, &rank);
68
t8_debugf ("rank = %i\n", rank);
69
}
70
}
71
72
void
73
t8_fortran_init_all_noMPI ()
74
{
75
sc_MPI_Comm commnull = sc_MPI_COMM_NULL;
76
t8_fortran_init_all (&commnull);
77
}
78
79
/* Build C MPI comm from Fortran MPI Comm. */
80
sc_MPI_Comm *
81
t8_fortran_MPI_Comm_new (MPI_T8_Fint Fcomm)
82
{
83
#if !T8_ENABLE_MPI
84
SC_ABORT ("t8code was not configured with MPI support.");
85
return NULL;
86
#endif
87
/* We use malloc instead of T8_ALLOC since t8code may not be initialized
88
* yet. */
89
sc_MPI_Comm *Ccomm = (sc_MPI_Comm *) malloc (sizeof (*Ccomm));
90
#if T8_ENABLE_MPI
91
/* If configured with MPI, transform the Fortran communicator handle to a C handle */
92
*Ccomm = MPI_Comm_f2c (Fcomm);
93
#else
94
/* In case it is not configured with MPI, set the communicator to NULL as a fallback */
95
*Ccomm = sc_MPI_COMM_NULL;
96
#endif
97
t8_debugf ("Created comm %lu\n", (long unsigned) Ccomm);
98
return Ccomm;
99
}
100
101
/* Delete C MPI Comm. */
102
void
103
t8_fortran_MPI_Comm_delete (sc_MPI_Comm *Ccomm)
104
{
105
#if !T8_ENABLE_MPI
106
SC_ABORT ("t8code was not configured with MPI support.");
107
#endif
108
free (Ccomm);
109
}
110
111
t8_cmesh_t
112
t8_cmesh_new_periodic_tri_wrap (sc_MPI_Comm *Ccomm)
113
{
114
return t8_cmesh_new_periodic_tri (*Ccomm);
115
}
116
117
t8_forest_t
118
t8_forest_new_uniform_default (t8_cmesh_t cmesh, int level, int do_face_ghost, sc_MPI_Comm *comm)
119
{
120
const t8_scheme_c *default_scheme = t8_scheme_new_default ();
121
122
T8_ASSERT (comm != NULL);
123
return t8_forest_new_uniform (cmesh, default_scheme, level, do_face_ghost, *comm);
124
}
125
126
int
127
t8_fortran_adapt_by_coordinates_callback (t8_forest_t forest, t8_forest_t forest_from, t8_locidx_t which_tree,
128
const t8_eclass_t tree_class,
129
__attribute__ ((unused)) t8_locidx_t lelement_id, const t8_scheme_c *scheme,
130
const int is_family, const int num_elements, t8_element_t *elements[])
131
{
132
t8_fortran_adapt_coordinate_callback callback
133
= (t8_fortran_adapt_coordinate_callback) t8_forest_get_user_function (forest);
134
double midpoint[3];
135
t8_forest_element_centroid (forest_from, which_tree, elements[0], midpoint);
136
t8_debugf ("Coord: %.2f\n", midpoint[0]);
137
int ret = callback (midpoint[0], midpoint[1], midpoint[2], num_elements > 0);
138
139
/* Coarsen if a family was given and return value is negative. */
140
if (is_family) {
141
/* The elements form a family */
142
T8_ASSERT (t8_elements_are_family (scheme, tree_class, elements));
143
/* Build the parent. */
144
t8_element_t *parent;
145
t8_element_new (scheme, tree_class, 1, &parent);
146
t8_element_get_parent (scheme, tree_class, elements[0], parent);
147
/* Get the coordinates of the parent. */
148
t8_forest_element_centroid (forest_from, which_tree, parent, midpoint);
149
150
ret = callback (midpoint[0], midpoint[1], midpoint[2], 1);
151
}
152
else {
153
/* The elements do not form a family. */
154
/* Get the coordinates of the first element and call callback */
155
t8_forest_element_centroid (forest_from, which_tree, elements[0], midpoint);
156
ret = callback (midpoint[0], midpoint[1], midpoint[2], 0);
157
T8_ASSERT (ret >= 0);
158
}
159
return ret;
160
}
161
162
t8_forest_t
163
t8_forest_adapt_by_coordinates (t8_forest_t forest, int recursive, t8_fortran_adapt_coordinate_callback callback)
164
{
165
t8_forest_t forest_new;
166
167
T8_ASSERT (t8_forest_is_committed (forest));
168
T8_ASSERT (callback != NULL);
169
170
/* Initialize new forest */
171
t8_forest_init (&forest_new);
172
/* Set the callback as user data */
173
t8_forest_set_user_function (forest_new, (t8_generic_function_pointer) callback);
174
/* Call set adapt */
175
t8_forest_set_adapt (forest_new, forest, t8_fortran_adapt_by_coordinates_callback, recursive);
176
/* Commit the forest */
177
t8_forest_commit (forest_new);
178
return forest_new;
179
}
180
181
void
182
t8_global_productionf_noargs (const char *string)
183
{
184
t8_global_productionf ("%s", string);
185
}
186
187