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

61 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2024-11-27 15:15 +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 expand_template, Parameter, has_get_plot_classes 

11 

12from ..base import Problem, ProblemConfig 

13 

14guts_prefix = 'grond' 

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

16km = 1e3 

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

18 

19 

20class SimpleLandslideProblemConfig(ProblemConfig): 

21 

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

23 distance_min = Float.T(default=0.0) 

24 

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

26 if event.depth is None: 

27 event.depth = 0. 

28 

29 base_source = gf.SimpleLandslideSource.from_pyrocko_event( 

30 event, 

31 anchor_stf='centroid') 

32 

33 subs = dict( 

34 event_name=event.name, 

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

36 

37 problem = SimpleLandslideProblem( 

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

39 base_source=base_source, 

40 target_groups=target_groups, 

41 targets=targets, 

42 ranges=self.ranges, 

43 distance_min=self.distance_min, 

44 norm_exponent=self.norm_exponent) 

45 

46 return problem 

47 

48 def get_depth_range(self): 

49 return self.ranges['depth'] 

50 

51 

52@has_get_plot_classes 

53class SimpleLandslideProblem(Problem): 

54 

55 problem_parameters = [ 

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

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

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

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

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

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

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

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

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

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

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

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

68 ] 

69 

70 dependants = [] 

71 

72 distance_min = Float.T(default=0.0) 

73 

74 def __init__(self, **kwargs): 

75 Problem.__init__(self, **kwargs) 

76 self.deps_cache = {} 

77 self.problem_parameters = self.problem_parameters 

78 

79 def get_source(self, x): 

80 d = self.get_parameter_dict(x) 

81 p = {} 

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

83 if k in d: 

84 p[k] = float( 

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

86 

87 stf_v = gf.SimpleLandslideSTF( 

88 duration_acceleration=d['duration_acc_v'], 

89 duration_deceleration=d['duration_dec_v']) 

90 stf_h = gf.SimpleLandslideSTF( 

91 duration_acceleration=d['duration_acc_h'], 

92 duration_deceleration=d['duration_dec_h']) 

93 

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

95 

96 def make_dependant(self, xs, pname): 

97 pass 

98 

99 def pack(self, source): 

100 x = num.array([ 

101 source.time - self.base_source.time, 

102 source.north_shift, 

103 source.east_shift, 

104 source.depth, 

105 source.impulse_n, 

106 source.impulse_e, 

107 source.impulse_d, 

108 source.stf_v.duration_acceleration, 

109 source.stf_h.duration_acceleration, 

110 source.stf_v.duration_deceleration, 

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

112 

113 return x 

114 

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

116 x = num.zeros(self.nparameters) 

117 for i in range(self.nparameters): 

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

119 

120 return x.tolist() 

121 

122 def preconstrain(self, x): 

123 d = self.get_parameter_dict(x) 

124 x = self.get_parameter_array(d) 

125 return x 

126 

127 @classmethod 

128 def get_plot_classes(cls): 

129 from ..singleforce import plot 

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

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

132 return plots 

133 

134 

135__all__ = ''' 

136 SimpleLandslideProblem 

137 SimpleLandslideProblemConfig 

138'''.split()