|
1 | 1 | # This is our own defined data that we will pass on to the
|
2 | 2 | # adaptation callback.
|
3 | 3 | 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 |
7 | 7 | end
|
8 | 8 |
|
9 | 9 | # The adaptation callback function. This function will be called once for each element
|
|
26 | 26 | # \param [in] num_elements The number of entries in \a elements elements that are defined.
|
27 | 27 | # \param [in] elements The element or family of elements to consider for refinement/coarsening.
|
28 | 28 | 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. |
32 | 32 |
|
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)) |
38 | 38 |
|
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) |
44 | 44 |
|
45 |
| - adapt_data = unsafe_load(adapt_data_ptr) |
| 45 | + adapt_data = unsafe_load(adapt_data_ptr) |
46 | 46 |
|
47 |
| - elements = unsafe_wrap(Array, elements_ptr, num_elements) |
| 47 | + elements = unsafe_wrap(Array, elements_ptr, num_elements) |
48 | 48 |
|
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)) |
51 | 51 |
|
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 |
62 | 62 |
|
63 |
| - # Do not change this element. |
64 |
| - return 0 |
| 63 | + # Do not change this element. |
| 64 | + return 0 |
65 | 65 | end
|
66 | 66 |
|
67 | 67 | # Adapt a forest according to our t8_step3_adapt_callback function.
|
68 | 68 | # This will create a new forest and return it.
|
69 | 69 | 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) |
75 | 73 |
|
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) |
78 | 76 |
|
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)) |
90 | 89 |
|
91 |
| - return forest_adapt |
| 90 | + return forest_adapt |
92 | 91 | end
|
93 | 92 |
|
94 | 93 | # Print the local and global number of elements of a forest.
|
95 | 94 | 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) |
98 | 97 |
|
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) |
103 | 102 |
|
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) |
106 | 105 | end
|
0 commit comments