void ControllerMappingLoader::load(const std::string& filename) { VFS* vfs = VFS::instance(); std::unique_ptr<RawData> data(vfs->open(filename)); size_t datalen = data->getDataLength(); std::unique_ptr<uint8_t[]> darray(new uint8_t[datalen]); data->readInto(darray.get(), datalen); SDL_RWops* rwops = SDL_RWFromConstMem(darray.get(), static_cast<int>(datalen)); if (SDL_GameControllerAddMappingsFromRW(rwops, 0) == -1) { throw SDLException(std::string("Error when loading gamecontroller mappings: ") + SDL_GetError()); } SDL_FreeRW(rwops); }
void ImageLoader::load(IResource* res) { VFS* vfs = VFS::instance(); Image* img = dynamic_cast<Image*>(res); //Have to save the images x and y shift or it gets lost when it's //loaded again. int32_t xShiftSave = img->getXShift(); int32_t yShiftSave = img->getYShift(); if(!img->isSharedImage()) { const std::string& filename = img->getName(); boost::scoped_ptr<RawData> data (vfs->open(filename)); size_t datalen = data->getDataLength(); boost::scoped_array<uint8_t> darray(new uint8_t[datalen]); data->readInto(darray.get(), datalen); SDL_RWops* rwops = SDL_RWFromConstMem(darray.get(), static_cast<int>(datalen)); SDL_Surface* surface = IMG_Load_RW(rwops, false); if (!surface) { throw SDLException(std::string("Fatal Error when loading image into a SDL_Surface: ") + SDL_GetError()); } RenderBackend* rb = RenderBackend::instance(); // in case of SDL we don't need to convert the surface if (rb->getName() == "SDL") { img->setSurface(surface); // in case of OpenGL we need a 32bit surface } else { SDL_PixelFormat dst_format = rb->getPixelFormat(); SDL_PixelFormat src_format = *surface->format; uint8_t dstbits = dst_format.BitsPerPixel; uint8_t srcbits = src_format.BitsPerPixel; if (srcbits != 32 || dst_format.Rmask != src_format.Rmask || dst_format.Gmask != src_format.Gmask || dst_format.Bmask != src_format.Bmask || dst_format.Amask != src_format.Amask) { dst_format.BitsPerPixel = 32; SDL_Surface* conv = SDL_ConvertSurface(surface, &dst_format, 0); dst_format.BitsPerPixel = dstbits; if (!conv) { throw SDLException(std::string("Fatal Error when converting surface to the screen format: ") + SDL_GetError()); } img->setSurface(conv); SDL_FreeSurface(surface); } else { img->setSurface(surface); } } SDL_FreeRW(rwops); } //restore saved x and y shifts img->setXShift(xShiftSave); img->setYShift(yShiftSave); }
int SDSS_statistics(compspec *cspec,spectrum *spec,int nspec, setblock *set){ int i=0,j=0; int *s=NULL; double *u=NULL; statset bstat; FILE *params = NULL; params = fopen("parameters.txt", "a"); for(i = 0; i<5763; i++){ fprintf(params, "%f\t%i\t%t\n", cspec[0].sum[i], cspec[0].nhist[i], cspec[0].sum2[i]); } fclose(params); for(j=0;j<set->nforest;j++){ i=0; while(cspec[j].nhist[i] == 0) i++; for(;i<3000;i++){ if(cspec[j].nhist[i]>0){ /* UNweighted */ cspec[j].fl[i]=cspec[j].sum[i]/cspec[j].nhist[i]; cspec[j].er[i]=cspec[j].sum2[i]/cspec[j].nhist[i]- pow(cspec[j].fl[i],2); cspec[j].er[i]=(cspec[j].nhist[i] > 1 ? sqrt(cspec[j].er[i]/(cspec[j].nhist[i]-1)) : 0); /* Wieghted */ // cspec[j].fl[i]=cspec[j].sum[i]/cspec[j].wsum[i]; // cspec[j].er[i]=cspec[j].sum2[i]/cspec[j].nhist[i]- // pow(cspec[j].fl[i],2); // cspec[j].er[i]=(cspec[j].nhist[i] > 1 ? // sqrt(cspec[j].er[i]/(cspec[j].nhist[i]-1)) : 0); } } } printf("nspec: %i\n", nspec); u=darray(nspec); s=iarray(nspec); for(i=0;i<nspec;i++) { u[i]=spec[i].alpha; s[i]=(spec[i].alpha<-900 ? 0 : 1); } stats(u,NULL,NULL,NULL,s,nspec,0,&bstat); cspec[0].mean_a=bstat.mean; cspec[0].sigma_a=bstat.rms; median(u,s,nspec,&bstat,0); cspec[0].med_a=bstat.med; cspec[0].siqr_a=bstat.siqr; free(u); free(s); return 1; }
NS_IMETHODIMP nsSpeechTask::SendAudio(JS::Handle<JS::Value> aData, JS::Handle<JS::Value> aLandmarks, JSContext* aCx) { MOZ_ASSERT(XRE_IsParentProcess()); if(NS_WARN_IF(!(mStream))) { return NS_ERROR_NOT_AVAILABLE; } if(NS_WARN_IF(mStream->IsDestroyed())) { return NS_ERROR_NOT_AVAILABLE; } if(NS_WARN_IF(!(mChannels))) { return NS_ERROR_FAILURE; } if(NS_WARN_IF(!(aData.isObject()))) { return NS_ERROR_INVALID_ARG; } if (mIndirectAudio) { NS_WARNING("Can't call SendAudio from an indirect audio speech service."); return NS_ERROR_FAILURE; } JS::Rooted<JSObject*> darray(aCx, &aData.toObject()); JSAutoCompartment ac(aCx, darray); JS::Rooted<JSObject*> tsrc(aCx, nullptr); // Allow either Int16Array or plain JS Array if (JS_IsInt16Array(darray)) { tsrc = darray; } else { bool isArray; if (!JS_IsArrayObject(aCx, darray, &isArray)) { return NS_ERROR_UNEXPECTED; } if (isArray) { tsrc = JS_NewInt16ArrayFromArray(aCx, darray); } } if (!tsrc) { return NS_ERROR_DOM_TYPE_MISMATCH_ERR; } uint32_t dataLen = JS_GetTypedArrayLength(tsrc); RefPtr<mozilla::SharedBuffer> samples; { JS::AutoCheckCannotGC nogc; samples = makeSamples(JS_GetInt16ArrayData(tsrc, nogc), dataLen); } SendAudioImpl(samples, dataLen); return NS_OK; }
double *fit_depth(double *zgrid, double dz, int Ngrid, double *zs, double *err, double *weights, int N){ /* Returns the number of events in depth bins. * * Input: * * zgrid, ygrid: vertical grid for which rate should be calculated. size [1...Ngrid] * dz: spacing between grid points. * zs: depth of events. size [1...N] * err= location error associated with each event. size [1...N]. Used as st. dev. for smoothing. * weights: weight assigned to each event. Can be used as flag for declustering (0/1 for excluded/selected events), or to weight some events more than others. * if weights==NULL, all events are selected with weight=1. size [1...N] * * Output: * vector of size [1...Ngrid] containing number of events in each grid point for the total time period. * */ double *prob0, *prob; double probCum; double rz, w; prob0=darray(1,Ngrid); prob=darray(1,Ngrid); for (int n=1; n<=Ngrid; n++) prob[n]=0; //todo could parallelize(omp) for (int eq=1; eq<=N; eq++){ probCum=0; for (int n=1; n<=Ngrid; n++){ rz=fabs(zgrid[n]-zs[eq]); //smooth event across grid points: prob0[n]=exact_prob_1d(rz, dz, err[eq]); } w=(weights)? weights[eq] : 1.0; for (int n=1; n<=Ngrid; n++) prob[n]+=w*prob0[n]; } free_darray(prob0,1,Ngrid); return prob; }
void init_cat1(struct catalog *cat, int Zsel){ /* Initialize variables in catalog structure to default values. */ (*cat).Z=Zsel; (*cat).t = darray(1, Zsel); (*cat).mag = darray(1, Zsel); (*cat).lat0 = darray(1, Zsel); (*cat).lon0 = darray(1, Zsel); (*cat).x0 = darray(1, Zsel); (*cat).y0 = darray(1, Zsel); (*cat).depths0 = darray(1, Zsel); (*cat).err = darray(1, Zsel); (*cat).verr = darray(1, Zsel); (*cat).ngrid = iarray(1, Zsel); //just allocate first level since subarrays may have different length (and will be initialized later). (*cat).ngridpoints=i2array_firstlevel(Zsel); (*cat).weights=d2array_firstlevel(Zsel); (*cat).b=1.0; }
struct eqkfm *eqkfm_array(long n1, long n2){ /* Allocate memory to array of eqkfm. */ struct eqkfm *v; v= (struct eqkfm *) malloc((size_t) ((n2-n1+1+offset)*sizeof(struct eqkfm))); for (int i=offset; i<=n2-n1+offset; i++){ v[i].slip_str= NULL; v[i].slip_dip= NULL; v[i].open= NULL; v[i].ts=NULL; v[i].nosnap=0; v[i].allslip_str= NULL; v[i].allslip_dip= NULL; v[i].allslip_open= NULL; v[i].tevol=NULL; v[i].pos_s= NULL; v[i].pos_d= NULL; v[i].selpoints= NULL; v[i].distance= NULL; v[i].is_slipmodel=0; v[i].np_st=v[i].np_di=v[i].whichfm=v[i].nsel=0; v[i].t=0; v[i].lat=0; v[i].lon=0; v[i].depth=0; v[i].mag=0; v[i].tot_slip=darray(0,0); //only need one element for earthquake sources, will reallocate for afterslip. v[i].L=0; v[i].W=0; v[i].str1=0; v[i].str2=0; v[i].dip1=0; v[i].dip2=0; v[i].rake1=0; v[i].rake2=0; v[i].index_cat=0; v[i].cuts_surf=0; v[i].co_aft_pointer=NULL; } return v-n1+offset; }
NS_IMETHODIMP nsSpeechTask::SendAudio(JS::Handle<JS::Value> aData, JS::Handle<JS::Value> aLandmarks, JSContext* aCx) { MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default); NS_ENSURE_TRUE(mStream, NS_ERROR_NOT_AVAILABLE); NS_ENSURE_FALSE(mStream->IsDestroyed(), NS_ERROR_NOT_AVAILABLE); NS_ENSURE_TRUE(mChannels, NS_ERROR_FAILURE); NS_ENSURE_TRUE(aData.isObject(), NS_ERROR_INVALID_ARG); if (mIndirectAudio) { NS_WARNING("Can't call SendAudio from an indirect audio speech service."); return NS_ERROR_FAILURE; } JS::Rooted<JSObject*> darray(aCx, &aData.toObject()); JSAutoCompartment ac(aCx, darray); JS::Rooted<JSObject*> tsrc(aCx, nullptr); // Allow either Int16Array or plain JS Array if (JS_IsInt16Array(darray)) { tsrc = darray; } else if (JS_IsArrayObject(aCx, darray)) { tsrc = JS_NewInt16ArrayFromArray(aCx, darray); } if (!tsrc) { return NS_ERROR_DOM_TYPE_MISMATCH_ERR; } SendAudioImpl(JS_GetInt16ArrayData(tsrc), JS_GetTypedArrayLength(tsrc)); return NS_OK; }
int rate_state_evolution(struct catalog cat, double *times, double **cmpdata, struct pscmp *DCFS, double tt0, double tt1, double dt_step, double Asig, double ta, int points[], double *out_NeX, double *NeT, double *ReT, double **all_NeT, int N, int NTS, int Neqks, double *gamma_init, double *back_rate, double *R, int last){ /* Calculates seismicity in space and time based on rate-and-state (Dieterich 1994) constitutive law. * Aseismic stresses in cmpdata are assumed to grow linearly between time steps. * * Input: * cat: catalog containing earthquakes to be included in LogLikelihood calculation. * times: timesteps corresponding to aseismic loading. [0...NTS] * cmpdata: stresses due to aseismic loading. [0...NTS-1; 1...N], where cmpdata[x][m] is the stress change at grid point [m] between times[x+1] and times[x]. * if NULL, it will be ignored. * DCFS: structure containing seismic sources. [0...Neqks-1] * tt0, tt1: start, end time of calculation * Asig, ta: rate-state parameters * points: list of indices of points to be included (referred to grid vectors in crst structure in main). if NULL, use sequence [1,2,3,...,N] * N, NTS, Neqks: no. of grid points, time steps for aseismic sources, seismic sources. * gamma_init: values of gammas at time tt0. NB: indices refer to *entire grid* (not just those elements of "points"): [1...NgridT] * back_rate: spatially nonuniform background rate. array containing NgridT times the ratio between avg rate and rate at that point. (so that the sum of back_rate is NgridT). * if NULL, will assume it's 1 for all points (uniform rate). NB: indices refer to *entire grid* (not just those elements of "points"): [1...NgridT] * last: flag indicating if the values of gamma_init should be overwritten at the end, with value at tt1. * * Output: * out_NeX: number of events in each grid cell. [1...N] (i.e. it refers to the subset of points in "points"). * NeT: total no of events in each time step (i.e., sum of out_NeX). [0...nts-1] * ReT: total seismicity rate at the end of each time step. [0...nts-1] * all_NeT: no of events binned in space and time [0...nts-1][1...N]; * R: seismicity rate at the time of the earthquakes given in cat (used for first term of LogLikelihood). * * NB: assumes that times, cat, DCFS, NTS, times are always the same (each time function is called), since many static internal structures are initialized in the first function call. */ // Variables used for MPI. int procId = 0; #ifdef _CRS_MPI MPI_Comm_rank(MPI_COMM_WORLD, &procId); #endif double tau, dtau_dt, dtau_dt00=Asig/ta, ta1; int TS0, TS1, n; double gamma, back_rate_n; double tt0step, tt1step; //events will contain times, magnitudes of all earthquakes (both from catalog and from DCFS): static double **events; static int **indices; //contains indices of events referred to DCFS and cat. int Neq; //size of events[0...Neq]; int is_incat, is_inDCFS; //flag for events in events int cat_i, DCFS_i; //indices of elements in events referred to cat, DCFS. static int *TS_eqk; //time step before each earthquake in events. // variables used to find earthquakes within each grid point (see below): static int **which_eqk; static int *num_eqk; static float ** cat_weights; static int ** DCFS_whichpt; //internal variables for seismicity calculation: double *NeX; double **Rprivate; //private variable for each OMP thread, corresponds to global R. double **NeTprivate, **ReTprivate; static double *dt, *NeXdum, *ReX; //dummy variables: int err=0, errtot=0; int j0, next_eqk, counter_eqk, next_TS; int counter1, counter2, k; double t_pre, t_now, t_endstep; double a,b; int reach_end; int step, nts; int nthreads, nthreadstot=omp_get_max_threads(); static int firsttimein=1; int warning_printed=0; if (firsttimein==1){ firsttimein=0; //allocate memory: NeXdum=darray(1,N); ReX=darray(1,N); //----------------------------------------------------------------------------------------------// // Set up list of earthquakes for each grid point // //----------------------------------------------------------------------------------------------// //create a merged set of events from cat and DCFS; this is needed since the both define calculation time steps. events=union_cats2(cat, DCFS, Neqks, &indices, &Neq); which_eqk=i2array(1,N,0,Neq-1); //list of earthquakes in each grid point num_eqk=iarray(1,N); //number of earthquakes in each grid point cat_weights=f2array(1,N,0,Neq-1); //catalog weights DCFS_whichpt=i2array(1,N,0,Neq-1); //grid point index in DCFS elements (since they contain stress field for a selection of pts only) for (int n=1; n<=N; n++) num_eqk[n]=0; for(int eq=0;eq<Neq;eq++){ counter1=counter2=1; cat_i=indices[1][eq]; // NB:cat_i[i]==-1 means no events selected. DCFS_i=indices[2][eq]; // NB:DCFS_i[i]==-1 means no events selected. for (int n=1; n<=N; n++){ if (counter2 > DCFS[DCFS_i].nsel && counter1 > cat.ngrid[cat_i]) break; //all points have already been included else{ //find index in cat and DCFS which may refer to this grid point: if (cat_i!=-1) while (counter1 <=cat.ngrid[cat_i] && cat.ngridpoints[cat_i][counter1]<n) counter1+=1; if (DCFS_i!=-1) while (counter2 <=DCFS[DCFS_i].nsel && DCFS[DCFS_i].which_pts[counter2]<n) counter2+=1; //check if index refers to this grid point: is_incat= (cat_i!=-1 && counter1 <= cat.ngrid[cat_i] && cat.ngridpoints[cat_i][counter1]==n) ? 1 : 0; is_inDCFS= (DCFS_i!=-1 && counter2 <= DCFS[DCFS_i].nsel && DCFS[DCFS_i].which_pts[counter2]==n) ? 1 : 0; //add earthquake to list for this grid point, and fill in cat_weights, DCFS_whichpt accordingly if (is_incat | is_inDCFS){ which_eqk[n][num_eqk[n]]=eq; cat_weights[n][num_eqk[n]]= (is_incat)? cat.weights[cat_i][counter1] : 0.0; //only in DCFS DCFS_whichpt[n][num_eqk[n]]= (is_inDCFS)? counter2 : 0; //only in cat: will not change gamma later. num_eqk[n]+=1; } } } } //----------------------------------------------------------------------------------------------// // Time steps initialization // //----------------------------------------------------------------------------------------------// //calculate time step duration: if (NTS!=0){ dt=darray(0,NTS-1); for (int g=0; g<NTS; g++) dt[g]=times[g+1]-times[g]; } //find last time step before each earthquake: TS_eqk=iarray(0,Neq-1); for (int i=0; i<Neq; i++){ if (NTS==0) TS_eqk[i]=1; //this will make t_pre be correct later (j0<NTS condition). else{ k=0; while(k<NTS && times[k]<events[1][i]) k++; if(times[k]>=events[1][i]) k--; TS_eqk[i]=k; if (k<0 && events[1][i]>=tt0) { print_screen("**Warning: no time steps available before earthquake no. %d ** (forecast_stepG2_new.c)\n",i); print_logfile("**Warning: no time steps available before earthquake no. %d ** (forecast_stepG2_new.c)\n",i); return 1; } } } } if (Asig==0 && ta==0.0) return(0); //in this case, function has only been called to setup variables above. NeX= (out_NeX)? out_NeX : NeXdum; // Initialize vector to 0; if (ReT) for(int m=1;m<=N;m++) ReX[m]=0.0; if (NeX) for(int m=1;m<=N;m++) NeX[m]=0.0; if (tt1<tt0) { print_screen("\n*** Warning: tt1<tt0 in forecast_stepG2_new.c ***\n"); print_logfile("\n*** Warning: tt1<tt0 in forecast_stepG2_new.c ***\n"); return 1; } if (NTS>0 && times[0]>tt0 && times[NTS]<tt1) { print_screen("\n** Warning: time steps in forecast_stepG don't cover entire forecast range!**\n"); print_logfile("\n** Warning: time steps in forecast_stepG don't cover entire forecast range!**\n"); return 1; } //TS0= First time step after tt0. TS0=0; if (NTS==0) TS0=1; //this will make t_pre be correct later (j0<NTS condition). else{ while(TS0<NTS && times[TS0]<=tt0) TS0++; if(procId == 0) { if(times[TS0]<=tt0 & !warning_printed) { warning_printed=1; print_screen("\n*** Warning: times[TS0]<=tt0 in forecast_stepG2_new.c ***\n"); print_logfile("\n*** Warning: times[TS0]<=tt0 in forecast_stepG2_new.c ***\n"); return 1; } } } //TS1= Last time step before tt1. TS1=1; //this will make t_pre be correct later (j0<NTS condition). if (NTS>0){ while(TS1<NTS-1 && times[TS1]<tt1) TS1++; if(times[TS1]>=tt1) TS1--; } //Rprivate is used for parallelization (a barrier creates a bottleneck and poor performance): Rprivate=d2array(0,nthreadstot-1, 0, cat.Z); if (!Rprivate) memory_error_quit; for (int t=0; t<omp_get_max_threads(); t++){ for (int eq=0; eq<=cat.Z; eq++) Rprivate[t][eq]=0.0; } //Similar to above, but for individual time steps: nts=(dt_step<tol0) ? 0 : ceil((tt1-tt0)/dt_step); //number of output time steps; for (int i=0; i<nts; i++){ if (ReT) ReT[i]=0.0; if (NeT) NeT[i]=0.0; } NeTprivate=d2array(0,nthreadstot-1, 0, nts-1); ReTprivate=d2array(0,nthreadstot-1, 0, nts-1); if (!NeTprivate | !ReTprivate) memory_error_quit; for (int t=0; t<omp_get_max_threads(); t++){ if (NeT) for (int i=0; i<nts; i++) NeTprivate[t][i]=0.0; if (ReT) for (int i=0; i<nts; i++) ReTprivate[t][i]=0.0; } err=0; //loop over grid points: #pragma omp parallel for firstprivate(err) private(n, gamma,tau,j0, next_eqk, next_TS, t_now, t_pre, reach_end, t_endstep, cat_i, DCFS_i, a, b, dtau_dt, counter_eqk, back_rate_n, ta1, tt0step, tt1step, step, TS0, TS1) reduction(+:errtot) for(int m=1;m<=N;m++){ nthreads=omp_get_num_threads(); tt0step=tt0; tt1step= (nts==1)? tt1 : tt0step+dt_step; //to make sure it enters at least once (floating point error may give problem otherwise). t_now=tt0; if (err!=0) continue; n=(points==0)? m : points[m]; //set background rate and starting gamma given as arguments: back_rate_n= (back_rate) ? back_rate[n] : 1.0; gamma=gamma_init[n]; if (NeX) NeX[m]=0.0; step=0; while (tt1step<=tt1){ //overall time span if (dt_step<tol0) break; //to avoid infinite loop if tt0=tt1 and dt_step=0; //TS0= First time step after tt0. TS0=0; if (NTS==0) TS0=1; //this will make t_pre be correct later (j0<NTS condition). else{ while(TS0<NTS && times[TS0]<=tt0step) TS0++; if(procId == 0) { if(times[TS0]<=tt0 & !warning_printed) { warning_printed=1; print_screen("\n*** Warning: times[TS0]<=tt0 in forecast_stepG2_new.c ***\n"); print_logfile("\n*** Warning: times[TS0]<=tt0 in forecast_stepG2_new.c ***\n"); } } } //TS1= Last time step before tt1. TS1=1; //this will make t_pre be correct later (j0<NTS condition). if (NTS>0){ while(TS1<NTS-1 && times[TS1]<tt1step) TS1++; if(times[TS1]>=tt1step) TS1--; } j0=TS0; //next time step (first time step after tt0) counter_eqk=0; reach_end=0; //find first earthquake after or at tt0: while (counter_eqk<num_eqk[n] && events[1][which_eqk[n][counter_eqk]]<tt0step) counter_eqk+=1; //loop over time steps until reaching tt1step: while (tol0<(tt1step-t_now)){ if (err!=0) break; //error in a previous loop; if (counter_eqk>=num_eqk[n]) reach_end=1; //all earthquakes for this grid point have been included (counter_eqk is out of range). else { next_eqk=which_eqk[n][counter_eqk]; if (events[1][next_eqk]>=tt1step) reach_end=1; } t_endstep= reach_end ? tt1step : events[1][next_eqk]; //end time of this while loop iteration; next_TS= reach_end ? TS1 : TS_eqk[next_eqk]; //last time step before t_endstep cat_i= reach_end ? 0 : indices[1][next_eqk]; //index of next event (occurring at time events[1][next_eqk]) DCFS_i= reach_end ? 0 : indices[2][next_eqk]; //index of next event (occurring at time events[1][next_eqk]) // evolve seismicity up to next barrier (which is the smallest between next earthquake (events[1][next_eqk]), next time step (times[j0]) or tt1step) t_pre= (j0<=NTS) ? fmin(t_endstep, times[j0]) - t_now : t_endstep - t_now; //find time left to next barrier dtau_dt=(cmpdata && j0-1>=0 && j0<=NTS)? (Asig/ta)+cmpdata[j0-1][n]/dt[j0-1] : (Asig/ta);//stressing rate during current time step (including stress step from cmpdata and background stressing rate): if (t_pre>tol0){ tau=dtau_dt*t_pre; //stress change during current time step ta1=Asig/dtau_dt; //dummy variable if (NeX) { //find no. of earthquakes a=gamma*dtau_dt-1; //dummy variable b=a*exp(-t_pre/ta1)+1; //dummy variable if (!isinf(fabs(b))) NeX[m]+= fmax(0.0, back_rate_n*(dtau_dt/dtau_dt00)*(t_pre+ta1*log(b/(gamma*dtau_dt)))); //due to numerical error it can give -ve values. } //update gamma: gamma=(fabs(tau/Asig)>1e-10)? (gamma-t_pre/(tau))*exp(-tau/Asig)+t_pre/(tau) : gamma*(1-tau/Asig)+t_pre/Asig; } //evolve seismicity between time steps: t_now+=t_pre; for(int j=j0;j<next_TS;j++){ dtau_dt=(cmpdata)? (Asig/ta)+cmpdata[j][n]/dt[j] : (Asig/ta); tau=dtau_dt*dt[j]; ta1=Asig/dtau_dt; if (NeX) { a=gamma*dtau_dt-1; b=a*exp(-dt[j]/ta1)+1; if (!isinf(fabs(b))) NeX[m]+=fmax(0.0, back_rate_n*(dtau_dt/dtau_dt00)*(dt[j]+ta1*log(b/(gamma*dtau_dt)))); //due to numerical error it can give -ve values for gamma->inf } gamma=(fabs(tau/Asig)>1e-10)? (gamma-dt[j]/(tau))*exp(-tau/Asig)+dt[j]/(tau) : gamma*(1-tau/Asig)+dt[j]/Asig; t_now+=dt[j]; } // evolve seismicity till the end: t_pre=t_endstep-t_now; if (t_pre>tol0) { dtau_dt=(cmpdata)? (Asig/ta)+(1.0/dt[next_TS])*cmpdata[next_TS][n] : (Asig/ta); tau=dtau_dt*t_pre; ta1=Asig/dtau_dt; if (NeX) { a=gamma*dtau_dt-1; b=a*exp(-t_pre/ta1)+1; if (!isinf(fabs(b))) NeX[m]+= fmax(0.0, back_rate_n*(dtau_dt/dtau_dt00)*(t_pre+ta1*log(b/(gamma*dtau_dt)))); //due to numerical error it can give -ve values for gamma->inf } gamma=(fabs(tau/Asig)>1e-10)? (gamma-t_pre/(tau))*exp(-tau/Asig)+t_pre/(tau) : gamma*(1-tau/Asig)+t_pre/Asig; } t_now+=t_pre; if (reach_end==0){ if (cat_i!=-1) { Rprivate[omp_get_thread_num()][cat_i]+= back_rate_n*cat_weights[n][counter_eqk]*(ta/Asig)/gamma; } gamma= (DCFS_i==-1 | DCFS_whichpt[n][counter_eqk]==0)? gamma : gamma*exp(-DCFS[DCFS_i].cmb[DCFS_whichpt[n][counter_eqk]]/Asig); //if (n==1) printf("[%.5e:%.5e][%.5e:%.5e]: gamma=%f\n",tt0,tt1, tt0step, tt1step,gamma); if (isinf(gamma)){ print_screen("*Warning: gamma==Inf, must choose larger Asig!*\n"); print_logfile("*Warning: gamma==Inf, must choose larger Asig!*\n"); err=1; errtot+=1; } } j0=next_TS+1; counter_eqk+=1; } if (NeT) NeTprivate[omp_get_thread_num()][step]+=NeX[m]; if (ReT) ReTprivate[omp_get_thread_num()][step]+=back_rate_n*(ta/Asig)/gamma; if (all_NeT) all_NeT[step][m]=NeX[m]; step+=1; tt0step=tt1step; tt1step+=dt_step; if (fabs(tt1step-tt1)<tol0) tt1step=tt1; //to avoid floating point error. } if (last) gamma_init[n]=gamma; //update gamma_init with final value; if (ReT) ReX[m]=back_rate_n*(ta/Asig)/gamma; //instantaneous seismicity rate at the end of the time step; } //collect values of R, NeT, ReT from threads: //final rate and tot no. of events given by the sum over all grid points: for (int i=0; i<nts; i++){ if (ReT) ReT[i]=0.0; if (NeT) NeT[i]=0.0; } for (int t=0; t<nthreads; t++){ if (R) for (int eq=1; eq<=cat.Z; eq++) R[eq]+=Rprivate[t][eq]; if (NeT) for (int i=0; i<nts; i++) { if (NeT) NeT[i]+= (i==0) ? NeTprivate[t][i] : NeTprivate[t][i]-NeTprivate[t][i-1]; if (ReT) ReT[i]+= ReTprivate[t][i]; } } free_d2array(Rprivate,0,nthreadstot-1, 0, cat.Z); free_d2array(NeTprivate,0,nthreadstot-1, 0, nts-1); free_d2array(ReTprivate,0,nthreadstot-1, 0, nts-1); return(errtot); }
int svdfit(double *x, double *y, double *sig, int ndata, double *a, int ma, double ***u, double ***v, double **w, double *chisq, int (*funcs)(double, double *, int)) { int i=0,j=0; double wmax=0.0,tmp=0.0,thresh=0.0,sum=0.0; double *b=NULL,*afunc=NULL; /* Allocate memory for relevant arrays/matrices */ if ((b=darray(ndata))==NULL) { nferrormsg("svdfit(): Cannot allocate memory to b\n\tarray of size %d", ndata); return 0; } if ((afunc=darray(ma))==NULL) { nferrormsg("svdfit(): Cannot allocate memory to afunc\n\tarray of size %d", ma); return 0; } if ((*w=darray(ma))==NULL) { nferrormsg("svdfit(): Cannot allocate memory to w\n\tarray of size %d", ma); return 0; } if ((*u=dmatrix(ndata,ma))==NULL) { nferrormsg("svdfit(): Cannot allocate memory to matrix\n\t of size %dx%d", ndata,ma); return 0; } if ((*v=dmatrix(ma,ma))==NULL) { nferrormsg("svdfit(): Cannot allocate memory to matrix\n\t of size %dx%d", ma,ma); return 0; } /* Begin SVD fitting */ for (i=0; i<ndata; i++) { if (!(*funcs)(x[i],afunc,ma)) { nferrormsg("svdfit(): Error returned from fitting function"); return 0; } tmp=1.0/sig[i]; for (j=0; j<ma; j++) (*u)[i][j]=afunc[j]*tmp; b[i]=y[i]*tmp; } if (!svdcmp(*u,ndata,ma,*w,*v)) { nferrormsg("svdfit(): Error returned from svdcmp()"); return 0; } wmax=0.0; for (j=0; j<ma; j++) wmax=MAX(wmax,(*w)[j]); thresh=SVDFIT_TOL*wmax; for (j=0; j<ma; j++) { if ((*w)[j]<thresh) { (*w)[j]=0.0; warnmsg("svdfit(): Setting coefficient %d's singular value to zero",j); } } if (!svbksb(*u,*w,*v,ndata,ma,b,a)) { nferrormsg("svdfit(): Error returned from svbksb()"); return 0; } *chisq=0.0; for (i=0; i<ndata; i++) { if (!(*funcs)(x[i],afunc,ma)) { nferrormsg("svdfit(): Error returned from fitting function"); return 0; } for (sum=0.0,j=0; j<ma; j++) sum+=a[j]*afunc[j]; *chisq+=(tmp=(y[i]-sum)/sig[i],tmp*tmp); } /* Clean up */ free(b); free(afunc); return 1; }