-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcOkadaEarthquake.cpp
325 lines (242 loc) · 8.94 KB
/
cOkadaEarthquake.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* EasyWave - A realtime tsunami simulation program with GPU support.
* Copyright (C) 2014 Andrey Babeyko, Johannes Spazier
* GFZ German Research Centre for Geosciences (http://www.gfz-potsdam.de)
*
* Parts of this program (especially the GPU extension) were developed
* within the context of the following publicly funded project:
* - TRIDEC, EU 7th Framework Programme, Grant Agreement 258723
* (http://www.tridec-online.eu)
*
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
* the European Commission - subsequent versions of the EUPL (the "Licence"),
* complemented with the following provision: For the scientific transparency
* and verification of results obtained and communicated to the public after
* using a modified version of the work, You (as the recipient of the source
* code and author of this modified version, used to produce the published
* results in scientific communications) commit to make this modified source
* code available in a repository that is easily and freely accessible for a
* duration of five years after the communication of the obtained results.
*
* You may not use this work except in compliance with the Licence.
*
* You may obtain a copy of the Licence at:
* https://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
// cOkadaEarthquake.cpp: implementation of the cOkadaEarthquake class
//
//=========================================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utilits.h"
#include "cOkadaEarthquake.h"
#include <cmath>
//=========================================================================
cOkadaEarthquake::cOkadaEarthquake()
{
finalized = 0;
m0 = 0;
nfault = 1;
fault = new cOkadaFault [1];
}
//=========================================================================
cOkadaEarthquake::~cOkadaEarthquake()
{
if( fault != NULL ) delete [] fault;
}
//=========================================================================
// Read fault(s) from a file
int cOkadaEarthquake::read( char *fname )
{
FILE *fp;
char record[256];
int ierr;
nfault = utlGetNumberOfRecords( fname );
if( fault != NULL ) delete [] fault;
fault = new cOkadaFault [nfault];
if( (fp = fopen(fname,"rt")) == NULL ) return Err.post(Err.msgOpenFile(fname));
for( int n=0, line=0; n<nfault; n++ ) {
ierr = utlReadNextRecord(fp, record, &line); if( ierr == EOF ) return Err.post(Err.msgReadFile(fname, line, "Unexpected EOF"));
ierr = fault[n].read( record ); if(ierr) return ierr;
}
fclose( fp );
return 0;
}
//=========================================================================
// check all ruptures for parameter integrity
int cOkadaEarthquake::finalizeInput()
{
int ierr,n;
for( m0=0., n=0; n<nfault; n++ ) {
ierr = fault[n].check();
if( ierr ) {
Err.post( "Fault number: %d", n+1 );
return( 10*n + ierr );
}
m0 += fault[n].getM0();
}
finalized = 1;
return 0;
}
//=========================================================================
double cOkadaEarthquake::getM0()
{
if( !finalized ) { Err.post( "cOkadaEarthquake::getM0: eq not finalized" ); return -RealMax; }
return m0;
}
//=========================================================================
double cOkadaEarthquake::getMw()
{
if( !finalized ) { Err.post( "cOkadaEarthquake::getMw: eq not finalized" ); return -RealMax; }
return (2. / 3. * (log10(m0) - 9.1));
}
//=========================================================================
// get ruptured area to calculate surface displacement
int cOkadaEarthquake::getDeformArea( int round, double& lonmin, double& lonmax, double& latmin, double& latmax )
{
int ierr;
double xmin,xmax,ymin,ymax;
if( !finalized ) return Err.post("cOkadaEarthquake::calculate: eq not finalized");
lonmin = latmin = RealMax;
lonmax = latmax = -RealMax;
for( int n=0; n<nfault; n++ ) {
ierr = fault[n].getDeformArea( xmin, xmax, ymin, ymax ); if(ierr) return ierr;
if( xmin < lonmin ) lonmin = xmin; if( xmax > lonmax ) lonmax = xmax;
if( ymin < latmin ) latmin = ymin; if( ymax > latmax ) latmax = ymax;
}
if( round ) {
lonmin = floor(lonmin);
lonmax = ceil(lonmax);
latmin = floor(latmin);
latmax = ceil(latmax);
}
return 0;
}
//=========================================================================
// Calculate surface displacements by summing effect from all ruptures
int cOkadaEarthquake::calculate( double lon, double lat, double& uz, double& ulon, double &ulat )
{
int n,ierr;
double uzf,ulonf,ulatf;
if( !finalized ) return Err.post("cOkadaEarthquake::calculate: eq not finalized");
for( ulon=ulat=uz=0, n=0; n<nfault; n++ ) {
ierr = fault[n].calculate( lon, lat, uzf, ulonf, ulatf ); if(ierr) return ierr;
uz+= uzf; ulon += ulonf; ulat += ulatf;
}
return 0;
}
//=========================================================================
// Calculate surface displacements by summing effect from all ruptures
int cOkadaEarthquake::calculate( double lon, double lat, double& uz )
{
int n,ierr;
double uzf;
if( !finalized ) return Err.post("cOkadaEarthquake::calculate: eq not finalized");
for( uz=0, n=0; n<nfault; n++ ) {
ierr = fault[n].calculate( lon, lat, uzf ); if(ierr) return ierr;
uz += uzf;
}
return 0;
}
//=========================================================================
// Calculate surface displacements by summing effect from all ruptures
int cOkadaEarthquake::calculate( cOgrd& uZ )
{
int i,j,n,ierr;
double uzf;
if( !finalized ) return Err.post("cOkadaEarthquake::calculate: eq not finalized");
ierr = setGrid( uZ ); if( ierr ) return ierr;
// calculate displacenents on a grid
for( i=0; i<uZ.nx; i++ ) {
for( j=0; j<uZ.ny; j++ ) {
for( n=0; n<nfault; n++ ) {
ierr = fault[n].calculate( uZ.getX(i,j), uZ.getY(i,j), uzf ); if(ierr) return ierr;
uZ(i,j) = uZ(i,j) + uzf;
}
}
}
return 0;
}
//=========================================================================
// Calculate surface displacements by summing effect from all ruptures
int cOkadaEarthquake::calculate( cOgrd& uZ, cOgrd& uLon, cOgrd& uLat )
{
int i,j,n,ierr;
double uzf,ulonf,ulatf;
if( !finalized ) return Err.post("cOkadaEarthquake::calculate: eq not finalized");
ierr = setGrid( uZ ); if(ierr) return ierr;
uLon = uZ;
uLat = uZ;
// calculate displacenents on a grid
for( i=0; i<uZ.nx; i++ ) {
for( j=0; j<uZ.ny; j++ ) {
for( n=0; n<nfault; n++ ) {
ierr = fault[n].calculate( uZ.getX(i,j), uZ.getY(i,j), uzf, ulonf, ulatf ); if(ierr) return ierr;
uZ(i,j) = uZ(i,j) + uzf;
uLon(i,j) = uLon(i,j) + ulonf;
uLat(i,j) = uLat(i,j) + ulatf;
}
}
}
return 0;
}
//=========================================================================
// Check and prepare grid for calculations
int cOkadaEarthquake::setGrid( cOgrd& u )
{
int ierr;
if( u.xmin == u.xmax || u.ymin == u.ymax ) {
ierr = getDeformArea( 1, u.xmin, u.xmax, u.ymin, u.ymax ); if(ierr) return ierr;
}
if( u.nx*u.ny != 0 && u.dx*u.dy != 0 )
u.resetVal();
else if( u.nx*u.ny != 0 )
u.initialize( u.xmin, u.xmax, u.nx, u.ymin, u.ymax, u.ny );
else if( u.dx*u.dy != 0 )
u.initialize( u.xmin, u.xmax, u.dx, u.ymin, u.ymax, u.dy );
else {
u.dx = u.dy = 0.02; // a bit more than 1 arc minute (0.0166667)
u.initialize( u.xmin, u.xmax, u.dx, u.ymin, u.ymax, u.dy );
}
return 0;
}
//=========================================================================
int cOkadaEarthquake::calculate( cObsArray& arr )
{
int k,n,ierr;
double uzf,ulonf,ulatf;
if( !finalized ) return Err.post("cOkadaEarthquake::calculate: eq not finalized");
for( k=0; k<arr.nPos; k++ ) {
for( arr.obs[k][0]=arr.obs[k][1]=arr.obs[k][2]=0., n=0; n<nfault; n++ ) {
ierr = fault[n].calculate( arr.lon[k], arr.lat[k], uzf, ulonf, ulatf ); if(ierr) return ierr;
arr.obs[k][0] += ulonf;
arr.obs[k][1] += ulatf;
arr.obs[k][2] += uzf;
}
}
return 0;
}
//=========================================================================
// print earthquake parameter data into a new string
char* cOkadaEarthquake::sprint()
{
char *info,buf[256];
int n,bufsize;
bufsize = 64 + nfault*128;
info = new char[bufsize];
sprintf( info, "Mw=%.2f M0=%-g", getMw(), getM0() );
sprintf( buf, "\nNumber of faults: %-3d", nfault ); strcat( info, buf );
for( n=0; n<nfault; n++ ) {
sprintf( buf, "\n Fault %d(%d): length=%-.1f width=%-.1f slip=%-.2f top=%-.2f",
n+1, nfault, fault[n].length/1000, fault[n].width/1000, fault[n].slip, fault[n].getZtop()/1000 );
strcat( info, buf );
}
return info;
}