Skip to content

Commit

Permalink
Merge pull request #45 from bendudson/black-format
Browse files Browse the repository at this point in the history
Black format
  • Loading branch information
bendudson committed Jan 29, 2021
2 parents 6183174 + 179e1dc commit 84fced4
Show file tree
Hide file tree
Showing 40 changed files with 4,656 additions and 2,763 deletions.
441 changes: 250 additions & 191 deletions freegs/_aeqdsk.py

Large diffs are not rendered by default.

34 changes: 19 additions & 15 deletions freegs/_divgeo.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from ._fileutils import f2s, ChunkOutput, write_1d, write_2d, next_value


def write(data, fh, label=None):
"""
Write a DivGeo file, given a dictionary of data
Expand All @@ -48,7 +49,8 @@ def write(data, fh, label=None):
"""

# Write header
fh.write(""" jm := no. of grid points in radial direction;
fh.write(
""" jm := no. of grid points in radial direction;
km := no. of grid points in vertical direction;
r := radial coordinates of grid points [m];
z := vertical coordinates of grid points [m];
Expand All @@ -64,33 +66,36 @@ def write(data, fh, label=None):
btf = {bcentr:1.14f} t;
rtf = {rcentr:1.14f} m;
""".format(**data))

""".format(
**data
)
)

try:
r = data["r"]
except KeyError:
# No "r" in the dictionary
# use rdim, rleft and nx (from eqdsk)

Rmin = data["rleft"]
Rmax = data["rleft"] + data["rdim"]
nx = data["nx"]

dR = (Rmax - Rmin)/(nx - 1)
r = np.arange(nx)*dR + Rmin
dR = (Rmax - Rmin) / (nx - 1)
r = np.arange(nx) * dR + Rmin

try:
z = data["z"]
except KeyError:
# No "z" in the dictionary
# use zdim, zmid and ny (from eqdsk)
Zmin = data["zmid"] - 0.5*data["zdim"]
Zmax = data["zmid"] + 0.5*data["zdim"]

Zmin = data["zmid"] - 0.5 * data["zdim"]
Zmax = data["zmid"] + 0.5 * data["zdim"]
ny = data["ny"]

dZ = (Zmax - Zmin)/(ny - 1)
z = np.arange(ny)*dZ + Zmin
dZ = (Zmax - Zmin) / (ny - 1)
z = np.arange(ny) * dZ + Zmin

# Now write r and z
fh.write(" r(1:jm);\n")
Expand All @@ -101,9 +106,8 @@ def write(data, fh, label=None):
fh.write(" \n z(1:km);\n")
write_1d(z, co)
co.newline()

fh.write(" \n ((psi(j,k)-psib,j=1,jm),k=1,km)\n")
write_2d( data["psi"] - data["sibdry"], co )

write_2d(data["psi"] - data["sibdry"], co)
co.newline()

43 changes: 23 additions & 20 deletions freegs/_fileutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import re


def f2s(f):
"""
Format a string containing a float
Expand All @@ -14,11 +15,13 @@ def f2s(f):
s += " "
return s + "%1.9E" % f


class ChunkOutput:
"""
This outputs values in lines, inserting
newlines when needed.
"""

def __init__(self, filehandle, chunksize=5, extraspaces=0):
"""
filehandle output to write to
Expand All @@ -31,27 +34,27 @@ def __init__(self, filehandle, chunksize=5, extraspaces=0):
self.extraspaces = extraspaces

def write(self, value):
""""
""" "
Write a value to the output, adding a newline if needed
Distinguishes between:
- list : Iterates over the list and writes each element
- int : Converts using str
- float : Converts using f2s to Fortran-formatted string
"""
if isinstance(value, list):
for elt in value:
self.write(elt)
return
self.fh.write(" "*self.extraspaces)

self.fh.write(" " * self.extraspaces)

if isinstance(value, int):
self.fh.write(" " + str(value))
else:
self.fh.write(f2s(value))

self.counter += 1
if self.counter == self.chunk:
self.fh.write("\n")
Expand All @@ -70,54 +73,54 @@ def endblock(self):
Make sure next block of data is on new line
"""
self.fh.write("\n")
self.counter=0
self.counter = 0

def __enter__(self):
return self

def __exit__(self, type, value, traceback):
"""Ensure that the chunk finishes with a new line
"""
"""Ensure that the chunk finishes with a new line"""
if self.counter != 0:
self.counter = 0
self.fh.write("\n")



def write_1d(val, out):
"""
Writes a 1D variable val to the file handle out
"""
for i in range(len(val)):
out.write(val[i])
out.newline()


def write_2d(val, out):
"""
Writes a 2D array. Note that this transposes
the array, looping over the first index fastest
"""
nx,ny = val.shape
nx, ny = val.shape
for y in range(ny):
for x in range(nx):
out.write(val[x,y])
out.write(val[x, y])
out.newline()


def next_value(fh):
"""
A generator which yields values from a file handle
Checks if the value is a float or int, returning
the correct type depending on if '.' is in the string
"""
pattern = re.compile(r"[ +\-]?\d+(?:\.\d+(?:[Ee][\+\-]\d\d)?)?")

# Go through each line, extract values, then yield them one by one
for line in fh:
matches = pattern.findall(line)
for match in matches:
if '.' in match:
if "." in match:
yield float(match)
else:
yield int(match)

0 comments on commit 84fced4

Please sign in to comment.