Skip to content

Commit

Permalink
adding workflow code #85
Browse files Browse the repository at this point in the history
  • Loading branch information
fnrizzi committed Feb 3, 2022
1 parent 5f721ef commit 109633f
Show file tree
Hide file tree
Showing 438 changed files with 151,737 additions and 0 deletions.
10 changes: 10 additions & 0 deletions workflow_codes/2d_gs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

from .constants import *
from .scenarios import *

from .specialize_execute_foms import *
from .specialize_extract_fom_states import *
from .specialize_compute_pod import *
from .specialize_compute_sample_meshes import *
from .specialize_execute_galerkin_roms import *
from .specialize_rom_reconstruction_and_error import *
4 changes: 4 additions & 0 deletions workflow_codes/2d_gs/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

problemName = "2d_gs"
dimensionality = 2
numDofsPerCell = 2
111 changes: 111 additions & 0 deletions workflow_codes/2d_gs/impl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/usr/bin/env python

#==============================================================
# imports
#==============================================================

import copy

# LOCAL imports
from .scenarios import \
base_dic, test_points, train_points
from .constants import \
problemName, numDofsPerCell


#==============================================================
# functions
#==============================================================

def _replace_dic_field(scenario, dic, val):
if scenario == 1:
dic['physicalCoefficients']['feedRate'] = float(val)

# !!!!!
# if you add another scenario, add specifics here
# !!!!!

else:
sys.exit("2d_gs: scenario = {} not supported yet".format(scenario))


def _create_fom_input_dics_impl(scenario, meshPath, kind):
assert(meshPath != None)

values = train_points[scenario] if kind=="train" else test_points[scenario]

result = []
for runNumber, v in values.items():
# make a copy of the base dic
thisDic = copy.deepcopy(base_dic[scenario])

thisDic['runId'] = runNumber

# we are making dics for the FOM, so remove the rom key
if 'rom' in thisDic: del thisDic['rom']

# always need to set mesh and problem
thisDic['general']['meshDir'] = meshPath
thisDic['general']['problem'] = problemName
thisDic['general']['numDofsPerCell'] = numDofsPerCell

_replace_dic_field(scenario, thisDic, v)
result.append(thisDic)

return result

#============================================================
def _create_rom_input_dics_impl(scenario, meshPath, algoName, \
numModes, lsvFile, refStateFile, \
projectorFile = None,\
# we need an optional sampleMeshPath because
# for masked galerkin the mesh to use is the FULL mesh
# but we still need to use gids from the sample mesh
sampleMeshPath = None):

# we only care about test points here
values = test_points[scenario]

result = []
for runNumber, v in values.items():
# make a copy of the base dic
thisDic = copy.deepcopy(base_dic[scenario])

# the run ID MUST be the runNumber here because it has to
# match the enumeration of the FOM
thisDic['runId'] = runNumber

# we are making dics for ROM, so remove the fom key
if 'fom' in thisDic: del thisDic['fom']

# always need to set mesh and problem
thisDic['general']['meshDir'] = meshPath
thisDic['general']['problem'] = problemName
thisDic['general']['numDofsPerCell'] = numDofsPerCell

thisDic['rom']['numModes'] = numModes
thisDic['rom']['podFile'] = lsvFile
thisDic['rom']['algoName'] = algoName
thisDic['rom']['referenceStateFile'] = refStateFile

if projectorFile != None:
# if the projector is nontrivial
thisDic['rom']['projectorFile'] = projectorFile

if "Masked" not in algoName:
# for real hyper-reduction, the mesh IS a sample mesh that contains gids
thisDic['rom']['sampleMeshGidsFile'] = meshPath+"/sample_mesh_gids.dat"
thisDic['rom']['stencilMeshGidsFile'] = meshPath+"/stencil_mesh_gids.dat"

elif "Masked" in algoName:
# if we are doing MASKED, then the mesh to use is a FULL mesh
# so the GIDs must be taken from the sampleMeshPath argument
assert( sampleMeshPath != None )
# gappy Galerkin needs to know about gids files
thisDic['rom']['sampleMeshGidsFile'] = sampleMeshPath+"/sample_mesh_gids.dat"
thisDic['rom']['stencilMeshGidsFile'] = sampleMeshPath+"/stencil_mesh_gids.dat"

_replace_dic_field(scenario, thisDic, v)
result.append(thisDic)

return result
96 changes: 96 additions & 0 deletions workflow_codes/2d_gs/scenarios.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env python

import numpy as np

base_dic = {}
train_points = {}
test_points = {}

sample_mesh_fractions = {}
leverage_scores_betas = {}

rom_energies = {}
rom_basis_sets = {}

'''
GalerkinFull
GalerkinCollocation
GalerkinGappy
GalerkinMaskedCollocation
GalerkinMaskedGappy
'''
rom_algos = {}

'''
SplitVars or DontSplitVars
'''
rom_basis_styles = {}

interpolation_sets = {}
polyfit_sets = {}

################################
################################

'''
------------------------------------------------------
SCENARIO 1
------------------------------------------------------
'''
base_dic[1] = {
'general' : {
'meshDir': "tbd",
},

'fom' : {
'finalTime': 1000.,
'odeScheme': "RungeKutta4",
'dt' : 0.25,
'stateSamplingFreq' : 5,
'velocitySamplingFreq' : 5
},

'rom' : {
'finalTime': 1000.,
'odeScheme': "RungeKutta4",
'dt' : 0.25,
'stateSamplingFreq' : 5,
# the following params are changed by driver scripts
'numModes' : 'tbd',
'podFile' : 'tbd',
'algoName' : 'tbd',
'romInitialStateFile' : 'tbd',
'referenceStateFile' : 'tbd'
},

'physicalCoefficients' : {
'diffusionA': 0.0002,
'diffusionB': 0.00005,
'feedRate' : "tbd",
'killRate' : 0.062
}
}

train_points[1] = {
0: 0.042
}

test_points[1] = {
0: 0.042
}

sample_mesh_fractions[1] = [0.05]
leverage_scores_betas[1] = [0.9]

rom_algos[1] = ["GalerkinFull", "GalerkinGappy", "GalerkinMaskedGappy"]
rom_basis_styles[1] = ["DontSplitVars"]
rom_energies[1] = [99.999999]
rom_basis_sets[1] = {
0: [0]
}


'''
male list of valid scenarios, figure out from keys in dic
'''
valid_scenarios_ids = list(base_dic.keys())
60 changes: 60 additions & 0 deletions workflow_codes/2d_gs/specialize_compute_pod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

#==============================================================
# imports
#==============================================================

from .constants import numDofsPerCell

#==============================================================
# functions (for when we don't split variables)
#==============================================================

def compute_state_pod_and_ref_state_dont_split_vars(dryRun, \
dataDirs,\
singValsFile,\
leftSingVecsFile, \
referenceStateFile):

from pod_functions import _compute_state_pod_and_ref_state_dont_split_vars_impl
_compute_state_pod_and_ref_state_dont_split_vars_impl(dryRun,\
numDofsPerCell,\
dataDirs,\
singValsFile,\
leftSingVecsFile, \
referenceStateFile)

def compute_rhs_pod_if_needed_dont_split_vars(dryRun, \
dataDirs,\
singValsFile,\
leftSingVecsFile):
from pod_functions import _compute_rhs_pod_if_needed_dont_split_vars_impl
_compute_rhs_pod_if_needed_dont_split_vars_impl(dryRun, \
numDofsPerCell,\
dataDirs,\
singValsFile,\
leftSingVecsFile)

#==============================================================
# functions (for when we split variables)
#==============================================================

def compute_state_pod_and_ref_state_split_vars(dryRun, \
dataDirs, \
singValsFile,\
leftSingVecsFile):
# import from parent module common to all demoapps
from pod_functions import _compute_state_pod_and_ref_state_split_vars_impl
_compute_state_pod_and_ref_state_split_vars_impl(dryRun, \
numDofsPerCell,\
dataDirs, \
singValsFile,\
leftSingVecsFile)

def compute_rhs_pod_if_needed_split_vars(dryRun, dataDirs, outDir):
# import from parent module common to all demoapps
from pod_functions import _compute_rhs_pod_if_needed_split_vars_impl
_compute_rhs_pod_if_needed_split_vars_impl(dryRun, \
numDofsPerCell,\
dataDirs,\
singValsFile, \
leftSingVecsFile)
32 changes: 32 additions & 0 deletions workflow_codes/2d_gs/specialize_compute_sample_meshes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

#==============================================================
# imports
#==============================================================

# specific to this problem
from .constants import numDofsPerCell, dimensionality

#==============================================================
# functions
#==============================================================

def make_random_sample_mesh(dryRun, workDir, fraction, outDir):
# import from parent module common to all demoapps
from compute_sample_meshes import _make_random_sample_mesh_impl
_make_random_sample_mesh_impl(dryRun, workDir, \
fraction, \
outDir, \
dimensionality)


def make_leverage_scores_sample_mesh(dryRun, workDir, \
setId, dataDirs, \
listOfFractions, \
listOfBetas):
# import from parent module common to all demoapps
from compute_sample_meshes import _make_leverage_scores_sample_mesh_impl
_make_leverage_scores_sample_mesh_impl(dryRun, workDir, \
setId, dataDirs, \
listOfFractions, \
listOfBetas, \
numDofsPerCell)
67 changes: 67 additions & 0 deletions workflow_codes/2d_gs/specialize_execute_foms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

#==============================================================
# imports
#==============================================================

import os, subprocess

#==============================================================
# functions
#==============================================================

def execute_foms_custom_args(parser):
parser.add_argument("--exedir",
dest="exeDir",
required=True,
help="Full path with executables. Required!")

parser.add_argument("--mesh",
nargs=2, \
dest="meshSize", \
type=int, \
required=True, \
help="Meshing")

parser.add_argument("--nthreads", "--nth",
dest="numThreads", \
type=int, \
default=1,
help="Num threads to use, default = 1")

def run_fom_exe(runDirectory, args):
# from parent module common to all demoapps
from run_exe import _run_exe
_run_exe(runDirectory, args)

def generate_or_set_full_mesh(workingDir, args):
pdaMeshDir = os.path.dirname(__file__) + "/../../meshing_scripts"

nx, ny = args.meshSize[0], args.meshSize[1]
meshDir = workingDir + "/full_mesh" + str(nx) + "x" + str(ny)

if os.path.exists(meshDir):
# if mesh exists, do nothing
print('Mesh {} already exists'.format(meshDir))
else:
# generate
print('Generating mesh {}'.format(meshDir))
# call script
owd = os.getcwd()
args = ("python3", pdaMeshDir + '/create_full_mesh.py',\
"-n", str(nx), str(ny),\
"--outDir", meshDir,\
"--bounds", "-1.25", "1.25", "-1.25", "1.25",\
"--periodic", "x y")

popen = subprocess.Popen(args, stdout=subprocess.PIPE); popen.wait()
output = popen.stdout.read();

return meshDir

def create_fom_train_input_dics(scenario, meshPath):
from .impl import _create_fom_input_dics_impl
return _create_fom_input_dics_impl(scenario, meshPath, "train")

def create_fom_test_input_dics(scenario, meshPath):
from .impl import _create_fom_input_dics_impl
return _create_fom_input_dics_impl(scenario, meshPath, "test")
Loading

0 comments on commit 109633f

Please sign in to comment.