Skip to content

Commit

Permalink
[FEM] Elmer: refactor writer
Browse files Browse the repository at this point in the history
- the writer.py is to large to keep the overview, thus sort out the handling of the different equations (also since more equations will be added)
- This PR sorts out the electrostatic equation handling is the test balloon if this works well (local tests are all OK)
  • Loading branch information
donovaly committed Feb 5, 2023
1 parent 82b6e03 commit b7a02f0
Show file tree
Hide file tree
Showing 3 changed files with 283 additions and 243 deletions.
1 change: 1 addition & 0 deletions src/Mod/Fem/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ SET(FemSolverElmerEquations_SRCS
femsolver/elmer/equations/elasticity.py
femsolver/elmer/equations/electricforce.py
femsolver/elmer/equations/electrostatic.py
femsolver/elmer/equations/electrostatic_writer.py
femsolver/elmer/equations/equation.py
femsolver/elmer/equations/flow.py
femsolver/elmer/equations/flux.py
Expand Down
155 changes: 155 additions & 0 deletions src/Mod/Fem/femsolver/elmer/equations/electrostatic_writer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# ***************************************************************************
# * Copyright (c) 2023 Uwe Stöhr <uwestoehr@lyx.org> *
# * *
# * This file is part of the FreeCAD CAx development system. *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************

__title__ = "FreeCAD FEM Electrostatics Elmer writer"
__author__ = "Uwe Stöhr"
__url__ = "https://www.freecad.org"

## \addtogroup FEM
# @{

from .. import sifio

class ESwriter:

def __init__(self, writer, solver):
self.write = writer
self.solver = solver

def getElectrostaticSolver(self, equation):
# check if we need to update the equation
self._updateElectrostaticSolver(equation)
# output the equation parameters
s = self.write.createLinearSolver(equation)
s["Equation"] = "Stat Elec Solver" # equation.Name
s["Procedure"] = sifio.FileAttr("StatElecSolve/StatElecSolver")
s["Variable"] = self.write.getUniqueVarName("Potential")
s["Variable DOFs"] = 1
if equation.CalculateCapacitanceMatrix is True:
s["Calculate Capacitance Matrix"] = equation.CalculateCapacitanceMatrix
s["Capacitance Matrix Filename"] = equation.CapacitanceMatrixFilename
if equation.CalculateElectricEnergy is True:
s["Calculate Electric Energy"] = equation.CalculateElectricEnergy
if equation.CalculateElectricField is True:
s["Calculate Electric Field"] = equation.CalculateElectricField
if equation.CalculateElectricFlux is True:
s["Calculate Electric Flux"] = equation.CalculateElectricFlux
if equation.CalculateSurfaceCharge is True:
s["Calculate Surface Charge"] = equation.CalculateSurfaceCharge
if equation.ConstantWeights is True:
s["Constant Weights"] = equation.ConstantWeights
s["Exec Solver"] = "Always"
s["Optimize Bandwidth"] = True
if (
equation.CalculateCapacitanceMatrix is False
and (equation.PotentialDifference != 0.0)
):
s["Potential Difference"] = equation.PotentialDifference
s["Stabilize"] = equation.Stabilize
return s

def _updateElectrostaticSolver(self, equation):
# updates older Electrostatic equations
if not hasattr(equation, "CapacitanceMatrixFilename"):
equation.addProperty(
"App::PropertyFile",
"CapacitanceMatrixFilename",
"Electrostatic",
(
"File where capacitance matrix is being saved\n"
"Only used if 'CalculateCapacitanceMatrix' is true"
)
)
equation.CapacitanceMatrixFilename = "cmatrix.dat"
if not hasattr(equation, "ConstantWeights"):
equation.addProperty(
"App::PropertyBool",
"ConstantWeights",
"Electrostatic",
"Use constant weighting for results"
)
if not hasattr(equation, "PotentialDifference"):
equation.addProperty(
"App::PropertyFloat",
"PotentialDifference",
"Electrostatic",
(
"Potential difference in Volt for which capacitance is\n"
"calculated if 'CalculateCapacitanceMatrix' is false"
)
)
equation.PotentialDifference = 0.0

def handleElectrostaticConstants(self):
self.write.constant(
"Permittivity Of Vacuum",
self.write.convert(self.write.constsdef["PermittivityOfVacuum"], "T^4*I^2/(L^3*M)")
)

def handleElectrostaticMaterial(self, bodies):
for obj in self.write.getMember("App::MaterialObject"):
m = obj.Material
refs = (
obj.References[0][1]
if obj.References
else self.write.getAllBodies())
for name in (n for n in refs if n in bodies):
self.write.material(name, "Name", m["Name"])
if "RelativePermittivity" in m:
self.write.material(
name, "Relative Permittivity",
float(m["RelativePermittivity"])
)

def handleElectrostaticBndConditions(self):
for obj in self.write.getMember("Fem::ConstraintElectrostaticPotential"):
if obj.References:
for name in obj.References[0][1]:
if obj.PotentialEnabled:
if hasattr(obj, "Potential"):
# Potential was once a float and scaled not fitting SI units
if isinstance(obj.Potential, float):
savePotential = obj.Potential
obj.removeProperty("Potential")
obj.addProperty(
"App::PropertyElectricPotential",
"Potential",
"Parameter",
"Electric Potential"
)
# scale to match SI units
obj.Potential = savePotential * 1e6
potential = float(obj.Potential.getValueAs("V"))
self.write.boundary(name, "Potential", potential)
if obj.PotentialConstant:
self.write.boundary(name, "Potential Constant", True)
if obj.ElectricInfinity:
self.write.boundary(name, "Electric Infinity BC", True)
if obj.ElectricForcecalculation:
self.write.boundary(name, "Calculate Electric Force", True)
if obj.CapacitanceBodyEnabled:
if hasattr(obj, "CapacitanceBody"):
self.write.boundary(name, "Capacitance Body", obj.CapacitanceBody)
self.write.handled(obj)

## @}

0 comments on commit b7a02f0

Please sign in to comment.