read_par_json.c 21.4 KB
Newer Older
Tilman Steinweg's avatar
Tilman Steinweg committed
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
/*------------------------------------------------------------------------
 * Copyright (C) 2011 For the list of authors, see file AUTHORS.
 *
 * This file is part of SOFI2D.
 * 
 * SOFI2D is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2.0 of the License only.
 * 
 * SOFI2D is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with SOFI2D. See file COPYING and/or 
  * <http://www.gnu.org/licenses/gpl-2.0.html>.
--------------------------------------------------------------------------*/
/*------------------------------------------------------------------------
 *   program SOFI2D, reading input-parameters from input-file or stdin
 *   that are formatted according to the json standard
 *
 *  ----------------------------------------------------------------------*/

#include <unistd.h>
#include "fd.h"

char ** varname_list,** value_list;

void read_par_json(FILE *fp, char *fileinp){

	/* declaration of extern variables */
33
	extern int   NX, NY, FDORDER, FDORDER_TIME, MAXRELERROR, SOURCE_TYPE, SOURCE_SHAPE, SNAP, SNAP_FORMAT, L;
Tilman Steinweg's avatar
Tilman Steinweg committed
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
	extern int SEISMO, NDT, SEIS_FORMAT, FREE_SURF, READMOD, READREC, SRCREC;
	extern int BOUNDARY, REC_ARRAY, DRX, LOG,  WRITE_MODELFILES; //RSG
	extern int  NPROCX, NPROCY, MYID, IDX, IDY, CHECKPTREAD, CHECKPTWRITE, RUN_MULTIPLE_SHOTS, ABS_TYPE, FW;
	extern int OUTNTIMESTEPINFO;
	/*extern int NGEOPH;*/
	extern float NGEOPH;
	extern float DH, TIME, DT, TS, *FL, TAU, DAMPING, PLANE_WAVE_DEPTH, PLANE_WAVE_ANGLE, FPML, VPPML,NPOWER, K_MAX_CPML;
	extern float XREC1, XREC2, YREC1, YREC2;
	extern float REC_ARRAY_DEPTH, REC_ARRAY_DIST;
	extern float TSNAP1, TSNAP2, TSNAPINC, REFREC[4];
	extern char  MFILE[STRING_SIZE], SIGNAL_FILE[STRING_SIZE], LOG_FILE[STRING_SIZE];
	extern char SNAP_FILE[STRING_SIZE], SOURCE_FILE[STRING_SIZE], REC_FILE[STRING_SIZE];
	extern char SEIS_FILE[STRING_SIZE], CHECKPTFILE[STRING_SIZE];


	/* definition of local variables */

	int number_readobjects=0,fserr=0;
	char errormessage[STRING_SIZE];

	char ** varname_list, ** value_list;

	if (MYID == 0){

		//allocate first object in list
		varname_list = malloc(STRING_SIZE*sizeof(char*));
		value_list = malloc(STRING_SIZE*sizeof(char*));

		//read in objects from file
		number_readobjects=read_objects_from_intputfile(fp, fileinp, varname_list, value_list);
		fprintf(fp,"\nFrom input file %s, %i objects have been read in. \n",fileinp, number_readobjects);

		//print objects to screen
		fprintf(fp, "\n===========================================================");
		fprintf(fp, "\n=   List of Parameters read by the built in Json Parser   =");
		print_objectlist_screen(fp, number_readobjects, varname_list, value_list);

		//extract variables form object list

		/*=================================
		section general grid and discretization parameters
		  =================================*/
		if (get_int_from_objectlist("NPROCX",number_readobjects,&NPROCX,varname_list, value_list))
			err("Variable NPROCX could not be retrieved from the json input file!");
		if (get_int_from_objectlist("NPROCY",number_readobjects,&NPROCY,varname_list, value_list))
			err("Variable NPROCY could not be retrieved from the json input file!");
	/*	if (get_int_from_objectlist("RSG",number_readobjects,&RSG,varname_list, value_list))
			err("Variable RSG could not be retrieved from the json input file!");*/
		if (get_int_from_objectlist("FDORDER",number_readobjects,&FDORDER,varname_list, value_list))
			err("Variable FDORDER could not be retrieved from the json input file!");
84 85 86 87 88 89 90
        if (get_int_from_objectlist("FDORDER_TIME",number_readobjects,&FDORDER_TIME,varname_list, value_list)) {
            FDORDER_TIME=2;
        } else {
            if(FDORDER_TIME!=2 && FDORDER_TIME!=4) {
                err("Only FDORDER_TIME 2 or 4 are supported!");
            }
        }
Tilman Steinweg's avatar
Tilman Steinweg committed
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
		if (get_int_from_objectlist("MAXRELERROR",number_readobjects,&MAXRELERROR,varname_list, value_list))
			err("Variable MAXRELERROR could not be retrieved from the json input file!");
		if (get_int_from_objectlist("NX",number_readobjects,&NX,varname_list, value_list))
			err("Variable NX could not be retrieved from the json input file!");
		if (get_int_from_objectlist("NY",number_readobjects,&NY,varname_list, value_list))
			err("Variable NY could not be retrieved from the json input file!");
		if (get_float_from_objectlist("DH",number_readobjects,&DH,varname_list, value_list))
			err("Variable DH could not be retrieved from the json input file!");
		if (get_float_from_objectlist("TIME",number_readobjects,&TIME,varname_list, value_list))
			err("Variable TIME could not be retrieved from the json input file!");
		if (get_float_from_objectlist("DT",number_readobjects,&DT,varname_list, value_list))
			err("Variable DT could not be retrieved from the json input file!");

		/*=================================
		 	 section source parameters
		  =================================*/

		if (get_int_from_objectlist("SOURCE_TYPE",number_readobjects,&SOURCE_TYPE,varname_list, value_list))
			err("Variable SOURCE_TYPE could not be retrieved from the json input file!");
		if (get_int_from_objectlist("SOURCE_SHAPE",number_readobjects,&SOURCE_SHAPE,varname_list, value_list))
			err("Variable SOURCE_SHAPE could not be retrieved from the json input file!");
		else {
			if (SOURCE_SHAPE==3) {
				if (get_string_from_objectlist("SIGNAL_FILE",number_readobjects,SIGNAL_FILE,varname_list, value_list))
					err("Variable SIGNAL_FILE could not be retrieved from the json input file!");
			}
		}
		if (get_int_from_objectlist("SRCREC",number_readobjects,&SRCREC,varname_list, value_list))
			err("Variable SRCREC could not be retrieved from the json input file!");
		else {
			if (SRCREC==1) {
				if (get_string_from_objectlist("SOURCE_FILE",number_readobjects,SOURCE_FILE,varname_list, value_list))
					err("Variable SOURCE_FILE could not be retrieved from the json input file!");
				if (get_int_from_objectlist("RUN_MULTIPLE_SHOTS",number_readobjects,&RUN_MULTIPLE_SHOTS,varname_list, value_list))
					err("Variable RUN_MULTIPLE_SHOTS could not be retrieved from the json input file!");
			}
			if (SRCREC==2) {
				if (get_float_from_objectlist("PLANE_WAVE_DEPTH",number_readobjects,&PLANE_WAVE_DEPTH,varname_list, value_list))
					err("Variable PLANE_WAVE_DEPTH could not be retrieved from the json input file!");
				else {
					if (PLANE_WAVE_DEPTH>0.0) {
						if (get_float_from_objectlist("PLANE_WAVE_ANGLE",number_readobjects,&PLANE_WAVE_ANGLE,varname_list, value_list))
							err("Variable PLANE_WAVE_ANGLE could not be retrieved from the json input file!");
						if (get_float_from_objectlist("TS",number_readobjects,&TS,varname_list, value_list))
							err("Variable TS could not be retrieved from the json input file!");
					}
				}
			}
		}


		/*=================================
		 	 section boundary parameters
		  =================================*/

		if (get_int_from_objectlist("FREE_SURF",number_readobjects,&FREE_SURF,varname_list, value_list))
			err("Variable FREE_SURF could not be retrieved from the json input file!");
		if (get_int_from_objectlist("BOUNDARY",number_readobjects,&BOUNDARY,varname_list, value_list))
			err("Variable BOUNDARY could not be retrieved from the json input file!");
		if (get_int_from_objectlist("FW",number_readobjects,&FW,varname_list, value_list))
			err("Variable FW could not be retrieved from the json input file!");
		if (get_int_from_objectlist("ABS_TYPE",number_readobjects,&ABS_TYPE,varname_list, value_list))
                	err("Variable ABS_TYPE could not be retrieved from the json input file!");

		if (ABS_TYPE==1) {
			if (get_float_from_objectlist("NPOWER",number_readobjects,&NPOWER,varname_list, value_list)) 
				err("Variable NPOWER could not be retrieved from the json input file!");
			if (get_float_from_objectlist("K_MAX_CPML",number_readobjects,&K_MAX_CPML,varname_list, value_list)) 
				err("Variable K_MAX_CPML could not be retrieved from the json input file!");
			if (get_float_from_objectlist("FPML",number_readobjects,&FPML,varname_list, value_list))
				err("Variable FPML could not be retrieved from the json input file!");
			if (get_float_from_objectlist("VPPML",number_readobjects,&VPPML,varname_list, value_list))
				err("Variable VPPML could not be retrieved from the json input file!");
                }
		if (ABS_TYPE==2) {
			if (get_float_from_objectlist("DAMPING",number_readobjects,&DAMPING,varname_list, value_list))
                        	err("Variable DAMPING could not be retrieved from the json input file!");
			}


		/*=================================
			 section snapshot parameters
		=================================*/
		if (get_int_from_objectlist("SNAP",number_readobjects,&SNAP,varname_list, value_list))
			err("Variable SNAP could not be retrieved from the json input file!");
		else {
			if (SNAP>0) {
				if (get_int_from_objectlist("SNAP_FORMAT",number_readobjects,&SNAP_FORMAT,varname_list, value_list))
					err("Variable SNAP_FORMAT could not be retrieved from the json input file!");
				if (get_float_from_objectlist("TSNAP1",number_readobjects,&TSNAP1,varname_list, value_list))
					err("Variable TSNAP1 could not be retrieved from the json input file!");
				if (get_float_from_objectlist("TSNAP2",number_readobjects,&TSNAP2,varname_list, value_list))
					err("Variable TSNAP2 could not be retrieved from the json input file!");
				if (get_float_from_objectlist("TSNAPINC",number_readobjects,&TSNAPINC,varname_list, value_list))
					err("Variable TSNAPINC could not be retrieved from the json input file!");
				if (get_string_from_objectlist("SNAP_FILE",number_readobjects,SNAP_FILE,varname_list, value_list))
					err("Variable SNAP_FILE could not be retrieved from the json input file!");
			}
		}
		/* increments are read in any case, because they will be also used as increment for model output */
		if (get_int_from_objectlist("IDX",number_readobjects,&IDX,varname_list, value_list))
			err("Variable IDX could not be retrieved from the json input file!");
		if (get_int_from_objectlist("IDY",number_readobjects,&IDY,varname_list, value_list))
			err("Variable IDY could not be retrieved from the json input file!");

		/*=================================
			section seismogramm parameters
		=================================*/
		if (get_int_from_objectlist("SEISMO",number_readobjects,&SEISMO,varname_list, value_list))
			err("Variable SEISMO could not be retrieved from the json input file!");
		else {
			if (SEISMO>0){
				if (get_string_from_objectlist("REC_FILE",number_readobjects,REC_FILE,varname_list, value_list))
					err("Variable REC_FILE could not be retrieved from the json input file!");
				if (get_string_from_objectlist("SEIS_FILE",number_readobjects,SEIS_FILE,varname_list, value_list))
					err("Variable SEIS_FILE could not be retrieved from the json input file!");
				if (get_int_from_objectlist("READREC",number_readobjects,&READREC,varname_list, value_list))
					err("Variable READREC could not be retrieved from the json input file!");
				else {
					if (READREC==0) {
						if (get_float_from_objectlist("XREC1",number_readobjects,&XREC1,varname_list, value_list))
							err("Variable XREC1 could not be retrieved from the json input file!");
						if (get_float_from_objectlist("XREC2",number_readobjects,&XREC2,varname_list, value_list))
							err("Variable XREC2T could not be retrieved from the json input file!");
						if (get_float_from_objectlist("YREC1",number_readobjects,&YREC1,varname_list, value_list))
							err("Variable YREC1 could not be retrieved from the json input file!");
						if (get_float_from_objectlist("YREC2",number_readobjects,&YREC2,varname_list, value_list))
							err("Variable YREC2 could not be retrieved from the json input file!");
					}
				}
				if (get_int_from_objectlist("NDT",number_readobjects,&NDT,varname_list, value_list))
					err("Variable NDT could not be retrieved from the json input file!");
				if (get_int_from_objectlist("SEIS_FORMAT",number_readobjects,&SEIS_FORMAT,varname_list, value_list))
					err("Variable SEIS_FORMAT could not be retrieved from the json input file!");

				if (get_int_from_objectlist("REC_ARRAY",number_readobjects,&REC_ARRAY,varname_list, value_list))
					err("Variable REC_ARRAY could not be retrieved from the json input file!");
				else {
					if (REC_ARRAY>0) {
						if (get_int_from_objectlist("DRX",number_readobjects,&DRX,varname_list, value_list))
							err("Variable DRX could not be retrieved from the json input file!");
						if (get_float_from_objectlist("REC_ARRAY_DEPTH",number_readobjects,&REC_ARRAY_DEPTH,varname_list, value_list))
							err("Variable REC_ARRAY_DEPTH could not be retrieved from the json input file!");
						if (get_float_from_objectlist("REC_ARRAY_DIST",number_readobjects,&REC_ARRAY_DIST,varname_list, value_list))
							err("Variable REC_ARRAY_DIST could not be retrieved from the json input file!");
					}
				}
				if (get_float_from_objectlist("REFRECX",number_readobjects,&REFREC[1],varname_list, value_list))
					err("Variable REFRECX could not be retrieved from the json input file!");
				if (get_float_from_objectlist("REFRECY",number_readobjects,&REFREC[2],varname_list, value_list))
					err("Variable REFRECY could not be retrieved from the json input file!");
				if (get_float_from_objectlist("NGEOPH",number_readobjects,&NGEOPH,varname_list, value_list))
					err("Variable NGEOPH could not be retrieved from the json input file!");
			}
		}

		/*=================================
			section general model and log parameters
		  =================================*/
		if (get_string_from_objectlist("MFILE",number_readobjects,MFILE,varname_list, value_list))
			err("Variable MFILE could not be retrieved from the json input file!");
		(get_int_from_objectlist("WRITE_MODELFILES",number_readobjects,&WRITE_MODELFILES,varname_list, value_list));
		if (get_int_from_objectlist("LOG",number_readobjects,&LOG,varname_list, value_list))
			err("Variable LOG could not be retrieved from the json input file!");
		if (get_int_from_objectlist("CHECKPTREAD",number_readobjects,&CHECKPTREAD,varname_list, value_list))
			err("Variable CHECKPTREAD could not be retrieved from the json input file!");
		if (get_int_from_objectlist("CHECKPTWRITE",number_readobjects,&CHECKPTWRITE,varname_list, value_list))
			err("Variable CHECKPTWRITE could not be retrieved from the json input file!");
		if (get_string_from_objectlist("LOG_FILE",number_readobjects,LOG_FILE,varname_list, value_list))
			err("Variable LOG_FILE could not be retrieved from the json input file!");
		if (get_string_from_objectlist("CHECKPT_FILE",number_readobjects,CHECKPTFILE,varname_list, value_list))
			err("Variable CHECKPT_FILE could not be retrieved from the json input file!");
		if (get_int_from_objectlist("READMOD",number_readobjects,&READMOD,varname_list, value_list))
			err("Variable READMOD could not be retrieved from the json input file!");
		if (get_int_from_objectlist("OUT_TIMESTEP_INFO",number_readobjects,&OUTNTIMESTEPINFO,varname_list, value_list)) {}

		if (get_float_from_objectlist("TAU",number_readobjects,&TAU,varname_list, value_list))
			err("Variable TAU could not be retrieved from the json input file!");
		if (get_int_from_objectlist("L",number_readobjects,&L,varname_list, value_list))
			err("Variable L could not be retrieved from the json input file!");
271 272 273
        else {
            FL=vector(1,L);
            switch(L) {
Tilman Steinweg's avatar
Tilman Steinweg committed
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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
			case 0:
				break;
			case 5:
				if (get_float_from_objectlist("FL5",number_readobjects,&FL[5],varname_list, value_list))
					err("Variable FL5 could not be retrieved from the json input file!");
			case 4:
				if (get_float_from_objectlist("FL4",number_readobjects,&FL[4],varname_list, value_list))
					err("Variable FL4 could not be retrieved from the json input file!");
			case 3:
				if (get_float_from_objectlist("FL3",number_readobjects,&FL[3],varname_list, value_list))
					err("Variable FL3 could not be retrieved from the json input file!");
			case 2:
				if (get_float_from_objectlist("FL2",number_readobjects,&FL[2],varname_list, value_list))
					err("Variable FL2 could not be retrieved from the json input file!");
			case 1:
				if (get_float_from_objectlist("FL1",number_readobjects,&FL[1],varname_list, value_list))
					err("Variable FL1 could not be retrieved from the json input file!");
				break;
			default:
				err("More than four relaxation Parameter (L>5) are not implemented yet!");
				break;
			}
		}

		/********************************************/
		/* Check files and directories if necessary */
		/********************************************/

		/* signal file */
		if (SOURCE_SHAPE == 3)
		{
			if (access(SIGNAL_FILE,0) != 0)
			{
				fprintf(fp, "\n==================================================================\n");
				fprintf(fp, "  ERROR parsing input file <%s>:\n", fileinp);
				fprintf(fp, "        The signal file does not exist!\n");
				fprintf(fp, "        File name: <%s>", SIGNAL_FILE);
				fprintf(fp, "\n==================================================================\n");
				fserr = 1;
			}
			else if (access(SIGNAL_FILE,4) != 0)
			{
				fprintf(fp, "\n==================================================================\n");
				fprintf(fp, "  ERROR parsing input file <%s>:\n", fileinp);
				fprintf(fp, "        The signal file does not have read access!\n");
				fprintf(fp, "        File name: <%s>", SIGNAL_FILE);
				fprintf(fp, "\n==================================================================\n");
				fserr = 1;
			}
		}

		/* source file */
		if (SRCREC==1)
		{
			if (access(SOURCE_FILE,0) != 0)
			{
				fprintf(fp, "\n==================================================================\n");
				fprintf(fp, "  ERROR parsing input file <%s>:\n", fileinp);
				fprintf(fp, "        The source file does not exist!\n");
				fprintf(fp, "        File name: <%s>", SOURCE_FILE);
				fprintf(fp, "\n==================================================================\n");
				fserr = 1;
			}
			else if (access(SOURCE_FILE,4) != 0)
			{
				fprintf(fp, "\n==================================================================\n");
				fprintf(fp, "  ERROR parsing input file <%s>:\n", fileinp);
				fprintf(fp, "        The source file does not have read access!\n");
				fprintf(fp, "        File name: <%s>", SOURCE_FILE);
				fprintf(fp, "\n==================================================================\n");
				fserr = 1;
			}
		}

		/* model file -> this is checked in more detail in readmod.c!
		if (READMOD)
		{
			if (access(MFILE,0) != 0)
			{
				fprintf(fp, "\n==================================================================\n");
				fprintf(fp, "  ERROR parsing input file <%s>:\n", fileinp);
				fprintf(fp, "        The model file does not exist!\n");
				fprintf(fp, "        File name: <%s>", MFILE);
				fprintf(fp, "\n==================================================================\n");
				fserr = 1;
			}
			else if (access(MFILE,4) != 0)
			{
				fprintf(fp, "\n==================================================================\n");
				fprintf(fp, "  ERROR parsing input file <%s>:\n", fileinp);
				fprintf(fp, "        The model file does not have read access!\n");
				fprintf(fp, "        File name: <%s>", MFILE);
				fprintf(fp, "\n==================================================================\n");
				fserr = 1;
			}
		}*/

		/* receiver file */
		if (READREC)
		{
			if (access(REC_FILE,0) != 0)
			{
				fprintf(fp, "\n==================================================================\n");
				fprintf(fp, "  ERROR parsing input file <%s>:\n", fileinp);
				fprintf(fp, "        The receiver file does not exist!\n");
				fprintf(fp, "        File name: <%s>", REC_FILE);
				fprintf(fp, "\n==================================================================\n");
				fserr = 1;
			}
			else if (access(REC_FILE,4) != 0)
			{
				fprintf(fp, "\n==================================================================\n");
				fprintf(fp, "  ERROR parsing input file <%s>:\n", fileinp);
				fprintf(fp, "        The receiver file does not have read access!\n");
				fprintf(fp, "        File name: <%s>", REC_FILE);
				fprintf(fp, "\n==================================================================\n");
				fserr = 1;
			}
		}

		/* checkpoint file */
		if (CHECKPTREAD || CHECKPTWRITE)
		{
			if (access(CHECKPTFILE,0) != 0)
			{
				fprintf(fp, "\n==================================================================\n");
				fprintf(fp, "  ERROR parsing input file <%s>:\n", fileinp);
				fprintf(fp, "        The checkpoint file does not exist!\n");
				fprintf(fp, "        File name: <%s>", CHECKPTFILE);
				fprintf(fp, "\n==================================================================\n");
				fserr = 1;
			}
			else if (access(CHECKPTFILE,6) != 0)
			{
				fprintf(fp, "\n==================================================================\n");
				fprintf(fp, "  ERROR parsing input file <%s>:\n", fileinp);
				fprintf(fp, "        The checkpoint file does not have read and/or write access!\n");
				fprintf(fp, "        File name: <%s>", CHECKPTFILE);
				fprintf(fp, "\n==================================================================\n");
				fserr = 1;
			}
		}


		/********************************************/
		/* ERROR                                    */
		/********************************************/
		if (fserr)
		{
			fprintf(fp, "\n");
			sprintf(errormessage, "\n  in: <read_par_json.c> \n");
			err(errormessage);
		}


	} /* End of if(MYID==0) */
}