00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 #define FLATMESHTESTC_ENORM 0
00079 #define FLATMESHTESTC_ESUB 1
00080 #define FLATMESHTESTC_EARG 2
00081 #define FLATMESHTESTC_EMEM 3
00082 #define FLATMESHTESTC_EDIM 4
00083 #define FLATMESHTESTC_ELEN 5
00084 #define FLATMESHTESTC_EFILE 6
00085
00086 #define FLATMESHTESTC_MSGENORM "Normal exit"
00087 #define FLATMESHTESTC_MSGESUB "Subroutine failed"
00088 #define FLATMESHTESTC_MSGEARG "Error parsing arguments"
00089 #define FLATMESHTESTC_MSGEMEM "Memory allocation error"
00090 #define FLATMESHTESTC_MSGEDIM "Inconsistent parameter space dimension"
00091 #define FLATMESHTESTC_MSGELEN "Too few points specified"
00092 #define FLATMESHTESTC_MSGEFILE "Could not open file"
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 #include <math.h>
00141 #include <stdlib.h>
00142 #include <lal/LALStdio.h>
00143 #include <lal/FileIO.h>
00144 #include <lal/LALStdlib.h>
00145 #include <lal/AVFactories.h>
00146 #include <lal/SeqFactories.h>
00147 #include <lal/FlatMesh.h>
00148 #include <lal/StreamInput.h>
00149
00150 NRCSID(FLATMESHTESTC,"$Id: FlatMeshTest.c,v 1.6 2007/06/08 14:41:52 bema Exp $");
00151
00152
00153 int lalDebugLevel = 0;
00154 #define MISMATCH 0.1
00155 #define DIM 2
00156 REAL4 defaultMatrix[] = { 1.0, 0.0, 0.0, 1.0 };
00157 REAL4 defaultMatrixInv[] = { 1.0, 0.0, 0.0, 1.0 };
00158 REAL4 defaultCorners[] = { 0.0, 0.0, 0.5, 1.0 };
00159
00160
00161 #define USAGE "Usage: %s [-o outfile] [-d debuglevel] [-m mismatch]\n\
00162 [eigenvectorfile inversefile rangefile]\n"
00163
00164
00165 #define ERROR( code, msg, statement ) \
00166 if ( lalDebugLevel & LALERROR ) \
00167 { \
00168 LALPrintError( "Error[0] %d: program %s, file %s, line %d, %s\n" \
00169 " %s %s\n", (code), *argv, __FILE__, \
00170 __LINE__, FLATMESHTESTC, statement ? statement : \
00171 "", (msg) ); \
00172 } \
00173 else (void)(0)
00174
00175 #define INFO( statement ) \
00176 if ( lalDebugLevel & LALINFO ) \
00177 { \
00178 LALPrintError( "Info[0]: program %s, file %s, line %d, %s\n" \
00179 " %s\n", *argv, __FILE__, __LINE__, \
00180 FLATMESHTESTC, (statement) ); \
00181 } \
00182 else (void)(0)
00183
00184 #define SUB( func, statusptr ) \
00185 if ( (func), (statusptr)->statusCode ) \
00186 { \
00187 ERROR( FLATMESHTESTC_ESUB, FLATMESHTESTC_MSGESUB, \
00188 "Function call \"" #func "\" failed:" ); \
00189 return FLATMESHTESTC_ESUB; \
00190 } \
00191 else (void)(0)
00192
00193
00194 #ifndef NDEBUG
00195 char *lalWatch;
00196 #endif
00197
00198 int
00199 main(int argc, char **argv)
00200 {
00201 INT4 arg;
00202 UINT4 dim;
00203 static LALStatus stat;
00204 CHAR *outfile = NULL;
00205 CHAR *eigenfile = NULL;
00206 CHAR *inversefile = NULL;
00207 CHAR *rangefile = NULL;
00208 REAL4 mismatch = MISMATCH;
00209 REAL4VectorSequence *matrix = NULL;
00210 REAL4VectorSequence *matrixInv = NULL;
00211 REAL4VectorSequence *corners = NULL;
00212 REAL4VectorSequence *mesh = NULL;
00213 FILE *fp;
00214
00215
00216 arg = 1;
00217 while ( arg < argc ) {
00218
00219 if ( !strcmp( argv[arg], "-o" ) ) {
00220 if ( argc > arg + 1 ) {
00221 arg++;
00222 outfile = argv[arg++];
00223 }else{
00224 ERROR( FLATMESHTESTC_EARG, FLATMESHTESTC_MSGEARG, 0 );
00225 LALPrintError( USAGE, *argv );
00226 return FLATMESHTESTC_EARG;
00227 }
00228 }
00229
00230 else if ( !strcmp( argv[arg], "-d" ) ) {
00231 if ( argc > arg + 1 ) {
00232 arg++;
00233 lalDebugLevel = atoi( argv[arg++] );
00234 }else{
00235 ERROR( FLATMESHTESTC_EARG, FLATMESHTESTC_MSGEARG, 0 );
00236 LALPrintError( USAGE, *argv );
00237 return FLATMESHTESTC_EARG;
00238 }
00239 }
00240
00241 else if ( !strcmp( argv[arg], "-m" ) ) {
00242 if ( argc > arg + 1 ) {
00243 arg++;
00244 mismatch = fabs( atof( argv[arg++] ) );
00245 }else{
00246 ERROR( FLATMESHTESTC_EARG, FLATMESHTESTC_MSGEARG, 0 );
00247 LALPrintError( USAGE, *argv );
00248 return FLATMESHTESTC_EARG;
00249 }
00250 }
00251
00252 else if ( argv[arg][0] == '-' ) {
00253 ERROR( FLATMESHTESTC_EARG, FLATMESHTESTC_MSGEARG, 0 );
00254 LALPrintError( USAGE, *argv );
00255 return FLATMESHTESTC_EARG;
00256 }
00257
00258 else {
00259 if ( argc > arg + 2 ) {
00260 eigenfile = argv[arg++];
00261 inversefile = argv[arg++];
00262 rangefile = argv[arg++];
00263 }else{
00264 ERROR( FLATMESHTESTC_EARG, FLATMESHTESTC_MSGEARG, 0 );
00265 LALPrintError( USAGE, *argv );
00266 return FLATMESHTESTC_EARG;
00267 }
00268 }
00269 }
00270
00271
00272 if ( eigenfile ) {
00273
00274
00275 if ( !( fp = LALOpenDataFile( eigenfile ) ) ) {
00276 ERROR( FLATMESHTESTC_EFILE, "- " FLATMESHTESTC_MSGEFILE,
00277 eigenfile );
00278 return FLATMESHTESTC_EFILE;
00279 }
00280 SUB( LALSReadVectorSequence( &stat, &matrix, fp ), &stat );
00281 fclose( fp );
00282 if ( !( fp = LALOpenDataFile( inversefile ) ) ) {
00283 ERROR( FLATMESHTESTC_EFILE, "- " FLATMESHTESTC_MSGEFILE,
00284 inversefile );
00285 return FLATMESHTESTC_EFILE;
00286 }
00287 SUB( LALSReadVectorSequence( &stat, &matrixInv, fp ), &stat );
00288 fclose( fp );
00289 if ( !( fp = LALOpenDataFile( rangefile ) ) ) {
00290 ERROR( FLATMESHTESTC_EFILE, "- " FLATMESHTESTC_MSGEFILE,
00291 rangefile );
00292 return FLATMESHTESTC_EFILE;
00293 }
00294 SUB( LALSReadVectorSequence( &stat, &corners, fp ), &stat );
00295 fclose( fp );
00296
00297
00298 dim = matrix->length;
00299 if ( matrix->vectorLength != dim ) {
00300 ERROR( FLATMESHTESTC_EDIM, FLATMESHTESTC_MSGEDIM,
00301 eigenfile );
00302 return FLATMESHTESTC_EDIM;
00303 }
00304 if ( ( matrixInv->length != dim ) ||
00305 ( matrixInv->vectorLength != dim ) ) {
00306 ERROR( FLATMESHTESTC_EDIM, FLATMESHTESTC_MSGEDIM, inversefile );
00307 return FLATMESHTESTC_EDIM;
00308 }
00309 if ( corners->vectorLength != dim ) {
00310 ERROR( FLATMESHTESTC_EDIM, FLATMESHTESTC_MSGEDIM, rangefile );
00311 return FLATMESHTESTC_EDIM;
00312 }
00313 if ( corners->length < 2 ) {
00314 ERROR( FLATMESHTESTC_ELEN, FLATMESHTESTC_MSGELEN, rangefile );
00315 return FLATMESHTESTC_ELEN;
00316 }
00317
00318 } else {
00319
00320
00321 CreateVectorSequenceIn in;
00322 dim = DIM;
00323 in.length = dim;
00324 in.vectorLength = dim;
00325 SUB( LALSCreateVectorSequence( &stat, &matrix, &in ), &stat );
00326 memcpy( matrix->data, defaultMatrix, dim*dim*sizeof(REAL4) );
00327 SUB( LALSCreateVectorSequence( &stat, &matrixInv, &in ), &stat );
00328 memcpy( matrixInv->data, defaultMatrixInv, dim*dim*sizeof(REAL4) );
00329 in.length = 2;
00330 SUB( LALSCreateVectorSequence( &stat, &corners, &in ), &stat );
00331 memcpy( corners->data, defaultCorners, 2*dim*sizeof(REAL4) );
00332 }
00333
00334
00335 {
00336 UINT4 i;
00337 REAL4 adjust = 2.0*mismatch/sqrt( (REAL4)(dim) );
00338 REAL4 *data;
00339
00340 i = matrix->length*matrix->vectorLength;
00341 data = matrix->data;
00342 while ( i-- )
00343 *(data++) *= adjust;
00344
00345 adjust = 1.0/adjust;
00346 i = matrixInv->length*matrixInv->vectorLength;
00347 data = matrixInv->data;
00348 while ( i-- )
00349 *(data++) *= adjust;
00350 }
00351
00352
00353 {
00354 UINT4 i, j;
00355 INT2 direct;
00356 REAL4 *data;
00357 REAL4 *width;
00358
00359
00360 width = (REAL4 *)LALCalloc( dim, sizeof(REAL4) );
00361 if ( !width ) {
00362 ERROR( FLATMESHTESTC_EMEM, FLATMESHTESTC_MSGEMEM, 0 );
00363 return FLATMESHTESTC_EMEM;
00364 }
00365
00366
00367 for ( data = matrix->data, i = 0; i < dim; i++ )
00368 for ( j = 0; j < dim; j++, data++ )
00369 width[j] += fabs( *data );
00370
00371
00372
00373 for ( data = corners->data, i = 0; i < dim; i++, data++ ) {
00374 direct = ( data[0] < data[dim] ) ? -1 : 1;
00375 data[0] += 0.5*direct*width[i];
00376 data[dim] -= 0.5*direct*width[i];
00377 }
00378
00379
00380 LALFree( width );
00381 }
00382
00383
00384 {
00385
00386 static FlatMeshParamStruc params;
00387 params.matrix = matrix;
00388 params.matrixInv = matrixInv;
00389 params.controlPoints = corners;
00390 params.intersection = LALRectIntersect;
00391 SUB( LALSCreateVector( &stat, &(params.xMin), dim ), &stat );
00392 memcpy( params.xMin->data, corners->data, dim*sizeof(REAL4) );
00393 SUB( LALSCreateVector( &stat, &(params.xMax), dim ), &stat );
00394 memcpy( params.xMax->data, corners->data+dim, dim*sizeof(REAL4) );
00395
00396
00397 SUB( LALCreateFlatMesh( &stat, &mesh, ¶ms ), &stat );
00398 SUB( LALSDestroyVector( &stat, &(params.xMin) ), &stat );
00399 SUB( LALSDestroyVector( &stat, &(params.xMax) ), &stat );
00400 SUB( LALSDestroyVectorSequence( &stat, &matrix ), &stat );
00401 SUB( LALSDestroyVectorSequence( &stat, &matrixInv ), &stat );
00402 SUB( LALSDestroyVectorSequence( &stat, &corners ), &stat );
00403 }
00404
00405
00406 {
00407 UINT4 i;
00408 REAL4 *data;
00409 if ( outfile ) {
00410 if ( !( fp = fopen( outfile, "w" ) ) ) {
00411 ERROR( FLATMESHTESTC_EFILE, "- " FLATMESHTESTC_MSGEFILE,
00412 outfile );
00413 return FLATMESHTESTC_EFILE;
00414 }
00415 } else
00416 fp = stdout;
00417 i = mesh->length;
00418 data = mesh->data;
00419 while ( i-- ) {
00420 UINT4 j = mesh->vectorLength;
00421 while ( j-- )
00422 fprintf( fp, "%10.3e ", *(data++) );
00423 fprintf( fp, "\n" );
00424 }
00425 if ( outfile )
00426 fclose( fp );
00427 }
00428
00429
00430 SUB( LALSDestroyVectorSequence( &stat, &mesh ), &stat );
00431 LALCheckMemoryLeaks();
00432 INFO( FLATMESHTESTC_MSGENORM );
00433 return FLATMESHTESTC_ENORM;
00434 }