Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NEUTRAL_DENSITY #73

Merged
merged 5 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:
- name: "Upload coverage to Codecov"
uses: codecov/codecov-action@v1
with:
fail_ci_if_error: true
fail_ci_if_error: false
34 changes: 31 additions & 3 deletions xemc3/core/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,7 @@ def read_mapped(
skip_first: int = 0,
ignore_broken: bool = False,
kinetic: bool = False,
unmapped: bool = False,
dtype: DTypeLike = float,
squeeze: bool = True,
) -> typing.Sequence[xr.DataArray]:
Expand All @@ -1033,6 +1034,9 @@ def read_mapped(
kinetic : bool (optional)
The file contains also data for cells that are only evolved by
EIRENE, rather then EMC3. Default: False
unmapped : bool (optional)
The file contains unmapped data, i.e. on value for each cell.
Default: False
dtype : datatype (optional)
The type of the data, e.g. float or int. Is passed to
numpy. Default: float
Expand All @@ -1052,7 +1056,10 @@ def read_mapped(
mapping = ensure_mapping(_dir_of(fn), mapping, fn=fn)
mapping = mapping["_plasma_map"]
if kinetic:
assert unmapped == False
max = np.max(mapping.data) + 1
elif unmapped:
max = mapping.attrs["numcells"]
else:
max = mapping.attrs["plasmacells"]
firsts = []
Expand All @@ -1061,7 +1068,11 @@ def read_mapped(
while True:
if skip_first:
first = ""
for _ in range(skip_first):
if isinstance(skip_first, int):
sf = skip_first
else:
sf = skip_first[min(len(raws), len(skip_first) - 1)]
for _ in range(sf):
first += f.readline()
firsts.append(first)
raw = _fromfile(f, dtype=dtype, count=max, sep=" ")
Expand All @@ -1081,7 +1092,7 @@ def read_mapped(
f"Incomplete dataset found ({len(raw)} out of {max}) after reading {len(raws)} datasets of file {fn}"
)

def to_da(raw):
def to_da_mapped(raw):
out = np.ones(mapping.shape) * np.nan
mapdat = mapping.data
for ijk in rrange(mapping.shape):
Expand All @@ -1090,6 +1101,10 @@ def to_da(raw):
out[ijk] = raw[mapid]
return xr.DataArray(data=out, dims=mapping.dims)

def to_da_unmapped(raw):
return xr.DataArray(data=raw.reshape(mapping.shape), dims=mapping.dims)

to_da = to_da_unmapped if unmapped else to_da_mapped
das = [to_da(raw) for raw in raws]
if skip_first:
for first, da in zip(firsts, das):
Expand Down Expand Up @@ -1272,6 +1287,7 @@ def write_mapped(
skip_first=0,
ignore_broken=False,
kinetic=False,
unmapped=False,
dtype=None,
fmt=None,
):
Expand All @@ -1293,6 +1309,8 @@ def write_mapped(
ignored.
kinetic : bool
If true the data is defined also outside of the plasma region.
unmapped : bool
If true the data is not mapped
dtype : any
if not None, it needs to match the dtype of the data
fmt : None or str
Expand All @@ -1311,7 +1329,11 @@ def write_mapped(
assert d.attrs["print_before"] == ""
if not isinstance(datas, (list, tuple)):
datas = [datas]
out = [to_mapped(x, mapping, kinetic, dtype) for x in datas]
if unmapped:
assert kinetic == False
out = [np.ravel(x) for x in datas]
else:
out = [to_mapped(x, mapping, kinetic, dtype) for x in datas]
with open(fn, "w") as f:
for i, da in zip(out, datas):
if "print_before" in da.attrs:
Expand Down Expand Up @@ -1451,6 +1473,12 @@ def write_info_file(fn: str, ds: xr.Dataset) -> None:
kinetic=True,
vars={"DENSITY_E_M_%d": dict()},
),
"NEUTRAL_DENSITY": dict(
type="mapped",
skip_first=[3, 2],
unmapped=True,
vars={"NEUTRAL_DENSITY_%d": dict()},
),
"fort.1": dict(
type="raw",
vars={"fort.1": dict(long_name="Geometry input file")},
Expand Down
15 changes: 11 additions & 4 deletions xemc3/test/gen_ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,26 @@ def get_attrs(vsv):
pre = load.files[f].get("skip_first", 0)
dtype = load.files[f].get("dtype", float)

def add_pre(ds, k, pre, i):
if pre:
if isinstance(pre, int):
pret = pre
else:
pret = pre[min(len(pre) - 1, i)]
ds[k].attrs["print_before"] = (" %d\n" % i) * pret
return ds

if "%" in v:
for i in range(i, i + ids):
ds[v % i] = genf(ds, index=i)
if dtype != float and genf != gen_depo:
ds[v % i] = genf(ds)[0], np.round(genf(ds)[1] * 20)
ds[v % i].attrs.update(get_attrs(vs[v]))
if pre:
ds[v % i].attrs["print_before"] = " %d\n" % i
ds = add_pre(ds, v % i, pre, i)
else:
ds[v] = genf(ds, index=i)
ds[v].attrs.update(get_attrs(vs[v]))
if pre:
ds[v].attrs["print_before"] = " %d\n" % i
ds = add_pre(ds, v, pre, i)
if genf == gen_depo and i == 0:
ds[v].attrs["description"] = "True means +1, False means -1"

Expand Down