Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
connectivity.c
Go to the documentation of this file.
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 
27  const char* elem_handle,
28  int nodes_per_elem,
29  long count,
30  long* first_id_out,
31  mhdf_Status* status )
32 {
33  FileHandle* file_ptr;
34  hid_t elem_id, table_id;
35  hsize_t dims[2];
36  long first_id;
37  API_BEGIN;
38 
39  file_ptr = (FileHandle*)( file_handle );
40  if( !mhdf_check_valid_file( file_ptr, status ) ) return -1;
41 
42  if( nodes_per_elem <= 0 || count < 0 || !first_id_out )
43  {
44  mhdf_setFail( status, "Invalid argument." );
45  return -1;
46  }
47 
48  elem_id = mhdf_elem_group_from_handle( file_ptr, elem_handle, status );
49  if( elem_id < 0 ) return -1;
50 
51  dims[0] = (hsize_t)count;
52  dims[1] = (hsize_t)nodes_per_elem;
53  table_id = mhdf_create_table( elem_id, CONNECTIVITY_NAME, file_ptr->id_type, 2, dims, status );
54  H5Gclose( elem_id );
55  if( table_id < 0 ) return -1;
56 
57  first_id = file_ptr->max_id + 1;
58  if( !mhdf_create_scalar_attrib( table_id, START_ID_ATTRIB, H5T_NATIVE_LONG, &first_id, status ) )
59  {
60  H5Dclose( table_id );
61  return -1;
62  }
63 
64  *first_id_out = first_id;
65  file_ptr->max_id += count;
66  if( !mhdf_write_max_id( file_ptr, status ) )
67  {
68  H5Dclose( table_id );
69  return -1;
70  }
71  file_ptr->open_handle_count++;
72  mhdf_setOkay( status );
73 
74  API_END_H( 1 );
75  return table_id;
76 }
77 
79  const char* elem_handle,
80  int* num_nodes_per_elem_out,
81  long* num_elements_out,
82  long* first_elem_id_out,
83  mhdf_Status* status )
84 {
85  FileHandle* file_ptr;
86  hid_t elem_id, table_id;
87  hsize_t dims[2];
88  API_BEGIN;
89 
90  file_ptr = (FileHandle*)( file_handle );
91  if( !mhdf_check_valid_file( file_ptr, status ) ) return -1;
92 
93  if( !num_nodes_per_elem_out || !num_elements_out || !first_elem_id_out )
94  {
95  mhdf_setFail( status, "Invalid argument." );
96  return -1;
97  }
98 
99  elem_id = mhdf_elem_group_from_handle( file_ptr, elem_handle, status );
100  if( elem_id < 0 ) return -1;
101 
102  table_id = mhdf_open_table2( elem_id, CONNECTIVITY_NAME, 2, dims, first_elem_id_out, status );
103 
104  H5Gclose( elem_id );
105  if( table_id < 0 ) return -1;
106 
107  *num_elements_out = dims[0];
108  *num_nodes_per_elem_out = dims[1];
109 
110  file_ptr->open_handle_count++;
111  mhdf_setOkay( status );
112  API_END_H( 1 );
113  return table_id;
114 }
115 
116 hid_t mhdf_openConnectivitySimple( mhdf_FileHandle file_handle, const char* elem_handle, mhdf_Status* status )
117 {
118  FileHandle* file_ptr;
119  hid_t elem_id, table_id;
120  API_BEGIN;
121 
122  file_ptr = (FileHandle*)( file_handle );
123  if( !mhdf_check_valid_file( file_ptr, status ) ) return -1;
124 
125  elem_id = mhdf_elem_group_from_handle( file_ptr, elem_handle, status );
126  if( elem_id < 0 ) return -1;
127 
128  table_id = mhdf_open_table_simple( elem_id, CONNECTIVITY_NAME, status );
129 
130  H5Gclose( elem_id );
131  if( table_id < 0 ) return -1;
132 
133  file_ptr->open_handle_count++;
134  mhdf_setOkay( status );
135  API_END_H( 1 );
136  return table_id;
137 }
138 
139 void mhdf_writeConnectivity( hid_t table_id,
140  long offset,
141  long count,
142  hid_t hdf_integer_type,
143  const void* nodes,
144  mhdf_Status* status )
145 {
146  API_BEGIN;
147  mhdf_write_data( table_id, offset, count, hdf_integer_type, nodes, H5P_DEFAULT, status );
148  API_END;
149 }
150 void mhdf_writeConnectivityWithOpt( hid_t table_id,
151  long offset,
152  long count,
153  hid_t hdf_integer_type,
154  const void* nodes,
155  hid_t prop,
156  mhdf_Status* status )
157 {
158  API_BEGIN;
159  mhdf_write_data( table_id, offset, count, hdf_integer_type, nodes, prop, status );
160  API_END;
161 }
162 
163 void mhdf_readConnectivity( hid_t table_id,
164  long offset,
165  long count,
166  hid_t hdf_integer_type,
167  void* nodes,
168  mhdf_Status* status )
169 {
170  API_BEGIN;
171  mhdf_read_data( table_id, offset, count, hdf_integer_type, nodes, H5P_DEFAULT, status );
172  API_END;
173 }
174 void mhdf_readConnectivityWithOpt( hid_t table_id,
175  long offset,
176  long count,
177  hid_t hdf_integer_type,
178  void* nodes,
179  hid_t prop,
180  mhdf_Status* status )
181 {
182  API_BEGIN;
183  mhdf_read_data( table_id, offset, count, hdf_integer_type, nodes, prop, status );
184  API_END;
185 }
186 
188  const char* elem_type,
189  long num_poly,
190  long data_list_length,
191  long* first_id_out,
192  hid_t handles_out[2],
193  mhdf_Status* status )
194 {
195  FileHandle* file_ptr;
196  hid_t elem_id, index_id, conn_id;
197  hsize_t dim;
198  long first_id;
199  API_BEGIN;
200 
201  file_ptr = (FileHandle*)( file_handle );
202  if( !mhdf_check_valid_file( file_ptr, status ) ) return;
203 
204  if( num_poly <= 0 || data_list_length <= 0 || !first_id_out )
205  {
206  mhdf_setFail( status, "Invalid argument." );
207  return;
208  }
209 
210  if( data_list_length < 3 * num_poly )
211  {
212  /* Could check agains 4*num_poly, but allow degenerate polys
213  (1 for count plus 2 dim-1 defining entities, where > 2
214  defining entities is a normal poly, 2 defining entities
215  is a degenerate poly and 1 defning entity is not valid.)
216  */
217 
218  mhdf_setFail( status,
219  "Invalid polygon data: data length of %ld is "
220  "insufficient for %ld poly(gons/hedra).\n",
221  data_list_length, num_poly );
222  return;
223  }
224 
225  elem_id = mhdf_elem_group_from_handle( file_ptr, elem_type, status );
226  if( elem_id < 0 ) return;
227 
228  dim = (hsize_t)num_poly;
229  index_id = mhdf_create_table( elem_id, POLY_INDEX_NAME, MHDF_INDEX_TYPE, 1, &dim, status );
230  if( index_id < 0 )
231  {
232  H5Gclose( elem_id );
233  return;
234  }
235 
236  dim = (hsize_t)data_list_length;
237  conn_id = mhdf_create_table( elem_id, CONNECTIVITY_NAME, file_ptr->id_type, 1, &dim, status );
238  H5Gclose( elem_id );
239  if( conn_id < 0 )
240  {
241  H5Dclose( index_id );
242  return;
243  }
244 
245  first_id = file_ptr->max_id + 1;
246  if( !mhdf_create_scalar_attrib( conn_id, START_ID_ATTRIB, H5T_NATIVE_LONG, &first_id, status ) )
247  {
248  H5Dclose( index_id );
249  H5Dclose( conn_id );
250  return;
251  }
252 
253  *first_id_out = first_id;
254  file_ptr->max_id += num_poly;
255  if( !mhdf_write_max_id( file_ptr, status ) )
256  {
257  H5Dclose( index_id );
258  H5Dclose( conn_id );
259  return;
260  }
261  file_ptr->open_handle_count++;
262  mhdf_setOkay( status );
263  handles_out[0] = index_id;
264  handles_out[1] = conn_id;
265  API_END_H( 2 );
266 }
267 
269  const char* element_handle,
270  long* num_poly_out,
271  long* data_list_length_out,
272  long* first_poly_id_out,
273  hid_t handles_out[2],
274  mhdf_Status* status )
275 {
276  FileHandle* file_ptr;
277  hid_t elem_id, table_id, index_id;
278  hsize_t row_count;
279  API_BEGIN;
280 
281  file_ptr = (FileHandle*)( file_handle );
282  if( !mhdf_check_valid_file( file_ptr, status ) ) return;
283 
284  if( !num_poly_out || !data_list_length_out || !first_poly_id_out )
285  {
286  mhdf_setFail( status, "Invalid argument." );
287  return;
288  }
289 
290  elem_id = mhdf_elem_group_from_handle( file_ptr, element_handle, status );
291  if( elem_id < 0 ) return;
292 
293  index_id = mhdf_open_table( elem_id, POLY_INDEX_NAME, 1, &row_count, status );
294  if( index_id < 0 )
295  {
296  H5Gclose( elem_id );
297  return;
298  }
299  *num_poly_out = (int)row_count;
300 
301  table_id = mhdf_open_table( elem_id, CONNECTIVITY_NAME, 1, &row_count, status );
302 
303  H5Gclose( elem_id );
304  if( table_id < 0 )
305  {
306  H5Dclose( index_id );
307  return;
308  }
309  *data_list_length_out = (long)row_count;
310 
311  if( !mhdf_read_scalar_attrib( table_id, START_ID_ATTRIB, H5T_NATIVE_LONG, first_poly_id_out, status ) )
312  {
313  H5Dclose( table_id );
314  H5Dclose( index_id );
315  return;
316  }
317 
318  file_ptr->open_handle_count++;
319  handles_out[0] = index_id;
320  handles_out[1] = table_id;
321  mhdf_setOkay( status );
322  API_END_H( 2 );
323 }
324 
325 void mhdf_writePolyConnIndices( hid_t table_id,
326  long offset,
327  long count,
328  hid_t hdf_integer_type,
329  const void* index_list,
330  mhdf_Status* status )
331 {
332  API_BEGIN;
333  mhdf_write_data( table_id, offset, count, hdf_integer_type, index_list, H5P_DEFAULT, status );
334  API_END;
335 }
336 void mhdf_writePolyConnIndicesWithOpt( hid_t table_id,
337  long offset,
338  long count,
339  hid_t hdf_integer_type,
340  const void* index_list,
341  hid_t prop,
342  mhdf_Status* status )
343 {
344  API_BEGIN;
345  mhdf_write_data( table_id, offset, count, hdf_integer_type, index_list, prop, status );
346  API_END;
347 }
348 
349 void mhdf_readPolyConnIndices( hid_t table_id,
350  long offset,
351  long count,
352  hid_t hdf_integer_type,
353  void* index_list,
354  mhdf_Status* status )
355 {
356  API_BEGIN;
357  mhdf_read_data( table_id, offset, count, hdf_integer_type, index_list, H5P_DEFAULT, status );
358  API_END;
359 }
360 void mhdf_readPolyConnIndicesWithOpt( hid_t table_id,
361  long offset,
362  long count,
363  hid_t hdf_integer_type,
364  void* index_list,
365  hid_t prop,
366  mhdf_Status* status )
367 {
368  API_BEGIN;
369  mhdf_read_data( table_id, offset, count, hdf_integer_type, index_list, prop, status );
370  API_END;
371 }
372 
373 void mhdf_writePolyConnIDs( hid_t table_id,
374  long offset,
375  long count,
376  hid_t hdf_integer_type,
377  const void* id_list,
378  mhdf_Status* status )
379 {
380  API_BEGIN;
381  mhdf_write_data( table_id, offset, count, hdf_integer_type, id_list, H5P_DEFAULT, status );
382  API_END;
383 }
384 void mhdf_writePolyConnIDsWithOpt( hid_t table_id,
385  long offset,
386  long count,
387  hid_t hdf_integer_type,
388  const void* id_list,
389  hid_t prop,
390  mhdf_Status* status )
391 {
392  API_BEGIN;
393  mhdf_write_data( table_id, offset, count, hdf_integer_type, id_list, prop, status );
394  API_END;
395 }
396 
397 void mhdf_readPolyConnIDs( hid_t table_id,
398  long offset,
399  long count,
400  hid_t hdf_integer_type,
401  void* id_list,
402  mhdf_Status* status )
403 {
404  API_BEGIN;
405  mhdf_read_data( table_id, offset, count, hdf_integer_type, id_list, H5P_DEFAULT, status );
406  API_END;
407 }
408 void mhdf_readPolyConnIDsWithOpt( hid_t table_id,
409  long offset,
410  long count,
411  hid_t hdf_integer_type,
412  void* id_list,
413  hid_t prop,
414  mhdf_Status* status )
415 {
416  API_BEGIN;
417  mhdf_read_data( table_id, offset, count, hdf_integer_type, id_list, prop, status );
418  API_END;
419 }