1 # A little script to illustrate exchanging data with Cubit
5 The following cubit journal file (actually a python script) exports the current mesh and
6 boundary conditions from cubit to a file "output.ucd" in the current directory.
7 The [output file format](http://www.csit.fsu.edu/~burkardt/data/ucd/ucd.html) is [`AVS UCD`](http://vis.lbl.gov/NERSC/Software/express/help6.2/help/reference/dvmac/UCD_Form.htm).
9 This is a modification of the original script that takes into account boundary ids via
10 sidesets ids. If you want to save the boundary faces as well, you just need to
11 add the relevant surfaces in 3d or curves in 2d to a sideset, and the id will be the one of the
16 # This script will output whatever mesh you have currently in CUBIT
17 # in the AVS UCD format. (http://www.csit.fsu.edu/~burkardt/data/ucd/ucd.html)
18 # You may need to tweak it to get exactly what you want out of
19 # it. Your mileage may vary.
21 # set the filename -- you may need the entire path
22 outucdfile = "output.ucd"
24 outfile = open(outucdfile,"w")
26 cubit.cmd("body all rotate -90 about y")
27 cubit.cmd("body all reflect x")
29 # ============================================================
30 # Collect all the nodes
31 # ============================================================
32 group_id = cubit.get_id_from_name("temp_bc_curves")
34 cubit.cmd("delete group " + str(group_id))
35 cubit.cmd("group 'temp_nodes' add node all")
36 group_id = cubit.get_id_from_name("temp_nodes")
37 node_list = cubit.get_group_nodes(group_id)
38 cubit.cmd("delete group " + str(group_id))
39 n_nodes = len(node_list)
41 # ============================================================
43 # ============================================================
44 group_id = cubit.get_id_from_name("temp_hexes")
46 cubit.cmd("delete group " + str(group_id))
47 cubit.cmd("group 'temp_hexes' add hex all")
48 group_id = cubit.get_id_from_name("temp_hexes")
49 hex_list = cubit.get_group_hexes(group_id)
50 cubit.cmd("delete group " + str(group_id))
51 n_hex_cells = len(hex_list)
54 # ============================================================
55 # Now the boundary conditions in 3d
56 # ============================================================
60 bc_ids = cubit.get_sideset_id_list()
62 bc_surfaces[= cubit.get_sideset_surfaces(bc_id)
63 for bc_surface in bc_surfaces[bc_id](bc_id]):
64 bc_quads = cubit.get_surface_quads(bc_surface)
65 n_bc_quads += len(bc_quads)
68 # ============================================================
69 # Collect all the surfaces. Notice that the surfaces that make up a
70 # volume are not grouped here. This is only for 2d objects, i.e.,
71 # when the number n_hex_cells is zero.
72 # ============================================================
77 group_id = cubit.get_id_from_name("temp_surfs")
79 cubit.cmd("delete group " + str(group_id))
80 cubit.cmd("group 'temp_surfs' add surf all")
81 group_id = cubit.get_id_from_name("temp_surfs")
82 surface_list = cubit.get_group_surfaces(group_id)
83 cubit.cmd("delete group " + str(group_id))
84 for surface_id in surface_list:
85 quad_cell_list[= cubit.get_surface_quads(surface_id)
86 n_quad_cells += len(quad_cell_list[surface_id](surface_id]))
88 # ============================================================
89 # Now the boundary conditions in 2d
90 # ============================================================
95 bc_ids = cubit.get_sideset_id_list()
97 bc_curves[= cubit.get_sideset_curves(bc_id)
98 group_id = cubit.get_id_from_name("temp_bc_curves")
100 cubit.cmd("delete group " + str(group_id))
101 for bc_curve in bc_curves[bc_id](bc_id]):
102 cubit.cmd("group 'temp_bc_curves' add edge all in curve " + str(bc_curve))
103 group_id = cubit.get_id_from_name("temp_bc_curves")
104 bc_edges[= cubit.get_group_edges(group_id)
105 cubit.cmd("delete group " + str(group_id))
106 n_bc_edges += len(bc_edges[bc_id](bc_id]))
108 print 'Edges: ' + str(n_bc_edges)
110 # ============================================================
111 # Now we write the header.
112 # ============================================================
113 n_elements = n_hex_cells + n_bc_quads + n_quad_cells + n_bc_edges
114 outfile.write(str(n_nodes) + " " + str(n_elements) + " 0 0 0\n")
117 # ============================================================
119 # ============================================================
120 for node_num in node_list:
121 outfile.write(str(node_num ))
123 node_coord = cubit.get_nodal_coordinates(node_num)
124 if abs(node_coord[outfile.write("0 ")
126 outfile.write(str(node_coord[2](2])<1e-15:)) + " ")
127 if abs(node_coord[outfile.write("0 ")
129 outfile.write(str(node_coord[1](1])<1e-15:)) + " ")
130 if abs(node_coord[outfile.write("0")
132 outfile.write(str(node_coord[0](0])<1e-15:)))
136 # ============================================================
138 # ============================================================
140 for hex_num in hex_list:
141 outfile.write(str(k) + " 0 " + " hex ")
143 hex_nodes = cubit.get_connectivity("Hex", hex_num)
146 outfile.write(str(hex_nodes[+ " ")
151 # ============================================================
152 # The quads on the boundaries. 3d
153 # Note that the boundary id is given by the sideset id.
154 # ============================================================
158 for bc_surface in bc_surfaces[bc_id](i])):
159 bc_quads = cubit.get_surface_quads(bc_surface)
160 for quad_num in bc_quads:
161 outfile.write(str(k))
163 outfile.write(" " + str(bc_id) + " quad ")
164 quad_nodes = cubit.get_connectivity("Quad", quad_num)
165 outfile.write(str(quad_nodes[+ " ")
166 outfile.write(str(quad_nodes[3](0]))) + " ")
167 outfile.write(str(quad_nodes[+ " ")
168 outfile.write(str(quad_nodes[1](2]))))
171 # ============================================================
172 # The quads on the surfaces. 2d
173 # ============================================================
176 for surface_id in surface_list:
177 for quad_num in quad_cell_list[outfile.write(str(k))
179 outfile.write(" " + str(surface_id) + " quad ")
180 quad_nodes = cubit.get_connectivity("Quad", quad_num)
181 outfile.write(str(quad_nodes[0](surface_id]:)) + " ")
182 outfile.write(str(quad_nodes[+ " ")
183 outfile.write(str(quad_nodes[2](3]))) + " ")
184 outfile.write(str(quad_nodes[outfile.write("\n")
186 # ============================================================
187 # The edges on the curves. 2d
188 # Boundary id = sideset_id
189 # ============================================================
193 for edge_num in bc_edges[bc_id](1]))):
194 outfile.write(str(k))
196 outfile.write(" " + str(bc_id) + " line ")
197 edge_nodes = cubit.get_connectivity("Edge", edge_num)
198 outfile.write(str(edge_nodes[+ " ")
199 outfile.write(str(edge_nodes[1](0]))))
204 cubit.cmd("body all reflect x")
205 cubit.cmd("body all rotate 90 about y")
207 print str(n_nodes) + " nodes\n"
208 print str(n_hex_cells) + " hexes\n"
209 print str(n_quad_cells) + " quads\n"
210 print str(n_bc_quads) + " face_quads\n"
211 print str(n_bc_edges) + " edges\n"
215 # Alternate versions of this script
219 The following script, which also outputs material IDs (as identified by the `block id` to which each element belongs), has been [verified to work](https://groups.google.com/forum/#!topic/dealii/ZnU3XIK_ipk) with `Cubit v15.2`.
223 # This script will output whatever mesh you have currently in CUBIT
224 # in the AVS UCD format. (http://www.csit.fsu.edu/~burkardt/data/ucd/ucd.html)
225 # You may need to tweak it to get exactly what you want out of
226 # it. Your mileage may vary.
228 # set the filename -- you may need the entire path
229 outucdfile = "output.ucd"
231 outfile = open(outucdfile,"w")
233 cubit.cmd("body all rotate -90 about y")
234 cubit.cmd("body all reflect x")
236 # ============================================================
237 # Collect all the nodes
238 # ============================================================
239 group_id = cubit.get_id_from_name("temp_bc_curves")
241 cubit.cmd("delete group " + str(group_id))
242 cubit.cmd("group 'temp_nodes' add node all")
243 group_id = cubit.get_id_from_name("temp_nodes")
244 node_list = cubit.get_group_nodes(group_id)
245 cubit.cmd("delete group " + str(group_id))
246 n_nodes = len(node_list)
248 # ============================================================
249 # Collect all the hex
250 # ============================================================
254 block_ids = cubit.get_block_id_list()
255 for block_id in block_ids:
256 hex_cell_list[block_id] = cubit.get_block_hexes(block_id)
257 n_hex_cells += len(hex_cell_list[block_id])
259 # ============================================================
260 # Now the boundary conditions in 3d
261 # ============================================================
265 bc_ids = cubit.get_sideset_id_list()
266 for bc_id in bc_ids :
267 bc_surfaces[bc_id] = cubit.get_sideset_surfaces(bc_id)
268 for bc_surface in bc_surfaces[bc_id]:
269 bc_quads = cubit.get_surface_quads(bc_surface)
270 n_bc_quads += len(bc_quads)
273 # ============================================================
274 # Collect all the surfaces. Notice that the surfaces that make up a
275 # volume are not grouped here. This is only for 2d objects, i.e.,
276 # when the number n_hex_cells is zero.
277 # ============================================================
282 group_id = cubit.get_id_from_name("temp_surfs")
284 cubit.cmd("delete group " + str(group_id))
285 cubit.cmd("group 'temp_surfs' add surf all")
286 group_id = cubit.get_id_from_name("temp_surfs")
287 surface_list = cubit.get_group_surfaces(group_id)
288 cubit.cmd("delete group " + str(group_id))
289 for surface_id in surface_list:
290 quad_cell_list[surface_id] = cubit.get_surface_quads(surface_id)
291 n_quad_cells += len(quad_cell_list[surface_id])
293 # ============================================================
294 # Now the boundary conditions in 2d
295 # ============================================================
300 bc_ids = cubit.get_sideset_id_list()
301 for bc_id in bc_ids :
302 bc_curves[bc_id] = cubit.get_sideset_curves(bc_id)
303 group_id = cubit.get_id_from_name("temp_bc_curves")
305 cubit.cmd("delete group " + str(group_id))
306 for bc_curve in bc_curves[bc_id]:
307 cubit.cmd("group 'temp_bc_curves' add edge all in curve " + str(bc_curve))
308 group_id = cubit.get_id_from_name("temp_bc_curves")
309 bc_edges[bc_id] = cubit.get_group_edges(group_id)
310 cubit.cmd("delete group " + str(group_id))
311 n_bc_edges += len(bc_edges[bc_id])
313 print 'Edges: ' + str(n_bc_edges)
315 # ============================================================
316 # Now we write the header.
317 # ============================================================
318 n_elements = n_hex_cells + n_bc_quads + n_quad_cells + n_bc_edges
319 outfile.write(str(n_nodes) + " " + str(n_elements) + " 0 0 0\n")
322 # ============================================================
324 # ============================================================
325 for node_num in node_list:
326 outfile.write(str(node_num ))
328 node_coord = cubit.get_nodal_coordinates(node_num)
329 if abs(node_coord[2])<1e-15:
332 outfile.write(str(node_coord[2]) + " ")
333 if abs(node_coord[1])<1e-15:
336 outfile.write(str(node_coord[1]) + " ")
337 if abs(node_coord[0])<1e-15:
340 outfile.write(str(node_coord[0]))
344 # ============================================================
346 # ============================================================
348 for block_id in block_ids:
349 for hex_num in hex_cell_list[block_id]:
350 print str(k) + " " + str(block_id) + " hex "
351 outfile.write(str(k) + " " + str(block_id) + " hex ")
353 hex_nodes = cubit.get_connectivity("Hex", hex_num)
356 outfile.write(str(hex_nodes[i]) + " ")
360 # ============================================================
361 # The quads on the boundaries. 3d
362 # Note that the boundary id is given by the sideset id.
363 # ============================================================
367 for bc_surface in bc_surfaces[bc_id]:
368 bc_quads = cubit.get_surface_quads(bc_surface)
369 for quad_num in bc_quads:
370 outfile.write(str(k))
372 outfile.write(" " + str(bc_id) + " quad ")
373 quad_nodes = cubit.get_connectivity("Quad", quad_num)
374 outfile.write(str(quad_nodes[0]) + " ")
375 outfile.write(str(quad_nodes[3]) + " ")
376 outfile.write(str(quad_nodes[2]) + " ")
377 outfile.write(str(quad_nodes[1]))
380 # ============================================================
381 # The quads on the surfaces. 2d
382 # ============================================================
385 for surface_id in surface_list:
386 for quad_num in quad_cell_list[surface_id]:
387 outfile.write(str(k))
389 outfile.write(" " + str(surface_id) + " quad ")
390 quad_nodes = cubit.get_connectivity("Quad", quad_num)
391 outfile.write(str(quad_nodes[0]) + " ")
392 outfile.write(str(quad_nodes[3]) + " ")
393 outfile.write(str(quad_nodes[2]) + " ")
394 outfile.write(str(quad_nodes[1]))
397 # ============================================================
398 # The edges on the curves. 2d
399 # Boundary id = sideset_id
400 # ============================================================
404 for edge_num in bc_edges[bc_id]:
405 outfile.write(str(k))
407 outfile.write(" " + str(bc_id) + " line ")
408 edge_nodes = cubit.get_connectivity("Edge", edge_num)
409 outfile.write(str(edge_nodes[0]) + " ")
410 outfile.write(str(edge_nodes[1]))
415 cubit.cmd("body all reflect x")
416 cubit.cmd("body all rotate 90 about y")
418 print str(n_nodes) + " nodes\n"
419 print str(n_hex_cells) + " hexes\n"
420 print str(n_quad_cells) + " quads\n"
421 print str(n_bc_quads) + " face_quads\n"
422 print str(n_bc_edges) + " edges\n"
426 In case it is of any use, one of the initial versions of this script is also provided below. This has been verified to work on `Cubit v13.2`, but unfortunately does not output material IDs for reasons documented in the paragraphs below.
430 # This script will output whatever mesh you have currently in CUBIT
431 # in the AVS UCD format. (http://www.csit.fsu.edu/~burkardt/data/ucd/ucd.html)
432 # You may need to tweak it to get exactly what you want out of
433 # it. Your mileage may vary.
435 # set the filename -- you may need the entire path
436 outucdfile = "output.ucd"
438 outfile = open(outucdfile,"w")
440 cubit.cmd("body all rotate -90 about y")
441 cubit.cmd("body all reflect x")
443 # ============================================================
444 # Collect all the nodes
445 # ============================================================
446 group_id = cubit.get_id_from_name("temp_bc_curves")
448 cubit.cmd("delete group " + str(group_id))
449 cubit.cmd("group 'temp_nodes' add node all")
450 group_id = cubit.get_id_from_name("temp_nodes")
451 node_list = cubit.get_group_nodes(group_id)
452 cubit.cmd("delete group " + str(group_id))
453 n_nodes = len(node_list)
455 # ============================================================
456 # Collect all the hex
457 # ============================================================
458 group_id = cubit.get_id_from_name("temp_hexes")
460 cubit.cmd("delete group " + str(group_id))
461 cubit.cmd("group 'temp_hexes' add hex all")
462 group_id = cubit.get_id_from_name("temp_hexes")
463 hex_list = cubit.get_group_hexes(group_id)
464 cubit.cmd("delete group " + str(group_id))
465 n_hex_cells = len(hex_list)
468 # ============================================================
469 # Now the boundary conditions in 3d
470 # ============================================================
474 bc_ids = cubit.get_sideset_id_list()
475 for bc_id in bc_ids :
476 bc_surfaces[bc_id] = cubit.get_sideset_surfaces(bc_id)
477 for bc_surface in bc_surfaces[bc_id]:
478 bc_quads = cubit.get_surface_quads(bc_surface)
479 n_bc_quads += len(bc_quads)
482 # ============================================================
483 # Collect all the surfaces. Notice that the surfaces that make up a
484 # volume are not grouped here. This is only for 2d objects, i.e.,
485 # when the number n_hex_cells is zero.
486 # ============================================================
491 group_id = cubit.get_id_from_name("temp_surfs")
493 cubit.cmd("delete group " + str(group_id))
494 cubit.cmd("group 'temp_surfs' add surf all")
495 group_id = cubit.get_id_from_name("temp_surfs")
496 surface_list = cubit.get_group_surfaces(group_id)
497 cubit.cmd("delete group " + str(group_id))
498 for surface_id in surface_list:
499 quad_cell_list[surface_id] = cubit.get_surface_quads(surface_id)
500 n_quad_cells += len(quad_cell_list[surface_id])
502 # ============================================================
503 # Now the boundary conditions in 2d
504 # ============================================================
509 bc_ids = cubit.get_sideset_id_list()
510 for bc_id in bc_ids :
511 bc_curves[bc_id] = cubit.get_sideset_curves(bc_id)
512 group_id = cubit.get_id_from_name("temp_bc_curves")
514 cubit.cmd("delete group " + str(group_id))
515 for bc_curve in bc_curves[bc_id]:
516 cubit.cmd("group 'temp_bc_curves' add edge all in curve " + str(bc_curve))
517 group_id = cubit.get_id_from_name("temp_bc_curves")
518 bc_edges[bc_id] = cubit.get_group_edges(group_id)
519 cubit.cmd("delete group " + str(group_id))
520 n_bc_edges += len(bc_edges[bc_id])
522 print 'Edges: ' + str(n_bc_edges)
524 # ============================================================
525 # Now we write the header.
526 # ============================================================
527 n_elements = n_hex_cells + n_bc_quads + n_quad_cells + n_bc_edges
528 outfile.write(str(n_nodes) + " " + str(n_elements) + " 0 0 0\n")
531 # ============================================================
533 # ============================================================
534 for node_num in node_list:
535 outfile.write(str(node_num ))
537 node_coord = cubit.get_nodal_coordinates(node_num)
538 if abs(node_coord[2])<1e-15:
541 outfile.write(str(node_coord[2]) + " ")
542 if abs(node_coord[1])<1e-15:
545 outfile.write(str(node_coord[1]) + " ")
546 if abs(node_coord[0])<1e-15:
549 outfile.write(str(node_coord[0]))
553 # ============================================================
555 # ============================================================
557 for hex_num in hex_list:
558 outfile.write(str(k) + " 0 " + " hex ")
560 hex_nodes = cubit.get_connectivity("Hex", hex_num)
563 outfile.write(str(hex_nodes[i]) + " ")
567 # ============================================================
568 # The quads on the boundaries. 3d
569 # Note that the boundary id is given by the sideset id.
570 # ============================================================
574 for bc_surface in bc_surfaces[bc_id]:
575 bc_quads = cubit.get_surface_quads(bc_surface)
576 for quad_num in bc_quads:
577 outfile.write(str(k))
579 outfile.write(" " + str(bc_id) + " quad ")
580 quad_nodes = cubit.get_connectivity("Quad", quad_num)
581 outfile.write(str(quad_nodes[0]) + " ")
582 outfile.write(str(quad_nodes[3]) + " ")
583 outfile.write(str(quad_nodes[2]) + " ")
584 outfile.write(str(quad_nodes[1]))
587 # ============================================================
588 # The quads on the surfaces. 2d
589 # ============================================================
592 for surface_id in surface_list:
593 for quad_num in quad_cell_list[surface_id]:
594 outfile.write(str(k))
596 outfile.write(" " + str(surface_id) + " quad ")
597 quad_nodes = cubit.get_connectivity("Quad", quad_num)
598 outfile.write(str(quad_nodes[0]) + " ")
599 outfile.write(str(quad_nodes[3]) + " ")
600 outfile.write(str(quad_nodes[2]) + " ")
601 outfile.write(str(quad_nodes[1]))
604 # ============================================================
605 # The edges on the curves. 2d
606 # Boundary id = sideset_id
607 # ============================================================
611 for edge_num in bc_edges[bc_id]:
612 outfile.write(str(k))
614 outfile.write(" " + str(bc_id) + " line ")
615 edge_nodes = cubit.get_connectivity("Edge", edge_num)
616 outfile.write(str(edge_nodes[0]) + " ")
617 outfile.write(str(edge_nodes[1]))
622 cubit.cmd("body all reflect x")
623 cubit.cmd("body all rotate 90 about y")
625 print str(n_nodes) + " nodes\n"
626 print str(n_hex_cells) + " hexes\n"
627 print str(n_quad_cells) + " quads\n"
628 print str(n_bc_quads) + " face_quads\n"
629 print str(n_bc_edges) + " edges\n"
632 ### A note on the (non-) output of material ID's
633 For the first and third versions of this script, the material IDs are not output (they are always set by default to zero).
634 This is because there appears to be a deficiency/bug in the implementation of `cubit.get_block_hexes(block_id)` (in `Cubit v13.2`, at least).
635 In theory one should be able to collect the cells in the following manner, assuming that the `block_id` represents the material id (this mirrors what is done already for `sideset_id`s and boundary ids):
638 # ============================================================
639 # Collect all the hex
640 # ============================================================
644 block_ids = cubit.get_block_id_list()
645 for block_id in block_ids:
646 hex_cell_list[block_id] = cubit.get_block_hexes(block_id)
647 n_hex_cells += len(hex_cell_list[block_id])
650 and one could then write out the elements to file with a set of commands along the lines of
653 # ============================================================
655 # ============================================================
657 for block_id in block_ids:
658 for hex_num in hex_cell_list[block_id]:
659 print str(k) + " " + str(block_id) + " hex "
660 outfile.write(str(k) + " " + str(block_id) + " hex ")
662 hex_nodes = cubit.get_connectivity("Hex", hex_num)
665 outfile.write(str(hex_nodes[i]) + " ")
670 However, there `cubit.get_block_hexes(block_id)` does not return anything other than an empty set when these sets of commands were last tested (`Cubit v13.2`).
671 By contrast the script verified to work with `Cubit v15.2` includes this logic as the bug has been corrected in later versions of the software.
673 Another version of the above script is available that produces a VTK file. You can find it here:
675 https://gist.github.com/luca-heltai/b127a69b86b4b140c87a01110053f751
677 You can add a shortcut in a Custom Toolbar to load the script into memory. Then when you are
678 ready to export, you can type the following command in the script tab:
680 saveVTK("filename.vtk")