Skip to content

Commit 02f40d3

Browse files
format (#54)
1 parent 2b5c0bf commit 02f40d3

16 files changed

+1035
-1006
lines changed

.JuliaFormatter.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Use SciML style: https://github.com/SciML/SciMLStyle
2+
style = "sciml"
3+
4+
# Python style alignment. See https://github.com/domluna/JuliaFormatter.jl/pull/732.
5+
yas_style_nesting = true
6+
7+
# Align struct fields for better readability of large struct definitions
8+
align_struct_field = true

.github/workflows/FormatCheck.yml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: format-check
2+
3+
on:
4+
push:
5+
branches:
6+
- 'main'
7+
tags: '*'
8+
pull_request:
9+
10+
jobs:
11+
check-format:
12+
runs-on: ${{ matrix.os }}
13+
strategy:
14+
matrix:
15+
julia-version: [1]
16+
julia-arch: [x86]
17+
os: [ubuntu-latest]
18+
steps:
19+
- uses: julia-actions/setup-julia@latest
20+
with:
21+
version: ${{ matrix.julia-version }}
22+
- uses: actions/checkout@v4
23+
- uses: julia-actions/cache@v1
24+
- name: Install JuliaFormatter and format
25+
run: |
26+
julia -e 'using Pkg; Pkg.add(PackageSpec(name = "JuliaFormatter"))'
27+
julia -e 'using JuliaFormatter; format(["examples", "src/T8code.jl", "test"])'
28+
- name: Format check
29+
run: |
30+
julia -e '
31+
out = Cmd(`git diff --name-only`) |> read |> String
32+
if out == ""
33+
exit(0)
34+
else
35+
@error "Some files have not been formatted !!!"
36+
write(stdout, out)
37+
exit(1)
38+
end'

examples/t8_step1_coarsemesh.jl

+14-14
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ using T8code.Libt8: SC_LP_PRODUCTION
2323
# \param [in] comm MPI Communicator to use.
2424
# \return The coarse mesh.
2525
function t8_step1_build_tetcube_coarse_mesh(comm)
26-
# Build a coarse mesh of 6 tetrahedral trees that form a cube.
27-
# You can modify the first parameter to build a cube with different
28-
# tree shapes, i.e. T8_ECLASS_QUAD for a unit square with 1 quadrilateral tree.
29-
# See t8_eclass.h, t8_cmesh.h for all possible shapes.
30-
#
31-
# The second argument is the MPI communicator to use for this cmesh.
32-
# The remaining arguments are 3 flags that control
33-
# do_bcast - If non-zero only the root process will build the cmesh and will broadcast it to the other processes. The result is the same.
34-
# do_partition - If non-zero the cmesh will be partitioned among the processes. If 0 each process has a copy of the whole cmesh.
35-
# periodic - If non-zero the cube will have periodic boundaries. That is, i.e. the left face is connected to the right face.
26+
# Build a coarse mesh of 6 tetrahedral trees that form a cube.
27+
# You can modify the first parameter to build a cube with different
28+
# tree shapes, i.e. T8_ECLASS_QUAD for a unit square with 1 quadrilateral tree.
29+
# See t8_eclass.h, t8_cmesh.h for all possible shapes.
30+
#
31+
# The second argument is the MPI communicator to use for this cmesh.
32+
# The remaining arguments are 3 flags that control
33+
# do_bcast - If non-zero only the root process will build the cmesh and will broadcast it to the other processes. The result is the same.
34+
# do_partition - If non-zero the cmesh will be partitioned among the processes. If 0 each process has a copy of the whole cmesh.
35+
# periodic - If non-zero the cube will have periodic boundaries. That is, i.e. the left face is connected to the right face.
3636

37-
cmesh = t8_cmesh_new_hypercube(T8_ECLASS_TET, comm, 0, 0, 0)
37+
cmesh = t8_cmesh_new_hypercube(T8_ECLASS_TET, comm, 0, 0, 0)
3838

39-
return cmesh
39+
return cmesh
4040
end
4141

4242
# Write vtk (or more accurately vtu) files of the cmesh.
@@ -47,13 +47,13 @@ end
4747
# If the coarse mesh would be repartitioned, then it would write the .pvtu file
4848
# and additionally one file prefix_MPIRANK.vtu per MPI rank.
4949
function t8_step1_write_cmesh_vtk(cmesh, prefix)
50-
t8_cmesh_vtk_write_file(cmesh, prefix, 1.0)
50+
t8_cmesh_vtk_write_file(cmesh, prefix, 1.0)
5151
end
5252

5353
# Destroy a cmesh. This will free all allocated memory.
5454
# \param [in] cmesh A cmesh.
5555
function t8_step1_destroy_cmesh(cmesh)
56-
t8_cmesh_destroy(Ref(cmesh))
56+
t8_cmesh_destroy(Ref(cmesh))
5757
end
5858

5959
# The prefix for our output files.

examples/t8_step2_uniform_forest.jl

+11-11
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ using T8code.Libt8: SC_LP_PRODUCTION
5454
# \param [in] comm MPI Communicator to use.
5555
# \return The coarse mesh.
5656
function t8_step2_build_prismcube_coarse_mesh(comm)
57-
# Build a coarse mesh of 2 prism trees that form a cube.
58-
cmesh = t8_cmesh_new_hypercube(T8_ECLASS_PRISM, comm, 0, 0, 0)
57+
# Build a coarse mesh of 2 prism trees that form a cube.
58+
cmesh = t8_cmesh_new_hypercube(T8_ECLASS_PRISM, comm, 0, 0, 0)
5959

60-
t8_global_productionf(" [step2] Constructed coarse mesh with 2 prism trees.\n")
60+
t8_global_productionf(" [step2] Constructed coarse mesh with 2 prism trees.\n")
6161

62-
return cmesh
62+
return cmesh
6363
end
6464

6565
# Build a uniform forest on a cmesh
@@ -70,12 +70,12 @@ end
7070
# \return A uniform forest with the given refinement level that is
7171
# partitioned across the processes in \a comm.
7272
function t8_step2_build_uniform_forest(comm, cmesh, level)
73-
# /* Create the refinement scheme. */
74-
scheme = t8_scheme_new_default_cxx()
75-
# /* Creat the uniform forest. */
76-
forest = t8_forest_new_uniform(cmesh, scheme, level, 0, comm)
73+
# /* Create the refinement scheme. */
74+
scheme = t8_scheme_new_default_cxx()
75+
# /* Creat the uniform forest. */
76+
forest = t8_forest_new_uniform(cmesh, scheme, level, 0, comm)
7777

78-
return forest
78+
return forest
7979
end
8080

8181
# Write vtk (or more accurately vtu) files of the forest.
@@ -85,7 +85,7 @@ end
8585
# This will create the file prefix.pvtu
8686
# and additionally one file prefix_MPIRANK.vtu per MPI rank.
8787
function t8_step2_write_forest_vtk(forest, prefix)
88-
t8_forest_write_vtk(forest, prefix)
88+
t8_forest_write_vtk(forest, prefix)
8989
end
9090

9191
# Destroy a forest. This will free all allocated memory.
@@ -95,7 +95,7 @@ end
9595
# If we do not want this behaviour, but want to reuse for example the cmesh,
9696
# we need to call t8_cmesh_ref (cmesh) before passing it to t8_forest_new_uniform.
9797
function t8_step2_destroy_forest(forest)
98-
t8_forest_unref(Ref(forest))
98+
t8_forest_unref(Ref(forest))
9999
end
100100

101101
# The prefix for our output files.

examples/t8_step3_common.jl

+58-59
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# This is our own defined data that we will pass on to the
22
# adaptation callback.
33
mutable struct t8_step3_adapt_data_t
4-
midpoint :: NTuple{3,Cdouble}
5-
refine_if_inside_radius :: Cdouble
6-
coarsen_if_outside_radius :: Cdouble
4+
midpoint :: NTuple{3, Cdouble}
5+
refine_if_inside_radius :: Cdouble
6+
coarsen_if_outside_radius :: Cdouble
77
end
88

99
# The adaptation callback function. This function will be called once for each element
@@ -26,81 +26,80 @@ end
2626
# \param [in] num_elements The number of entries in \a elements elements that are defined.
2727
# \param [in] elements The element or family of elements to consider for refinement/coarsening.
2828
function t8_step3_adapt_callback(forest, forest_from, which_tree, lelement_id,
29-
ts, is_family, num_elements, elements_ptr) :: Cint
30-
# Our adaptation criterion is to look at the midpoint coordinates of the current element and if
31-
# they are inside a sphere around a given midpoint we refine, if they are outside, we coarsen.
29+
ts, is_family, num_elements, elements_ptr)::Cint
30+
# Our adaptation criterion is to look at the midpoint coordinates of the current element and if
31+
# they are inside a sphere around a given midpoint we refine, if they are outside, we coarsen.
3232

33-
centroid = Vector{Cdouble}(undef,3) # Will hold the element midpoint.
34-
# In t8_step3_adapt_forest we pass a t8_step3_adapt_data pointer as user data to the
35-
# t8_forest_new_adapt function. This pointer is stored as the used data of the new forest
36-
# and we can now access it with t8_forest_get_user_data (forest).
37-
adapt_data_ptr = Ptr{t8_step3_adapt_data_t}(t8_forest_get_user_data(forest))
33+
centroid = Vector{Cdouble}(undef, 3) # Will hold the element midpoint.
34+
# In t8_step3_adapt_forest we pass a t8_step3_adapt_data pointer as user data to the
35+
# t8_forest_new_adapt function. This pointer is stored as the used data of the new forest
36+
# and we can now access it with t8_forest_get_user_data (forest).
37+
adapt_data_ptr = Ptr{t8_step3_adapt_data_t}(t8_forest_get_user_data(forest))
3838

39-
# You can use assert for assertions that are active in debug mode (when configured with --enable-debug).
40-
# If the condition is not true, then the code will abort.
41-
# In this case, we want to make sure that we actually did set a user pointer to forest and thus
42-
# did not get the NULL pointer from t8_forest_get_user_data.
43-
@T8_ASSERT(adapt_data_ptr != C_NULL)
39+
# You can use assert for assertions that are active in debug mode (when configured with --enable-debug).
40+
# If the condition is not true, then the code will abort.
41+
# In this case, we want to make sure that we actually did set a user pointer to forest and thus
42+
# did not get the NULL pointer from t8_forest_get_user_data.
43+
@T8_ASSERT(adapt_data_ptr!=C_NULL)
4444

45-
adapt_data = unsafe_load(adapt_data_ptr)
45+
adapt_data = unsafe_load(adapt_data_ptr)
4646

47-
elements = unsafe_wrap(Array, elements_ptr, num_elements)
47+
elements = unsafe_wrap(Array, elements_ptr, num_elements)
4848

49-
# Compute the element's centroid coordinates.
50-
t8_forest_element_centroid(forest_from, which_tree, elements[1], pointer(centroid))
49+
# Compute the element's centroid coordinates.
50+
t8_forest_element_centroid(forest_from, which_tree, elements[1], pointer(centroid))
5151

52-
# Compute the distance to our sphere midpoint.
53-
dist = sqrt(sum((centroid .- adapt_data.midpoint).^2))
54-
if dist < adapt_data.refine_if_inside_radius
55-
# Refine this element.
56-
return 1
57-
elseif is_family == 1 && dist > adapt_data.coarsen_if_outside_radius
58-
# Coarsen this family. Note that we check for is_family before, since returning < 0
59-
# if we do not have a family as input is illegal.
60-
return -1
61-
end
52+
# Compute the distance to our sphere midpoint.
53+
dist = sqrt(sum((centroid .- adapt_data.midpoint) .^ 2))
54+
if dist < adapt_data.refine_if_inside_radius
55+
# Refine this element.
56+
return 1
57+
elseif is_family == 1 && dist > adapt_data.coarsen_if_outside_radius
58+
# Coarsen this family. Note that we check for is_family before, since returning < 0
59+
# if we do not have a family as input is illegal.
60+
return -1
61+
end
6262

63-
# Do not change this element.
64-
return 0
63+
# Do not change this element.
64+
return 0
6565
end
6666

6767
# Adapt a forest according to our t8_step3_adapt_callback function.
6868
# This will create a new forest and return it.
6969
function t8_step3_adapt_forest(forest)
70-
adapt_data = t8_step3_adapt_data_t(
71-
(0.5, 0.5, 1.0), # Midpoints of the sphere.
72-
0.2, # Refine if inside this radius.
73-
0.4 # Coarsen if outside this radius.
74-
)
70+
adapt_data = t8_step3_adapt_data_t((0.5, 0.5, 1.0), # Midpoints of the sphere.
71+
0.2, # Refine if inside this radius.
72+
0.4)
7573

76-
# Check that forest is a committed, that is valid and usable, forest.
77-
@T8_ASSERT(t8_forest_is_committed(forest) == 1)
74+
# Check that forest is a committed, that is valid and usable, forest.
75+
@T8_ASSERT(t8_forest_is_committed(forest)==1)
7876

79-
# Create a new forest that is adapted from \a forest with our adaptation callback.
80-
# We provide the adapt_data as user data that is stored as the used_data pointer of the
81-
# new forest (see also t8_forest_set_user_data).
82-
# The 0, 0 arguments are flags that control
83-
# recursive - If non-zero adaptation is recursive, thus if an element is adapted the children
84-
# or parents are plugged into the callback again recursively until the forest does not
85-
# change any more. If you use this you should ensure that refinement will stop eventually.
86-
# One way is to check the element's level against a given maximum level.
87-
# do_face_ghost - If non-zero additionally a layer of ghost elements is created for the forest.
88-
# We will discuss ghost in later steps of the tutorial.
89-
forest_adapt = t8_forest_new_adapt(forest, @t8_adapt_callback(t8_step3_adapt_callback), 0, 0, Ref(adapt_data))
77+
# Create a new forest that is adapted from \a forest with our adaptation callback.
78+
# We provide the adapt_data as user data that is stored as the used_data pointer of the
79+
# new forest (see also t8_forest_set_user_data).
80+
# The 0, 0 arguments are flags that control
81+
# recursive - If non-zero adaptation is recursive, thus if an element is adapted the children
82+
# or parents are plugged into the callback again recursively until the forest does not
83+
# change any more. If you use this you should ensure that refinement will stop eventually.
84+
# One way is to check the element's level against a given maximum level.
85+
# do_face_ghost - If non-zero additionally a layer of ghost elements is created for the forest.
86+
# We will discuss ghost in later steps of the tutorial.
87+
forest_adapt = t8_forest_new_adapt(forest, @t8_adapt_callback(t8_step3_adapt_callback),
88+
0, 0, Ref(adapt_data))
9089

91-
return forest_adapt
90+
return forest_adapt
9291
end
9392

9493
# Print the local and global number of elements of a forest.
9594
function t8_step3_print_forest_information(forest)
96-
# Check that forest is a committed, that is valid and usable, forest.
97-
@T8_ASSERT(t8_forest_is_committed(forest) == 1)
95+
# Check that forest is a committed, that is valid and usable, forest.
96+
@T8_ASSERT(t8_forest_is_committed(forest)==1)
9897

99-
# Get the local number of elements.
100-
local_num_elements = t8_forest_get_local_num_elements(forest)
101-
# Get the global number of elements.
102-
global_num_elements = t8_forest_get_global_num_elements(forest)
98+
# Get the local number of elements.
99+
local_num_elements = t8_forest_get_local_num_elements(forest)
100+
# Get the global number of elements.
101+
global_num_elements = t8_forest_get_global_num_elements(forest)
103102

104-
t8_global_productionf(" [step3] Local number of elements:\t\t%i\n", local_num_elements)
105-
t8_global_productionf(" [step3] Global number of elements:\t%li\n", global_num_elements)
103+
t8_global_productionf(" [step3] Local number of elements:\t\t%i\n", local_num_elements)
104+
t8_global_productionf(" [step3] Global number of elements:\t%li\n", global_num_elements)
106105
end

0 commit comments

Comments
 (0)