# Plotting Geometry Problem

**URL:** <https://openmc.discourse.group/t/plotting-geometry-problem/3975>\
**Category:** Validation and Verification\
**Created:** [February 28, 2024, 9:51am UTC](https://openmc.discourse.group/t/plotting-geometry-problem/3975 "2024-02-28T09:51:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hkumi](https://avatars.discourse-cdn.com/v4/letter/h/85f322/32.png) [@hkumi](https://openmc.discourse.group/u/hkumi)\
**Post date:** [February 28, 2024, 9:51am UTC](https://openmc.discourse.group/t/plotting-geometry-problem/3975/1 "2024-02-28T09:51:12Z")

</div>

Hello everyone,

I am new openmc. I want to make a simple geometry of a wall filled with rock with thickness 0.5m. I have created the geometry following the examples. I tried to plot the geometry and I just see a 2D image which is not very clear to me. I was expecting the wall in 3D. this is my code and the output. how can I visualise my geometry well. thank you all.  
“”"  
import openmc  
import numpy as np  
import openmc.data  
import matplotlib.pyplot as plt

import os

os.environ[“OPENMC\_CROSS\_SECTIONS”] = “/home/harriet/OPENMC/mcnp\_endfb70/cross\_sections.xml”

print(os.environ[“OPENMC\_CROSS\_SECTIONS”])

###############################################################################

# Simulation Input File Parameters

###############################################################################

# OpenMC simulation parameters

batches = 20  
inactive = 10  
particles = 10000

###############################################################################

# Exporting to OpenMC materials.xml file

###############################################################################

# Create materials

rock = openmc.Material(material\_id=1, name=‘Rock’)  
rock.add\_element(‘Si’, 0.277)  
rock.add\_nuclide(‘O16’, 0.622)  
rock.add\_element(‘Al’, 0.101)  
rock.add\_element(‘Fe’,1.0)

rock.set\_density(‘g/cm3’, 2.65)

water = openmc.Material(material\_id=2,name=‘Water’)  
water.add\_element(‘H’, 2)  
water.add\_nuclide(‘O16’, 1)  
water.set\_density(‘g/cm3’, 1.0)  
water.add\_s\_alpha\_beta(‘c\_H\_in\_H2O’)

# Instantiate a Materials collection and export to XML

materials\_file = openmc.Materials([water, rock])  
materials\_file.export\_to\_xml()

###############################################################################

# Exporting to OpenMC geometry.xml file

###############################################################################

# Create surfaces

wall\_thickness = 0.5  
water\_thickness = 0.5  
wall\_length= 1.5

# Create surfaces for rock wall

left\_surface\_rock = openmc.XPlane(surface\_id=1,x0=-wall\_length/2, boundary\_type=‘vacuum’)  
right\_surface\_rock = openmc.XPlane(surface\_id=2,x0=wall\_length/2, boundary\_type=‘vacuum’)  
bottom\_surface\_rock = openmc.YPlane(surface\_id=3,y0=-wall\_length/2, boundary\_type=‘vacuum’)  
top\_surface\_rock = openmc.YPlane(surface\_id=4,y0=wall\_length/2, boundary\_type=‘vacuum’)  
front\_surface\_rock = openmc.ZPlane(surface\_id=5,z0=-wall\_thickness/2, boundary\_type=‘vacuum’)  
back\_surface\_rock = openmc.ZPlane(surface\_id=6,z0=wall\_thickness/2, boundary\_type=‘vacuum’)

# Instantiate Cells

cell1 = openmc.Cell(cell\_id=1, name=‘Cell 1’)  
cell1.region = +left\_surface\_rock & -right\_surface\_rock & +bottom\_surface\_rock & -top\_surface\_rock & +front\_surface\_rock & -back\_surface\_rock

# Register Materials with Cells

cell1.fill = rock

# Create universe

root = openmc.Universe(universe\_id=0, name=‘root universe’)  
root.add\_cell(cell1)

# Instantiate a Geometry, register the root Universe, and export to XML

geometry = openmc.Geometry(root)  
geometry.export\_to\_xml()

plot=openmc.Plot()  
plot.geometry=geometry  
openmc.plot\_inline(plot)  
“”

 ![plot_1](https://global.discourse-cdn.com/free1/uploads/openmc/original/2X/2/285753794872597ce20a6f12ce8208d83496fdd7.png)

---

<div class="post-metadata">

**Author:** ![Shimwell](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/shimwell/32/3876_2.png) [@Shimwell](https://openmc.discourse.group/u/Shimwell)\
**Post date:** [February 28, 2024, 12:16pm UTC](https://openmc.discourse.group/t/plotting-geometry-problem/3975/2 "2024-02-28T12:16:10Z")

</div>

The plot() method plots a slice through the geometry. The geometry is 3D but a slice through the geometry gives a 2D image.

There are ways of making a 3D view using the voxel plot method  
[https://docs.openmc.org/en/latest/usersguide/plots.html#voxel-plots](https://docs.openmc.org/en/latest/usersguide/plots.html#voxel-plots)

There is another 3D plotting method in development that might also be of interest

> <https://github.com/openmc-dev/openmc/pull/2655>
>
> \# Description
> 
> This PR implements 3D plotting with Phong shading. I factored o…ut the basic ray creation from a camera into an abstract base class called \`RayTracePlot\`. Then, \`ProjectionPlot\` implements the logic for moving rays all the way through a geometry in order to wireframe cells or materials of interest and color them in an x-ray-like view. The new \`PhongPlot\` (happy to rename) lets the user specify a few materials or cells which they would like to have be opaque, with everything else in the model being transparent. By default it assumes that a light is placed at the camera's location, but the light can optionally be moved.
> 
> As an example, here is a reminder of what a \`ProjectionPlot\` looks like:
> 
> !\[orthographic\_example1\](https://github.com/openmc-dev/openmc/assets/18088906/425b889d-d9ab-48c1-b191-4dd22993263a)
> 
> A Phong plot on a similar geometry looks kinda neat, and could certainly be a helpful tool for visualization on intricately structured stuff--especially for communicating what a model looks like to other people.
> 
> !\[phong\](https://github.com/openmc-dev/openmc/assets/18088906/f5f713e2-0529-4525-b911-47a62a298947)
> 
> You can adjust the fraction of diffuse light to give a slightly different look. By default, it uses 10% diffuse lighting, 90% lighting from the light source with single scattered isotropic scattering off surfaces.
> 
> !\[phong\_diffuse\](https://github.com/openmc-dev/openmc/assets/18088906/7bc859c1-2546-49cd-865b-fcfb5b7687a7)
> 
> You can move the light source location, which can make a nice dramatic effect:
> 
> !\[phong\_move\_light\](https://github.com/openmc-dev/openmc/assets/18088906/21e391b9-cec7-472c-9333-48bcbe44c989)
> 
> I was frustrated that we have to create a full \`Particle\` object just for geometric operations. As a result, I factored out the geometry state of \`Particle\` into a base class called \`Geometron\`. This comes in handy for \`openmc\_find\_cell\` as we no longer have to initialize the cross section caches and all that other physics cache data just to do a cell search. Similarly the \`Geometron\` is instead used for ray tracing in the plotter, now. It could also be used for the implementation of the random ray method which I hear is coming along. My last note on the \`Geometron\` is that all the geometric operations take \`Geometron\` references or pointers, not \`Particle\`. Of course, simply passing a \`Particle\` pointer works as usual. ~~This does not invoke any polymorphism, so there will be zero performance overhead as a result of this, or perhaps even performance gains since the compiler will know that only a smaller subset of the data can potentially be accessed.~~ Polymorphism is used for error handling in geometric routines. \`Geometron\`s \`fatal\_error\` on erroneous leakage from the model, but \`Particle\`s save the leaked ID and then the simulation continues.
> 
> ~~In this direction, there was one larger change I had to make for error handling in geometry. It seems that raising an exception is the right thing to do here, with calling code defining the exception handling. Code in \`Particle\` now will resort to \`mark\_as\_lost\` for lost stuff, but in the plotter, you just get a plain old exception that stops the program. Previously it would just say "particle with ID=-1 leaked" or something like that. For MOC or other things like that, of course you would just want the program to exit if you leak a ray, so I hope other people like this error handling approach in the geometry now. Also, the compiler knows that exceptions happen with low frequency, so there might be a slight performance gain from the improved branch prediction in those few places where we previously had just called \`mark\_as\_lost\`.~~
> 
> Lastly, in the \`ParticleData\` class, I moved the comments to the setters/getters rather than on the private variables, since this is the public-facing API that people would actually want to read.
> 
> There are a few other things I want to do before merging this. It is a WIP. Of course I need to add the new plot and its options to the python API. Really not looking forward to the XML boilerplate. Also I want to factor the plot ray tracer into its own RayKernel-object. I'm thinking it'd be nice to create a ray that just has some lambdas attached to it like \`on\_intersection\_\` or \`on\_cross\_surface\_\` to avoid repeating it in \`PhongPlot\` and \`ProjectionPlot\`. Of course this could help with parallelization and could be used in a random ray implementation. Lastly I'd like to add a 3D version of \`Universe.plot\` that automatically picks a camera position based on the bounding box size and some simple position argument like "top left" or "front left".
> 
> ~~Also, I need to figure out how to rotate surface normals in transformed nested universes in order to get the direction to the light in the root universe. That's going to be a fun one.~~
> 
> Oh wait... there's one more thing. The ProjectionPlot camera rays were not uniform in pixel space, which did not create distortion effects for things that are far away, but are distorted for things up close. The old plotter would make straight surfaces appear curved, e.g:
> 
> !\[example1\](https://github.com/openmc-dev/openmc/assets/18088906/e5dca313-3eba-49c2-976e-facf8048d80f)
> 
> With this PR, it correctly maps pixels onto ray directions, making it look a lot better:
> 
> !\[example1\](https://github.com/openmc-dev/openmc/assets/18088906/66d20e39-969e-442d-8ca3-b65aaebeea09)
> 
> \# Checklist
> 
> \- \[x\] I have performed a self-review of my own code
> \- \[x\] I have run \[clang-format\](https://docs.openmc.org/en/latest/devguide/styleguide.html#automatic-formatting) on any C++ source files (if applicable)
> \- \[x\] I have followed the \[style guidelines\](https://docs.openmc.org/en/latest/devguide/styleguide.html#python) for Python source files (if applicable)
> \- \[x\] I have made corresponding changes to the documentation (if applicable)
> \- \[x\] I have added tests that prove my fix is effective or that my feature works (if applicable)
> \- \[x\] Add PhongPlot to python API
> \- \[x\] Factor out ray tracing logic into RayTracePlot (it is duplicated right now)
> \- \[\] ~~Add Universe \`plot3d\` method for easily obtained 3D visualization in ipython notebooks~~
> \- \[\] ~~Plot a slice of the BEAVRS core with pins and core structure shown as solids.~~
> \- \[x\] Make it work with rotated/translated subuniverses

---

<div class="post-metadata">

**Author:** ![hkumi](https://avatars.discourse-cdn.com/v4/letter/h/85f322/32.png) [@hkumi](https://openmc.discourse.group/u/hkumi)\
**Post date:** [February 28, 2024, 12:43pm UTC](https://openmc.discourse.group/t/plotting-geometry-problem/3975/3 "2024-02-28T12:43:32Z")

</div>

thank you for the response. I have followed the manual. I have this in my code but the geometry is not showing is there something I did wrong. I do not get any errors though. vox\_plot = openmc.Plot(plot\_id=2)

vox\_plot.type = ‘voxel’

vox\_plot.width = [100., 100.,50.]

vox\_plot.pixels = [400, 400,200]

vox\_plot.color\_by = ‘material’

# Instantiate a Plots collection and export to XML

plot = openmc.Plots([vox\_plot])

vox\_plot.geometry=geometry
