# Generate finite elments by CGX for a grid of voxels

**URL:** <https://calculix.discourse.group/t/generate-finite-elments-by-cgx-for-a-grid-of-voxels/1838>\
**Category:** CalculiX GraphiX\
**Created:** [September 24, 2023, 5:58pm UTC](https://calculix.discourse.group/t/generate-finite-elments-by-cgx-for-a-grid-of-voxels/1838 "2023-09-24T17:58:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Megidd](https://yyz2.discourse-cdn.com/free1/user_avatar/calculix.discourse.group/megidd/32/1875_2.png) [@Megidd](https://calculix.discourse.group/u/Megidd)\
**Post date:** [September 24, 2023, 5:58pm UTC](https://calculix.discourse.group/t/generate-finite-elments-by-cgx-for-a-grid-of-voxels/1838/1 "2023-09-24T17:58:59Z")

</div>

# Voxel

Let’s assume there is a 3D grid of voxels like below. Filled voxels represent a 3D model and empty voxels represent space.

 ![2023-09-24 21_05_33-Window](https://global.discourse-cdn.com/free1/uploads/calculix/original/2X/a/ac86d271ea9028ef815734f824e1ab12a197ac84.png)

 ![2023-09-24 21_40_39-Window](https://global.discourse-cdn.com/free1/uploads/calculix/original/2X/8/8e6e0a96f58f318b5198f85c070f67cfc81e832f.png)

# Loop over voxels

Shown by this pseudocode, the iteration over voxels is done by their `x, y, z` index:

```auto
for x := 1, Nx;
    for y := 1, Ny;
        for z := 1, Nz;
            if voxelIsEmpty continue;
            // 3D coordinates of 8 voxel corners are floating-point values.
            voxelCornersX = [x1, x2, x3, x4, x5, x6, x7, x8];
            voxelCornersY = [y1, y2, y3, y4, y5, y6, y7, y8];
            voxelCornersZ = [z1, z2, z3, z4, z5, z6, z7, z8];
            // TODO: create a hexahedral element by the 8 corners.
        endfor
    endfor
endfor

```

# Question

How can I create a single hex element for a filled voxel, if the voxel corner coordinates are known?

As far as I study the documentation, the CGX should be used by creating a geometry (line, surface, volume) and then creating finite elements through the geometry. The problem is that I don’t create a geometry in advance. I’m a bit confused. I appreciate any hints. Thanks.

---

<div class="post-metadata">

**Author:** ![Megidd](https://yyz2.discourse-cdn.com/free1/user_avatar/calculix.discourse.group/megidd/32/1875_2.png) [@Megidd](https://calculix.discourse.group/u/Megidd)\
**Post date:** [September 25, 2023, 7:33am UTC](https://calculix.discourse.group/t/generate-finite-elments-by-cgx-for-a-grid-of-voxels/1838/2 "2023-09-25T07:33:56Z")

</div>

I’m studying examples like this, to figure out how to:

> <https://github.com/calculix/CalculiX-Examples/blob/master/Linear/Plates/pre.fbd>

---

<div class="post-metadata">

**Author:** ![Megidd](https://yyz2.discourse-cdn.com/free1/user_avatar/calculix.discourse.group/megidd/32/1875_2.png) [@Megidd](https://calculix.discourse.group/u/Megidd)\
**Post date:** [September 25, 2023, 8:17am UTC](https://calculix.discourse.group/t/generate-finite-elments-by-cgx-for-a-grid-of-voxels/1838/3 "2023-09-25T08:17:05Z")

</div>

Currently, I’m generating finite elements by a data structure in Golang. While iterating over the voxels, the data structure stores elements by their corner coordinates and has functions to write nodes, elements, restraints, and loads as an `INP` file.

But I’m thinking of replacing the Golang data structure. I can imagine writing CGX commands to an `FBD` file line by line while I’m iterating over the voxels. I can imagine writing any other needed command to the `FBD` file. Then I will generate the final `INP` file by calling CGX with the `FBD` file as a parameter. These are all imaginations. I’m not sure 🙄

The objective is leveraging the CGX to generate higher quality finite elements and also a cleaner and maintainable code ✅

---

<div class="post-metadata">

**Author:** ![Megidd](https://yyz2.discourse-cdn.com/free1/user_avatar/calculix.discourse.group/megidd/32/1875_2.png) [@Megidd](https://calculix.discourse.group/u/Megidd)\
**Post date:** [September 25, 2023, 4:58pm UTC](https://calculix.discourse.group/t/generate-finite-elments-by-cgx-for-a-grid-of-voxels/1838/4 "2023-09-25T16:58:04Z")

</div>

I guess I have to create _volume_ before creating finite elements. Maybe I can make a volume for each voxel 🙄

One requirement is that I have to be able to separate sets of elements in layers along the Z-axis. I mean, the elements on the 1st layer, 2nd layer, and so on, should be separatable.

---

<div class="post-metadata">

**Author:** ![rsmith](https://yyz2.discourse-cdn.com/free1/user_avatar/calculix.discourse.group/rsmith/32/1684_2.png) [@rsmith](https://calculix.discourse.group/u/rsmith)\
**Post date:** [September 25, 2023, 11:07pm UTC](https://calculix.discourse.group/t/generate-finite-elments-by-cgx-for-a-grid-of-voxels/1838/5 "2023-09-25T23:07:12Z")

</div>

You don’t _have_ to use `cgx`.

Since you already have the voxel coordinates, you could basically use those to create nodes and convert each voxel into a HE8 or HE20R element.

---

<div class="post-metadata">

**Author:** ![Megidd](https://yyz2.discourse-cdn.com/free1/user_avatar/calculix.discourse.group/megidd/32/1875_2.png) [@Megidd](https://calculix.discourse.group/u/Megidd)\
**Post date:** [September 26, 2023, 6:49am UTC](https://calculix.discourse.group/t/generate-finite-elments-by-cgx-for-a-grid-of-voxels/1838/6 "2023-09-26T06:49:22Z")

</div>

> [@rsmith](#):
>
> You don’t _have_ to use `cgx`.

Right. I guess I would stick with my current Golang data structure.

# Darker lines

Another thing that motivated me to try CGX for element generation, is the fact that I’m observing darker lines on the surface of my models:

 ![2023-09-26 10_12_17-Window](https://global.discourse-cdn.com/free1/uploads/calculix/original/2X/0/0154a31f79da06bf8573dd8067ed850ece03efdd.png)

It’s [suggested](https://calculix.discourse.group/t/connection-of-hex-and-tet-elements/1755/6) that the darker lines are a warning sign. So far, I couldn’t fix those darker lines.

---

<div class="post-metadata">

**Author:** ![Disla](https://yyz2.discourse-cdn.com/free1/user_avatar/calculix.discourse.group/disla/32/3548_2.png) [@Disla](https://calculix.discourse.group/u/Disla)\
**Post date:** [September 26, 2023, 7:57am UTC](https://calculix.discourse.group/t/generate-finite-elments-by-cgx-for-a-grid-of-voxels/1838/7 "2023-09-26T07:57:01Z")

</div>

Your model was a mixture of linear and quadratic elements.

C3D4  
C3D8  
C3D10  
C3D20R

Those darker lines are probably disconnected midside nodes

---

<div class="post-metadata">

**Author:** ![Megidd](https://yyz2.discourse-cdn.com/free1/user_avatar/calculix.discourse.group/megidd/32/1875_2.png) [@Megidd](https://calculix.discourse.group/u/Megidd)\
**Post date:** [September 26, 2023, 9:39am UTC](https://calculix.discourse.group/t/generate-finite-elments-by-cgx-for-a-grid-of-voxels/1838/8 "2023-09-26T09:39:55Z")

</div>

> [@Disla](#):
>
> Those darker lines are probably disconnected midside nodes

Right, probably that’s the case 👍 I move forward ✅ Maybe down the road, I’ll figure out a definite answer as to why.

---

<div class="post-metadata">

**Author:** ![iori\_g](https://avatars.discourse-cdn.com/v4/letter/i/5e9695/32.png) [@iori\_g](https://calculix.discourse.group/u/iori_g)\
**Post date:** [September 27, 2023, 4:58pm UTC](https://calculix.discourse.group/t/generate-finite-elments-by-cgx-for-a-grid-of-voxels/1838/9 "2023-09-27T16:58:35Z")

</div>

hi there, we have a python package for CalculiX that can help you… you might want ti check it out:

> **[GitHub - gianthk/ciclope: Computed Tomography to Finite Elements](https://github.com/gianthk/ciclope/tree/master)**
>
> master

documentation and manual here:  
[https://ciclope.readthedocs.io/en/latest/](https://ciclope.readthedocs.io/en/latest/)

---

<div class="post-metadata">

**Author:** ![Megidd](https://yyz2.discourse-cdn.com/free1/user_avatar/calculix.discourse.group/megidd/32/1875_2.png) [@Megidd](https://calculix.discourse.group/u/Megidd)\
**Post date:** [September 29, 2023, 4:48pm UTC](https://calculix.discourse.group/t/generate-finite-elments-by-cgx-for-a-grid-of-voxels/1838/10 "2023-09-29T16:48:26Z")

</div>

Thanks 👍 Interesting ✅
