Skip to content

Commit

Permalink
back compatibility with old way of setting diffusion parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Ury committed Sep 6, 2024
1 parent 043af52 commit 808c92c
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 23 deletions.
2 changes: 1 addition & 1 deletion examples/01_Binary_Precipitation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
"\tbeta\t0.000e+00\t\t0.0000\t\t0.0000e+00\t5.7737e+03\n",
"\n",
"N\tTime (s)\tSim Time (s)\tTemperature (K)\tMatrix Comp\n",
"3675\t1.8e+06\t\t15.1\t\t723\t\t0.0126\n",
"3675\t1.8e+06\t\t12.2\t\t723\t\t0.0126\n",
"\n",
"\tPhase\tPrec Density (#/m3)\tVolume Frac\tAvg Radius (m)\tDriving Force (J/mol)\n",
"\tbeta\t1.374e+22\t\t1.5504\t\t6.1126e-09\t3.2902e+02\n",
Expand Down
8 changes: 4 additions & 4 deletions examples/06_Single_Phase_Diffusion.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@
"text": [
"Iteration\tSim Time (h)\tRun time (s)\n",
"0\t\t0.0e+00\t\t0.0\n",
"100\t\t2.9e+01\t\t2.8\n",
"200\t\t5.7e+01\t\t5.3\n",
"300\t\t8.6e+01\t\t7.3\n",
"349\t\t1.0e+02\t\t8.1\n"
"100\t\t2.9e+01\t\t2.5\n",
"200\t\t5.7e+01\t\t4.9\n",
"300\t\t8.6e+01\t\t6.6\n",
"349\t\t1.0e+02\t\t7.2\n"
]
}
],
Expand Down
28 changes: 12 additions & 16 deletions examples/07_Homogenization_Model.ipynb

Large diffs are not rendered by default.

56 changes: 55 additions & 1 deletion kawin/diffusion/Diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from kawin.solver.Solver import DESolver, SolverType
from kawin.GenericModel import GenericModel
import kawin.diffusion.Plot as diffPlot
from kawin.diffusion.DiffusionParameters import DiffusionParameters
from kawin.diffusion.DiffusionParameters import DiffusionParameters, BoundaryConditions

class DiffusionModel(GenericModel):
#Boundary conditions
Expand Down Expand Up @@ -75,6 +75,15 @@ def setThermodynamics(self, thermodynamics):
'''
self.therm = thermodynamics

def setTemperature(self, T):
self.parameters.temperature.setIsothermalTemperature(T)

def setTemperatureArray(self, times, temperatures):
self.parameters.temperature.setTemperatureArray(times, temperatures)

def setTemperatureFunction(self, func):
self.parameters.temperature.setTemperatureFunction(func)

def _getVarDict(self):
'''
Returns mapping of { variable name : attribute name } for saving
Expand Down Expand Up @@ -103,6 +112,15 @@ def load(filename):
model._loadData(data)
model.isSetup = True
return model

def setHashSensitivity(self, s):
self.parameters.hashTable.setHashSensitivity(s)

def useCache(self, use):
self.parameters.hashTable.enableCaching(use)

def clearCache(self):
self.parameters.hashTable.clearCache()

def enableRecording(self, dtype = np.float32):
'''
Expand Down Expand Up @@ -194,6 +212,42 @@ def _getPhaseIndex(self, phase = None):
return 0
else:
return self.phases.index(phase)

def setBC(self, LBCtype = BoundaryConditions.FLUX_BC, LBCValue = 0, RBCType = BoundaryConditions.FLUX_BC, RBCValue = 0, element = None):
self.parameters.boundaryConditions.setBoundaryCondition(BoundaryConditions.LEFT,
LBCtype, LBCValue, element)
self.parameters.boundaryConditions.setBoundaryCondition(BoundaryConditions.RIGHT,
RBCType, RBCValue, element)

def setCompositionLinear(self, Lvalue, Rvalue, element = None):
element = self.elements[0] if element is None else element
self.parameters.compositionProfile.clearCompositionBuildSteps(element)
self.parameters.compositionProfile.addLinearCompositionStep(element, Lvalue, Rvalue)

def setCompositionStep(self, Lvalue, Rvalue, z, element = None):
element = self.elements[0] if element is None else element
self.parameters.compositionProfile.clearCompositionBuildSteps(element)
self.parameters.compositionProfile.addStepCompositionStep(element, Lvalue, Rvalue, z)

def setCompositionSingle(self, value, z, element = None):
element = self.elements[0] if element is None else element
self.parameters.compositionProfile.clearCompositionBuildSteps(element)
self.parameters.compositionProfile.addSingleCompositionStep(element, value, z)

def setCompositionInBounds(self, value, Lbound, Rbound, element = None):
element = self.elements[0] if element is None else element
self.parameters.compositionProfile.clearCompositionBuildSteps(element)
self.parameters.compositionProfile.addBoundedCompositionStep(element, value, Lbound, Rbound)

def setCompositionFunction(self, func, element = None):
element = self.elements[0] if element is None else element
self.parameters.compositionProfile.clearCompositionBuildSteps(element)
self.parameters.compositionProfile.addFunctionCompositionStep(element, func)

def setCompositionProfile(self, z, x, element = None):
element = self.elements[0] if element is None else element
self.parameters.compositionProfile.clearCompositionBuildSteps(element)
self.parameters.compositionProfile.addProfileCompositionStep(element, x, z)

def setup(self):
'''
Expand Down
24 changes: 23 additions & 1 deletion kawin/diffusion/DiffusionParameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,25 @@ def setPostProcessFunction(self, functionName, functionArgs = None):
Returns post process function
'''
self.postProcessParameters = [functionArgs]
if isinstance(functionName, str):
self._setPostProcessFunctionByStr(functionName)
else:
self._setPostProcessFunctionByID(functionName)

def _setPostProcessFunctionByStr(self, functionName):
keywords_map = {
'none': self.NO_POST,
'predefined': self.PREDEFINED,
'majority': self.MAJORITY,
'exclude': self.EXCLUDE
}
if functionName in keywords_map:
self._setHomogenizationFunctionByID(keywords_map[functionName])

str_options = ', '.join(list(keywords_map.keys()))
raise Exception(f'Error: post process function by str should be {str_options}')

def _setPostProcessFunctionByID(self, functionName):
if functionName == self.NO_POST:
self.postProcessFunction = _postProcessDoNothing
elif functionName == self.PREDEFINED:
Expand All @@ -449,7 +468,9 @@ def setPostProcessFunction(self, functionName, functionArgs = None):
elif functionName == self.EXCLUDE:
self.postProcessFunction = _postProcessExcludePhases
else:
raise Exception("Error: post process function should be \'predefined\', \'majority\' or \'exclude\'")
func_types = ['NO_POST', 'PREDEFINED', 'MAJORITY', 'EXCLUDE']
int_options = ', '.join([f'HomogenizationParameters.{t}' for t in func_types])
raise Exception(f'Error: post process function by ID should be {int_options}')

def setHomogenizationFunction(self, function):
'''
Expand Down Expand Up @@ -478,6 +499,7 @@ def _setHomogenizationFunctionByStr(self, function):
for func_id, keywords in keywords_map.items():
if all([kw in function for kw in keywords]):
self._setHomogenizationFunctionByID(func_id)
return

func_types = ['wiener upper', 'wiener lower', 'hashin upper', 'hashin lower', 'labyrinth']
str_options = ', '.join(func_types)
Expand Down
12 changes: 12 additions & 0 deletions kawin/diffusion/Homogenization.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
import copy

class HomogenizationModel(DiffusionModel):
def setMobilityFunction(self, function):
self.parameters.homogenizationParameters.setHomogenizationFunction(function)

def setLabyrinthFactor(self, n):
self.parameters.homogenizationParameters.setLabyrinthFactor(n)

def setMobilityPostProcessFunction(self, function, functionArgs = None):
self.parameters.homogenizationParameters.setPostProcessFunction(function, functionArgs)

def setIdealEps(self, eps):
self.parameters.homogenizationParameters.eps = eps

def _getFluxes(self, t, x_curr):
'''
Return fluxes and time interval for the current iteration
Expand Down

0 comments on commit 808c92c

Please sign in to comment.