source: CIVL/examples/mpi-omp/AMG2013/struct_mv/struct_stencil.c@ beab7f2

main test-branch
Last change on this file since beab7f2 was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 7.1 KB
Line 
1/*BHEADER**********************************************************************
2 * Copyright (c) 2008, Lawrence Livermore National Security, LLC.
3 * Produced at the Lawrence Livermore National Laboratory.
4 * This file is part of HYPRE. See file COPYRIGHT for details.
5 *
6 * HYPRE is free software; you can redistribute it and/or modify it under the
7 * terms of the GNU Lesser General Public License (as published by the Free
8 * Software Foundation) version 2.1 dated February 1999.
9 *
10 * $Revision: 2.4 $
11 ***********************************************************************EHEADER*/
12
13
14/******************************************************************************
15 *
16 * Constructors and destructors for stencil structure.
17 *
18 *****************************************************************************/
19
20#include "headers.h"
21
22/*--------------------------------------------------------------------------
23 * hypre_StructStencilCreate
24 *--------------------------------------------------------------------------*/
25
26hypre_StructStencil *
27hypre_StructStencilCreate( int dim,
28 int size,
29 hypre_Index *shape )
30{
31 hypre_StructStencil *stencil;
32
33 int abs_offset;
34 int max_offset;
35 int s, d;
36
37 stencil = hypre_TAlloc(hypre_StructStencil, 1);
38
39 hypre_StructStencilShape(stencil) = shape;
40 hypre_StructStencilSize(stencil) = size;
41 hypre_StructStencilDim(stencil) = dim;
42 hypre_StructStencilRefCount(stencil) = 1;
43
44 /* compute max_offset */
45 max_offset = 0;
46 for (s = 0; s < size; s++)
47 {
48 for (d = 0; d < 3; d++)
49 {
50 abs_offset = hypre_IndexD(shape[s], d);
51 abs_offset = (abs_offset < 0) ? -abs_offset : abs_offset;
52 max_offset = hypre_max(abs_offset, max_offset);
53 }
54 }
55 hypre_StructStencilMaxOffset(stencil) = max_offset;
56
57 return stencil;
58}
59
60/*--------------------------------------------------------------------------
61 * hypre_StructStencilRef
62 *--------------------------------------------------------------------------*/
63
64hypre_StructStencil *
65hypre_StructStencilRef( hypre_StructStencil *stencil )
66{
67 hypre_StructStencilRefCount(stencil) ++;
68
69 return stencil;
70}
71
72/*--------------------------------------------------------------------------
73 * hypre_StructStencilDestroy
74 *--------------------------------------------------------------------------*/
75
76int
77hypre_StructStencilDestroy( hypre_StructStencil *stencil )
78{
79 int ierr = 0;
80
81 if (stencil)
82 {
83 hypre_StructStencilRefCount(stencil) --;
84 if (hypre_StructStencilRefCount(stencil) == 0)
85 {
86 hypre_TFree(hypre_StructStencilShape(stencil));
87 hypre_TFree(stencil);
88 }
89 }
90
91 return ierr;
92}
93
94/*--------------------------------------------------------------------------
95 * hypre_StructStencilElementRank
96 * Returns the rank of the `stencil_element' in `stencil'.
97 * If the element is not found, a -1 is returned.
98 *--------------------------------------------------------------------------*/
99
100int
101hypre_StructStencilElementRank( hypre_StructStencil *stencil,
102 hypre_Index stencil_element )
103{
104 hypre_Index *stencil_shape;
105 int rank;
106 int i;
107
108 rank = -1;
109 stencil_shape = hypre_StructStencilShape(stencil);
110 for (i = 0; i < hypre_StructStencilSize(stencil); i++)
111 {
112 if ((hypre_IndexX(stencil_shape[i]) == hypre_IndexX(stencil_element)) &&
113 (hypre_IndexY(stencil_shape[i]) == hypre_IndexY(stencil_element)) &&
114 (hypre_IndexZ(stencil_shape[i]) == hypre_IndexZ(stencil_element)) )
115 {
116 rank = i;
117 break;
118 }
119 }
120
121 return rank;
122}
123
124/*--------------------------------------------------------------------------
125 * hypre_StructStencilSymmetrize:
126 * Computes a new "symmetrized" stencil.
127 *
128 * An integer array called `symm_elements' is also set up. A non-negative
129 * value of `symm_elements[i]' indicates that the `i'th stencil element
130 * is a "symmetric element". That is, this stencil element is the
131 * transpose element of an element that is not a "symmetric element".
132 *--------------------------------------------------------------------------*/
133
134int
135hypre_StructStencilSymmetrize( hypre_StructStencil *stencil,
136 hypre_StructStencil **symm_stencil_ptr,
137 int **symm_elements_ptr )
138{
139 hypre_Index *stencil_shape = hypre_StructStencilShape(stencil);
140 int stencil_size = hypre_StructStencilSize(stencil);
141
142 hypre_StructStencil *symm_stencil;
143 hypre_Index *symm_stencil_shape;
144 int symm_stencil_size;
145 int *symm_elements;
146
147 int no_symmetric_stencil_element;
148 int i, j, d;
149
150 int ierr = 0;
151
152 /*------------------------------------------------------
153 * Copy stencil elements into `symm_stencil_shape'
154 *------------------------------------------------------*/
155
156 symm_stencil_shape = hypre_CTAlloc(hypre_Index, 2*stencil_size);
157 for (i = 0; i < stencil_size; i++)
158 {
159 hypre_CopyIndex(stencil_shape[i], symm_stencil_shape[i]);
160 }
161
162 /*------------------------------------------------------
163 * Create symmetric stencil elements and `symm_elements'
164 *------------------------------------------------------*/
165
166 symm_elements = hypre_CTAlloc(int, 2*stencil_size);
167 for (i = 0; i < 2*stencil_size; i++)
168 symm_elements[i] = -1;
169
170 symm_stencil_size = stencil_size;
171 for (i = 0; i < stencil_size; i++)
172 {
173 if (symm_elements[i] < 0)
174 {
175 /* note: start at i to handle "center" element correctly */
176 no_symmetric_stencil_element = 1;
177 for (j = i; j < stencil_size; j++)
178 {
179 if ( (hypre_IndexX(symm_stencil_shape[j]) ==
180 -hypre_IndexX(symm_stencil_shape[i]) ) &&
181 (hypre_IndexY(symm_stencil_shape[j]) ==
182 -hypre_IndexY(symm_stencil_shape[i]) ) &&
183 (hypre_IndexZ(symm_stencil_shape[j]) ==
184 -hypre_IndexZ(symm_stencil_shape[i]) ) )
185 {
186 /* only "off-center" elements have symmetric entries */
187 if (i != j)
188 symm_elements[j] = i;
189 no_symmetric_stencil_element = 0;
190 }
191 }
192
193 if (no_symmetric_stencil_element)
194 {
195 /* add symmetric stencil element to `symm_stencil' */
196 for (d = 0; d < 3; d++)
197 {
198 hypre_IndexD(symm_stencil_shape[symm_stencil_size], d) =
199 -hypre_IndexD(symm_stencil_shape[i], d);
200 }
201
202 symm_elements[symm_stencil_size] = i;
203 symm_stencil_size++;
204 }
205 }
206 }
207
208 symm_stencil = hypre_StructStencilCreate(hypre_StructStencilDim(stencil),
209 symm_stencil_size,
210 symm_stencil_shape);
211
212 *symm_stencil_ptr = symm_stencil;
213 *symm_elements_ptr = symm_elements;
214
215 return ierr;
216}
217
Note: See TracBrowser for help on using the repository browser.