void random_config( void ) {

    int i, m, j, k, ind = 0 ;
    int tmp_dr[Dim];


    for ( k=0 ; k<nD ; k++ ) {
        for ( j=0 ; j<Dim ; j++ )
            x[ind][j] = ran2() * L[j] ;

        x[ind][2] = ran2()*(L[2]-2*wall_thick)*(1-phiHA-phiHB-phiP-phiHC-phisol) + wall_thick;

        tp[ ind ] = ( Nda > 0 ? 0 : 1 ) ;

        ind += 1 ;

        for ( m=1 ; m<Nda + Ndb ; m++ ) {

            for ( j=0 ; j<Dim ; j++ ) {
                x[ind][j] = x[ ind-1 ][ j ] + gasdev2() ;

                if ( x[ind][j] > L[j] )
                    x[ind][j] -= L[j] ;
                else if ( x[ind][j] < 0.0 )
                    x[ind][j] += L[j] ;
            }
            x[ind][2] = x[ind-1][2];

            tp[ ind ] = ( m < Nda ? 0 : 1 ) ;

            ind++ ;

        }
    }

    // Random A homopolymers //
    for ( k=0 ; k<nA ; k++ ) {
        for ( j=0 ; j<Dim ; j++ )
            x[ind][j] = ran2() * L[j] ;

        tp[ ind ] = 0 ;

        ind += 1 ;

        for ( m=1 ; m<Nha ; m++ ) {

            for ( j=0 ; j<Dim ; j++ ) {
                x[ind][j] = x[ ind-1 ][ j ] + gasdev2() ;

                if ( x[ind][j] > L[j] )
                    x[ind][j] -= L[j] ;
                else if ( x[ind][j] < 0.0 )
                    x[ind][j] += L[j] ;
            }

            tp[ ind ] = 0 ;

            ind++ ;

        }
    }

    // Random B homopolymers //
    for ( k=0 ; k<nB ; k++ ) {
        for ( j=0 ; j<Dim ; j++ )
            x[ind][j] = ran2() * L[j] ;

        tp[ ind ] = 1 ;

        ind += 1 ;

        for ( m=1 ; m<Nhb ; m++ ) {

            for ( j=0 ; j<Dim ; j++ ) {
                x[ind][j] = x[ ind-1 ][ j ] + gasdev2() ;

                if ( x[ind][j] > L[j] )
                    x[ind][j] -= L[j] ;
                else if ( x[ind][j] < 0.0 )
                    x[ind][j] += L[j] ;
            }

            tp[ ind ] = 1 ;

            ind++ ;

        }
    }

    // Random particle centers, graft locations //
    for ( k=0 ; k<nP ; k++ ) {
        int center_ind , gind,prev_graft ;
        double u[Dim] ;


        for ( j=0 ; j<Dim ; j++ )
            x[ind][j] = ran2() * L[j] ;

        x[ind][2] = ran2()*((L[2]-2*wall_thick)*(1-phiHA-phiHB-phiP-phiHC-phisol) - 2*Rp) + wall_thick+Rp;
        for(j=0 ; j<3 ; j++) {

            euler_ang[k][j] = 0;
            euler_q[k][j+1] = 0;
        }

        euler_q[k][0] = 1;
        // euler_ang[k][0] = PI/2.0;

        tp[ ind ] = 2 ;

        center_ind = ind ;

        ind += 1 ;


        for ( m=0 ; m<ng_per_partic ; m++ ) {

            // Place the grafting site //
            if(uni_sig ==1) { //fibonacci grafting sites
                fibonacci_u ( u , m );
                gind = k*ng_per_partic + m;
                if(k==0 and m==0)cout<<"fibonacci grafting is used"<<endl;
            }
            else if(uni_sig ==2) { //uniform grafting
                unif_sig ( u , m );
                gind = k*ng_per_partic + m;
                if(k==0  and m==0)cout<<"uniform grafting is used"<<endl;
            }
            else { //random grafting sites
                random_u( u ) ;
                gind = k*ng_per_partic + m;
                if(k==0 and m==0)cout<<"random grafting is used"<<endl;
            }


            for ( j=0 ; j<Dim ; j++ ) {
                x[ind][j] = x[center_ind][j] + Rp * u[j] ;
                grf_bf_x[gind][j] = Rp * u[j];

                if ( x[ind][j] > L[j] )
                    x[ind][j] -= L[j] ;
                else if ( x[ind][j] < 0.0 )
                    x[ind][j] += L[j] ;
            }
            //  cout<<" ng "<<m<<" "<<x[ind][0]<<" "<<x[ind][1]<<" "<<x[ind][2]<<endl;

            tp[ ind ] = -1 ;

            if ( m > 0 ) {
                double mdr2, dr[Dim], mdr ;
                mdr2 = pbc_mdr2( x[ prev_graft ] , x[ ind ] , dr ) ;
                mdr = sqrt( mdr2 ) ;

                graft_req[k][m-1] = mdr ;
            }

            prev_graft = ind ;

            ind++ ;

            // Place the chain grafted to this site
            for ( i=0 ; i<Ng ; i++ ) {

                for ( j=0 ; j<Dim ; j++ ) {
                    x[ind][j] = x[ind-1][j] + 0.5 * u[j] ;

                    if ( x[ind][j] > L[j] )
                        x[ind][j] -= L[j] ;
                    else if ( x[ind][j] < 0.0 )
                        x[ind][j] += L[j] ;
                }

                tp[ ind ] = 0 ;
                ind++ ;
            }
        }
    }

    // Random C homopolymers //
    for ( k=0 ; k<nC ; k++ ) {
        for ( j=0 ; j<Dim ; j++ )
            x[ind][j] = ran2() * L[j] ;

        x[ind][2] = -ran2()*(L[2]-2*wall_thick)*(phiHC+phisol) +L[2] -  wall_thick;

        tp[ ind ] = 3 ;

        ind += 1 ;

        for ( m=1 ; m<Nhc ; m++ ) {

            for ( j=0 ; j<Dim ; j++ ) {
                x[ind][j] = x[ ind-1 ][ j ] + gasdev2() ;

                if ( x[ind][j] > L[j] )
                    x[ind][j] -= L[j] ;
                else if ( x[ind][j] < 0.0 )
                    x[ind][j] += L[j] ;
            }

            x[ind][2] = x[ind-1][2];

            tp[ ind ] = 3 ;

            ind++ ;

        }
    }

    ind = nD*(Nda+Ndb) + nA*Nha +nB*Nhb + nP * ( 1 + ng_per_partic * ( Ng + 1 ) ) + max_nC*Nhc ;
// Random sol  //
    for ( k=0 ; k<nsol ; k++ ) {
        for ( j=0 ; j<Dim ; j++ )
            x[ind][j] = ran2() * L[j] ;

        x[ind][2] = -ran2()*(L[2]-2*wall_thick)*(phiHC+phisol) +L[2] -  wall_thick;

        tp[ ind ] = 4 ;

        ind += 1 ;


    }






    // Assign the labels //
    for ( i=0 ; i<nstot ; i++ ) {
        if ( tp[i] == 0 )
            xc[i] = "H" ;
        else if ( tp[i] == 1 )
            xc[i] = "He" ;
        else if ( tp[i] == 2 )
            xc[i] = "O" ;
        else if ( tp[i] == 3 )
            xc[i] = "S" ;
        else if ( tp[i] == 4 )
            xc[i] = "N" ;
        else if ( tp[i] == 5 )
            xc[i] = "Br" ;
        else if ( tp[i] == 6 )
            xc[i] = "C" ;
        else if ( tp[i] == 7 )
            xc[i] = "Na" ;
        else if ( tp[i] == 8 )
            xc[i] = "P" ;
        else if ( tp[i] == 9 )
            xc[i] = "Ca" ;
        else if ( tp[i] == -1 )
            xc[i] = "X" ;
    }

    calc_A();

    printf("config generated!\n") ;
    fflush( stdout ) ;
}
void update_euler(){

	int i, j,k, cent_ind,gind, ind;
	int sites_per_gnp = 1 + ng_per_partic * ( 1 + Ng );
	double **tmp_q,lamb;
    
        tmp_q = (double**) calloc(nP, sizeof(double*));
	for(i=0 ; i<nP ; i++)
		tmp_q[i] = (double*) calloc(4, sizeof(double));

     rot_noise(q_noise);

#pragma omp parallel for private(j,lamb)
	for(i=0 ; i<nP ; i++){
	  for(j=0;j<4;j++){

           	//get the predicted quaterns, q~//	
		tmp_q[i][j] =  euler_q[i][j] + Diff_rot*delt*trq[i][j] + q_noise[i][j];	
		
	      
	  }//j
	  lamb = crct_q(euler_q[i],tmp_q[i]); // Lagrange multiplier , lambda
	 
	  
	  for(j=0;j<4;j++){

	  	euler_q[i][j] = tmp_q[i][j] + lamb*euler_q[i][j]; //unit length correction 
	  }//j

	}//i=nP

       calc_A();


          

#pragma omp parallel for private(k,j,cent_ind,ind,gind)
       for(i = 0 ; i<nP ;i++){
           cent_ind = nD * ( Nda + Ndb ) + nA * Nha + nB * Nhb + sites_per_gnp * i ;
	    for(j = 0; j<ng_per_partic; j++){
		ind = cent_ind + j * ( Ng + 1 ) + 1 ; 
		gind = ng_per_partic*i + j;
		matrix_trs_vect(euler_A[i],grf_bf_x[gind],x[ind]);

       		if(tp[ind] != -1) { cout<<"wrong em update !!"<<endl; exit(1);} 
		
		for(k=0 ;k<Dim; k++){
			x[ind][k] += x[cent_ind][k] ;
		      if ( x[ind][k] > L[k] )
		            x[ind][k] -= L[k] ;
			else if ( x[ind][k] < 0.0 )
			    x[ind][k] += L[k] ;


		}
	   }//ng_per_partic


       }//nP

       for(i=0 ;i<nP;i++)
       	 free(tmp_q[i]);
       free(tmp_q);

}
void read_quaternions(FILE *ip) {

    int center_ind, gind , i, m, j, k, di , ind ;
    int sites_per_gnp = 1 + ng_per_partic * ( 1 + Ng );
    double dr[Dim],mdr;
    char tt[80] ;

    fgets( tt , 80 , ip ) ;
    fscanf( ip , "%d" , &di ) ;
    fgets( tt, 80 , ip ) ;
    if ( di != nP)
        die("Number of nP in input_q.gro does not match!\n");


    for(i=0; i<nP ; i++) {

        fscanf( ip , "%d" , &ind ) ;

        for ( j=0 ; j<4 ; j++ ) {
            fscanf( ip , "%lf" , &euler_q[ind][j] ) ;
        }


        fgets( tt, 80 , ip ) ;


        //cout<<"i "<<i<<endl;

    }

    //iniitialize A and B matrixes
    calc_A();

    //initialize x_grft at bf frames
    for(i=0; i<nP; i++) {

        center_ind = nD * ( Nda + Ndb ) + nA * Nha + nB * Nhb + sites_per_gnp * i ;

        for(m=0; m<ng_per_partic ; m++) {

            ind = center_ind + m * ( Ng + 1 ) + 1 ;

            gind = ng_per_partic*i + m;

            mdr = pbc_mdr2(x[ind],x[center_ind],dr);

            for(j=0; j<Dim; j++) {

                grf_bf_x[gind][j] = 0.0;

                for(k=0 ; k<Dim ; k++) {
                    grf_bf_x[gind][j] += euler_A[i][j][k]*dr[k];

                }//k

            }//j

        }//ng_per_partic

    }//nP

}
Exemplo n.º 4
0
Arquivo: xrr.c Projeto: FHe/tdl
/******************************************************************************
* xref()
* Master function for reflectivity and reflection xsw calculations
*
* Parameters
* ---------
*
* Returns
* -------
*
* Notes
* -----
* take all raw arrays, use static copies of structs and assign pointers
* then pass along.  This is the main public function
*
* Note theta array and results
* arrays are dim nthet
* int     nthet;
* double *theta_arr;
* double *R; 
* double *Y;
******************************************************************************/
int xref( int nlayer, int nelem, int nthet, double *calc_params, 
          double *d, double *rho, double *sigma, double **comp,
          double *elem_z, double *fp, double *fpp, double *amu, double *mu_at,
          double *theta_arr, double *R, double *Y, 
          double *del, double *bet, double *amu_t, double *mu_t,
          double *Re_X, double *Im_X, double *Re_Ai, double *Im_Ai, 
          double *Re_Ar, double *Im_Ar, double *Re_g, double *Im_g)
{

    int         j, ret, fy_idx;
    double      energy, ref_scale;
    double      theta, k, lam, y, wconv;
    ref_model   ref;
    layer_calc  lay_c;    
    angle_calc  ang_c;
    gsl_complex Xtop;

    //calc k
    energy = calc_params[0];
    lam = 12398.0 / (energy);
    k = 2*M_PI/lam;

    // get fy_idx 
    fy_idx = (int) calc_params[6];

    //ref scale factor
    ref_scale = calc_params[13];

    //fill in the data structure
    //ref_model
    ref.calc_params = calc_params;
    ref.k           = k;
    ref.nlayer      = nlayer;
    ref.d           = d;
    ref.rho         = rho;
    ref.comp        = comp;    
    ref.sigma       = sigma;
    ref.nelem       = nelem;
    ref.elem_z      = elem_z;    
    ref.fp          = fp;
    ref.fpp         = fpp;
    ref.amu         = amu;
    ref.mu_at       = mu_at;

    //layer_calc
    lay_c.nlayer    = nlayer;
    lay_c.del       = del;
    lay_c.bet       = bet;
    lay_c.mu_t      = mu_t;
    lay_c.amu_t     = amu_t;

    //angle_calc
    ang_c.nlayer    = nlayer;
    ang_c.Re_X      = Re_X;
    ang_c.Im_X      = Im_X;
    ang_c.Re_Ai     = Re_Ai;
    ang_c.Im_Ai     = Im_Ai;
    ang_c.Re_Ar     = Re_Ar;
    ang_c.Im_Ar     = Im_Ar;
    ang_c.Re_g      = Re_g;
    ang_c.Im_g      = Im_g;

    // get layer_calc data
    // this stuff doesnt depend on
    // incidence angle
    ret = calc_del_bet_mu(&ref, &lay_c);
    if (ret == FAILURE) return(FAILURE);

    // loop over theta
    for (j=0;j<nthet;j++){

        theta = theta_arr[j];
   
        // calc X's and compute reflectivity     
        ret = calc_X(theta, &ref, &ang_c, &lay_c);
        if (ret == FAILURE) return(FAILURE);

        GSL_SET_COMPLEX(&Xtop,ang_c.Re_X[nlayer-1],ang_c.Im_X[nlayer-1]);  
        R[j] = gsl_complex_abs2(Xtop);

        if (calc_params[5] > 0.0){
            R[j] = R[j]*calc_spilloff(theta,calc_params[3],calc_params[2]);
        }
        if (ref_scale > 0.0){
            R[j] = R[j]*ref_scale;
        }

        if (fy_idx >= 0) {
            // calculate A's
            ret = calc_A(theta, &ref, &ang_c, &lay_c);
            if (ret == FAILURE) return(FAILURE);
    
            // calc FY
            y = calc_FY(theta, &ref, &ang_c, &lay_c);

            // mult by area
            if (calc_params[5] > 0.0){
                y = y*calc_area(theta,calc_params[3],calc_params[4],calc_params[2]);
                y = y*calc_spilloff(theta,calc_params[3],calc_params[2]);
            }
            Y[j] = y; 
        }
    }

    // convolve and normalize
    wconv = calc_params[1];

    // Note convolve is not padded!
    if (wconv > 0.0) {
        convolve(theta_arr, R, nthet, wconv);
    } 

    if (fy_idx >= 0){
        //convolve yield
        if (wconv > 0.0){
            convolve(theta_arr, Y, nthet, wconv);
        }
        // normalize yield
        norm_array(theta_arr, Y, nthet, calc_params[9], 1.0);
    }

    return(SUCCESS);
}