Skip to content

Commit

Permalink
format (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaLampert authored Feb 2, 2024
1 parent 2b5c0bf commit 02f40d3
Show file tree
Hide file tree
Showing 16 changed files with 1,035 additions and 1,006 deletions.
8 changes: 8 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Use SciML style: https://github.com/SciML/SciMLStyle
style = "sciml"

# Python style alignment. See https://github.com/domluna/JuliaFormatter.jl/pull/732.
yas_style_nesting = true

# Align struct fields for better readability of large struct definitions
align_struct_field = true
38 changes: 38 additions & 0 deletions .github/workflows/FormatCheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: format-check

on:
push:
branches:
- 'main'
tags: '*'
pull_request:

jobs:
check-format:
runs-on: ${{ matrix.os }}
strategy:
matrix:
julia-version: [1]
julia-arch: [x86]
os: [ubuntu-latest]
steps:
- uses: julia-actions/setup-julia@latest
with:
version: ${{ matrix.julia-version }}
- uses: actions/checkout@v4
- uses: julia-actions/cache@v1
- name: Install JuliaFormatter and format
run: |
julia -e 'using Pkg; Pkg.add(PackageSpec(name = "JuliaFormatter"))'
julia -e 'using JuliaFormatter; format(["examples", "src/T8code.jl", "test"])'
- name: Format check
run: |
julia -e '
out = Cmd(`git diff --name-only`) |> read |> String
if out == ""
exit(0)
else
@error "Some files have not been formatted !!!"
write(stdout, out)
exit(1)
end'
28 changes: 14 additions & 14 deletions examples/t8_step1_coarsemesh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ using T8code.Libt8: SC_LP_PRODUCTION
# \param [in] comm MPI Communicator to use.
# \return The coarse mesh.
function t8_step1_build_tetcube_coarse_mesh(comm)
# Build a coarse mesh of 6 tetrahedral trees that form a cube.
# You can modify the first parameter to build a cube with different
# tree shapes, i.e. T8_ECLASS_QUAD for a unit square with 1 quadrilateral tree.
# See t8_eclass.h, t8_cmesh.h for all possible shapes.
#
# The second argument is the MPI communicator to use for this cmesh.
# The remaining arguments are 3 flags that control
# 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.
# do_partition - If non-zero the cmesh will be partitioned among the processes. If 0 each process has a copy of the whole cmesh.
# periodic - If non-zero the cube will have periodic boundaries. That is, i.e. the left face is connected to the right face.
# Build a coarse mesh of 6 tetrahedral trees that form a cube.
# You can modify the first parameter to build a cube with different
# tree shapes, i.e. T8_ECLASS_QUAD for a unit square with 1 quadrilateral tree.
# See t8_eclass.h, t8_cmesh.h for all possible shapes.
#
# The second argument is the MPI communicator to use for this cmesh.
# The remaining arguments are 3 flags that control
# 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.
# do_partition - If non-zero the cmesh will be partitioned among the processes. If 0 each process has a copy of the whole cmesh.
# periodic - If non-zero the cube will have periodic boundaries. That is, i.e. the left face is connected to the right face.

cmesh = t8_cmesh_new_hypercube(T8_ECLASS_TET, comm, 0, 0, 0)
cmesh = t8_cmesh_new_hypercube(T8_ECLASS_TET, comm, 0, 0, 0)

return cmesh
return cmesh
end

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

# Destroy a cmesh. This will free all allocated memory.
# \param [in] cmesh A cmesh.
function t8_step1_destroy_cmesh(cmesh)
t8_cmesh_destroy(Ref(cmesh))
t8_cmesh_destroy(Ref(cmesh))
end

# The prefix for our output files.
Expand Down
22 changes: 11 additions & 11 deletions examples/t8_step2_uniform_forest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ using T8code.Libt8: SC_LP_PRODUCTION
# \param [in] comm MPI Communicator to use.
# \return The coarse mesh.
function t8_step2_build_prismcube_coarse_mesh(comm)
# Build a coarse mesh of 2 prism trees that form a cube.
cmesh = t8_cmesh_new_hypercube(T8_ECLASS_PRISM, comm, 0, 0, 0)
# Build a coarse mesh of 2 prism trees that form a cube.
cmesh = t8_cmesh_new_hypercube(T8_ECLASS_PRISM, comm, 0, 0, 0)

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

return cmesh
return cmesh
end

# Build a uniform forest on a cmesh
Expand All @@ -70,12 +70,12 @@ end
# \return A uniform forest with the given refinement level that is
# partitioned across the processes in \a comm.
function t8_step2_build_uniform_forest(comm, cmesh, level)
# /* Create the refinement scheme. */
scheme = t8_scheme_new_default_cxx()
# /* Creat the uniform forest. */
forest = t8_forest_new_uniform(cmesh, scheme, level, 0, comm)
# /* Create the refinement scheme. */
scheme = t8_scheme_new_default_cxx()
# /* Creat the uniform forest. */
forest = t8_forest_new_uniform(cmesh, scheme, level, 0, comm)

return forest
return forest
end

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

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

# The prefix for our output files.
Expand Down
117 changes: 58 additions & 59 deletions examples/t8_step3_common.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# This is our own defined data that we will pass on to the
# adaptation callback.
mutable struct t8_step3_adapt_data_t
midpoint :: NTuple{3,Cdouble}
refine_if_inside_radius :: Cdouble
coarsen_if_outside_radius :: Cdouble
midpoint :: NTuple{3, Cdouble}
refine_if_inside_radius :: Cdouble
coarsen_if_outside_radius :: Cdouble
end

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

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

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

adapt_data = unsafe_load(adapt_data_ptr)
adapt_data = unsafe_load(adapt_data_ptr)

elements = unsafe_wrap(Array, elements_ptr, num_elements)
elements = unsafe_wrap(Array, elements_ptr, num_elements)

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

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

# Do not change this element.
return 0
# Do not change this element.
return 0
end

# Adapt a forest according to our t8_step3_adapt_callback function.
# This will create a new forest and return it.
function t8_step3_adapt_forest(forest)
adapt_data = t8_step3_adapt_data_t(
(0.5, 0.5, 1.0), # Midpoints of the sphere.
0.2, # Refine if inside this radius.
0.4 # Coarsen if outside this radius.
)
adapt_data = t8_step3_adapt_data_t((0.5, 0.5, 1.0), # Midpoints of the sphere.
0.2, # Refine if inside this radius.
0.4)

# Check that forest is a committed, that is valid and usable, forest.
@T8_ASSERT(t8_forest_is_committed(forest) == 1)
# Check that forest is a committed, that is valid and usable, forest.
@T8_ASSERT(t8_forest_is_committed(forest)==1)

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

return forest_adapt
return forest_adapt
end

# Print the local and global number of elements of a forest.
function t8_step3_print_forest_information(forest)
# Check that forest is a committed, that is valid and usable, forest.
@T8_ASSERT(t8_forest_is_committed(forest) == 1)
# Check that forest is a committed, that is valid and usable, forest.
@T8_ASSERT(t8_forest_is_committed(forest)==1)

# Get the local number of elements.
local_num_elements = t8_forest_get_local_num_elements(forest)
# Get the global number of elements.
global_num_elements = t8_forest_get_global_num_elements(forest)
# Get the local number of elements.
local_num_elements = t8_forest_get_local_num_elements(forest)
# Get the global number of elements.
global_num_elements = t8_forest_get_global_num_elements(forest)

t8_global_productionf(" [step3] Local number of elements:\t\t%i\n", local_num_elements)
t8_global_productionf(" [step3] Global number of elements:\t%li\n", global_num_elements)
t8_global_productionf(" [step3] Local number of elements:\t\t%i\n", local_num_elements)
t8_global_productionf(" [step3] Global number of elements:\t%li\n", global_num_elements)
end
Loading

0 comments on commit 02f40d3

Please sign in to comment.