Coverage for /usr/local/lib/python3.11/dist-packages/grond/problems/simple_landslide/problem.py: 56%

55 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2025-04-03 09:31 +0000

1# https://pyrocko.org/grond - GPLv3 

2# 

3# The Grond Developers, 21st Century 

4import numpy as num 

5import logging 

6 

7from pyrocko import gf, util 

8from pyrocko.guts import String, Float, Dict 

9 

10from grond.meta import Forbidden, expand_template, Parameter, \ 

11 has_get_plot_classes 

12 

13from ..base import Problem, ProblemConfig 

14 

15guts_prefix = 'grond' 

16logger = logging.getLogger('grond.problems.simple_landslide.problem') 

17km = 1e3 

18as_km = dict(scale_factor=km, scale_unit='km') 

19 

20 

21class SimpleLandslideProblemConfig(ProblemConfig): 

22 

23 ranges = Dict.T(String.T(), gf.Range.T()) 

24 distance_min = Float.T(default=0.0) 

25 

26 def get_problem(self, event, target_groups, targets): 

27 if event.depth is None: 

28 event.depth = 0. 

29 

30 base_source = gf.SimpleLandslideSource.from_pyrocko_event( 

31 event, 

32 anchor_stf='centroid') 

33 

34 subs = dict( 

35 event_name=event.name, 

36 event_time=util.time_to_str(event.time)) 

37 

38 problem = SimpleLandslideProblem( 

39 name=expand_template(self.name_template, subs), 

40 base_source=base_source, 

41 target_groups=target_groups, 

42 targets=targets, 

43 ranges=self.ranges, 

44 distance_min=self.distance_min, 

45 norm_exponent=self.norm_exponent) 

46 

47 return problem 

48 

49 

50@has_get_plot_classes 

51class SimpleLandslideProblem(Problem): 

52 

53 problem_parameters = [ 

54 Parameter('time', 's', label='Time'), 

55 Parameter('north_shift', 'm', label='Northing', **as_km), 

56 Parameter('east_shift', 'm', label='Easting', **as_km), 

57 Parameter('depth', 'm', label='Depth', **as_km), 

58 Parameter('impulse_n', 'Ns', label='$p_{n}$'), 

59 Parameter('impulse_e', 'Ns', label='$p_{e}$'), 

60 Parameter('impulse_d', 'Ns', label='$p_{d}$'), 

61 Parameter('azimuth', 'deg', label='Azimuth'), 

62 Parameter('duration_acc_v', 's', label='T_{av}'), 

63 Parameter('duration_acc_h', 's', label='T_{ah}'), 

64 Parameter('duration_dec_v', 's', label='T_{dv}'), 

65 Parameter('duration_dec_h', 's', label='T_{dh}'), 

66 ] 

67 

68 dependants = [] 

69 

70 distance_min = Float.T(default=0.0) 

71 

72 def __init__(self, **kwargs): 

73 Problem.__init__(self, **kwargs) 

74 self.deps_cache = {} 

75 self.problem_parameters = self.problem_parameters 

76 

77 def get_source(self, x): 

78 d = self.get_parameter_dict(x) 

79 p = {} 

80 for k in self.base_source.keys(): 

81 if k in d: 

82 p[k] = float( 

83 self.ranges[k].make_relative(self.base_source[k], d[k])) 

84 

85 stf_v = gf.SimpleLandslideSTF( 

86 duration_acceleration=d['duration_acc_v'], 

87 duration_deceleration=d['duration_dec_v']) 

88 stf_h = gf.SimpleLandslideSTF( 

89 duration_acceleration=d['duration_acc_h'], 

90 duration_deceleration=d['duration_dec_h']) 

91 

92 return self.base_source.clone(**p, stf_v=stf_v, stf_h=stf_h) 

93 

94 def make_dependant(self, xs, pname): 

95 pass 

96 

97 def pack(self, source): 

98 x = num.array([ 

99 source.time - self.base_source.time, 

100 source.north_shift, 

101 source.east_shift, 

102 source.depth, 

103 source.impulse_n, 

104 source.impulse_e, 

105 source.impulse_d, 

106 source.stf_v.duration_acceleration, 

107 source.stf_h.duration_acceleration, 

108 source.stf_v.duration_deceleration, 

109 source.stf_h.duration_deceleration], dtype=float) 

110 

111 return x 

112 

113 def preconstrain(self, x): 

114 source = self.get_source(x) 

115 if any(self.distance_min > source.distance_to(t) 

116 for t in self.waveform_targets + self.phase_pick_targets): 

117 raise Forbidden() 

118 return x 

119 

120 @classmethod 

121 def get_plot_classes(cls): 

122 from ..singleforce import plot 

123 plots = super(SimpleLandslideProblem, cls).get_plot_classes() 

124 plots.extend([plot.SFLocationPlot, plot.SFForcePlot]) 

125 return plots 

126 

127 

128__all__ = ''' 

129 SimpleLandslideProblem 

130 SimpleLandslideProblemConfig 

131'''.split()