1 /*
2 ! @file test_unstruc_quad.c
3 ! @author Greg Sjaardema <gsjaardema@gmail.com>
4 ! @version 0.1
5 !
6 ! @section LICENSE
7 ! BSD style license
8 !
9 ! @section DESCRIPTION
10 ! Test program for pcgns library
11 ! -- Created to test cgp_parent_data_write function
12 ! -- Based on test_unstructured.c
13 */
14 
15 /*
16 
17 2....4....6....8 8...10...12...14 14...16...18...20 L+1.L+3...L+5...L+7
18 |    |    |    | |    |    |    | |     |    |    | |     |     |     |
19 1....3....5....7 7....9...11...13 13...15...17...19 L...L+2...L+4...L+6
20    1    2   3      4    5    6       7    8    9      M     M+1   M+2
21        P0               P1                P2             PN
22 
23 L = P*6+1   M = P*3+1
24 The BC "Bottom" is on the bottom (y=0) edge of the mesh.
25 The BC "Left"   is on the left (x=0) edge of the mesh.
26  -- included to test whether works with 0 entries on some procs.
27 */
28 
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 
33 #include "pcgnslib.h"
34 #include "mpi.h"
35 
36 #define cgp_doError {printf("Error at %s:%u\n",__FILE__, __LINE__); return 1;}
37 
main(int argc,char * argv[])38 int main(int argc, char* argv[]) {
39   int err;
40   int comm_size;
41   int comm_rank;
42   MPI_Info info;
43   int fn;
44   int B;
45   int Z;
46   int S;
47   int Cx,Cy,Cz;
48   int cell_dim = 3;
49   int phys_dim = 3;
50   cgsize_t nijk[3], min, max;
51   int k, vert_proc;
52   double *x, *y, *z;
53   int nelem, nvert;
54   cgsize_t start, end, emin, emax, *elements;
55   cgsize_t *el_ptr = NULL;
56 
57   err = MPI_Init(&argc,&argv);
58   if(err!=MPI_SUCCESS) cgp_doError;
59   err = MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
60   if(err!=MPI_SUCCESS) cgp_doError;
61   err = MPI_Comm_rank(MPI_COMM_WORLD, &comm_rank);
62   if(err!=MPI_SUCCESS) cgp_doError;
63   err = MPI_Info_create(&(info));
64   if(err!=MPI_SUCCESS) cgp_doError;
65 
66   nelem = 3 * comm_size;
67   nvert = 2 * nelem + 2;
68   nijk[0] = nvert;
69   nijk[1] = nelem;
70   nijk[2] = 0;
71 
72 
73   if (cgp_open("test_unstruc_quad.cgns", CG_MODE_WRITE, &fn) ||
74       cg_base_write(fn, "Base 1", cell_dim, phys_dim, &B) ||
75       cg_zone_write(fn, B, "Zone 1", nijk, CGNS_ENUMV(Unstructured), &Z))
76     cgp_error_exit();
77 
78   vert_proc = 8;
79   x = (double *)malloc(vert_proc*sizeof(double));
80   y = (double *)malloc(vert_proc*sizeof(double));
81   z = (double *)malloc(vert_proc*sizeof(double));
82 
83   min = 6*comm_rank+1;
84   max = min+7;
85 
86   for(k=0;k<vert_proc;k+=2) {
87     x[k+0] = (double) (min+k);
88     x[k+1] = (double) (min+k);
89     y[k+0] = 0.0;
90     y[k+1] = 1.0;
91     z[k+0] = 0.0;
92     z[k+1] = 0.0;
93   }
94 
95   if (cgp_coord_write(fn,B,Z,CGNS_ENUMV(RealDouble),"CoordinateX",&Cx) ||
96       cgp_coord_write(fn,B,Z,CGNS_ENUMV(RealDouble),"CoordinateY",&Cy) ||
97       cgp_coord_write(fn,B,Z,CGNS_ENUMV(RealDouble),"CoordinateZ",&Cz))
98     cgp_error_exit();
99 
100   if (cgp_coord_write_data(fn,B,Z,Cx,&min,&max,x) ||
101       cgp_coord_write_data(fn,B,Z,Cy,&min,&max,y) ||
102       cgp_coord_write_data(fn,B,Z,Cz,&min,&max,z))
103     cgp_error_exit();
104 
105   start = 1;
106   end = comm_size*3;
107   if (cgp_section_write(fn,B,Z,"Elements",CGNS_ENUMV(QUAD_4),start,end,0,&S))
108     cgp_error_exit();
109 
110   nelem = 3;
111   printf("%d:%d\n",comm_rank,nelem);
112   emin = comm_rank*3+1;
113   emax = emin+2;
114 
115   elements = (cgsize_t *)malloc(nelem*4*sizeof(cgsize_t));
116   for(k=0;k<nelem;k++) {
117     elements[4*k+0] = 2*(emin+k)-1;
118     elements[4*k+1] = 2*(emin+k)+1;
119     elements[4*k+2] = 2*(emin+k)+2;
120     elements[4*k+3] = 2*(emin+k)+0;
121   }
122   printf("%d:%d %d %d\n",comm_rank,nelem,(int)emin,(int)emax);
123 
124   if (cgp_elements_write_data(fn,B,Z,S,emin,emax,elements))
125     cgp_error_exit();
126 
127   start = 3*comm_size + 1;
128   end   = start + 3*comm_size - 1;
129   if (cgp_section_write(fn,B,Z,"Bottom",CGNS_ENUMV(BAR_2),start,end,0,&S))
130     cgp_error_exit();
131 
132   /* Parent Element/Side data */
133   for(k=0; k < 3; k++) {
134     elements[3*0 + k] = comm_rank*3+1+k; /* Element */
135     elements[3*1 + k] = 0;
136     elements[3*2 + k] = 1; /* Side */
137     elements[3*3 + k] = 0;
138   }
139 
140   emin = comm_rank*3+start;
141   emax = emin+2;
142   printf("%d:%d %d\n",comm_rank,(int)emin,(int)emax);
143   if (cgp_parent_data_write(fn,B,Z,S,emin,emax,elements))
144     cgp_error_exit();
145 
146   /* Side Connectivity */
147   for(k=0; k < 3; k++) {
148     elements[2*k+0] = 2*(comm_rank*3+k)+1;
149     elements[2*k+1] = 2*(comm_rank*3+k)+3;
150   }
151   if (cgp_elements_write_data(fn,B,Z,S,emin,emax,elements))
152     cgp_error_exit();
153 
154   /* Left BC */
155   start = end + 1;
156   end   = start;
157   if (cgp_section_write(fn,B,Z,"Left",CGNS_ENUMV(BAR_2),start,end,0,&S))
158     cgp_error_exit();
159 
160   if (comm_rank == 0) {
161     emin = start;
162     emax = end;
163 
164     /* Parent Element/Side data */
165     elements[0] = 1; /* Element */
166     elements[1] = 0;
167     elements[2] = 4; /* Side */
168     elements[3] = 0;
169     el_ptr = elements;
170   }
171   else {
172     emin = 0;
173     emax = 0;
174     el_ptr = NULL;
175   }
176   printf("%d:%d %d\n",comm_rank,(int)emin,(int)emax);
177   if (cgp_parent_data_write(fn,B,Z,S,emin,emax,el_ptr))
178     cgp_error_exit();
179 
180   /* Side Connectivity */
181   if (comm_rank == 0) {
182     elements[0] = 1;
183     elements[1] = 2;
184   }
185   if (cgp_elements_write_data(fn,B,Z,S,emin,emax,el_ptr))
186     cgp_error_exit();
187 
188   if (cgp_close(fn)) cgp_error_exit();
189 
190   err = MPI_Finalize();
191   if(err!=MPI_SUCCESS) cgp_doError;
192   return 0;
193 }
194