Skip to content
Snippets Groups Projects
Commit 05f07dfe authored by Malte Nyhuis's avatar Malte Nyhuis
Browse files

running casecreation

parent 9c634fa5
No related branches found
No related tags found
No related merge requests found
from snakemake.utils import validate
from snakemake.utils import Paramspace
import pandas as pd
import os
#todo move this snakepipe to according dir in sourcecode
......@@ -7,30 +10,58 @@ configfile : "casesettings.yaml"
validate(config,"config.schema.yaml")
basedir = workflow.current_basedir
# declare a dataframe to be a paramspace
paramspace = Paramspace(pd.read_csv("params.tsv", sep="\t"))
paramspace.param_sep="-"
#paramspace.pattern="{}~{}~{}"
def create_filelist_from_template(template):
"""
:param template: path
:return: list of files
"""
import ntrfc
from ntrfc.utils.dictionaries.dict_utils import nested_dict_pairs_iterator
from case_creation import get_directory_structure
templatepath = os.path.join(os.path.dirname(ntrfc.__file__),"database","case_templates",template)
assert os.path.isdir(templatepath)
files = list(nested_dict_pairs_iterator(get_directory_structure(templatepath)))
fpaths = [os.path.join(*i[:-1]) for i in files]
return fpaths
TEMPLATE = config["case_params"]["case_type"]
DATASETS = create_filelist_from_template(TEMPLATE)
rule all:
input:
"done.txt"
# Aggregate over entire parameter space (or a subset thereof if needed)
# of course, something like this can happen anywhere in the workflow (not
# only at the end).
#create_filelist_from_template(TEMPLATE)
"99_Report/parametrization.txt"
#expand("results/plots/{params}.pdf", params=paramspace.instance_patterns)
rule copy_template:
output:
#todo: is there a readable dataformat alternative (csv) instead of pickle?
#dynamic("01_case/{case_type}/{case_files}"),
dynamic("01_case/{level_1}/{level_2}"),
"01_case/check.txt"
create_filelist_from_template(TEMPLATE)
#dynamic("01_case/{level_1}/{level_2}"),
# "01_case/check.txt"
params:
case_type = config["case_params"]["case_type"],
case_parameters = config["case_params"]["parameters"]
run:
from case_creation import create_case_fromtemplate
create_case_fromtemplate(params["case_type"],params["case_parameters"],os.path.join(basedir,"01_case"))
from case_creation import copy_template
copy_template(output,params["case_type"],params["case_parameters"])
rule check_template:
rule set_paras:
input:
#dynamic("01_case/{case_type}/{case_files}")
dynamic("01_case/{level_1}/{level_2}")
create_filelist_from_template(TEMPLATE)
output:
"done.txt"
"99_Report/parametrization.txt"
run:
print("tbd")
with open(output[0],"w") as fhandle:
fhandle.write(" ")
......@@ -4,8 +4,10 @@ import shutil
from pathlib import Path
import copy
import re
import warnings
from ntrfc.utils.dictionaries.dict_utils import nested_dict_pairs_iterator, setInDict
from ntrfc.utils.dictionaries.dict_utils import setInDict
from ntrfc.utils.dictionaries.dict_utils import nested_dict_pairs_iterator
TEMPLATEDIR = r"D:\CodingProjects\NTRfC\ntrfc\database\case_templates"
path_to_sim=r"D:\CodingProjects\NTRfC\examples\gwk_compressor_casegeneration\01_case"
......@@ -33,6 +35,9 @@ def find_vars_opts(case_structure, sign, all_pairs, path_to_sim):
# allowing names like JOB_NUMBERS, only capital letters and underlines - no digits, no whitespaces
datadict = copy.deepcopy(case_structure)
varsignature = r"<PLACEHOLDER [A-Z]{3,}(_{1,1}[A-Z]{3,}){,} PLACEHOLDER>".replace(r'PLACEHOLDER', sign)
#int
#float
#string
siglim = (len(sign)+2, -(len(sign)+2))
for pair in all_pairs:
......@@ -110,8 +115,21 @@ def check_settings_necessarities(case_structure, settings_dict):
used.append(variable)
return defined, undefined, used, unused
def create_case_fromtemplate(template, settings, path_to_sim):
def inplace_change(filename, old_string, new_string):
# Safely read the input filename using 'with'
with open(filename) as f:
s = f.read()
if old_string not in s:
print('"{old_string}" not found in {filename}.'.format(**locals()))
return
# Safely write the changed content, if found in the file
with open(filename, 'w') as f:
print('Changing "{old_string}" to "{new_string}" in {filename}'.format(**locals()))
s = s.replace(old_string, new_string)
f.write(s)
def copy_template(output, template, paras):
"""
:param template: str - template-name
......@@ -124,31 +142,25 @@ def create_case_fromtemplate(template, settings, path_to_sim):
assert found, "template unknown. check ntrfc.database.casetemplates directory"
case_structure = get_directory_structure(os.path.join(TEMPLATEDIR, template))
case_files = [i[:-1] for i in list(nested_dict_pairs_iterator(case_structure)) if os.path.isfile(os.path.join(TEMPLATEDIR,*list(i[:-1])))]
variables = find_vars_opts(case_structure, "var", list(nested_dict_pairs_iterator(case_structure)),os.path.join(TEMPLATEDIR))
for fpath in case_files:
filename = fpath[-1]
dirstructure = fpath[:-1]
if dirstructure == ():
dirstructure = ""
template_fpath = os.path.join(TEMPLATEDIR,
*dirstructure,
filename)
create_simdirstructure(case_structure,path_to_sim)
sim_fpath = os.path.join(path_to_sim, *dirstructure)
shutil.copyfile(template_fpath, os.path.join(sim_fpath,filename))
defined, undefined, used, unused = check_settings_necessarities(variables, settings)
defined, undefined, used, unused = check_settings_necessarities(variables, paras)
print("found ", str(len(defined)), " defined parameters")
print("found ", str(len(undefined)), " undefined parameters")
print("used ", str(len(used)), " parameters")
print("unused ", str(len(unused)), " parameters")
if len(undefined)>0:
warnings.warn("undefined variables")
warnings.warn(str(undefined))
return -1
if len(unused)>0:
warnings.warn("unused "+str(len(unused)))
warnings.warn(str(unused))
settings = {}
case_structure = create_case_fromtemplate('trace-compressor-cascade-ras', settings, path_to_sim)
for fpath in output:
template_fpath = os.path.join(TEMPLATEDIR,fpath)
shutil.copyfile(template_fpath, fpath)
for para in used:
inplace_change(fpath,para,paras[para])
case_params:
case_type: trace-compressor-cascade-ras
case_type: "trace-compressor-cascade-ras"
parameters:
haha : nope
TRACE_CONTROLINPUT : nope
MESHNAME: nope
OUTFLOW_PRESSURE: nope
GLOBAL_TOLERANCE: nope
LENGTH_YPER: nope
STAGPOINT_ANNOMALLYFIX: nope
QUASIUNSTEADYMODEL: nope
WAVINESSMODEL: nope
VERSION_TWONULLNULLNINE: nope
HEATFLUXMODEL: nope
PRANDTLTURBULENT: nope
ROTATIONAL_EFF: nope
TRANSITIONMODEL: nope
VGJMODEL: nope
ACCURACY_ORDER: nope
OUTLET_MFLOW: nope
OUTLET_RADIAL_EQUI: nope
INLET_TOTAL_PRESSURE: nope
VEL_YANGLE: nope
ABS_STAGN_TEMP: nope
TURBULENTINTENSITY: nope
TURBULENTLENGTHSCALE: nope
INLET_TEMP: nope
INLET_VELX: nope
INLET_VELY: nope
INLET_VELZ: nope
OUTFLOW_TEMP: nope
OUTFLOW_PRESSURE: nope
OUTFLOW_VELX: nope
OUTFLOW_VELY: nope
OUTFLOW_VELZ: nope
param_1 param_2 param_3
1 2 3
4 3 2
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment