Rupture Variation Generator v5.4.2 code changes
From SCECpedia
Jump to navigationJump to searchThis page documents the code changes required to turn the stand-alone version, genslip-v5.4.2, into an API callable by the CyberShake post-processing.
In this document, $ROOT_DIR is taken to be the <CyberShake home>/software/RuptureCodes/RupGen-api-5.4.2 directory. GenRandV5.0 is the code as received from Rob Graves.
Contents
Installing files
- Create src, include, lib, and bin directories inside $ROOT_DIR .
- Inside src, create GenRandV5.0.
- Copy rupgen_api.c from a previous API version into src.
- Copy rupgen_defs.h from a previous API version into src.
- Copy rupgen_api.h from a previous API version into src.
- Confusingly, there are two files named structure.h. One is in StandRupFormat and contains the structures needed for an SRF. The other is in GenRandV5.0 and contains the definition for complex, along with a few other supporting structures.
- Copy the StandRupFormat/structure.h into src/srf_structure.h .
- Copy the GenRandV5.0/structure.h into src/structure.h .
- Create a symlink to both files in src/GenRandV5.0.
- There are also two files named defs.h.
- Copy GenRandV5.0/StandRupFormat/defs.h into src/GenRandV5.0/srf_defs.h .
- Copy GenRandV5.0/defs.h into src/GenRandV5.0/defs.h .
- From the code provided by Rob, copy the following files into src/GenRandV5.0:
- defs.h
- function.h
- include.h
- genslip-v5.4.2.c
- iofunc.c
- misc.c
- ruptime.c
- slip.c
- srf_subs.c (from StandRupFormat)
- makefile
- Copy Makefile from a previous API version into src.
Editing files
In $ROOT_DIR/src
structure.h
- At the top of the file, change '#include "StandRupFormat/structure.h"' to '#include srf_structure.h'
- Above that line, add:
#ifndef STRUCT_H #define STRUCT_H
- At the end of the file, add:
#endif
srf_structure.h
- At the top of the file, add:
#ifndef SRF_STRUCT_H #define SRF_STRUCT_H
- At the end of the file, add:
#endif
Makefile
- Edit the GETPAR values to point to the getpar install.
- Edit GENSLIP to have the same name as the GenRandV5.0 directory.
- Edit the library creation line (under the librupgen.a target, starts with $(AR)) to point to the correct version of genslip.
In GenRandV5.0
defs.h
- At the top of the file, add:
#ifndef DEFS_H #define DEFS_H #include "srf_defs.h"
- Delete DEFAULT_SHAL_VRUP_FRAC, as it is not used anymore.
- At the end of the file, add:
#endif
srf_defs.h
- At the top of the file, add:
#ifndef SRF_DEFS_H #define SRF_DEFS_H
- Delete DEFAULT_SHAL_VRUP_FRAC, as it is not used anymore.
- Delete the lines corresponding to RDONLY_FLAGS, RDWR_FLAGS, CROPTR_FLAGS, and the _FILE_OFFSET_BITS if statement, as this already exists in defs.h.
- At the end of the file, add:
#endif
include.h
- At the top of the file, add:
#ifndef RUPGEN_INCLUDE_H #define RUPGEN_INCLUDE_H
- At the end of the file, add:
#include <string.h> #include "fftw3.h" #ifdef _USE_MEMCACHED #include <libmemcached/memcached.h> #endif #endif
function.h
- At the top of the file, add:
#ifndef RUPGEN_FUNC_H #define RUPGEN_FUNC_H #include "structure.h"
- At the end of the file, add:
void write_srf1(struct standrupformat *srf,char *file,int bflag); void write_srf2(struct standrupformat *srf,char *file,int bflag); #endif
makefile
- Edit the 2nd line that begins with OBJS so it contains
OBJS = iofunc.o misc.o slip.o ruptime.o srf_subs.o
- Delete the SRF_OBJS and FDRT_OBJS lines, 3-5.
- Edit GETPARLIB and GETPARINC so it correctly points to the Getpar install in <CyberShake home>/software/Getpar .
- Delete all targets except genslip-v5.4.2, ${OBJS}, and clean.
- Modify the genslip-v5.4.2 target. We need to remove unnecessary objects, and compile it to an object rather than an executable for inclusion in the genslip library. It should look like:
genslip-v5.4.2.o : genslip-v5.4.2.c ${OBJS} ${CC} -c -o genslip-v5.4.2.o genslip-v5.4.2.c ${LDLIBS}Note that if you copy/paste the line above, make will probably give you a whitespace error. Remember to replace the whitespace in front of ${CC} with a tab.
genslip-v5.4.2.c
This is where the majority of changes are made to turn this into an API. I recommend having an original copy for reference.
Note that line numbers are approximate.
- In line 352, change from
int main(int ac,char **av)
to
#ifdef _USE_MEMCACHED int mc_genslip(int ac, char** av, rg_stats_t *stats, struct standrupformat* srf, int state, char* memcached_server) #else int genslip(int ac,char **av, rg_stats_t *stats, struct standrupformat* srf, int state) #endif
- Change target_hypo_spacing to 4.5:
float target_hypo_spacing = 4.5;
- Delete SRF definition in line 584:
struct standrupformat srf;
- After line 601:
int outbin = 0;
Add
int doslip = -1; int dohypo = -1;
- Change 'srf.version' to 'srf->version' in line 631.
- In line 699, after 'getpar("outbin","d",&outbin);', add:
getpar("doslip","d",&doslip); getpar("dohypo","d",&dohypo);
- In line 959, change
psrc_orig = read_ruppars(infile,psrc_orig,&mag,&nstk,&ndip,&dstk,&ddip,&dtop,&avgstk,&avgdip,&elon,&elat);
to
#ifdef _USE_MEMCACHED psrc_orig = _mc_read_ruppars(infile,psrc_orig,&mag,&nstk,&ndip,&dstk,&ddip,&dtop,&avgstk,&avgdip,&elon,&elat,memcached_server); #else psrc_orig = _read_ruppars(infile,psrc_orig,&mag,&nstk,&ndip,&dstk,&ddip,&dtop,&avgstk,&avgdip,&elon,&elat); #endif
- In line 1315, after 'dhypo = dhypo_frac*fwid;', add:
stats->numslip = ns; stats->numhypo = nh; if (state==GET_STATS) { free(rvfac_seg); free(gbnd); free(gwid); free(psrc); free(psrc_orig); free(slip_c); free(rake_c); free(tsfac1_c); free(rtime1_c); free(rough_c); free(tsfac2_c); free(rtime2_c); free(slip_r); free(rake_r); free(tsfac1_r); free(rtime1_r); free(rough_r); free(tsfac2_r); free(rtime2_r); free(stk_r); free(dip_r); free(psrc_rake); return 0; } if (dohypo>=nh) { fprintf(stderr,"Error: You requested hypocenter ID %d but from infile %s genslip calculates %d hypocenters.\n", dohypo, infile, nh); exit(1); } if (doslip>=ns) { fprintf(stderr,"Error: You requested slip ID %d but from infile %s genslip calculates %d slips.\n", doslip, infile, ns); exit(2); } if (ns<-1 || nh<-1) { fprintf(stderr, "Error: hypocenter and slip ID need to be >= -1."); exit(3); }
- In line 1363, after:
for(js=0;js<ns;js++) /* loop over slip/rupture realizations */ {
add
if (doslip!=-1 && js>doslip) { //We've already done the slip we need, break out of the loop break; } if (doslip!=-1 && js<doslip) { //Haven't gotten to the right slip yet, continue continue; }
- In lines 1394-1403, change iterative calculation of shypo and dhypo to explicit. Delete
if(ih_scnt == nhypo_s) { ih_scnt = 0; ih_dcnt++; } shypo = hypo_s0 + ih_scnt*hypo_ds; dhypo = hypo_d0 + ih_dcnt*hypo_dd; ih_scnt++;
and replace with
shypo = hypo_s0 + (js%nhypo_s)*hypo_ds; dhypo = hypo_d0 + (js/nhypo_s)*hypo_dd;
- In the for loop starting in line 2452, we replace some of these calculations with refactored ones to pull out constants and take advantage of FMA. Specifically, make the following changes:
- Before the for loop, define the following variables:
float half_ddip = 0.5*ddip; float half_dstk_minus_half_flen = 0.5*dstk - 0.5*flen; float half_flen_plus_shypo = 0.5*flen + shypo; y0 = (deep_vrdep1-dtop)/sin(avgdip*rperd); float deep_alpha = 1.0 + (deep_vrdep1-deep_rup*deep_vrdep1)/(deep_vrdep2-deep_vrdep1); float deep_const = (deep_rup - 1.0)/(deep_vrdep2-deep_vrdep1);
- Replace
yy = (j + 0.5)*ddip;
with
yy = j*ddip + half_ddip;
- Replace
xx = (i+0.5)*dstk - 0.5*flen;
with
xx = i*dstk + half_dstk_minus_half_flen;
- Replace
hg = (gbnd[ig]-0.5*flen)-shypo;
with
hg = gbnd[ig] - half_flen_plus_shypo;
- Replace
vrfac = 1.0 + (deep_vrup - 1.0)*((psrc[ip].dep)-deep_vrdep1)/(deep_vrdep2-deep_vrdep1);
with
vrfac = deep_alpha + deep_const*psrc[ip].dep;
- Before the for loop, define the following variables:
- In line 2573, after 'psrc[j].rupt = psrc[j].rupt - tsmin + rupture_delay;', add:
if (dohypo!=ih || doslip!=js) { continue; }
- In line 2611, remove
free_srf_stf(&srf);
- At the very end of the function, we want to free everything not needed by the SRF object. Add:
free(rvfac_seg); free(gbnd); free(gwid); free(psrc); free(psrc_orig); free(slip_c); free(rake_c); free(tsfac1_c); free(rtime1_c); free(rough_c); free(tsfac2_c); free(rtime2_c); free(slip_r); free(rake_r); free(tsfac1_r); free(rtime1_r); free(rough_r); free(tsfac2_r); free(rtime2_r); free(stk_r); free(dip_r); free(psrc_rake); free(rspd); free(vmod.vp); free(vmod.vs); free(vmod.den); free(vmod.th); free(vmod.dep); free(vmod.mu); free(vmod.invb2); free(rvmod.vs); free(rvmod.th); free(rvmod.invb2); return 0;
- Once verification is complete, you may wish to comment out some of the print statements.