Skip to content

Commit ed16323

Browse files
committed
cli: added visualize skip flags
1 parent a6e0615 commit ed16323

File tree

6 files changed

+107
-51
lines changed

6 files changed

+107
-51
lines changed

src/pymcnp/Visualize.py

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def _grid(self):
4949
origin=(-(surfaces.bounds[1] - surfaces.bounds[0]) / 2, -(surfaces.bounds[3] - surfaces.bounds[2]) / 2, -(surfaces.bounds[5] - surfaces.bounds[4]) / 2),
5050
)
5151

52-
def to_show_cells(self) -> pyvista.Plotter:
52+
def to_show_cells(self, skip=tuple()) -> pyvista.Plotter:
5353
"""
54-
Visualizes INP cells.
54+
Visualizes INP all cells.
5555
"""
5656

5757
plot = pyvista.Plotter()
@@ -61,9 +61,9 @@ def to_show_cells(self) -> pyvista.Plotter:
6161
cells = {}
6262

6363
for cell in self.inpt.cells:
64-
if isinstance(cell, inp.Cell):
64+
if isinstance(cell, inp.Cell) and cell.number not in skip:
6565
shape = cell.to_show(surfaces, cells)
66-
elif isinstance(cell, inp.Like):
66+
elif isinstance(cell, inp.Like) and cell.number not in skip:
6767
shape = cells[str(cell.cell)]
6868
else:
6969
continue
@@ -77,16 +77,16 @@ def to_show_cells(self) -> pyvista.Plotter:
7777

7878
return plot
7979

80-
def to_show_surfaces(self) -> pyvista.Plotter:
80+
def to_show_surfaces(self, skip=tuple()) -> pyvista.Plotter:
8181
"""
82-
Visualizes INP surfaces.
82+
Visualizes INP all surfaces.
8383
"""
8484

8585
plot = pyvista.Plotter()
8686
plot.add_axes()
8787

8888
for surface in self.inpt.surfaces:
89-
if not isinstance(surface, inp.Surface):
89+
if not isinstance(surface, inp.Surface) or surface.number in skip:
9090
continue
9191

9292
shape = surface.to_show()
@@ -95,12 +95,12 @@ def to_show_surfaces(self) -> pyvista.Plotter:
9595

9696
return plot
9797

98-
def to_show_cell(self, number: str) -> pyvista.Plotter:
98+
def to_show_cell(self, number: tuple[str]) -> pyvista.Plotter:
9999
"""
100-
Visualizes INP cells.
100+
Visualizes INP cell(s).
101101
102102
Parameters:
103-
number: Cell number to visualize.
103+
numbers: Cell number to visualize.
104104
"""
105105

106106
plot = pyvista.Plotter()
@@ -119,23 +119,19 @@ def to_show_cell(self, number: str) -> pyvista.Plotter:
119119

120120
cells[str(cell.number)] = shape
121121

122-
if str(cell.number) != number:
122+
if str(cell.number) not in number:
123123
continue
124124

125125
grid = self._grid
126126
grid['cell'] = shape.cell(grid.points).astype(numpy.float32)
127127
plot.add_volume(grid, scalars='cell', opacity=[0, 0, 0.01, 0.01])
128128
plot.add_mesh(shape.surface, opacity=0.9)
129129

130-
break
131-
else:
132-
raise errors.CliError(errors.CliCode.RUNTIME_DOER, number)
133-
134130
return plot
135131

136-
def to_show_surface(self, number: str) -> pyvista.Plotter:
132+
def to_show_surface(self, number: tuple[str]) -> pyvista.Plotter:
137133
"""
138-
Visualizes INP surfaces.
134+
Visualizes INP surface(s).
139135
140136
Parameters:
141137
number: Surface number to visualize.
@@ -145,14 +141,13 @@ def to_show_surface(self, number: str) -> pyvista.Plotter:
145141
plot.add_axes()
146142

147143
for surface in self.inpt.surfaces:
148-
if not isinstance(surface, inp.Surface) or str(surface.number) != number:
144+
if not isinstance(surface, inp.Surface):
149145
continue
150146

151-
plot.add_mesh(surface.to_show().surface)
147+
if str(surface.number) not in number:
148+
continue
152149

153-
break
154-
else:
155-
raise errors.CliError(errors.CliCode.RUNTIME_DOER, number)
150+
plot.add_mesh(surface.to_show().surface)
156151

157152
return plot
158153

@@ -181,3 +176,31 @@ def to_pdf_surfaces(self, path: str | pathlib.Path):
181176

182177
if 'PYTEST_CURRENT_TEST' not in os.environ: # pragma: no cover
183178
plot.save_graphic(str(path))
179+
180+
def to_pdf_cell(self, number: tuple[str], path: str | pathlib.Path):
181+
"""
182+
Saves render of cells as PDF.
183+
184+
Parameters:
185+
number: Cell numbers to visualize.
186+
path: Path to new pdf file.
187+
"""
188+
189+
plot = self.to_show_cell(number)
190+
191+
if 'PYTEST_CURRENT_TEST' not in os.environ: # pragma: no cover
192+
plot.save_graphic(str(path))
193+
194+
def to_pdf_surface(self, number: tuple[str], path: str | pathlib.Path):
195+
"""
196+
Saves render of surfaces as PDF.
197+
198+
Parameters:
199+
number: Surface numbers to visualize.
200+
path: Path to new pdf file.
201+
"""
202+
203+
plot = self.to_show_surface(number)
204+
205+
if 'PYTEST_CURRENT_TEST' not in os.environ: # pragma: no cover
206+
plot.save_graphic(str(path))

src/pymcnp/_cli/visualize.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
pymcnp visualize <inp> [ options... ]
44
55
Options:
6-
-c --cells Visualize all cells.
7-
-s --surfaces Visualize all surfaces.
8-
-c --cell=[<number>] Visualize cell.
9-
-s --surface=[<number>] Visualize surface.
10-
--pdf Write PDF.
6+
-c --cells Visualize all cells.
7+
-s --surfaces Visualize all surfaces.
8+
-c --cells-skip=<numbers> Visualize all cells except listed cells.
9+
-s --surfaces-skip=<numbers> Visualize all surfaces excepted listed surfaces.
10+
-c --cell=<numbers> Visualize listed cells.
11+
-s --surface=<numbers> Visualize listed surfaces.
12+
--pdf Write PDF.
1113
"""
1214

1315
import os
@@ -17,7 +19,6 @@
1719

1820
from . import _io
1921
from .. import errors
20-
from .. import inp
2122
from ..Inp import Inp
2223
from ..Visualize import Visualize
2324

@@ -47,11 +48,9 @@ def main() -> None:
4748
_io.error(str(err))
4849
exit(3)
4950

50-
if not (args['--cells'] or args['--surfaces'] or args['--cell'] or args['--surface']):
51+
if not (args['--cells-skip'] or args['--surfaces-skip'] or args['--cells'] or args['--surfaces'] or args['--cell'] or args['--surface']):
5152
args['--cells'] = True
5253
args['--surfaces'] = True
53-
args['--cell'] = [str(cell.number) for cell in inpt.cells if isinstance(cell, inp.Cell)]
54-
args['--surface'] = [str(surface.number) for surface in inpt.surfaces if isinstance(surface, inp.Surface)]
5554

5655
# Visualizing!
5756
try:
@@ -73,26 +72,49 @@ def main() -> None:
7372
if 'PYTEST_CURRENT_TEST' not in os.environ: # pragma: no cover
7473
plot.show()
7574

76-
for number in args['--cell']:
75+
for number in args['--cells-skip']:
76+
number = number.split(',')
77+
7778
if args['--pdf']:
78-
visualize.to_pdf_cells(_io.get_outfile(file, 'pdf', f'cell_{number}'))
79+
visualize.to_pdf_cells(_io.get_outfile(file, 'pdf', 'cells'))
7980
else:
80-
plot = visualize.to_show_cell(number)
81+
plot = visualize.to_show_cells(skip=number)
82+
83+
if 'PYTEST_CURRENT_TEST' not in os.environ: # pragma: no cover
84+
plot.show()
85+
86+
for number in args['--surfaces-skip']:
87+
number = number.split(',')
88+
89+
if args['--pdf']:
90+
visualize.to_pdf_surfaces(_io.get_outfile(file, 'pdf', 'surfaces'))
91+
else:
92+
plot = visualize.to_show_surfaces(skip=number)
8193

8294
if 'PYTEST_CURRENT_TEST' not in os.environ: # pragma: no cover
8395
plot.show()
8496

97+
for number in args['--cell']:
98+
number = number.split(',')
99+
100+
if args['--pdf']:
101+
visualize.to_pdf_cell(number, _io.get_outfile(file, 'pdf', f'cell_{number}'))
102+
else:
103+
plot = visualize.to_show_cell(number)
104+
105+
if 'PYTEST_CURRENT_TEST' not in os.environ: # pragma: no cover
106+
plot.show()
107+
85108
for number in args['--surface']:
109+
number = number.split(',')
110+
86111
if args['--pdf']:
87-
visualize.to_pdf_cells(_io.get_outfile(file, 'pdf', f'surface_{number}'))
112+
visualize.to_pdf_cells(number, _io.get_outfile(file, 'pdf', f'surface_{number}'))
88113
else:
89114
plot = visualize.to_show_surface(number)
90115

91116
if 'PYTEST_CURRENT_TEST' not in os.environ: # pragma: no cover
92117
plot.show()
93-
except errors.CliError as err:
94-
_io.error(str(err))
95-
exit(3)
96118
except errors.TypesError as err:
97119
_io.error(str(err))
98120
exit(3)

src/pymcnp/_show/pyvista/_shape.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def rotate(self, axis: numpy.ndarray, angle: float, center: tuple[float]):
8383
if axis[0] or axis[1] or axis[2]:
8484
axis = axis / numpy.linalg.norm(axis)
8585

86-
return PyvistaShape(self.surface.rotate_vector(vector=(axis[0], axis[1], axis[2]), angle=angle, point=center), lambda p: self.cell(p))
86+
return PyvistaShape(self.surface.rotate_vector(vector=(axis[0], axis[1], axis[2]), angle=angle, point=center), lambda p: self.cell(p)) if angle and not (axis == 0).all() else self
8787

8888
def translate(self, vector: numpy.ndarray):
8989
"""

src/pymcnp/inp/surface/Rcc.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,13 @@ def to_show(self, shapes: _show.Endpoint = _show.pyvista) -> _show.Shape:
345345
h = numpy.array((float(self.hx), float(self.hy), float(self.hz)))
346346

347347
cross = numpy.cross(v, numpy.array((0, 0, 1)))
348-
angle = numpy.degrees(numpy.arccos(v[2] / numpy.linalg.norm(numpy.linalg.norm(v))))
349348

350349
vis = shapes.CylinderCircular(numpy.linalg.norm(h), float(self.r))
351-
vis = vis.rotate(cross, angle, (0, 0, 0))
350+
351+
if not (v == 0).all():
352+
angle = numpy.degrees(numpy.arccos(v[2] / numpy.linalg.norm(numpy.linalg.norm(v))))
353+
vis = vis.rotate(cross, angle, (0, 0, 0))
354+
352355
vis = vis.translate(v)
353356

354357
return vis

tests/test_pymcnp/test_Visualize.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pathlib
22

3-
import pytest
43

54
import pymcnp
65
from .. import consts
@@ -33,19 +32,11 @@ def test_to_show_surface(self):
3332
for example in self.EXAMPLES:
3433
element = self.element(**example)
3534
element.to_show_surface('1')
36-
element.to_show_surface('2')
37-
38-
with pytest.raises(pymcnp.errors.CliError):
39-
element.to_show_surface('13209458743')
4035

4136
def test_to_show_cell(self):
4237
for example in self.EXAMPLES:
4338
element = self.element(**example)
4439
element.to_show_cell('1')
45-
element.to_show_cell('2')
46-
47-
with pytest.raises(pymcnp.errors.CliError):
48-
element.to_show_cell('13209458743')
4940

5041
def test_to_pdf_surfaces(self):
5142
path = pathlib.Path('hello.pdf')
@@ -60,3 +51,17 @@ def test_to_pdf_cells(self):
6051
for example in self.EXAMPLES:
6152
element = self.element(**example)
6253
element.to_pdf_cells(path)
54+
55+
def test_to_pdf_surface(self):
56+
path = pathlib.Path('hello.pdf')
57+
58+
for example in self.EXAMPLES:
59+
element = self.element(**example)
60+
element.to_pdf_surface('1', path)
61+
62+
def test_to_pdf_cell(self):
63+
path = pathlib.Path('hello.pdf')
64+
65+
for example in self.EXAMPLES:
66+
element = self.element(**example)
67+
element.to_pdf_cell('1', path)

tests/test_pymcnp/test__cli/test_visualize.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ def test_valid(self):
1515
os.system(f'pymcnp visualize {pathlib.Path(__file__).parent.parent.parent.parent / "files" / "inp" / "valid_00.inp"} --surface=1')
1616
os.system(f'pymcnp visualize {pathlib.Path(__file__).parent.parent.parent.parent / "files" / "inp" / "valid_00.inp"} --cell=1 --pdf')
1717
os.system(f'pymcnp visualize {pathlib.Path(__file__).parent.parent.parent.parent / "files" / "inp" / "valid_00.inp"} --surface=1 --pdf')
18+
os.system(f'pymcnp visualize {pathlib.Path(__file__).parent.parent.parent.parent / "files" / "inp" / "valid_00.inp"} --cells-skip=1')
19+
os.system(f'pymcnp visualize {pathlib.Path(__file__).parent.parent.parent.parent / "files" / "inp" / "valid_00.inp"} --surfaces-skip=1')
20+
os.system(f'pymcnp visualize {pathlib.Path(__file__).parent.parent.parent.parent / "files" / "inp" / "valid_00.inp"} --cells-skip=1 --pdf')
21+
os.system(f'pymcnp visualize {pathlib.Path(__file__).parent.parent.parent.parent / "files" / "inp" / "valid_00.inp"} --surfaces-skip=1 --pdf')
1822

1923
def test_invalid(self):
2024
os.system(f'pymcnp visualize {pathlib.Path(__file__).parent.parent.parent.parent / "files" / "inp" / "invalid_00.inp"}')
2125
os.system(f'pymcnp visualize {pathlib.Path(__file__).parent.parent.parent.parent / "files" / "inp" / "invalid_01.inp"}')
2226
os.system(f'pymcnp visualize {pathlib.Path(__file__).parent.parent.parent.parent / "files" / "inp" / "invalid_02.inp"}')
2327
os.system(f'pymcnp visualize {pathlib.Path(__file__).parent.parent.parent.parent / "files" / "inp" / "todo_41.inp"}')
24-
os.system(f'pymcnp visualize {pathlib.Path(__file__).parent.parent.parent.parent / "files" / "inp" / "valid_00.inp"} --cell=934587439')
2528
os.system('pymcnp visualize hello')

0 commit comments

Comments
 (0)