GeoTessCExamples  2.0
Integrate2D.c
Go to the documentation of this file.
1 //- ****************************************************************************
2 //-
3 //- Copyright 2009 Sandia Corporation. Under the terms of Contract
4 //- DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
5 //- retains certain rights in this software.
6 //-
7 //- BSD Open Source License.
8 //- All rights reserved.
9 //-
10 //- Redistribution and use in source and binary forms, with or without
11 //- modification, are permitted provided that the following conditions are met:
12 //-
13 //- * Redistributions of source code must retain the above copyright notice,
14 //- this list of conditions and the following disclaimer.
15 //- * Redistributions in binary form must reproduce the above copyright
16 //- notice, this list of conditions and the following disclaimer in the
17 //- documentation and/or other materials provided with the distribution.
18 //- * Neither the name of Sandia National Laboratories nor the names of its
19 //- contributors may be used to endorse or promote products derived from
20 //- this software without specific prior written permission.
21 //-
22 //- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 //- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 //- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 //- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 //- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 //- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 //- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 //- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 //- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 //- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 //- POSSIBILITY OF SUCH DAMAGE.
33 //-
34 //- ****************************************************************************
35 
36 #include "CpuTimerC.h"
37 #include "GeoTessMetaDataC.h"
38 #include "GeoTessModelC.h"
39 #include "GeoTessUtilsC.h"
40 #include "PointMapC.h"
41 #include "PolygonC.h"
42 #include "GeoTessGridC.h"
43 #include "InterpolatorTypeC.h"
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include "DataTypeC.h"
48 #include "ErrorHandler.h"
49 #include "bool.h"
50 
51 #define len 512
52 
53 // conversion factors between radians and degrees
54 static const double rtd = 180./3.1415926535897932384626;
55 static const double dtr = 3.1415926535897932384626/180.;
56 
57 double integrate2D(GeoTessModelC* model, double* pointA, double* pointB, int attributeIndex);
58 
59 void errorCheck();
60 
61 GeoTessModelC* velocityModel(char* gridFile);
62 
63 
64 int main(int argc, char** argv)
65 {
66  int i;
67 
68  GeoTessModelC* model;
69 
70  EarthShapeC* ellipsoid;
71 
72  double station[3], event[3];
73 
74  double pathIntegral_seconds;
75 
76  // Generate a starting model. In this example, the starting model is
77  // very simple, consisting of constant values of velocity at each
78  // node of the grid. Real applications could start from some other
79  // starting model loaded from a file.
80  //
81  // If an error message appears indicating that the file could not be
82  // found, it is likely because the program was not executed from
83  // directory GeoTessBuilderExamples/tomo2dTest
84  model = velocityModel("geotess_grid_04000.geotess");
85 
86  ellipsoid = geomodel_getEarthShape(model);
87 
88  earthshape_getVectorDegrees(ellipsoid, 0., 30., station);
89 
90  earthshape_getVectorDegrees(ellipsoid, 0., 40., event);
91 
92  pathIntegral_seconds = integrate2D(model, station, event, 0);
93 
94  printf("pathIntegral_seconds = %1.3f\n", pathIntegral_seconds);
95 
96  earthshape_destroy(ellipsoid);
97 
98  // free all the models we created.
99  geomodel_destroy(model);
100 
101  // check for errors and abort if any have occurred.
102  errorCheck();
103 
104  printf("Integrate2D completed successfully.\n");
105  return 0;
106 }
107 
108 double integrate2D(GeoTessModelC* model, double* pointA, double* pointB, int attributeIndex)
109 {
110  int j;
111 
112  // point spacing for integrating along the path, in radians.
113  double pointSpacing = 0.1 * dtr;
114 
115  double earthRadius = -1.;
116 
117  double integral = 0;
118 
119  double sumWeights = 0;
120 
121  // Initialize these arrays to NULL. Memory will be allocated for them
122  // in function geomodel_getPathIntegral2DW() as needed.
123  int nWeights=0;
124  int allocatedSize=0;
125  int* pointIndices=NULL;
126  double* weights=NULL;
127 
128  geomodel_getWeights2D(model, pointA, pointB, pointSpacing, earthRadius, LINEAR, &pointIndices, &weights, &allocatedSize, &nWeights);
129 
130  // check for errors and abort if any have occurred.
131  errorCheck();
132 
133  for (j=0; j<nWeights; ++j)
134  {
135  // the sum of the weights will equal the length of the path in km.
136  sumWeights += weights[j];
137 
138  // model stores velocity but we want to integrate slowness, so divide the weights by velocity.
139  integral += weights[j] / geomodel_getValueDouble(model, pointIndices[j], 0, 0, attributeIndex);
140  }
141 
142  // the sum of the weights should equal the length of the ray path in km.
143  printf("Sum of weights (length of ray path) = %10.4f km \n\n", sumWeights);
144 
145  // the sum of the weights should equal the length of the ray path in km.
146  printf("Path integral = %10.4f sec \n\n", integral);
147 
148  // free allocated memory
149  if (pointIndices) free(pointIndices);
150  if (weights) free(weights);
151 
152  // check for errors and abort if any have occurred. Omit in production code.
153  errorCheck();
154 
155  return integral;
156 }
157 
158 /**
159  * Generate a starting model for the Integrate2D example program. The model
160  * will have a single attribute (velocity in km/sec), and will be a 2D model, i.e.,
161  * there will be no radius associated with the nodes of the model. For this
162  * simple example, the model is populated with a single, constant value of
163  * 10 km/sec.
164  *
165  * @param gridFile the name of the file containing the GeoTessGrid upon
166  * which the starting model will be based.
167  * @return a GeoTessModelC
168  */
169 GeoTessModelC* velocityModel(char* gridFile)
170 {
171  GeoTessMetaDataC* metaData;
172  GeoTessModelC* model;
173  float velocity;
174  int i;
175  char* str;
176 
177  // If an error message appears indicating that file geotess_grid_04000.geotes
178  // could not be found, it is likely because the program was not executed from
179  // directory GeoTessBuilderExamples/tomo2dTest
180 
181  // construct a GeoTessMetaDataC object, which is basically a wrapper
182  // around a C++ GeoTessMetaData object.
183  metaData = geometadata_create();
184 
185  // specify the ellipsoid to use for converting between geographic and geocentric
186  // coordinates and between radius and depth. This is really not necessary here since
187  // WGS84 is the default, but other options are available.
188  geometadata_setEarthShape(metaData, "WGS84");
189 
190  geometadata_setDescription(metaData, "GeoTessModel for example program Integrate2D\n");
191 
192  // Specify a list of layer names. A model could have many layers,
193  // e.g., ("core", "mantle", "crust"), specified in order of increasing
194  // radius. This simple example has only one layer.
195  geometadata_setLayerNames1(metaData, "surface");
196 
197  // specify the names of the attributes and the units of the attributes in
198  // two String arrays. This model only includes one attribute.
199  geometadata_setAttributes1(metaData, "velocity", "km/sec");
200 
201  // specify the DataType for the data. All attributes, in all profiles, will
202  // have the same data type.
203  geometadata_setDataType1(metaData, FLOAT);
204 
205  // specify the name of the software that is going to generate
206  // the model. This gets stored in the model for future reference.
207 
208  geometadata_setModelSoftwareVersion(metaData, "GeoTessCExamples.Integrate2D 1.0.0");
209 
210  // specify the date when the model was generated. This gets
211  // stored in the model for future reference.
212  str = cpu_now();
213  geometadata_setModelGenerationDate(metaData, str);
214  free(str);
215 
216  // call a GeoTessModel constructor to build the model. This will build the
217  // grid, and initialize all the data structures to null. To be useful, we
218  // will have to populate the data structures.
219  model = geomodel_create3(gridFile, metaData);
220 
221  // check for errors and abort if any have occurred.
222  errorCheck();
223 
224  // Delete the GeoTessMetaDataC object, which is a wrapper around a C++ GeoTessMetaData
225  // object. Ownership of the underlying C++ GeoTessMetaData object has been transfered
226  // to the newly created GeoTessModel object. The model will delete the C++ GeoTessMetaData
227  // object when it is done with it. We need to delete the memory allocated to the
228  // GeoTessMetaDataC wrapper, but not the wrapped C++ GeoTessMetaData object.
229  // Hence the second parameter is false.
230  geometadata_destroy(metaData);
231 
232  velocity = 10.F;
233 
234  // generate some data and store it in the model.
235  for (i = 0; i < geomodel_getNVertices(model); ++i)
236  geomodel_setProfSurfFloats(model, i, &velocity, 1);
237 
238  // check for errors and abort if any have occurred.
239  errorCheck();
240 
241  //printf("%s", geomodel_toString(model));
242 
243  return model;
244 }
245 
246 /**
247  * Check to see if any errors have occurred. If any, print out the error
248  * messages and abort.
249  */
251 {
252  // print out error messages from the message stack,
253  // most recent error first.
254  while (error_exists())
255  {
256  fprintf(stderr, "%s\n", error_getMessage());
257 
258  // if there were any error messages to start with,
259  // and the last error message has been printed,
260  // then exit.
261  if (!error_exists())
262  exit(-1);
263  }
264 
265 }
266 
main
int main(int argc, char **argv)
Definition: Integrate2D.c:64
errorCheck
void errorCheck()
Definition: Integrate2D.c:250
dtr
static const double dtr
Definition: Integrate2D.c:55
integrate2D
double integrate2D(GeoTessModelC *model, double *pointA, double *pointB, int attributeIndex)
Definition: Integrate2D.c:108
rtd
static const double rtd
Definition: Integrate2D.c:54
velocityModel
GeoTessModelC * velocityModel(char *gridFile)
Definition: Integrate2D.c:169