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

69 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-10-26 16:25 +0000

1import numpy as num 

2import logging 

3 

4from pyrocko import gf, util 

5from pyrocko.guts import String, Float, Dict, Int, Bool 

6 

7from grond.meta import expand_template, Parameter, has_get_plot_classes 

8 

9from ..base import Problem, ProblemConfig 

10from .. import CMTProblem 

11 

12guts_prefix = 'grond' 

13logger = logging.getLogger('grond.problems.rectangular.problem') 

14km = 1e3 

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

16 

17 

18class RectangularProblemConfig(ProblemConfig): 

19 

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

21 decimation_factor = Int.T(default=1) 

22 distance_min = Float.T(default=0.) 

23 point_source_target_balancing = Bool.T( 

24 default=False, 

25 help='If ``True``, target balancing (if used) is performed on a ' 

26 'moment tensor point source at the events location. It increases ' 

27 'the speed, but might lead to not fully optimized target weights.' 

28 ) 

29 

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

31 self.check_deprecations() 

32 

33 if self.decimation_factor != 1: 

34 logger.warning( 

35 'Decimation factor for rectangular source set to %i. Results ' 

36 'may be inaccurate.' % self.decimation_factor) 

37 

38 base_source = gf.RectangularSource.from_pyrocko_event( 

39 event, 

40 anchor='top', 

41 decimation_factor=self.decimation_factor) 

42 

43 subs = dict( 

44 event_name=event.name, 

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

46 

47 cmt_problem = None 

48 if self.point_source_target_balancing: 

49 base_source_cmt = gf.MTSource.from_pyrocko_event(event) 

50 

51 stf = gf.HalfSinusoidSTF() 

52 stf.duration = event.duration or 0.0 

53 

54 base_source_cmt.stf = stf 

55 

56 ranges = dict( 

57 time=self.ranges['time'], 

58 north_shift=self.ranges['north_shift'], 

59 east_shift=self.ranges['east_shift'], 

60 depth=self.ranges['depth'], 

61 magnitude=gf.Range( 

62 start=event.magnitude - 1., 

63 stop=event.magnitude + 1.), 

64 duration=gf.Range(start=0., stop=stf.duration * 2.), 

65 rmnn=gf.Range(start=-1.41421, stop=1.41421), 

66 rmee=gf.Range(start=-1.41421, stop=1.41421), 

67 rmdd=gf.Range(start=-1.41421, stop=1.41421), 

68 rmne=gf.Range(start=-1., stop=1.), 

69 rmnd=gf.Range(start=-1., stop=1.), 

70 rmed=gf.Range(start=-1., stop=1.)) 

71 

72 cmt_problem = CMTProblem( 

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

74 base_source=base_source_cmt, 

75 distance_min=self.distance_min, 

76 target_groups=target_groups, 

77 targets=targets, 

78 ranges=ranges, 

79 mt_type='dc', 

80 stf_type='HalfSinusoidSTF', 

81 norm_exponent=self.norm_exponent) 

82 

83 problem = RectangularProblem( 

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

85 base_source=base_source, 

86 distance_min=self.distance_min, 

87 target_groups=target_groups, 

88 targets=targets, 

89 ranges=self.ranges, 

90 norm_exponent=self.norm_exponent, 

91 cmt_problem=cmt_problem) 

92 

93 return problem 

94 

95 

96@has_get_plot_classes 

97class RectangularProblem(Problem): 

98 

99 problem_parameters = [ 

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

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

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

103 Parameter('length', 'm', label='Length', optional=False, **as_km), 

104 Parameter('width', 'm', label='Width', optional=False, **as_km), 

105 Parameter('slip', 'm', label='Slip', optional=False), 

106 Parameter('strike', 'deg', label='Strike'), 

107 Parameter('dip', 'deg', label='Dip'), 

108 Parameter('rake', 'deg', label='Rake') 

109 ] 

110 

111 problem_waveform_parameters = [ 

112 Parameter('nucleation_x', 'offset', label='Nucleation X'), 

113 Parameter('nucleation_y', 'offset', label='Nucleation Y'), 

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

115 Parameter('velocity', 'm/s', label='Rupture Velocity') 

116 ] 

117 

118 dependants = [] 

119 

120 distance_min = Float.T(default=0.0) 

121 

122 cmt_problem = Problem.T(optional=True) 

123 

124 def set_engine(self, engine): 

125 self._engine = engine 

126 

127 if self.cmt_problem is not None: 

128 self.cmt_problem.set_engine(engine) 

129 

130 def pack(self, source): 

131 arr = self.get_parameter_array(source) 

132 for ip, p in enumerate(self.parameters): 

133 if p.name == 'time': 

134 arr[ip] -= self.base_source.time 

135 return arr 

136 

137 def get_source(self, x): 

138 d = self.get_parameter_dict(x) 

139 p = {} 

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

141 if k in d: 

142 p[k] = float( 

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

144 

145 source = self.base_source.clone(**p) 

146 

147 return source 

148 

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

150 x = num.zeros(self.nparameters) 

151 for i in range(self.nparameters): 

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

153 

154 return x 

155 

156 def preconstrain(self, x, optimizer=False): 

157 # source = self.get_source(x) 

158 # if any(self.distance_min > source.distance_to(t) 

159 # for t in self.targets): 

160 # raise Forbidden() 

161 return x 

162 

163 @classmethod 

164 def get_plot_classes(cls): 

165 plots = super(RectangularProblem, cls).get_plot_classes() 

166 return plots 

167 

168 

169__all__ = ''' 

170 RectangularProblem 

171 RectangularProblemConfig 

172'''.split()