From 9cf2f7dffd17a21888f94cd22e1428dc39ca0a3c Mon Sep 17 00:00:00 2001 From: ims Date: Thu, 20 Apr 2017 10:27:24 +0200 Subject: [PATCH 1/2] Slightly rearranged stateq. In stateq.c I moved the function getmatrix() to before stateq(), which allows its template to be removed from lime.h. --- src/lime.h | 2 +- src/stateq.c | 170 ++++++++++++++++++++++++++------------------------- 2 files changed, 87 insertions(+), 85 deletions(-) diff --git a/src/lime.h b/src/lime.h index b8973a1..d6e3005 100644 --- a/src/lime.h +++ b/src/lime.h @@ -337,7 +337,7 @@ void getclosest(double, double, double, long*, long*, double*, double*, double*) double geterf(const double, const double); void getjbar(int, molData*, struct grid*, const int, configInfo*, struct blendInfo, int, gridPointData*, double*); void getMass(configInfo*, struct grid*, const gsl_rng*); -void getmatrix(int, gsl_matrix*, molData*, struct grid*, int, gridPointData*); +//void getmatrix(int, gsl_matrix*, molData*, struct grid*, int, gridPointData*); void getVelocities(configInfo *, struct grid *); void getVelocities_pregrid(configInfo *, struct grid *); void gridPopsInit(configInfo*, molData*, struct grid*); diff --git a/src/stateq.c b/src/stateq.c index bd64125..a669086 100644 --- a/src/stateq.c +++ b/src/stateq.c @@ -13,6 +13,92 @@ #include +/*....................................................................*/ +void +getmatrix(int id, gsl_matrix *matrix, molData *md, struct grid *gp, int ispec, gridPointData *mp){ + int ti,k,l,li,ipart,di; + struct getmatrix { + double *ctot; + gsl_matrix * colli; + } *partner; + + partner = malloc(sizeof(struct getmatrix)*md[ispec].npart); + + /* Initialize matrix with zeros */ + for(ipart=0;ipart0) partner[ipart].ctot = malloc(sizeof(double)*md[ispec].nlev); + else { + if(!silent)bail_out("Matrix initialization error in stateq"); + exit(0); + } + for(k=0;k=0) + gsl_matrix_set(matrix,k,k,gsl_matrix_get(matrix,k,k)+gp[id].dens[di]*partner[ipart].ctot[k]); + } + for(l=0;l=0) + gsl_matrix_set(matrix,k,l,gsl_matrix_get(matrix,k,l)-gp[id].dens[di]*gsl_matrix_get(partner[ipart].colli,l,k)); + } + } + } + gsl_matrix_set(matrix, md[ispec].nlev, k, 1.); + gsl_matrix_set(matrix, k, md[ispec].nlev, 0.); + } + + for(ipart=0;ipart0) partner[ipart].ctot = malloc(sizeof(double)*md[ispec].nlev); - else { - if(!silent)bail_out("Matrix initialization error in stateq"); - exit(0); - } - for(k=0;k=0) - gsl_matrix_set(matrix,k,k,gsl_matrix_get(matrix,k,k)+gp[id].dens[di]*partner[ipart].ctot[k]); - } - for(l=0;l=0) - gsl_matrix_set(matrix,k,l,gsl_matrix_get(matrix,k,l)-gp[id].dens[di]*gsl_matrix_get(partner[ipart].colli,l,k)); - } - } - } - gsl_matrix_set(matrix, md[ispec].nlev, k, 1.); - gsl_matrix_set(matrix, k, md[ispec].nlev, 0.); - } - - for(ipart=0;ipart Date: Thu, 20 Apr 2017 12:17:27 +0200 Subject: [PATCH 2/2] Moved unchanging calculation out of loop. In stateq.c the part of the matrix involving collision coefficients does not depend on the results of getjbar() and thus has been moved outside the loop now starting at line 141 (old line 45) in stateq.c. This results in a slight speedup. --- src/lime.h | 1 - src/stateq.c | 93 +++++++++++++++++++++++++++++++--------------------- 2 files changed, 55 insertions(+), 39 deletions(-) diff --git a/src/lime.h b/src/lime.h index d6e3005..4f021ea 100644 --- a/src/lime.h +++ b/src/lime.h @@ -337,7 +337,6 @@ void getclosest(double, double, double, long*, long*, double*, double*, double*) double geterf(const double, const double); void getjbar(int, molData*, struct grid*, const int, configInfo*, struct blendInfo, int, gridPointData*, double*); void getMass(configInfo*, struct grid*, const gsl_rng*); -//void getmatrix(int, gsl_matrix*, molData*, struct grid*, int, gridPointData*); void getVelocities(configInfo *, struct grid *); void getVelocities_pregrid(configInfo *, struct grid *); void gridPopsInit(configInfo*, molData*, struct grid*); diff --git a/src/stateq.c b/src/stateq.c index a669086..87033b0 100644 --- a/src/stateq.c +++ b/src/stateq.c @@ -12,44 +12,34 @@ #include #include +struct collPartMatrixType { + double *ctot; + gsl_matrix *colli; +}; /*....................................................................*/ void -getmatrix(int id, gsl_matrix *matrix, molData *md, struct grid *gp, int ispec, gridPointData *mp){ - int ti,k,l,li,ipart,di; - struct getmatrix { - double *ctot; - gsl_matrix * colli; - } *partner; +getFixedMatrix(molData *md, int ispec, struct grid *gp, int id, struct collPartMatrixType **collPartMat){ + int ipart,k,l,ti; - partner = malloc(sizeof(struct getmatrix)*md[ispec].npart); + (*collPartMat) = malloc(sizeof(**collPartMat)*md[ispec].npart); /* Initialize matrix with zeros */ for(ipart=0;ipart0) partner[ipart].ctot = malloc(sizeof(double)*md[ispec].nlev); - else { - if(!silent)bail_out("Matrix initialization error in stateq"); - exit(0); + if(md[ispec].nlev<=0){ + if(!silent) bail_out("Matrix initialization error in stateq"); + exit(1); } + + (*collPartMat)[ipart].colli = gsl_matrix_alloc(md[ispec].nlev+1,md[ispec].nlev+1); + (*collPartMat)[ipart].ctot = malloc(sizeof(double)*md[ispec].nlev); for(k=0;k=0) - gsl_matrix_set(matrix,k,k,gsl_matrix_get(matrix,k,k)+gp[id].dens[di]*partner[ipart].ctot[k]); + gsl_matrix_set(matrix,k,k,gsl_matrix_get(matrix,k,k)+gp[id].dens[di]*collPartMat[ipart].ctot[k]); } for(l=0;l=0) - gsl_matrix_set(matrix,k,l,gsl_matrix_get(matrix,k,l)-gp[id].dens[di]*gsl_matrix_get(partner[ipart].colli,l,k)); + gsl_matrix_set(matrix,k,l,gsl_matrix_get(matrix,k,l)-gp[id].dens[di]*gsl_matrix_get(collPartMat[ipart].colli,l,k)); } } } gsl_matrix_set(matrix, md[ispec].nlev, k, 1.); gsl_matrix_set(matrix, k, md[ispec].nlev, 0.); } - - for(ipart=0;ipartTOL && iter