1 /**
2  * MOAB, a Mesh-Oriented datABase, is a software component for creating,
3  * storing and accessing finite element mesh data.
4  *
5  * Copyright 2004 Sandia Corporation.  Under the terms of Contract
6  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
7  * retains certain rights in this software.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  */
15 
16 #include <H5Tpublic.h>
17 #include <H5Dpublic.h>
18 #include <H5Gpublic.h>
19 #include <H5Ppublic.h>
20 #include "mhdf.h"
21 #include "util.h"
22 #include "file-handle.h"
23 #include "status.h"
24 #include "names-and-paths.h"
25 
26 hid_t
mhdf_createConnectivity(mhdf_FileHandle file_handle,const char * elem_handle,int nodes_per_elem,long count,long * first_id_out,mhdf_Status * status)27 mhdf_createConnectivity( mhdf_FileHandle file_handle,
28                          const char* elem_handle,
29                          int nodes_per_elem,
30                          long count,
31                          long* first_id_out,
32                          mhdf_Status* status )
33 {
34   FileHandle* file_ptr;
35   hid_t elem_id, table_id;
36   hsize_t dims[2];
37   long first_id;
38   API_BEGIN;
39 
40   file_ptr = (FileHandle*)(file_handle);
41   if (!mhdf_check_valid_file( file_ptr, status ))
42     return -1;
43 
44   if (nodes_per_elem <= 0 || count < 0 || !first_id_out)
45   {
46     mhdf_setFail( status, "Invalid argument." );
47     return -1;
48   }
49 
50   elem_id = mhdf_elem_group_from_handle( file_ptr, elem_handle, status );
51   if (elem_id < 0) return -1;
52 
53 
54   dims[0] = (hsize_t)count;
55   dims[1] = (hsize_t)nodes_per_elem;
56   table_id = mhdf_create_table( elem_id,
57                                 CONNECTIVITY_NAME,
58                                 file_ptr->id_type,
59                                 2, dims,
60                                 status );
61   H5Gclose( elem_id );
62   if (table_id < 0)
63     return -1;
64 
65   first_id = file_ptr->max_id + 1;
66   if (!mhdf_create_scalar_attrib( table_id,
67                                  START_ID_ATTRIB,
68                                  H5T_NATIVE_LONG,
69                                  &first_id,
70                                  status ))
71   {
72     H5Dclose( table_id );
73     return -1;
74   }
75 
76   *first_id_out = first_id;
77   file_ptr->max_id += count;
78   if (!mhdf_write_max_id( file_ptr, status))
79   {
80     H5Dclose( table_id );
81     return -1;
82   }
83   file_ptr->open_handle_count++;
84   mhdf_setOkay( status );
85 
86   API_END_H(1);
87   return table_id;
88 }
89 
90 hid_t
mhdf_openConnectivity(mhdf_FileHandle file_handle,const char * elem_handle,int * num_nodes_per_elem_out,long * num_elements_out,long * first_elem_id_out,mhdf_Status * status)91 mhdf_openConnectivity( mhdf_FileHandle file_handle,
92                        const char* elem_handle,
93                        int* num_nodes_per_elem_out,
94                        long* num_elements_out,
95                        long* first_elem_id_out,
96                        mhdf_Status* status )
97 {
98   FileHandle* file_ptr;
99   hid_t elem_id, table_id;
100   hsize_t dims[2];
101   API_BEGIN;
102 
103   file_ptr = (FileHandle*)(file_handle);
104   if (!mhdf_check_valid_file( file_ptr, status ))
105     return -1;
106 
107   if (!num_nodes_per_elem_out || !num_elements_out || !first_elem_id_out)
108   {
109     mhdf_setFail( status, "Invalid argument." );
110     return -1;
111   }
112 
113   elem_id = mhdf_elem_group_from_handle( file_ptr, elem_handle, status );
114   if (elem_id < 0) return -1;
115 
116   table_id = mhdf_open_table2( elem_id, CONNECTIVITY_NAME,
117                                2, dims,
118                                first_elem_id_out, status );
119 
120   H5Gclose( elem_id );
121   if (table_id < 0)
122     return -1;
123 
124   *num_elements_out = dims[0];
125   *num_nodes_per_elem_out = dims[1];
126 
127   file_ptr->open_handle_count++;
128   mhdf_setOkay( status );
129   API_END_H(1);
130   return table_id;
131 }
132 
133 hid_t
mhdf_openConnectivitySimple(mhdf_FileHandle file_handle,const char * elem_handle,mhdf_Status * status)134 mhdf_openConnectivitySimple( mhdf_FileHandle file_handle,
135                              const char* elem_handle,
136                              mhdf_Status* status )
137 {
138   FileHandle* file_ptr;
139   hid_t elem_id, table_id;
140   API_BEGIN;
141 
142   file_ptr = (FileHandle*)(file_handle);
143   if (!mhdf_check_valid_file( file_ptr, status ))
144     return -1;
145 
146   elem_id = mhdf_elem_group_from_handle( file_ptr, elem_handle, status );
147   if (elem_id < 0) return -1;
148 
149   table_id = mhdf_open_table_simple( elem_id, CONNECTIVITY_NAME, status );
150 
151   H5Gclose( elem_id );
152   if (table_id < 0)
153     return -1;
154 
155   file_ptr->open_handle_count++;
156   mhdf_setOkay( status );
157   API_END_H(1);
158   return table_id;
159 }
160 
161 void
mhdf_writeConnectivity(hid_t table_id,long offset,long count,hid_t hdf_integer_type,const void * nodes,mhdf_Status * status)162 mhdf_writeConnectivity( hid_t table_id,
163                         long offset,
164                         long count,
165                         hid_t hdf_integer_type,
166                         const void* nodes,
167                         mhdf_Status* status )
168 {
169   API_BEGIN;
170   mhdf_write_data( table_id, offset, count, hdf_integer_type, nodes, H5P_DEFAULT, status );
171   API_END;
172 }
173 void
mhdf_writeConnectivityWithOpt(hid_t table_id,long offset,long count,hid_t hdf_integer_type,const void * nodes,hid_t prop,mhdf_Status * status)174 mhdf_writeConnectivityWithOpt( hid_t table_id,
175                         long offset,
176                         long count,
177                         hid_t hdf_integer_type,
178                         const void* nodes,
179                         hid_t prop,
180                         mhdf_Status* status )
181 {
182   API_BEGIN;
183   mhdf_write_data( table_id, offset, count, hdf_integer_type, nodes, prop, status );
184   API_END;
185 }
186 
187 void
mhdf_readConnectivity(hid_t table_id,long offset,long count,hid_t hdf_integer_type,void * nodes,mhdf_Status * status)188 mhdf_readConnectivity( hid_t table_id,
189                        long offset,
190                        long count,
191                        hid_t hdf_integer_type,
192                        void* nodes,
193                        mhdf_Status* status )
194 {
195   API_BEGIN;
196   mhdf_read_data( table_id, offset, count, hdf_integer_type, nodes, H5P_DEFAULT,status );
197   API_END;
198 }
199 void
mhdf_readConnectivityWithOpt(hid_t table_id,long offset,long count,hid_t hdf_integer_type,void * nodes,hid_t prop,mhdf_Status * status)200 mhdf_readConnectivityWithOpt( hid_t table_id,
201                        long offset,
202                        long count,
203                        hid_t hdf_integer_type,
204                        void* nodes,
205                        hid_t prop,
206                        mhdf_Status* status )
207 {
208   API_BEGIN;
209   mhdf_read_data( table_id, offset, count, hdf_integer_type, nodes, prop, status );
210   API_END;
211 }
212 
213 
214 void
mhdf_createPolyConnectivity(mhdf_FileHandle file_handle,const char * elem_type,long num_poly,long data_list_length,long * first_id_out,hid_t handles_out[2],mhdf_Status * status)215 mhdf_createPolyConnectivity( mhdf_FileHandle file_handle,
216                              const char* elem_type,
217                              long num_poly,
218                              long data_list_length,
219                              long* first_id_out,
220                              hid_t handles_out[2],
221                              mhdf_Status* status )
222 {
223   FileHandle* file_ptr;
224   hid_t elem_id, index_id, conn_id;
225   hsize_t dim;
226   long first_id;
227   API_BEGIN;
228 
229   file_ptr = (FileHandle*)(file_handle);
230   if (!mhdf_check_valid_file( file_ptr, status ))
231     return;
232 
233   if (num_poly <= 0 || data_list_length <= 0 || !first_id_out)
234   {
235     mhdf_setFail( status, "Invalid argument." );
236     return;
237   }
238 
239   if (data_list_length < 3*num_poly)
240   {
241     /* Could check agains 4*num_poly, but allow degenerate polys
242        (1 for count plus 2 dim-1 defining entities, where > 2
243         defining entities is a normal poly, 2 defining entities
244         is a degenerate poly and 1 defning entity is not valid.)
245     */
246 
247     mhdf_setFail( status, "Invalid polygon data:  data length of %ld is "
248                           "insufficient for %ld poly(gons/hedra).\n",
249                           data_list_length, num_poly );
250     return;
251   }
252 
253   elem_id = mhdf_elem_group_from_handle( file_ptr, elem_type, status );
254   if (elem_id < 0) return;
255 
256   dim = (hsize_t)num_poly;
257   index_id = mhdf_create_table( elem_id,
258                                 POLY_INDEX_NAME,
259                                 MHDF_INDEX_TYPE,
260                                 1, &dim,
261                                 status );
262   if (index_id < 0)
263     { H5Gclose(elem_id); return; }
264 
265 
266   dim = (hsize_t)data_list_length;
267   conn_id = mhdf_create_table( elem_id,
268                                 CONNECTIVITY_NAME,
269                                 file_ptr->id_type,
270                                 1, &dim,
271                                 status );
272   H5Gclose( elem_id );
273   if (conn_id < 0)
274   {
275     H5Dclose(index_id);
276     return;
277   }
278 
279   first_id = file_ptr->max_id + 1;
280   if (!mhdf_create_scalar_attrib( conn_id,
281                                  START_ID_ATTRIB,
282                                  H5T_NATIVE_LONG,
283                                  &first_id,
284                                  status ))
285   {
286     H5Dclose(index_id);
287     H5Dclose( conn_id );
288     return;
289   }
290 
291   *first_id_out = first_id;
292   file_ptr->max_id += num_poly;
293   if (!mhdf_write_max_id( file_ptr, status))
294   {
295     H5Dclose(index_id);
296     H5Dclose( conn_id );
297     return;
298   }
299   file_ptr->open_handle_count++;
300   mhdf_setOkay( status );
301   handles_out[0] = index_id;
302   handles_out[1] = conn_id;
303   API_END_H(2);
304 }
305 
306 void
mhdf_openPolyConnectivity(mhdf_FileHandle file_handle,const char * element_handle,long * num_poly_out,long * data_list_length_out,long * first_poly_id_out,hid_t handles_out[2],mhdf_Status * status)307 mhdf_openPolyConnectivity( mhdf_FileHandle file_handle,
308                            const char* element_handle,
309                            long* num_poly_out,
310                            long* data_list_length_out,
311                            long* first_poly_id_out,
312                            hid_t handles_out[2],
313                            mhdf_Status* status )
314 {
315   FileHandle* file_ptr;
316   hid_t elem_id, table_id, index_id;
317   hsize_t row_count;
318   API_BEGIN;
319 
320   file_ptr = (FileHandle*)(file_handle);
321   if (!mhdf_check_valid_file( file_ptr, status ))
322     return ;
323 
324   if (!num_poly_out || !data_list_length_out || !first_poly_id_out)
325   {
326     mhdf_setFail( status, "Invalid argument." );
327     return ;
328   }
329 
330   elem_id = mhdf_elem_group_from_handle( file_ptr, element_handle, status );
331   if (elem_id < 0) return ;
332 
333   index_id = mhdf_open_table( elem_id, POLY_INDEX_NAME,
334                               1, &row_count, status );
335   if (index_id < 0)
336     { H5Gclose(elem_id); return ; }
337   *num_poly_out = (int)row_count;
338 
339   table_id = mhdf_open_table( elem_id, CONNECTIVITY_NAME,
340                               1, &row_count, status );
341 
342   H5Gclose( elem_id );
343   if (table_id < 0)
344   {
345     H5Dclose( index_id );
346     return ;
347   }
348   *data_list_length_out = (long)row_count;
349 
350   if (!mhdf_read_scalar_attrib( table_id, START_ID_ATTRIB, H5T_NATIVE_LONG,
351                                 first_poly_id_out, status ))
352   {
353     H5Dclose( table_id );
354     H5Dclose( index_id );
355     return ;
356   }
357 
358   file_ptr->open_handle_count++;
359   handles_out[0] = index_id;
360   handles_out[1] = table_id;
361   mhdf_setOkay( status );
362   API_END_H(2);
363 }
364 
365 void
mhdf_writePolyConnIndices(hid_t table_id,long offset,long count,hid_t hdf_integer_type,const void * index_list,mhdf_Status * status)366 mhdf_writePolyConnIndices( hid_t table_id,
367                            long offset,
368                            long count,
369                            hid_t hdf_integer_type,
370                            const void* index_list,
371                            mhdf_Status* status )
372 {
373   API_BEGIN;
374   mhdf_write_data( table_id, offset, count, hdf_integer_type, index_list, H5P_DEFAULT, status );
375   API_END;
376 }
377 void
mhdf_writePolyConnIndicesWithOpt(hid_t table_id,long offset,long count,hid_t hdf_integer_type,const void * index_list,hid_t prop,mhdf_Status * status)378 mhdf_writePolyConnIndicesWithOpt( hid_t table_id,
379                            long offset,
380                            long count,
381                            hid_t hdf_integer_type,
382                            const void* index_list,
383                            hid_t prop,
384                            mhdf_Status* status )
385 {
386   API_BEGIN;
387   mhdf_write_data( table_id, offset, count, hdf_integer_type, index_list, prop, status );
388   API_END;
389 }
390 
391 void
mhdf_readPolyConnIndices(hid_t table_id,long offset,long count,hid_t hdf_integer_type,void * index_list,mhdf_Status * status)392 mhdf_readPolyConnIndices( hid_t table_id,
393                           long offset,
394                           long count,
395                           hid_t hdf_integer_type,
396                           void* index_list,
397                           mhdf_Status* status )
398 {
399   API_BEGIN;
400   mhdf_read_data( table_id, offset, count, hdf_integer_type, index_list, H5P_DEFAULT, status );
401   API_END;
402 }
403 void
mhdf_readPolyConnIndicesWithOpt(hid_t table_id,long offset,long count,hid_t hdf_integer_type,void * index_list,hid_t prop,mhdf_Status * status)404 mhdf_readPolyConnIndicesWithOpt( hid_t table_id,
405                           long offset,
406                           long count,
407                           hid_t hdf_integer_type,
408                           void* index_list,
409                           hid_t prop,
410                           mhdf_Status* status )
411 {
412   API_BEGIN;
413   mhdf_read_data( table_id, offset, count, hdf_integer_type, index_list, prop, status );
414   API_END;
415 }
416 
417 void
mhdf_writePolyConnIDs(hid_t table_id,long offset,long count,hid_t hdf_integer_type,const void * id_list,mhdf_Status * status)418 mhdf_writePolyConnIDs( hid_t table_id,
419                        long offset,
420                        long count,
421                        hid_t hdf_integer_type,
422                        const void* id_list,
423                        mhdf_Status* status )
424 {
425   API_BEGIN;
426   mhdf_write_data( table_id, offset, count, hdf_integer_type, id_list, H5P_DEFAULT, status );
427   API_END;
428 }
429 void
mhdf_writePolyConnIDsWithOpt(hid_t table_id,long offset,long count,hid_t hdf_integer_type,const void * id_list,hid_t prop,mhdf_Status * status)430 mhdf_writePolyConnIDsWithOpt( hid_t table_id,
431                        long offset,
432                        long count,
433                        hid_t hdf_integer_type,
434                        const void* id_list,
435                        hid_t prop,
436                        mhdf_Status* status )
437 {
438   API_BEGIN;
439   mhdf_write_data( table_id, offset, count, hdf_integer_type, id_list, prop, status );
440   API_END;
441 }
442 
443 void
mhdf_readPolyConnIDs(hid_t table_id,long offset,long count,hid_t hdf_integer_type,void * id_list,mhdf_Status * status)444 mhdf_readPolyConnIDs( hid_t table_id,
445                       long offset,
446                       long count,
447                       hid_t hdf_integer_type,
448                       void* id_list,
449                       mhdf_Status* status )
450 {
451   API_BEGIN;
452   mhdf_read_data( table_id, offset, count, hdf_integer_type, id_list, H5P_DEFAULT, status );
453   API_END;
454 }
455 void
mhdf_readPolyConnIDsWithOpt(hid_t table_id,long offset,long count,hid_t hdf_integer_type,void * id_list,hid_t prop,mhdf_Status * status)456 mhdf_readPolyConnIDsWithOpt( hid_t table_id,
457                       long offset,
458                       long count,
459                       hid_t hdf_integer_type,
460                       void* id_list,
461                       hid_t prop,
462                       mhdf_Status* status )
463 {
464   API_BEGIN;
465   mhdf_read_data( table_id, offset, count, hdf_integer_type, id_list, prop, status );
466   API_END;
467 }
468 
469