Skip to content

region_parser

converter.converter.fluka.helper_parsers.region_parser

BoolOperation

Bases: Enum

Enum representing boolean operations used in Fluka zones.

Source code in yaptide/converter/converter/fluka/helper_parsers/region_parser.py
13
14
15
16
17
class BoolOperation(Enum):
    """Enum representing boolean operations used in Fluka zones."""

    INTERSECTION = 1
    SUBTRACTION = 2

INTERSECTION class-attribute instance-attribute

INTERSECTION = 1

SUBTRACTION class-attribute instance-attribute

SUBTRACTION = 2

FlukaRegion dataclass

Dataclass mapping for Fluka regions.

Source code in yaptide/converter/converter/fluka/helper_parsers/region_parser.py
20
21
22
23
24
25
26
27
@dataclass(frozen=False)
class FlukaRegion:
    """Dataclass mapping for Fluka regions."""

    name: str = ""
    figures_operators: list[list[tuple[BoolOperation, str]]] = field(
        default_factory=lambda: []
    )

figures_operators class-attribute instance-attribute

figures_operators = field(default_factory=lambda: [])

name class-attribute instance-attribute

name = ''

__init__

__init__(name='', figures_operators=lambda: []())

parse_csg_operations

parse_csg_operations(operations, figures)

Parse dict of csg operations to a list of lists of tuples. Each tuple consists of a figure name and intersection or subtraction operation. Each list of tuples represents a Fluka zone. The list of lists represents a Fluka region, which consists of union of all zones.

Source code in yaptide/converter/converter/fluka/helper_parsers/region_parser.py
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
def parse_csg_operations(
    operations: list[list[dict]], figures: list[FlukaFigure]
) -> list[tuple[BoolOperation, str]]:
    """
    Parse dict of csg operations to a list of lists of tuples.
    Each tuple consists of a figure name and intersection or subtraction operation.
    Each list of tuples represents a Fluka zone.
    The list of lists represents a Fluka region, which consists of union of all zones.
    """
    zones_json = list(operations)
    region = []
    for zone in zones_json:
        operations_list = []
        for operation in zone:
            figure_name = get_figure_name_by_uuid(figures, operation["objectUuid"])
            if figure_name is None:
                raise ValueError(f"Cant find figure of uuid {operation['objectUuid']}")
            if operation["mode"] == "union" or operation["mode"] == "intersection":
                # In JSON received from frontend, first figure added to each zone is marked as a union operation.
                # However Fluka requires each zone to contain at least one figure marked with intersection.
                # This doesn't change the logic, as there is no way to add another union with a figure in the UI
                operations_list.append((BoolOperation.INTERSECTION, figure_name))
            elif operation["mode"] == "subtraction":
                operations_list.append((BoolOperation.SUBTRACTION, figure_name))
            else:
                raise ValueError(f"Unexpected CSG operation: {operation['mode']}")
        region.append(operations_list)

    return region

parse_regions

parse_regions(zones_json, figures)

Parse zones from JSON to Fluka regions. Returns list of regions and list of additional figures generated by parsing world zone.

Source code in yaptide/converter/converter/fluka/helper_parsers/region_parser.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def parse_regions(
    zones_json: dict, figures: list[FlukaFigure]
) -> (list[FlukaRegion], list[FlukaFigure]):
    """
    Parse zones from JSON to Fluka regions.
    Returns list of regions and list of additional figures generated by parsing world zone.
    """
    # Naming is different in Fluka - Fluka zones consist of figures joined by subtractions and intersections
    # Fluka regions consist of zones joined by unions
    # uuid -> FlukaRegion
    regions = {}
    zone_name = "region{}"
    for idx, zone in enumerate(zones_json["zones"]):
        regions[zone["uuid"]] = FlukaRegion(
            name=zone_name.format(idx),
            figures_operators=parse_csg_operations(zone["unionOperations"], figures),
        )
    if "worldZone" in zones_json:
        world_region, boundary_region, world_figure, world_boundary = parse_world_zone(
            zones_json, figures
        )
        regions[zones_json["worldZone"]["uuid"]] = world_region
        regions[zones_json["worldZone"]["uuid"] + "boundary"] = boundary_region

    return regions, [world_figure, world_boundary]

parse_world_zone

parse_world_zone(zones_json, figures)

Parse the world zone. Returns tuple consisting of world region, boundary region and the two figures of which they consist.

Source code in yaptide/converter/converter/fluka/helper_parsers/region_parser.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def parse_world_zone(
    zones_json: dict, figures: list[FlukaFigure]
) -> (FlukaRegion, FlukaFigure, FlukaFigure):
    """
    Parse the world zone.
    Returns tuple consisting of world region, boundary region and the two figures of which they consist.
    """
    # Parse the world figure, then create boundary figure by expanding it
    # The boundary will have the black hole material
    world_zone_json = zones_json["worldZone"]
    world_figure = solid_figures.parse_figure(world_zone_json)
    world_boundary = copy.deepcopy(world_figure)
    world_boundary.expand(10)

    fluka_world_figure = parse_fluka_figure(world_figure)
    fluka_world_figure.name = "figworld"
    fluka_world_boundary = parse_fluka_figure(world_boundary)
    fluka_world_boundary.name = "figbound"

    # The boundary region consists of boundary figure with world figure subtracted
    boundary_region = FlukaRegion(
        name="boundary",
        figures_operators=[
            [
                (BoolOperation.INTERSECTION, fluka_world_boundary.name),
                (BoolOperation.SUBTRACTION, fluka_world_figure.name),
            ]
        ],
    )

    # Subtract all other figures from world region
    world_operations = [(BoolOperation.INTERSECTION, fluka_world_figure.name)]
    for figure in figures:
        world_operations.append((BoolOperation.SUBTRACTION, figure.name))

    # The world region consists of world figure with all other figures subtracted
    world_region = FlukaRegion(
        name="world",
        figures_operators=[world_operations],
    )

    return world_region, boundary_region, fluka_world_figure, fluka_world_boundary