Skip to content
Snippets Groups Projects
Commit 327acffd authored by Martin Bauer's avatar Martin Bauer
Browse files

Phase field tutorials + small changes in functional derivative code

parent b6dfd5d3
Branches
No related merge requests found
...@@ -16,6 +16,7 @@ class Diff(sp.Expr): ...@@ -16,6 +16,7 @@ class Diff(sp.Expr):
""" """
is_number = False is_number = False
is_Rational = False is_Rational = False
_diff_wrt = True
def __new__(cls, argument, target=-1, superscript=-1, **kwargs): def __new__(cls, argument, target=-1, superscript=-1, **kwargs):
if argument == 0: if argument == 0:
...@@ -142,7 +143,7 @@ class DiffOperator(sp.Expr): ...@@ -142,7 +143,7 @@ class DiffOperator(sp.Expr):
return result return result
@staticmethod @staticmethod
def apply(expr, argument): def apply(expr, argument, applyToConstants=True):
""" """
Returns a new expression where each 'DiffOperator' is replaced by a 'Diff' node. Returns a new expression where each 'DiffOperator' is replaced by a 'Diff' node.
Multiplications of 'DiffOperator's are interpreted as nested application of differentiation: Multiplications of 'DiffOperator's are interpreted as nested application of differentiation:
...@@ -152,7 +153,7 @@ class DiffOperator(sp.Expr): ...@@ -152,7 +153,7 @@ class DiffOperator(sp.Expr):
args = normalizeProduct(mul) args = normalizeProduct(mul)
diffs = [a for a in args if isinstance(a, DiffOperator)] diffs = [a for a in args if isinstance(a, DiffOperator)]
if len(diffs) == 0: if len(diffs) == 0:
return mul * argument return mul * argument if applyToConstants else mul
rest = [a for a in args if not isinstance(a, DiffOperator)] rest = [a for a in args if not isinstance(a, DiffOperator)]
diffs.sort(key=defaultDiffSortKey) diffs.sort(key=defaultDiffSortKey)
result = argument result = argument
...@@ -166,7 +167,7 @@ class DiffOperator(sp.Expr): ...@@ -166,7 +167,7 @@ class DiffOperator(sp.Expr):
elif expr.func == sp.Add: elif expr.func == sp.Add:
return expr.func(*[handleMul(a) for a in expr.args]) return expr.func(*[handleMul(a) for a in expr.args])
else: else:
return expr * argument return expr * argument if applyToConstants else expr
# ---------------------------------------------------------------------------------------------------------------------- # ----------------------------------------------------------------------------------------------------------------------
...@@ -442,7 +443,7 @@ def evaluateDiffs(expr, var=None): ...@@ -442,7 +443,7 @@ def evaluateDiffs(expr, var=None):
return expr.func(*newArgs) if newArgs else expr return expr.func(*newArgs) if newArgs else expr
def functionalDerivative(functional, v, constants=None): def functionalDerivative(functional, v):
""" """
Computes functional derivative of functional with respect to v using Euler-Lagrange equation Computes functional derivative of functional with respect to v using Euler-Lagrange equation
...@@ -457,9 +458,10 @@ def functionalDerivative(functional, v, constants=None): ...@@ -457,9 +458,10 @@ def functionalDerivative(functional, v, constants=None):
of the derivative terms. of the derivative terms.
""" """
diffs = functional.atoms(Diff) diffs = functional.atoms(Diff)
nonDiffPart = functional.subs({d: sp.Dummy() for d in diffs}) bulkSubstitutions = {d: sp.Dummy() for d in diffs}
bulkSubstitutionsInverse = {v: k for k, v in bulkSubstitutions.items()}
partialF_partialV = sp.diff(nonDiffPart, v) nonDiffPart = functional.subs(bulkSubstitutions)
partialF_partialV = sp.diff(nonDiffPart, v).subs(bulkSubstitutionsInverse)
gradientPart = 0 gradientPart = 0
for diffObj in diffs: for diffObj in diffs:
...@@ -470,4 +472,4 @@ def functionalDerivative(functional, v, constants=None): ...@@ -470,4 +472,4 @@ def functionalDerivative(functional, v, constants=None):
gradientPart += Diff(partialF_partialGradV, target=diffObj.target, superscript=diffObj.superscript) gradientPart += Diff(partialF_partialGradV, target=diffObj.target, superscript=diffObj.superscript)
result = partialF_partialV - gradientPart result = partialF_partialV - gradientPart
return expandUsingLinearity(result, constants=constants) return result
...@@ -84,6 +84,12 @@ def multipleScalarFields(field, **kwargs): ...@@ -84,6 +84,12 @@ def multipleScalarFields(field, **kwargs):
colorbar() colorbar()
def sympyFunction(f, var, bounds, **kwargs):
import sympy as sp
xArr = np.linspace(bounds[0], bounds[1], 101)
yArr = sp.lambdify(var, f)(xArr)
plot(xArr, yArr, **kwargs)
# ------------------------------------------- Animations --------------------------------------------------------------- # ------------------------------------------- Animations ---------------------------------------------------------------
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment