# Copyright 2023, 2024 Johannes Reding, Julius Zimmermann
# SPDX-License-Identifier: GPL-3.0-or-later
from dataclasses import dataclass
import ngsolve
[docs]@dataclass
class FieldSolution:
"""Data structure for NGSolve solutions."""
solution: ngsolve.CoefficientFunction
label: str
mesh: ngsolve.comp.Mesh
is_complex: bool
[docs] def save(self, filename: str, subdivision: int = 0) -> None:
"""Save solution to VTK file.
``subdivision`` controls how many times each element is subdivided
before sampling — higher values resolve the higher-order
polynomial basis at the cost of larger files. ``0`` writes only
the corner DOFs and produces visibly faceted plots in ParaView
for HP-refined meshes.
"""
names = [f"{self.label}_real"]
if self.is_complex:
names.append(f"{self.label}_imag")
coefficients = [self.solution.real]
if self.is_complex:
coefficients.append(self.solution.imag)
ngsolve.VTKOutput(
ma=self.mesh,
coefs=coefficients,
names=names,
filename=filename,
subdivision=subdivision,
).Do()