Skip to content
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 avaframe/ana1Tests/simiSolTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def mainSimilaritySol(simiSolCfg):
"""

# Load configuration
cfg = cfgUtils.getModuleConfig(com1DFA, simiSolCfg)
cfg = cfgUtils.getModuleConfig(com1DFA, fileOverride=simiSolCfg)
cfgGen = cfg["GENERAL"]
cfgSimi = cfg["SIMISOL"]
bedFrictionAngleDeg = cfgSimi.getfloat("bedFrictionAngle")
Expand Down
6 changes: 3 additions & 3 deletions avaframe/ana5Utils/DFAPathGeneration.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,20 @@ def generatePathAndSplitpoint(avalancheDir, cfgDFAPath, cfgMain, runDFAModule):
# Clean avalanche directory of old work and output files from module
initProj.cleanModuleFiles(avalancheDir, com1DFA, deleteOutput=True)
# create and read the default com1DFA config (no local is read)
com1DFACfg = cfgUtils.getModuleConfig(com1DFA, fileOverride='', modInfo=False, toPrint=False,
com1DFACfg = cfgUtils.getModuleConfig(com1DFA, avalancheDir, toPrint=False,
onlyDefault=cfgDFAPath['com1DFA_com1DFA_override'].getboolean(
'defaultConfig'))
# and override with settings from DFAPath config
com1DFACfg, cfgDFAPath = cfgHandling.applyCfgOverride(com1DFACfg, cfgDFAPath, com1DFA,
addModValues=False)
outDir = pathlib.Path(avalancheDir, 'Outputs', 'ana5Utils', 'DFAPath')
fU.makeADir(outDir)
# write configuration to file
# write configuration to file for documentation
com1DFACfgFile = outDir / 'com1DFAPathGenerationCfg.ini'
with open(com1DFACfgFile, 'w') as configfile:
com1DFACfg.write(configfile)
# call com1DFA and perform simulations
dem, plotDict, reportDictList, simDF = com1DFA.com1DFAMain(cfgMain, cfgInfo=com1DFACfgFile)
dem, plotDict, reportDictList, simDF = com1DFA.com1DFAMain(cfgMain, cfgInfo=com1DFACfg)
else: # read existing simulation results
# read simulation dem
demOri = gI.readDEM(avalancheDir)
Expand Down
5 changes: 3 additions & 2 deletions avaframe/ana5Utils/distanceTimeAnalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,10 +583,11 @@ def initializeRangeTime(modName, cfg, dem, simHash):
"""

# fetch configuration and add info
cfgRangeTime = cfgUtils.getModuleConfig(modName)
avalancheDir = cfg['GENERAL']['avalancheDir']
cfgRangeTime = cfgUtils.getModuleConfig(modName, avalancheDir)

cfgRangeTime['GENERAL']['tEnd'] = cfg['GENERAL']['tEnd']
cfgRangeTime['GENERAL']['avalancheDir'] = cfg['GENERAL']['avalancheDir']
cfgRangeTime['GENERAL']['avalancheDir'] = avalancheDir
cfgRangeTime['GENERAL']['simHash'] = simHash
# fetch time steps for creating range time diagram
dtRangeTime = fU.splitTimeValueToArrayInterval(cfgRangeTime['GENERAL']['distanceTimeSteps'],
Expand Down
31 changes: 15 additions & 16 deletions avaframe/com1DFA/com1DFA.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Main functions for python DFA kernel
"""

import configparser
import copy
import inspect
import logging
Expand Down Expand Up @@ -65,7 +66,7 @@
debugPlot = cfgAVA["FLAGS"].getboolean("debugPlot")


def com1DFAPreprocess(cfgMain, typeCfgInfo, cfgInfo, module=com1DFA):
def com1DFAPreprocess(cfgMain, cfgInfo, module=com1DFA):
"""preprocess information from configuration, read input data and gather into inputSimFiles,
create one config object for each of all desired simulations,
create dataFrame with one line per simulations of already existing sims in avalancheDir
Expand All @@ -74,12 +75,9 @@ def com1DFAPreprocess(cfgMain, typeCfgInfo, cfgInfo, module=com1DFA):
------------
cfgMain: configparser object
main configuration of AvaFrame
typeCfgInfo: str
name of type of cfgInfo (cfgFromFile or cfgFromObject)
cfgInfo: str or pathlib Path or configparser object
path to configuration file if overwrite is desired - optional
if not local (if available) or default configuration will be loaded
if cfgInfo is a configparser object take this as initial config
if ConfigParser object: use directly as initial config
if str/Path: path to configuration file override (empty string uses default/local config)
module: module
module to be used for task (optional)

Expand All @@ -96,10 +94,11 @@ def com1DFAPreprocess(cfgMain, typeCfgInfo, cfgInfo, module=com1DFA):
avalancheDir = cfgMain["MAIN"]["avalancheDir"]

# read initial configuration
if typeCfgInfo in ["cfgFromFile", "cfgFromDefault"]:
cfgStart = cfgUtils.getModuleConfig(module, fileOverride=cfgInfo, toPrint=False)
elif typeCfgInfo == "cfgFromObject":
if isinstance(cfgInfo, configparser.ConfigParser):
cfgStart = cfgInfo
else:
# cfgInfo is file path (str or Path) or empty string
cfgStart = cfgUtils.getModuleConfig(module, avalancheDir, fileOverride=cfgInfo, toPrint=False)

# fetch input data and create work and output directories
inputSimFilesAll, outDir, simDFExisting, simNameExisting = com1DFATools.initializeInputs(
Expand Down Expand Up @@ -140,14 +139,13 @@ def com1DFAMain(cfgMain, cfgInfo=""):

avalancheDir = cfgMain["MAIN"]["avalancheDir"]

# fetch type of cfgInfo
typeCfgInfo = com1DFATools.checkCfgInfoType(cfgInfo)
if typeCfgInfo == "cfgFromDir":
# preprocessing to create configuration objects for all simulations to run by reading multiple cfg files
# Route based on cfgInfo type: directory Path = batch mode, otherwise single config mode
if isinstance(cfgInfo, pathlib.Path) and cfgInfo.is_dir():
# Batch mode - cfgInfo is directory path (from getModuleConfig with batchCfgDir)
simDict, inputSimFiles, simDFExisting, outDir = com1DFATools.createSimDictFromCfgs(cfgMain, cfgInfo)
else:
# preprocessing to create configuration objects for all simulations to run
simDict, outDir, inputSimFiles, simDFExisting = com1DFAPreprocess(cfgMain, typeCfgInfo, cfgInfo)
# Single config mode - cfgInfo is ConfigParser, file path string, or empty string
simDict, outDir, inputSimFiles, simDFExisting = com1DFAPreprocess(cfgMain, cfgInfo)

log.info("The following simulations will be performed")
for key in simDict:
Expand Down Expand Up @@ -3111,7 +3109,8 @@ def _findWrapperModuleInStack():
# Extract the last component (the actual module name)
moduleName = frameModule.split(".")[-1]
# Check if it matches the comN pattern (starts with "com" followed by a digit)
if re.match(r"^com\d+", moduleName) and not frameModule.endswith("com1DFA.com1DFA"):
# Exclude all modules in the com1DFA package (com1DFA.com1DFA, com1DFA.com1DFATools, etc.)
if re.match(r"^com\d+", moduleName) and not frameModule.startswith("avaframe.com1DFA"):
return moduleName
return None

Expand Down
44 changes: 1 addition & 43 deletions avaframe/com1DFA/com1DFATools.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def createSimDictFromCfgs(cfgMain, cfgPath, module=com1DFA):
# loop over all cfgFiles and create simDict
for index, cfgFile in enumerate(cfgFilesAll):
# read configuration
cfgFromFile = cfgUtils.getModuleConfig(module, fileOverride=cfgFile, toPrint=False)
cfgFromFile = cfgUtils.getModuleConfig(module, avalancheDir, fileOverride=cfgFile, toPrint=False)

# create dictionary with one key for each simulation that shall be performed
# NOTE: sims that are added don't need to be added to the simNameExisting list as
Expand Down Expand Up @@ -357,48 +357,6 @@ def initializeInputs(avalancheDir, cleanRemeshedRasters, module=com1DFA):
return inputSimFilesAll, outDir, simDFExisting, simNameExisting


def checkCfgInfoType(cfgInfo):
"""check if cfgInfo is a configparser object, a file or a directory

Parameters
------------
cfgInfo: configparser object, str or pathlib path

Returns
---------
typeCfgInfo: str
name of type of cfgInfo
"""

if cfgInfo == "":
typeCfgInfo = "cfgFromDefault"

elif isinstance(cfgInfo, (pathlib.Path, str)):
# if path is provided check if file or directory
cfgInfoPath = pathlib.Path(cfgInfo)
if cfgInfoPath.is_dir():
typeCfgInfo = "cfgFromDir"
log.info("----- CFG override from directory is used -----")
elif cfgInfo.is_file():
typeCfgInfo = "cfgFromFile"
log.info("----- CFG override from file is used ----")

elif isinstance(cfgInfo, configparser.ConfigParser):
typeCfgInfo = "cfgFromObject"
log.info("---- CFG override object is used ----")

else:
message = (
"cfgInfo is not of valid format, needs to be a path to a cfg file, \
directory, configparser object or an empty str, cfgInfo is: %s"
% cfgInfo
)
log.error(message)
raise AssertionError(message)

return typeCfgInfo


def updateResCoeffFields(fields, cfg):
"""update fields of cRes and detK, coefficients of resistance parameter and detrainment parameter
according to the thresholds of FV and FT
Expand Down
23 changes: 11 additions & 12 deletions avaframe/com3Hybrid/com3Hybrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"""
import pathlib
import logging
from configupdater import ConfigUpdater
import numpy as np
import copy

Expand Down Expand Up @@ -54,20 +53,18 @@ def maincom3Hybrid(cfgMain, cfgHybrid):

# ++++++++++ set configurations for all the used modules and override ++++++++++++
# get comDFA configuration and save to file
com1DFACfg = cfgUtils.getModuleConfig(com1DFA, fileOverride='', modInfo=False, toPrint=False,
com1DFACfg = cfgUtils.getModuleConfig(com1DFA, avalancheDir, toPrint=False,
onlyDefault=cfgHybrid['com1DFA_com1DFA_override'].getboolean('defaultConfig'))
com1DFACfg, cfgHybrid = cfgHandling.applyCfgOverride(com1DFACfg, cfgHybrid, com1DFA, addModValues=False)
com1DFACfgFile = cfgUtils.writeCfgFile(avalancheDir, com1DFA, com1DFACfg, fileName='com1DFA_settings',
filePath=workPath)

# fetch configuration for DFAPathGeneration
DFAPathGenerationCfg = cfgUtils.getModuleConfig(DFAPath, fileOverride='', modInfo=False, toPrint=False,
DFAPathGenerationCfg = cfgUtils.getModuleConfig(DFAPath, avalancheDir, toPrint=False,
onlyDefault=cfgHybrid['ana5Utils_DFAPathGeneration_override'].getboolean('defaultConfig'))
DFAPathGenerationCfg, cfgHybrid = cfgHandling.applyCfgOverride(DFAPathGenerationCfg, cfgHybrid, DFAPath,
addModValues=False)

# first create configuration object for com2AB
com2ABCfg = cfgUtils.getModuleConfig(com2AB, fileOverride='', modInfo=False, toPrint=False,
com2ABCfg = cfgUtils.getModuleConfig(com2AB, avalancheDir, toPrint=False,
onlyDefault=cfgHybrid['com1DFA_com1DFA_override'].getboolean('defaultConfig'))
com2ABCfg, cfgHybrid = cfgHandling.applyCfgOverride(com2ABCfg, cfgHybrid, com2AB, addModValues=False)

Expand All @@ -81,18 +78,16 @@ def maincom3Hybrid(cfgMain, cfgHybrid):
iterate = True
resultsHybrid = {}
while iteration < nIterMax and iterate:
# update the com1DFA mu value in configuration file
updater = ConfigUpdater()
updater.read(com1DFACfgFile)
updater['GENERAL']['mucoulomb'].value = ('%.4f' % muArray[-1])
updater.update_file()
# create a copy to avoid com1DFAMain modifying the original config
com1DFACfgCopy = copy.deepcopy(com1DFACfg)
com1DFACfgCopy['GENERAL']['mucoulomb'] = '%.4f' % muArray[-1]

log.info('Mu is set to: %f' % muArray[-1])
# ++++++++++ RUN COM1DFA +++++++++++
# Run dense flow with coulomb friction
# Clean input directory of old work and output files from module
initProj.cleanModuleFiles(avalancheDir, com1DFA, deleteOutput=False)
dem, _, _, simDF = com1DFA.com1DFAMain(cfgMain, cfgInfo=com1DFACfgFile)
dem, _, _, simDF = com1DFA.com1DFAMain(cfgMain, cfgInfo=com1DFACfgCopy)
simID = simDF.index[0]
particlesList, timeStepInfo = particleTools.readPartFromPickle(avalancheDir, simName=simID, flagAvaDir=True,
comModule='com1DFA')
Expand Down Expand Up @@ -140,6 +135,10 @@ def maincom3Hybrid(cfgMain, cfgHybrid):
iterate = keepIterating(cfgHybrid, alphaArray)
iteration = iteration + 1

# write final com1DFA configuration to file for documentation
cfgUtils.writeCfgFile(avalancheDir, com1DFA, com1DFACfgCopy, fileName='com1DFA_settings',
filePath=workPath)

# fetch fields for desired time step
fields, fieldHeader, timeList = com1DFA.readFields(avalancheDir, ['pta'], simName=simID,
flagAvaDir=True, comModule='com1DFA', timeStep=timeStepInfo[-1])
Expand Down
1 change: 1 addition & 0 deletions avaframe/com5SnowSlide/com5SnowSlide.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def com5SnowSlideMain(cfgMain, snowSlideCfg):
# get comDFA configuration and update with snow slide parameter set
com1DFACfg = cfgUtils.getModuleConfig(
com1DFA,
avalancheDir,
fileOverride="",
modInfo=False,
toPrint=False,
Expand Down
3 changes: 3 additions & 0 deletions avaframe/com6RockAvalanche/com6RockAvalanche.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ def com6RockAvalancheMain(cfgMain, rockAvalancheCfg):

"""

avalancheDir = cfgMain["MAIN"]["avalancheDir"]

# ++++++++++ set configurations for com1DFA and override ++++++++++++
# get comDFA configuration and update with snow slide parameter set
com1DFACfg = cfgUtils.getModuleConfig(
com1DFA,
avalancheDir,
fileOverride="",
modInfo=False,
toPrint=False,
Expand Down
4 changes: 3 additions & 1 deletion avaframe/com7Regional/com7Regional.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def getTotalNumberOfSims(avaDirs, cfgMain, cfgCom7):
# Get com1DFA config with regional overrides (same as in processAvaDirCom1Regional)
cfgCom1DFA = cfgUtils.getModuleConfig(
com1DFA,
str(avaDir),
fileOverride="",
toPrint=False,
onlyDefault=cfgCom7["com1DFA_com1DFA_override"].getboolean("defaultConfig"),
Expand All @@ -149,7 +150,7 @@ def getTotalNumberOfSims(avaDirs, cfgMain, cfgCom7):

# Get simulations for this directory
try:
simDict, _, _, _ = com1DFA.com1DFAPreprocess(cfgMainCopy, "cfgFromObject", cfgCom1DFA)
simDict, _, _, _ = com1DFA.com1DFAPreprocess(cfgMainCopy, cfgCom1DFA)
totalSims += len(simDict)
except Exception as e:
log.warning(f"Could not get simulations for {avaDir}: {e}")
Expand Down Expand Up @@ -194,6 +195,7 @@ def processAvaDirCom1Regional(cfgMain, cfgCom7, avalancheDir):
# Create com1DFA configuration for the current avalanche directory and override with regional settings
cfgCom1DFA = cfgUtils.getModuleConfig(
com1DFA,
str(avalancheDir),
fileOverride="",
toPrint=False,
onlyDefault=cfgCom7["com1DFA_com1DFA_override"].getboolean("defaultConfig"),
Expand Down
2 changes: 1 addition & 1 deletion avaframe/com8MoTPSA/runAna4ProbAnaCom8MoTPSA.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def runProbAna(avalancheDir=''):
initProj.cleanSingleAvaDir(avalancheDir, deleteOutput=False)

# Load configuration file for probabilistic run and analysis
cfgProb = cfgUtils.getModuleConfig(probAna)
cfgProb = cfgUtils.getModuleConfig(probAna, avalancheDir)

# create configuration files for com8MoTPSA simulations including parameter
# variation - defined in the probabilistic config
Expand Down
2 changes: 1 addition & 1 deletion avaframe/com8MoTPSA/runCom8MoTPSA.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def runCom8MoTPSA(avalancheDir=''):
initProj.cleanSingleAvaDir(avalancheDir, deleteOutput=False)

# Get module config
cfgCom8MoTPSA = cfgUtils.getModuleConfig(com8MoTPSA, toPrint=False)
cfgCom8MoTPSA = cfgUtils.getModuleConfig(com8MoTPSA, avalancheDir, toPrint=False)

# ----------------
# Run psa
Expand Down
2 changes: 1 addition & 1 deletion avaframe/com9MoTVoellmy/runCom9MoTVoellmy.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def runCom9MoTVoellmy(avalancheDir="", simType=""):
initProj.cleanSingleAvaDir(avalancheDir, deleteOutput=False)

# Get module config
cfgCom9MoTVoellmy = cfgUtils.getModuleConfig(com9MoTVoellmy, toPrint=False)
cfgCom9MoTVoellmy = cfgUtils.getModuleConfig(com9MoTVoellmy, avalancheDir, toPrint=False)

# Override simTypeList if provided via command line
if simType != "":
Expand Down
7 changes: 2 additions & 5 deletions avaframe/in3Utils/MoTUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import numpy as np

from avaframe.com1DFA import com1DFATools as com1DFATools, com1DFA as com1DFA
from avaframe.com1DFA import com1DFA as com1DFA
from avaframe.in2Trans import rasterUtils as rU

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -148,12 +148,9 @@ def MoTGenerateConfigs(cfgMain, cfgInfo, currentModule):
dictionary with input files info
"""

# fetch type of cfgInfo
typeCfgInfo = com1DFATools.checkCfgInfoType(cfgInfo)

# preprocessing to create configuration objects for all simulations to run
simDict, outDir, inputSimFiles, simDFExisting = com1DFA.com1DFAPreprocess(
cfgMain, typeCfgInfo, cfgInfo, module=currentModule
cfgMain, cfgInfo, module=currentModule
)

return simDict, inputSimFiles
Expand Down
Loading
Loading