Skip to content

Interpolation

superfish.interpolate

Classes

Functions:

superfish.interpolate.get_t7
get_t7(path)

Find T7 files in a directory.

Parameters:

Name Type Description Default
path str

Directory to search.

required

Returns:

Type Description
list of str

Paths of the T7 files found.

Source code in superfish/interpolate.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def get_t7(path: str) -> list[str]:
    """
    Find T7 files in a directory.

    Parameters
    ----------
    path : str
        Directory to search.

    Returns
    -------
    list of str
        Paths of the T7 files found.
    """
    return glob(os.path.join(path, "*T7"))
superfish.interpolate.interpolate2d
interpolate2d(
    sf: Superfish,
    zmin: float = ...,
    zmax: float = ...,
    nz: int = ...,
    rmin: float = ...,
    rmax: float = ...,
    nr: int = ...,
    return_fieldmesh: Literal[False] = ...,
) -> FishT7Data | PoissonT7Data
interpolate2d(
    sf: Superfish,
    zmin: float = ...,
    zmax: float = ...,
    nz: int = ...,
    rmin: float = ...,
    rmax: float = ...,
    nr: int = ...,
    *,
    return_fieldmesh: Literal[True],
) -> FieldMesh
interpolate2d(sf, zmin=-1000, zmax=1000, nz=100, rmin=0, rmax=0, nr=1, return_fieldmesh=False)

Interpolate the solved field onto a grid using SF7.

Runs SF7 on a Superfish object, requesting interpolating data, and reads the resulting Parmela T7 file.

SF7 automatically adjusts the bounds if they are requested outside of the computational domain.

TODO: detect the column labels from the SF object

Parameters:

Name Type Description Default
sf Superfish

Superfish object that has been run.

required
zmin float

z extent of the grid, in the input units of the program.

-1000
zmax float

z extent of the grid, in the input units of the program.

-1000
nz int

Number of z points.

100
rmin float

Radial extent of the grid, in the input units of the program.

0
rmax float

Radial extent of the grid, in the input units of the program.

0
nr int

Number of radius points.

1
return_fieldmesh bool

Return an openPMD-beamphysics FieldMesh instead of a t7data dict.

False

Returns:

Type Description
FishT7Data or PoissonT7Data or FieldMesh

If return_fieldmesh is False, a t7data dict with keys:

  • rmin, rmax : float, radial extent in cm.
  • nr : int, number of radius points.
  • zmin, zmax : float, z extent in cm.
  • nz : int, number of z points.
  • freq : float, frequency in MHz (fish problems).
  • field components : ndarray of shape (nr, nz).

Otherwise, an openPMD-beamphysics FieldMesh.

Source code in superfish/interpolate.py
 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
 99
100
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
def interpolate2d(
    sf: "Superfish",
    zmin: float = -1000,
    zmax: float = 1000,
    nz: int = 100,
    rmin: float = 0,
    rmax: float = 0,
    nr: int = 1,
    return_fieldmesh: bool = False,
) -> FishT7Data | PoissonT7Data | FieldMesh:
    """
    Interpolate the solved field onto a grid using SF7.

    Runs SF7 on a Superfish object, requesting interpolating data, and
    reads the resulting Parmela T7 file.

    SF7 automatically adjusts the bounds if they are requested outside of
    the computational domain.

    TODO: detect the column labels from the SF object

    Parameters
    ----------
    sf : Superfish
        Superfish object that has been run.
    zmin, zmax : float
        z extent of the grid, in the input units of the program.
    nz : int
        Number of z points.
    rmin, rmax : float
        Radial extent of the grid, in the input units of the program.
    nr : int
        Number of radius points.
    return_fieldmesh : bool
        Return an openPMD-beamphysics FieldMesh instead of a t7data dict.

    Returns
    -------
    FishT7Data or PoissonT7Data or FieldMesh
        If ``return_fieldmesh`` is False, a t7data dict with keys:

        - ``rmin``, ``rmax`` : float, radial extent in cm.
        - ``nr`` : int, number of radius points.
        - ``zmin``, ``zmax`` : float, z extent in cm.
        - ``nz`` : int, number of z points.
        - ``freq`` : float, frequency in MHz (fish problems).
        - field components : ndarray of shape (nr, nz).

        Otherwise, an openPMD-beamphysics FieldMesh.
    """

    problem = sf.problem

    # fish and poisson have the opposite conventions:
    if problem == "fish":
        # The interpolation grid
        F = f"""Parmela
{zmin} {rmin} {zmax} {rmax}
{nz - 1} {nr - 1}
End"""

    elif problem == "poisson":
        F = f"""Parmela
{rmin} {zmin} {rmax} {zmax}
{nr - 1} {nz - 1}
End"""
    else:
        raise ValueError(f"unknowm problem: {problem}")

    # Clear old T7
    for f in get_t7(sf.path):
        os.remove(f)

    # Write
    ifile = sf.basename + ".IN7"
    with open(os.path.join(sf.path, ifile), "w") as f:
        f.write(F)

    # Needed so that the fields aren't normalized to 1 MV/m average
    inifile = os.path.join(sf.path, "SF.INI")
    with open(inifile, "w") as f:
        f.write("""[global]
Force1MVperMeter=No""")

    # Needed on WSL, otherwise optional
    t35file = sf.basename + ".T35"

    # Run
    sf.run_cmd("sf7", ifile, t35file)

    # Get the filename
    t7file = get_t7(sf.path)
    assert len(t7file) == 1, "T7 file is missing."
    t7file = t7file[0]

    # Optional fieldmesh parsing
    if return_fieldmesh:
        # Parsing is different for each:
        if problem == "fish":
            type = "electric"
            return FieldMesh.from_superfish(t7file)
        else:
            if sf.output["sfo"]["header"]["variable"]["XJFACT"] == 0:
                type = "electric"
            else:
                type = "magnetic"
            return FieldMesh.from_superfish(t7file, type=type)

    # Parsing is different for each:
    if problem == "fish":
        type = "electric"
        d = parse_fish_t7(t7file)
    else:
        if sf.output["sfo"]["header"]["variable"]["XJFACT"] == 0:
            type = "electric"
        else:
            type = "magnetic"
        d = parse_poisson_t7(t7file, type=type)

    return d