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

59 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2024-10-28 08:36 +0000

1import numpy as num 

2import logging 

3 

4from pyrocko import gf, util 

5from pyrocko.guts import String, Float, Dict 

6 

7from grond.meta import expand_template, Parameter, has_get_plot_classes 

8 

9from ..base import Problem, ProblemConfig 

10 

11guts_prefix = 'grond' 

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

13km = 1e3 

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

15 

16 

17class SimpleLandslideProblemConfig(ProblemConfig): 

18 

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

20 distance_min = Float.T(default=0.0) 

21 

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

23 if event.depth is None: 

24 event.depth = 0. 

25 

26 base_source = gf.SimpleLandslideSource.from_pyrocko_event( 

27 event, 

28 anchor_stf='centroid') 

29 

30 subs = dict( 

31 event_name=event.name, 

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

33 

34 problem = SimpleLandslideProblem( 

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

36 base_source=base_source, 

37 target_groups=target_groups, 

38 targets=targets, 

39 ranges=self.ranges, 

40 distance_min=self.distance_min, 

41 norm_exponent=self.norm_exponent) 

42 

43 return problem 

44 

45 

46@has_get_plot_classes 

47class SimpleLandslideProblem(Problem): 

48 

49 problem_parameters = [ 

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

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

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

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

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

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

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

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

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

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

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

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

62 ] 

63 

64 dependants = [] 

65 

66 distance_min = Float.T(default=0.0) 

67 

68 def __init__(self, **kwargs): 

69 Problem.__init__(self, **kwargs) 

70 self.deps_cache = {} 

71 self.problem_parameters = self.problem_parameters 

72 

73 def get_source(self, x): 

74 d = self.get_parameter_dict(x) 

75 p = {} 

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

77 if k in d: 

78 p[k] = float( 

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

80 

81 stf_v = gf.SimpleLandslideSTF( 

82 duration_acceleration=d['duration_acc_v'], 

83 duration_deceleration=d['duration_dec_v']) 

84 stf_h = gf.SimpleLandslideSTF( 

85 duration_acceleration=d['duration_acc_h'], 

86 duration_deceleration=d['duration_dec_h']) 

87 

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

89 

90 def make_dependant(self, xs, pname): 

91 pass 

92 

93 def pack(self, source): 

94 x = num.array([ 

95 source.time - self.base_source.time, 

96 source.north_shift, 

97 source.east_shift, 

98 source.depth, 

99 source.impulse_n, 

100 source.impulse_e, 

101 source.impulse_d, 

102 source.stf_v.duration_acceleration, 

103 source.stf_h.duration_acceleration, 

104 source.stf_v.duration_deceleration, 

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

106 

107 return x 

108 

109 def random_uniform(self, xbounds, rstate, fixed_magnitude=None): 

110 x = num.zeros(self.nparameters) 

111 for i in range(self.nparameters): 

112 x[i] = rstate.uniform(xbounds[i, 0], xbounds[i, 1]) 

113 

114 return x.tolist() 

115 

116 def preconstrain(self, x): 

117 d = self.get_parameter_dict(x) 

118 x = self.get_parameter_array(d) 

119 return x 

120 

121 @classmethod 

122 def get_plot_classes(cls): 

123 from ..singleforce import plot 

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

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

126 return plots 

127 

128 

129__all__ = ''' 

130 SimpleLandslideProblem 

131 SimpleLandslideProblemConfig 

132'''.split()