Skip to content

Commit

Permalink
config: enable Yoffe in Inversion
Browse files Browse the repository at this point in the history
  • Loading branch information
hvasbath committed Apr 25, 2023
1 parent a8192f3 commit 59323b8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 23 deletions.
50 changes: 45 additions & 5 deletions beat/apps/beat.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ def setup(parser):
% ('", "'.join(name for name in bconfig.source_names)),
)

parser.add_option(
"--stf_type",
dest="stf_type",
choices=bconfig.stf_names,
default="HalfSinusoid",
help="Source time function type to solve for; %s"
'. Default: "HalfSinusoid"'
% ('", "'.join(name for name in bconfig.stf_names)),
)

parser.add_option(
"--n_sources",
dest="n_sources",
Expand Down Expand Up @@ -347,6 +357,7 @@ def setup(parser):
datatypes=options.datatypes,
mode=options.mode,
source_type=options.source_type,
stf_type=options.stf_type,
n_sources=options.n_sources,
waveforms=options.waveforms,
sampler=options.sampler,
Expand Down Expand Up @@ -841,6 +852,16 @@ def setup(parser):
% ('", "'.join(name for name in bconfig.source_names)),
)

parser.add_option(
"--stf_type",
dest="stf_type",
choices=bconfig.stf_names,
default=None,
help="Source time function type to replace in config; %s"
'. Default: "dont change"'
% ('", "'.join(name for name in bconfig.stf_names)),
)

parser.add_option(
"--mode",
dest="mode",
Expand Down Expand Up @@ -950,18 +971,37 @@ def setup(parser):
shutil.copytree(linear_gf_dir_name, cloned_linear_gf_dir_name)
logger.info("Successfully cloned linear GF libraries.")

# update source type
if options.source_type is None:
old_priors = copy.deepcopy(c.problem_config.priors)

new_priors = c.problem_config.select_variables()
variable_names = c.problem_config.select_variables(request=["source_type"])
for variable_name in variable_names:
if variable_name in list(old_priors.keys()):
c.problem_config.priors[variable_name] = old_priors[variable_name]
else:
logger.info('Replacing source with "%s"' % options.source_type)

c.problem_config.source_type = options.source_type
variables = c.problem_config.select_variables(request=["source_type"])
c.problem_config.init_vars(variables, update=False)
c.problem_config.set_decimation_factor()
re_init = False

# update stf type
if options.stf_type is None:
old_priors = copy.deepcopy(c.problem_config.priors)

new_priors = c.problem_config.select_variables(request=["stf_type"])
for prior in new_priors:
if prior in list(old_priors.keys()):
c.problem_config.priors[prior] = old_priors[prior]

else:
logger.info('Replacing source with "%s"' % options.source_type)
c.problem_config.source_type = options.source_type
c.problem_config.init_vars()
logger.info('Replacing STF with "%s"' % options.stf_type)

c.problem_config.stf_type = options.stf_type
variables = c.problem_config.select_variables(request=["stf_type"])
c.problem_config.init_vars(variables, update=True)
c.problem_config.set_decimation_factor()
re_init = False

Expand Down
53 changes: 35 additions & 18 deletions beat/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pyrocko import gf, model, trace, util
from pyrocko.cake import load_model
from pyrocko.gf import RectangularSource as PyrockoRS
from pyrocko.gf.seismosizer import Cloneable, LocalEngine, stf_classes
from pyrocko.gf.seismosizer import Cloneable, LocalEngine
from pyrocko.guts import (
ArgumentError,
Bool,
Expand Down Expand Up @@ -84,13 +84,21 @@
Boxcar
Triangular
HalfSinusoid
RegularizedYoffe
""".split()

stf_classes = [
gf.BoxcarSTF,
gf.TriangularSTF,
gf.HalfSinusoidSTF,
gf.RegularizedYoffeSTF,
]

source_catalog = {
name: source_class for name, source_class in zip(source_names, source_classes)
}

stf_catalog = {name: stf_class for name, stf_class in zip(stf_names, stf_classes[1:4])}
stf_catalog = {name: stf_class for name, stf_class in zip(stf_names, stf_classes)}

interseismic_vars = [
"east_shift",
Expand Down Expand Up @@ -1340,7 +1348,7 @@ def __init__(self, **kwargs):

Object.__init__(self, **kwargs)

def init_vars(self, variables=None, nvars=None):
def init_vars(self, variables=None, nvars=None, update=False):
"""
Initiate priors based on the problem mode and datatypes.
Expand All @@ -1352,7 +1360,8 @@ def init_vars(self, variables=None, nvars=None):
if variables is None:
variables = self.select_variables()

self.priors = OrderedDict()
if not update:
self.priors = OrderedDict()

for variable in variables:

Expand Down Expand Up @@ -1396,7 +1405,7 @@ def set_vars(self, bounds_dict, attribute="priors", init=False):

setattr(self, attribute, upd_dict)

def select_variables(self):
def select_variables(self, request=["stf_type", "source_type"]):
"""
Return model variables depending on problem config.
"""
Expand All @@ -1410,21 +1419,24 @@ def select_variables(self):
for datatype in self.datatypes:
if datatype in vars_catalog.keys():
if self.mode == geometry_mode_str:
if self.source_type in vars_catalog[datatype].keys():
source = vars_catalog[datatype][self.source_type]
svars = set(source.keys())
if "source_type" in request:
if self.source_type in vars_catalog[datatype].keys():
source = vars_catalog[datatype][self.source_type]
svars = set(source.keys())

if isinstance(source(), (PyrockoRS, gf.ExplosionSource)):
svars.discard("magnitude")
if isinstance(source(), (PyrockoRS, gf.ExplosionSource)):
svars.discard("magnitude")

variables += utility.weed_input_rvs(svars, self.mode, datatype)
else:
raise ValueError(
"Source Type not supported for type"
" of problem, and datatype!"
)
variables += utility.weed_input_rvs(
svars, self.mode, datatype
)
else:
raise ValueError(
"Source Type not supported for type"
" of problem, and datatype!"
)

if datatype == "seismic":
if datatype == "seismic" and "stf_type" in request:
if self.stf_type in stf_catalog.keys():
stf = stf_catalog[self.stf_type]
variables += utility.weed_input_rvs(
Expand Down Expand Up @@ -2044,6 +2056,7 @@ def init_config(
datatypes=["geodetic"],
mode="geometry",
source_type="RectangularSource",
stf_type="TriangularSTF",
n_sources=1,
waveforms=["any_P"],
sampler="SMC",
Expand Down Expand Up @@ -2230,7 +2243,11 @@ def init_dataset_config(config, datatype):
c.seismic_config.gf_config = lgf_config

c.problem_config = ProblemConfig(
n_sources=n_sources, datatypes=datatypes, mode=mode, source_type=source_type
n_sources=n_sources,
datatypes=datatypes,
mode=mode,
source_type=source_type,
stf_type=stf_type,
)
c.problem_config.init_vars()
c.problem_config.set_decimation_factor()
Expand Down

0 comments on commit 59323b8

Please sign in to comment.