kip-hart / MicroStructPy

Microstructure modeling, mesh generation, analysis, and visualization.
https://docs.microstructpy.org
MIT License
74 stars 19 forks source link

Add raster mesh #45

Closed kip-hart closed 3 years ago

kip-hart commented 3 years ago

PR Summary

Purpose

This PR adds a RasterMesh class to create pixel/voxel meshes from a PolyMesh. It addresses #44.

Approach

A raster grid is created, then the centers of the pixels are computed. For each region in the PolyMesh, pixels within the region are determined by creating a bounding box as a cheap screen, then testing which of the remaining points are within the region.

The array of element attributes is created by converting the lower-left corner of each pixel into a matrix coordinate, then populating the array with the attributes.

For meshes with holes or non-box domains, pixels will have an attribute of -1 if there is no element associated with them.

Example

Adjusting the first example input to:

<?xml version="1.0" encoding="UTF-8"?>
<input>
    <material>
        <name> Matrix </name>
        <material_type> matrix </material_type>
        <fraction> 2 </fraction>
        <shape> circle </shape>
        <size>
            <dist_type> uniform </dist_type>
            <loc> 0 </loc>
            <scale> 1.5 </scale>
        </size>
    </material>

    <material>
        <name> Inclusions </name>
        <fraction> 1 </fraction>
        <shape> circle </shape>
        <diameter> 2 </diameter>
    </material>

    <domain>
        <shape> square </shape>
        <side_length> 20 </side_length>
        <corner> (0, 0) </corner>
    </domain>

    <settings>
        <verbose> True </verbose>
        <directory> intro_1_basic </directory>

        <mesher> raster </mesher>
        <mesh_size> 0.5 </mesh_size>
    </settings>
</input>

Creates an output raster mesh that looks like this:

rastermesh

Opening this mesh and calling the RasterMesh.as_array() function yields the following output:

>>> import microstructpy as msp
>>> rmesh = msp.meshing.RasterMesh.from_file('intro_1_basic/rastermesh.txt')
>>> print(rmesh.as_array())
[[  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0   0   0   0 229 229 229 489  64
   64  64   0   0]
 [511 511   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0   0   0 229 229 229 229  64  64
   64  64   0   0]
 [511 511   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0 272 272 272   0   0 229 229 229   0  64
   64  64   0   0]
 [511 511   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0 272 272 272 272   0   0   0 229   0  95  95
   95   0   0   0]
 [511   0   0   0   0 300 300 300   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0 272 272 272 272   0   0   0   0   0  95  95
   95  95   0   0]
 [  0   0   0   0 300 300 300 300   0   0   0   0   0   0 227 227   0   0
    0   0   0   0   0   0   0   0 272 272 272   0   0   0   0   0  95  95
   95  95   0   0]
 [  0   0   0   0 300 300 300 300   0   0   0   0   0 227 227 227 227   0
    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  95  95
   95   0   0   0]
 [  0   0   0   0   0 300 300 300   0   0   0   0   0 227 227 227 227   0
    0   0   0   0   0   0   0   0  68  68  68   0   0   0   0   0   0   0
    0   0   0   0]
 [  0   0   0   0 428 428 428 428   0   0   0   0   0   0 227 227   0   0
    0   0   0   0 346 346   0  68  68  68  68   0   0   0   0   0   0   0
    0   0   0   0]
 [  0   0   0   0 428 428 428 428   0   0   0   0   0   0   0   0   0   0
    0   0   0 346 346 346 346  68  68  68  68   0   0   0   0   0   0   0
    0   0   0   0]
 [  0   0   0   0   0 428 428 377 377 377   0   0   0   0   0   0   0   0
    0   0   0 346 346 346 346   0  68  68   0   0   0   0   0   0   0   0
    0   0   0   0]
 [  0   0   0   0   0   0 377 377 377 377 377   0   0   0   0   0 520 520
  520 520   0 346 346 346 346   0   0   0   0   0   0   0   0   0 107 107
  107 107   0   0]
 [ 67  67  67  67   0   0   0 377 377 377   0   0   0   0   0   0 520 520
  520 520   0   0   0   0   0 397 397 397   0   0   0   0   0   0 107 107
  107 107   0   0]
 [ 67  67  67  67   0   0   0   0 377 377   0   0   0   0   0   0 520 520
  520 520   0   0   0   0   0 397 397 397 397   0   0   0   0   0   0 107
  107 107 498 498]
 [ 67  67  67  67   0   0   0   0   0   0   0 118 118 118   0   0   0   0
  520   0   0   0   0   0   0 397 397 397 397   0 403   0   0   0   0   0
    0 498 498 498]
 [440 301   0   0   0   0   0 461 461 461 118 118 118 118   0   0   0   0
    0   0   0   0   0   0   0   0 397 397   0 403 403 403   0   0   0   0
    0 498 498 498]
 [301 301 301 301   0   0   0 461 461 461 461 118 118 118   0   0   0   0
    0   0   0   0  83  83   0   0   0   0 403 403 403 403   0   0   0   0
    0 498 498 498]
 [301 301 301 301   0   0   0 461 461 461   0 118 118 118   0   0   0   0
    0   0   0  83  83  83  83   0   0   0   0 403 403 403   0   0   0   0
    0   0   0   0]
 [301 301 301   0   0   0   0   0   0   0   0   0   0   0   0   0 391 391
  391   0   0  83  83  83  83   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 391 391 391
  391   0   0  83  83  83  83   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0]
 [  0   0   0  43  43  43   0   0   0   0   0   0   0   0   0 391 391 391
  391   0   0   0   0   0   0   0 479 479 479   0   0 269 269 269   0   0
    0   0   0   0]
 [  0   0   0  43  43  43  43   0   0   0   0   0   0   0   0   0 391 391
  391   0 383 383   0   0   0 479 479 479 479   0   0 269 269 269 269   0
    0   0   0   0]
 [  0   0   0  43  43  43   0   0   0   0 233 233 233 233   0   0   0   0
    0 383 383 383 383   0   0 479 479 479 479   0   0 269 269 269 269   0
    0 275   0   0]
 [  0   0   0  43  43  43   0   0   0   0 233 233 233 233   0   0   0   0
    0 383 383 383 383   0  76  76 479 479   0 431 431 431 269 269   0   0
  275 275 275 275]
 [  0   0   0   0   0   0   0   0   0   0 233 233 233 233   0   0  16  16
    0   0 383 383 383  76  76  76  76   0 431 431 431 431  50  50  50   0
  275 275 275 275]
 [  0   0   0   0   0   0   0   0   0   0   0 233 233   0   0  16  16  16
   16   0   0   0   0  76  76  76  76   0 431 431 431 431  50  50  50   0
    0 275 275 275]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  16  16  16
   16   0   0   0   0  76  76  76   0   0   0 431 431 431  50  50  50   0
    0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  16  16  16
   16   0   0   0   0   0   0   0   0   0   0   0   0   0  50  50  50   0
    0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0 110 110 110   0   0   0   0
    0   0   0   0   0   0   0   0  41  41  41   0   0   0   0   0   0   0
    0   0   0   0]
 [  0   0   0   0   0  51  51   0   0   0 110 110 110 110 110   0   0   0
    0   0   0   0   0   0   0  41  41  41  41   0   0   0   0   0   0   0
    0   0   0   0]
 [  0   0   0   0  51  51  51   0   0 467 110 110 110 110   0   0   0   0
    0 305 305 305   0   0   0  41  41  41  41   0   0   0   0   0   0   0
    0   0   0   0]
 [  0   0   0   0  51  51  51  51 467 467 467   0 110   0   0   0   0   0
  305 305 305 305   0   0   0   0  41  41  41   0   0   0   0   0   0   0
    0   0   0   0]
 [  0   0   0   0  51  51  51 467 467 467 467   0   0   0   0   0   0   0
  305 305 305 305   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0]
 [  0   0   0   0   0 503   0 467 467 467 467   0   0   0   0   0   0   0
    0 305 305   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0]
 [434 434 434   0 503 503 503 503   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    0   0   0   0]
 [434 434 434 503 503 503 503 503   0   0   0   0   0   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 263 263
  263   0   0   0]
 [434 434 434   3 503 503 503   0   0   0 257 257 257   0   0   0   0   0
    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 263 263
  263 263   0   0]
 [434 434   3   3   3 503   0   0   0   0 257 257 257 257   0   0   0   0
  327 327 327   0   0   0   0   0   0   0   0   0   0   0  89  89 263 263
  263 263   0   0]
 [  3   3   3   3 362 362 362   0   0   0 257 257 257 257   0   0   0 327
  327 327 327   0   0   0   0   0   0   0   0   0   0  89  89  89  89 263
  263   0   0   0]
 [  3   3   3   3 362 362 362 362   0   0   0 257 257   0   0   0   0 327
  327 327 327   0   0   0   0   0   0   0   0   0   0  89  89  89  89   0
    0   0   0   0]]

PR Checklist

Closing Issues

Fixes #44