UCFD_SPARSE  v1.0
Documentation
Loading...
Searching...
No Matches
pbutils.c
Go to the documentation of this file.
1
22#include <stdio.h>
23#include <stdlib.h>
24#include "pbutils.h"
25#include "queue.h"
26
27
36void make_nei_ele(const int m, const int n, const int l, int **nei_ele)
37{
38
39 int idx = 0;
40 int ml = m*l;
41 int nml = n*m*l;
42 int ny, py, nx, px, nz, pz;
43
44 for (int j=0; j<n; j++) {
45 for (int i=0; i<m; i++) {
46 for (int k=0; k<l; k++) {
47 // Next or previous neighbor cell count
48 ny = idx-ml;
49 py = idx+ml;
50 nx = idx-l;
51 px = idx+l;
52 nz = idx-1;
53 pz = idx+1;
54
55 // Handling grid boundary
56 if (ny<0) nei_ele[0][idx] = idx; else nei_ele[0][idx] = ny;
57 if (nx<j*ml) nei_ele[1][idx] = idx; else nei_ele[1][idx] = nx;
58 if (pz >= j*ml+(i+1)*l) nei_ele[2][idx] = idx; else nei_ele[2][idx] = pz;
59 if (px >= (j+1)*ml) nei_ele[3][idx] = idx; else nei_ele[3][idx] = px;
60 if (nz < j*ml+i*l) nei_ele[4][idx] = idx; else nei_ele[4][idx] = nz;
61 if (py >= nml) nei_ele[5][idx] = idx; else nei_ele[5][idx] = py;
62
63 idx++;
64 }
65 }
66 }
67}
68
69
78int searcharr(int val, int *arr, int size)
79{
80 for (int i=0; i<size; i++) {
81 // If value is found, return 1
82 if (arr[i] == val)
83 return 1;
84 }
85 // If not found, return 0
86 return 0;
87}
88
89
99void make_reordering(const int nele, const int nface, int **nei_ele, int *mapping, int *unmapping)
100{
101 struct Queue q;
102 int fe = nele - 1;
103 int idx, jdx, ele, neib;
105 // Initialize queue structure
106 initQueue(&q);
107 // Starts from the last element index
108 enqueue(&q, fe);
109 mapping[0] = fe;
110 idx = 1;
111
112 // Compute `mapping` array
113 while (!isEmpty(&q)) {
114 ele = dequeue(&q);
115 for (jdx=0; jdx<nface; jdx++) {
116 neib = nei_ele[jdx][ele];
117
118 // If neighbor element index is not stored in the mapping array,
119 // append index in mapping and queue
120 if (!searcharr(neib, mapping, nele)) {
121 mapping[idx] = neib;
122 enqueue(&q, neib);
123 idx++;
124 }
125 }
126 }
127
128 // Compute `unmapping` array
129 for (int i=0; i<nele; i++) {
130 idx = 0;
131
132 // Find each mapping element index
133 while (mapping[idx] != i)
134 idx ++;
135
136 unmapping[i] = idx;
137 }
138}
139
140
153void make_coloring(const int m, const int n, const int l, \
154 int *icolor, int *lcolor)
155{
156 int neles = m*n*l;
157 int cb = neles/2;
158 int denom = cb;
159 int ml = m*l;
160 int ca = 0;
161 int ele = 0;
162 int ind, j, ist, iend, tmp;
163
164 // Starts from zero-index element
165 while (ele < neles) {
166 for (j=0; j<n; j++) {
167 ist = (j*ml)/2;
168 iend = (j+1)*ml/2;
169 for (ind=ist; ind<iend; ind++) {
170 icolor[ca+ind] = ele;
171 lcolor[ele] = ca/denom;
172 ele++;
173 icolor[cb+ind] = ele;
174 lcolor[ele] = cb/denom;
175 }
176 tmp = ca;
177 ca = cb;
178 cb = tmp;
179 }
180 }
181}
int searcharr(int val, int *arr, int size)
Search element in the given array. If element exists, return 1.
Definition: pbutils.c:78
void make_coloring(const int m, const int n, const int l, int *icolor, int *lcolor)
Coloring algorithm for unstructured grid.
Definition: pbutils.c:153
void make_reordering(const int nele, const int nface, int **nei_ele, int *mapping, int *unmapping)
Reverse Cuthill-McKee algorithm using neighbor elements.
Definition: pbutils.c:99
void make_nei_ele(const int m, const int n, const int l, int **nei_ele)
Computes neighbor cell elements array.
Definition: pbutils.c:36
void initQueue(struct Queue *queue)
Initialize queue structure with NULL value.
Definition: queue.c:30
int isEmpty(struct Queue *queue)
Check if queue structure is empty.
Definition: queue.c:41
int dequeue(struct Queue *queue)
Take out the first added element.
Definition: queue.c:77
void enqueue(struct Queue *queue, int data)
Allocate new node and add to queue structure.
Definition: queue.c:55
Queue data structure.
Definition: queue.h:16