Skip to content

Commit

Permalink
Separate _create property functions, add doc= (thanks @ken-lauer )
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherMayes committed Oct 23, 2024
1 parent 5bd31c1 commit 923467c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 77 deletions.
45 changes: 5 additions & 40 deletions docs/examples/fields/field_examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -889,14 +889,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2024-10-17T23:18:50.442628Z",
"iopub.status.busy": "2024-10-17T23:18:50.442530Z",
"iopub.status.idle": "2024-10-17T23:18:50.451459Z",
"shell.execute_reply": "2024-10-17T23:18:50.451257Z"
}
},
"metadata": {},
"outputs": [],
"source": [
"FM3D = FieldMesh.from_ansys_ascii_3d(\n",
Expand All @@ -912,14 +905,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2024-10-17T23:18:50.452610Z",
"iopub.status.busy": "2024-10-17T23:18:50.452525Z",
"iopub.status.idle": "2024-10-17T23:18:50.454832Z",
"shell.execute_reply": "2024-10-17T23:18:50.454618Z"
}
},
"metadata": {},
"outputs": [],
"source": [
"FM3D.attrs"
Expand All @@ -935,14 +921,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2024-10-17T23:18:50.456002Z",
"iopub.status.busy": "2024-10-17T23:18:50.455927Z",
"iopub.status.idle": "2024-10-17T23:18:50.461253Z",
"shell.execute_reply": "2024-10-17T23:18:50.461050Z"
}
},
"metadata": {},
"outputs": [],
"source": [
"FM3D.write(\"../data/rfgun_rectangular.h5\")"
Expand All @@ -958,14 +937,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2024-10-17T23:18:50.462395Z",
"iopub.status.busy": "2024-10-17T23:18:50.462304Z",
"iopub.status.idle": "2024-10-17T23:18:50.464240Z",
"shell.execute_reply": "2024-10-17T23:18:50.464026Z"
}
},
"metadata": {},
"outputs": [],
"source": [
"FM2D = FM3D.to_cylindrical()\n",
Expand All @@ -982,14 +954,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2024-10-17T23:18:50.465256Z",
"iopub.status.busy": "2024-10-17T23:18:50.465160Z",
"iopub.status.idle": "2024-10-17T23:18:50.467295Z",
"shell.execute_reply": "2024-10-17T23:18:50.467076Z"
}
},
"metadata": {},
"outputs": [],
"source": [
"import os\n",
Expand Down
81 changes: 44 additions & 37 deletions pmd_beamphysics/fields/fieldmesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,50 @@
}


def _create_delta_property(name):
def getter(self):
return self.deltas[self.axis_index(name)]

return property(getter, doc=f"Mesh spacing in {name}")


def _create_max_property(name):
def getter(self):
return self.maxs[self.axis_index(name)]

def setter(self, value):
# Setting the max => shift the min
i = self.axis_index(name)
mins = list(self.attrs["gridOriginOffset"])
mins[i] = mins[i] + float(value) - self.maxs[i]
self.attrs["gridOriginOffset"] = tuple(mins)

return property(
getter, setter, doc=f"Mesh maximum in {name}. This can also be set."
)


def _create_min_property(name):
def getter(self):
return self.mins[self.axis_index(name)]

def setter(self, value):
mins = list(self.attrs["gridOriginOffset"])
mins[self.axis_index(name)] = float(value)
self.attrs["gridOriginOffset"] = tuple(mins)

return property(
getter, setter, doc=f"Mesh minimim in {name}. This can also be set."
)


def _create_scaled_component_property(name):
def getter(self):
return self.scaled_component(name)

return property(getter, doc=f"Scaled 3D mesh for {name} in {pg_units(name)}")


class FieldMesh:
"""
Class for openPMD External Field Mesh data.
Expand Down Expand Up @@ -833,32 +877,13 @@ def z(self):
return self.coord_vec("z")

# Deltas
def _create_delta_property(name):
def getter(self):
return self.deltas[self.axis_index(name)]

return property(getter)

dx = _create_delta_property("x")
dy = _create_delta_property("y")
dz = _create_delta_property("z")
dr = _create_delta_property("r")
dtheta = _create_delta_property("theta")

# Maxs
def _create_max_property(name):
def getter(self):
return self.maxs[self.axis_index(name)]

def setter(self, value):
# Setting the max => shift the min
i = self.axis_index(name)
mins = list(self.attrs["gridOriginOffset"])
mins[i] = mins[i] + float(value) - self.maxs[i]
self.attrs["gridOriginOffset"] = tuple(mins)

return property(getter, setter)

# Create max properties dynamically
xmax = _create_max_property("x")
ymax = _create_max_property("y")
Expand All @@ -867,17 +892,6 @@ def setter(self, value):
thetamax = _create_max_property("theta")

# Mins
def _create_min_property(name):
def getter(self):
return self.mins[self.axis_index(name)]

def setter(self, value):
mins = list(self.attrs["gridOriginOffset"])
mins[self.axis_index(name)] = float(value)
self.attrs["gridOriginOffset"] = tuple(mins)

return property(getter, setter)

# Create min properties dynamically
xmin = _create_min_property("x")
ymin = _create_min_property("y")
Expand All @@ -886,13 +900,6 @@ def setter(self, value):
thetamin = _create_min_property("theta")

# Scaled components
# TODO: Check geometry
def _create_scaled_component_property(name):
def getter(self):
return self.scaled_component(name)

return property(getter)

# Dynamically create scaled properties
Bx = _create_scaled_component_property("Bx")
By = _create_scaled_component_property("By")
Expand Down

0 comments on commit 923467c

Please sign in to comment.