-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquerymesh.c
More file actions
214 lines (171 loc) · 5.02 KB
/
querymesh.c
File metadata and controls
214 lines (171 loc) · 5.02 KB
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
/**
* querymesh.c
*
* Copyright (c) 2008 Ricardo Taborda, 2004 Tiankai Tu
* All rights reserved. May not be used, modified, or copied
* without permission.
*
* Contact:
* Ricardo Taborda
* Civil and Environmental Engineering
* Carnegie Mellon University
* 5000 Forbes Avenue
* Pittsburgh, PA 15213
* rtaborda@andrew.cmu.edu
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <sys/time.h>
#include "etree.h"
//#include "cvm.h"
#define CVMBUFFERSIZE 100 /* performance knob */
/**
* mdata_t: Mesh database record payload (excluing the locational code).
*
*/
typedef struct mdata_t {
int64_t nid[8];
float edgesize, Vp, Vs, rho;
} mdata_t;
/**
* mesh_query:
*
* - return 0 if OK, -1 on error
*
*/
int mesh_query(etree_t *ep, double east_m, double north_m, double depth_m,
const char *field, void *payload)
{
double tickSize;
etree_addr_t queryAddr;
/* new factor of 2 at the end... who knows why but works */
tickSize = 600000.0 / ( 2147483648.0 / 2 );
queryAddr.y = (etree_tick_t)(east_m / tickSize);
queryAddr.x = (etree_tick_t)(north_m / tickSize);
queryAddr.z = (etree_tick_t)(depth_m / tickSize);
queryAddr.level = ETREE_MAXLEVEL;
if (etree_search(ep, queryAddr, NULL, field, payload) != 0) {
fprintf(stderr, "mesh_query: %s\n",etree_strerror(etree_errno(ep)));
return -1;
}
return 0;
}
int main(int argc, char **argv)
{
char *meshetree, *outputfile;
etree_t *meshEp = NULL;
double east_m, north_m, depth_m, value, res;
double EAST = 600000, NORTH = 300000;
double tolerancefence = 0.0001;
mdata_t rawElem;
int resp, useId, field, i=0, j=0, imax, jmax;
FILE *fp;
/* -------------------------------------------------------------------------
* BASIC FOR ALL OPTIONS
* -------------------------------------------------------------------------
*/
if (argc < 3 ) {
fprintf(stderr, "\nReview querymesh usage\n");
} else {
meshetree = argv[1];
meshEp = etree_open(meshetree, O_RDONLY, CVMBUFFERSIZE, 0, 0);
if (!meshEp) {
fprintf(stderr, "Cannot open mesh etree %s\n", meshetree);
exit(1);
}
sscanf(argv[2], "%d", &useId);
}
/* -------------------------------------------------------------------------
* OPTION 1: SINGLE QUERY
* -------------------------------------------------------------------------
*/
if (useId == 1) { /* Start option 1 */
if (argc != 6) {
fprintf(stderr,
"\nusage: querymesh meshetree use east_m north_m depth_m\n");
exit(1);
}
sscanf(argv[3], "%lf", &east_m);
sscanf(argv[4], "%lf", &north_m);
sscanf(argv[5], "%lf", &depth_m);
resp = mesh_query(meshEp, east_m, north_m, depth_m, NULL, &rawElem.nid[0]);
if (resp != 0) {
fprintf(stderr, "Cannot find the query point\n");
exit(1);
} else {
fprintf(stdout,"%f %f %f %.4f %.4f %.4f\n",
east_m, north_m, depth_m, rawElem.Vp, rawElem.Vs, rawElem.rho);
}
} /* End Option 1 */
/* -------------------------------------------------------------------------
* OPTION 2: HORIZONTAL SLICE
* -------------------------------------------------------------------------
*/
if (useId == 2) { /* Start option 2 */
if (argc != 7) {
fprintf(stderr,
"\nusage: querymesh meshetree use field res depth_m outputfile\n");
exit(1);
}
sscanf(argv[3], "%d", &field);
sscanf(argv[4],"%lf", &res);
sscanf(argv[5],"%lf", &depth_m);
outputfile = argv[6];
fp = fopen(outputfile,"w");
if ( fp == NULL ) {
fprintf(stderr, "Cannot open the output file");
exit(1);
}
imax = EAST / res + 1;
jmax = NORTH / res + 1;
for (i = 0; i < imax; i++) {
if ( i == imax - 1 ) {
east_m = EAST - tolerancefence;
} else {
east_m = i * res;
}
for (j = 0; j < jmax; j++) {
if ( j == jmax - 1 ) {
north_m = NORTH - tolerancefence;
} else {
north_m = j * res;
}
resp = mesh_query(meshEp, east_m, north_m, depth_m,
NULL, &rawElem.nid[0]);
switch ( field )
{
case 1:
value = rawElem.Vp;
break;
case 2:
value = rawElem.Vs;
break;
case 3:
value = rawElem.rho;
break;
default:
fprintf(stderr, "Cannot assign correct field\n");
exit(1);
break;
}
if (resp != 0) {
fprintf(stderr, "Cannot find the query point\n");
exit(1);
} else {
fprintf(fp, "%12.0f %12.0f %12.4f\n",
east_m, north_m, value);
}
}
}
fclose(fp);
} /* End Option 2 */
/* -------------------------------------------------------------------------
* OPTIONs END
* -------------------------------------------------------------------------
*/
etree_close(meshEp);
return 0;
}