Пример #1
0
void
test_eigen_herm_matrix(const gsl_matrix_complex * m, size_t count,
                       const char * desc)
{
  const size_t N = m->size1;
  gsl_matrix_complex * A = gsl_matrix_complex_alloc(N, N);
  gsl_vector * eval = gsl_vector_alloc(N);
  gsl_vector * evalv = gsl_vector_alloc(N);
  gsl_vector * x = gsl_vector_alloc(N);
  gsl_vector * y = gsl_vector_alloc(N);
  gsl_matrix_complex * evec = gsl_matrix_complex_alloc(N, N);
  gsl_eigen_herm_workspace * w = gsl_eigen_herm_alloc(N);
  gsl_eigen_hermv_workspace * wv = gsl_eigen_hermv_alloc(N);

  gsl_matrix_complex_memcpy(A, m);

  gsl_eigen_hermv(A, evalv, evec, wv);
  test_eigen_herm_results(m, evalv, evec, count, desc, "unsorted");

  gsl_matrix_complex_memcpy(A, m);

  gsl_eigen_herm(A, eval, w);

  /* sort eval and evalv */
  gsl_vector_memcpy(x, eval);
  gsl_vector_memcpy(y, evalv);
  gsl_sort_vector(x);
  gsl_sort_vector(y);
  test_eigenvalues_real(y, x, desc, "unsorted");

  gsl_eigen_hermv_sort(evalv, evec, GSL_EIGEN_SORT_VAL_ASC);
  test_eigen_herm_results(m, evalv, evec, count, desc, "val/asc");

  gsl_eigen_hermv_sort(evalv, evec, GSL_EIGEN_SORT_VAL_DESC);
  test_eigen_herm_results(m, evalv, evec, count, desc, "val/desc");

  gsl_eigen_hermv_sort(evalv, evec, GSL_EIGEN_SORT_ABS_ASC);
  test_eigen_herm_results(m, evalv, evec, count, desc, "abs/asc");

  gsl_eigen_hermv_sort(evalv, evec, GSL_EIGEN_SORT_ABS_DESC);
  test_eigen_herm_results(m, evalv, evec, count, desc, "abs/desc");

  gsl_matrix_complex_free(A);
  gsl_vector_free(eval);
  gsl_vector_free(evalv);
  gsl_vector_free(x);
  gsl_vector_free(y);
  gsl_matrix_complex_free(evec);
  gsl_eigen_herm_free(w);
  gsl_eigen_hermv_free(wv);
} /* test_eigen_herm_matrix() */
Пример #2
0
void
lls_complex_free(lls_complex_workspace *w)
{
  if (w->AHA)
    gsl_matrix_complex_free(w->AHA);

  if (w->AHb)
    gsl_vector_complex_free(w->AHb);

  if (w->work_A)
    gsl_matrix_complex_free(w->work_A);

  if (w->work_b)
    gsl_vector_complex_free(w->work_b);

  if (w->AF)
    gsl_matrix_complex_free(w->AF);

  if (w->S)
    gsl_vector_free(w->S);

  if (w->c)
    gsl_vector_complex_free(w->c);

  if (w->r_complex)
    gsl_vector_complex_free(w->r_complex);

  if (w->r)
    gsl_vector_free(w->r);

  if (w->eigen_p)
    gsl_eigen_herm_free(w->eigen_p);

  if (w->eval)
    gsl_vector_free(w->eval);

  if (w->w_robust)
    gsl_vector_free(w->w_robust);

  if (w->robust_workspace_p)
    gsl_multifit_robust_free(w->robust_workspace_p);

  free(w);
} /* lls_complex_free() */
Пример #3
0
PetscErrorCode cHamiltonianMatrix::measurement(){
	double	 *ALLdepart = new double[Nt];
	double	 *ALLentropy = new double[Nt];
	gsl_matrix*     density1 = gsl_matrix_alloc(L,Nt);//background fermion density
	gsl_matrix*     density2 = gsl_matrix_alloc(L,Nt);//background fermion density
	gsl_vector*     corr12 = gsl_vector_alloc(Nt);//correlation betwen fermion up and down.                                         
        // The density correlation is in fact proportional to the interacting energy.                                                     
	double var_rank;
	PetscScalar var_tmp, var_tmp2;
	gsl_complex var_tmp_gsl;
	Vec vectort;
	VecScatter ctx;
	ofstream output;
	VecScatterCreateToZero(WFt[0],&ctx,&vectort);
	if(rank==0) cout << size << endl;
	gsl_matrix_complex*	RDM = gsl_matrix_complex_alloc(dim2,dim2);
	gsl_vector *eval_RDM = gsl_vector_alloc(dim2);
	gsl_eigen_herm_workspace* w_RDM = gsl_eigen_herm_alloc(dim2);
	for (int itime = 0; itime < Nt; ++itime) {
	  if (rank==0&&itime%10==0) cout << "this is time " << itime << endl;
	  // % ## departure ##
	  var_rank = 0.0;
	  for (int ivar = rstart; ivar < rend; ++ivar) {
	    ierr = VecGetValues(WFt[itime],1,&ivar,&var_tmp);CHKERRQ(ierr);
	    var_rank += pow(gsl_vector_get(rr,ivar)*PetscAbsComplex(var_tmp),2);
	  }
	  MPI_Reduce(&var_rank, &(ALLdepart[itime]), 1, MPI_DOUBLE, MPI_SUM, 0, PETSC_COMM_WORLD);
	  ALLdepart[itime] = sqrt(ALLdepart[itime]);
	  // % ## entropy ##
	  VecScatterBegin(ctx,WFt[itime],vectort,INSERT_VALUES,SCATTER_FORWARD);
	  VecScatterEnd(ctx,WFt[itime],vectort,INSERT_VALUES,SCATTER_FORWARD);
	  if(rank==0) {
	    int ivar;double eigen_RDM;
	    gsl_matrix_complex_set_zero(RDM);
	    for (int row2 = 0; row2 < dim2; ++row2) {
	      for (int col2 = row2; col2 < dim2; ++col2) {
		var_tmp_gsl.dat[0] = 0.0; var_tmp_gsl.dat[1] = 0.0;
		for (int jjj = 0; jjj < dim; ++jjj) {
		  ivar = row2*dim+jjj;
		  ierr = VecGetValues(vectort,1,&ivar,&var_tmp);CHKERRQ(ierr);
		  ivar = col2*dim+jjj;
		  ierr = VecGetValues(vectort,1,&ivar,&var_tmp2);CHKERRQ(ierr);
		  var_tmp_gsl.dat[0] += PetscRealPart(var_tmp*PetscConj(var_tmp2));
		  var_tmp_gsl.dat[1] += PetscImaginaryPart(var_tmp*PetscConj(var_tmp2));
		}
		gsl_matrix_complex_set(RDM,row2,col2,var_tmp_gsl);
		if (col2 != row2) {
		  gsl_matrix_complex_set(RDM,col2,row2,gsl_matrix_complex_get(RDM,row2,col2));
		}
	      }
	    }
	    gsl_eigen_herm(RDM,eval_RDM,w_RDM);
	    ALLentropy[itime] = 0;
	    for (ivar = 0; ivar < dim2; ++ivar) {
	      eigen_RDM = gsl_vector_get(eval_RDM, ivar);
	      //  cout << eigen_RDM << endl;
	      ALLentropy[itime] += -eigen_RDM*log(eigen_RDM);
	    }
	  }

	  // % ## density distribution of impurity fermion
	  if(rank==0) {
            int ivar;
            for (int row2 = 0; row2 < dim2; ++row2) {
	      for (int jpar = 0; jpar < N2; ++jpar) {
		double density_tmp=0;
		for (int jjj = 0; jjj < dim; ++jjj) {
		  ivar = row2*dim+jjj;
		  ierr = VecGetValues(vectort,1,&ivar,&var_tmp);CHKERRQ(ierr);
		  density_tmp +=pow(PetscAbsComplex(var_tmp),2);
		}
		/*if (itime==0) {
		  if (rank==0) cout << "density_tmp=" << density_tmp << endl;
		  }*/
		gsl_matrix_set(density2,gsl_matrix_get(basis2,jpar,row2)-1,itime,gsl_matrix_get(density2,gsl_matrix_get(basis2,jpar,row2)-1,itime)+density_tmp);
	      }
	    }
	  }

	  /*if (rank==0) {
	    cout << "density of impurity:" << endl;
	    for (int jpar =0; jpar < L; ++jpar) {
	      cout << gsl_matrix_get(density2,jpar,itime) << "\t";
	    }
	    cout << endl;
	    }*/
	            
	  // % ## density distribution of majority fermions
	  if(rank==0) {
            int ivar;
	    for (int jjj = 0; jjj < dim; ++jjj) {
	      for (int jpar = 0; jpar < N; ++jpar) {
                double density_tmp=0;
		for (int row2 = 0; row2 < dim2; ++row2) {
                  ivar = row2*dim+jjj;
                  ierr = VecGetValues(vectort,1,&ivar,&var_tmp);CHKERRQ(ierr);
		  density_tmp +=pow(PetscAbsComplex(var_tmp),2);
		}
		gsl_matrix_set(density1,gsl_matrix_get(basis1,jpar,jjj)-1,itime,gsl_matrix_get(density1,gsl_matrix_get(basis1,jpar,jjj)-1,itime)+density_tmp);
              }
            }
          }

	  // correlation between impurity and majority fermion                                                                            
          if(rank==0) {
            int ivar;
            double corr_tmp=0;
            for (int jimp=0; jimp<dim2; ++jimp) {
              for (int jmaj=0; jmaj<dim; ++jmaj) {
                for (int jpar=0; jpar<N; ++jpar)  {
                  if (gsl_matrix_get(basis1,jpar,jmaj)==jimp+1){
                    ivar = jimp*dim+jmaj;
                    ierr = VecGetValues(vectort,1,&ivar,&var_tmp);CHKERRQ(ierr);
                    corr_tmp+=pow(PetscAbsComplex(var_tmp),2);
                  }
                }
              }
            }
            gsl_vector_set(corr12,itime,corr_tmp);
          }// end of correlation

	}

	  
	if (rank == 0) {
          char filename[50];
	  sprintf(filename,"measurement.data");
          output.open(filename);
          output.is_open();
          output.precision(16);
          for (int itime = 0; itime < Nt; ++itime) {
            if (itime==0) {
	      //              cout << "time t[1] " << '\t' << "departure[2] " << '\t' << "entropy[3]" << '\t' << "density of majority [L]" <<'\t' << "density of impurity [L]" << endl;
            }
            output << dt*itime-3 << '\t' << ALLdepart[itime] << '\t' << ALLentropy[itime] << '\t';
	    for (int jpar = 0; jpar < L; ++jpar) {
              output << gsl_matrix_get(density1,jpar,itime) << '\t';
            }
	    for (int jpar = 0; jpar < L; ++jpar) {
              output << gsl_matrix_get(density2,jpar,itime) << '\t';
            }
	    output << gsl_vector_get(corr12,itime) << '\t';
	    output << endl;
          }
          output.close();
	}
 

	//	CopyFile(source,destination,FALSE);
	
	delete[] ALLdepart;
	VecScatterDestroy(&ctx);
	VecDestroy(&vectort);
	gsl_matrix_complex_free(RDM);
	gsl_vector_free(eval_RDM);
	gsl_eigen_herm_free(w_RDM);
	gsl_matrix_free(density1);
	gsl_matrix_free(density2);
	gsl_vector_free(corr12);
	//	CopyFile(source,destination,FALSE);
	return ierr;
}