int main(int argc, char *argv[]) { PetscErrorCode ierr; params params; Mat H; SlepcInitialize(&argc,&argv,(char*)0,help); ierr = PetscPrintf(PETSC_COMM_WORLD,"--------------------------------------------------------------------------------------\n");CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," _______ __ __ _______ _______ ______ _______ \n");CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," |__ __| \\/ |/ ____\\ \\ / /_ _| ____|__ __| \n");CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," | | | \\ / | (___ \\ \\ /\\ / / | | | |__ | | \n");CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," | | | |\\/| |\\___ \\ \\ \\/ \\/ / | | | __| | | \n");CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," | | | | | |____) | \\ /\\ / _| |_| | | | \n");CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," |_| |_| |_|_____/ \\/ \\/ |_____|_| |_| \n");CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," True Muonium Solver with Iterative Front-Form Techniques \n");CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," \n");CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"--------------------------------------------------------------------------------------\n");CHKERRQ(ierr); read_input(ierr,argv,¶ms); print_input(ierr,¶ms); if(check_file(params.hfile)) { PetscViewer viewer_H; PetscViewerBinaryOpen(PETSC_COMM_WORLD,params.hfile.c_str(),FILE_MODE_READ,&viewer_H); MatCreate(PETSC_COMM_WORLD,&H); MatSetFromOptions(H); MatLoad(H,viewer_H); PetscViewerDestroy(&viewer_H); }else { discretize(ierr,¶ms); // ierr = VecView(params.mu,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); // ierr = VecView(params.theta,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); write_output(ierr,¶ms); coulomb_trick(ierr,¶ms); // ierr = VecView(params.CT,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ct_discrete(ierr,¶ms); // ierr = VecView(params.CT,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); hamiltonian(ierr,¶ms,H); // ierr = PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_DENSE);//DENSE<-->COMMON // MatView(H,PETSC_VIEWER_STDOUT_WORLD); } cleanup(ierr,¶ms); eigensolver(ierr,¶ms,H,argc,argv); ierr = MatDestroy(&H);CHKERRQ(ierr); ierr = SlepcFinalize(); return 0; }
TEST_F(McmcHmcIntegratorsExplLeapfrogF, end_update_p) { // setup z stan::mcmc::unit_e_point z(1); z.V = 1.39887860643153; z.q(0) = 1.67264975797776; z.p(0) = -1.68611660683688; z.g(0) = 1.67264975797776; EXPECT_NEAR(z.V, 1.39887860643153, 1e-15); EXPECT_NEAR(z.q(0), 1.67264975797776, 1e-15); EXPECT_NEAR(z.p(0), -1.68611660683688, 1e-15); EXPECT_NEAR(z.g(0), 1.67264975797776, 1e-15); // setup hamiltonian stan::mcmc::unit_e_metric<command_model_namespace::command_model, rng_t> hamiltonian(*model); // setup epsilon double epsilon = 0.1; unit_e_integrator.end_update_p(z, hamiltonian, 0.5 * epsilon, logger); EXPECT_NEAR(z.V, 1.39887860643153, 5e-14); EXPECT_NEAR(z.q(0), 1.67264975797776, 5e-14); EXPECT_NEAR(z.p(0), -1.76974909473577, 5e-14); EXPECT_NEAR(z.g(0), 1.67264975797776, 5e-14); EXPECT_EQ("", debug.str()); EXPECT_EQ("", info.str()); EXPECT_EQ("", warn.str()); EXPECT_EQ("", error.str()); EXPECT_EQ("", fatal.str()); }
TEST_F(McmcHmcIntegratorsExplLeapfrogF, update_q) { // setup z stan::mcmc::unit_e_point z(1); z.V = 1.99974742955684; z.q(0) = 1.99987371079118; z.p(0) = -1.68611660683688; z.g(0) = 1.99987371079118; EXPECT_NEAR(z.V, 1.99974742955684, 1e-15); EXPECT_NEAR(z.q(0), 1.99987371079118, 1e-15); EXPECT_NEAR(z.p(0), -1.68611660683688, 1e-15); EXPECT_NEAR(z.g(0), 1.99987371079118, 1e-15); // setup hamiltonian stan::mcmc::unit_e_metric<command_namespace::command, rng_t> hamiltonian(*model, &std::cout); // setup epsilon double epsilon = 0.1; unit_e_integrator.update_q(z, hamiltonian, epsilon); EXPECT_NEAR(z.V, 1.99974742955684, 5e-14); EXPECT_NEAR(z.q(0), 1.83126205010749, 5e-14); EXPECT_NEAR(z.p(0), -1.68611660683688, 5e-14); EXPECT_NEAR(z.g(0), 1.99987371079118, 5e-14); }
TEST_F(McmcHmcIntegratorsExplLeapfrogF, evolve_3) { // setup z stan::mcmc::unit_e_point z(1); z.V = 1.99974742955684; z.q(0) = 1.99987371079118; z.p(0) = 0.531143888645192; z.g(0) = 1.99987371079118; EXPECT_NEAR(z.V, 1.99974742955684, 1e-15); EXPECT_NEAR(z.q(0), 1.99987371079118, 1e-15); EXPECT_NEAR(z.p(0), 0.531143888645192, 1e-15); EXPECT_NEAR(z.g(0), 1.99987371079118, 1e-15); // setup hamiltonian stan::mcmc::unit_e_metric<command_model_namespace::command_model, rng_t> hamiltonian(*model); // setup epsilon double epsilon = 0.2; unit_e_integrator.evolve(z, hamiltonian, epsilon, logger); EXPECT_NEAR(z.V, 2.13439496506688, 5e-14); EXPECT_NEAR(z.q(0), 2.06610501430439, 5e-14); EXPECT_NEAR(z.p(0), 0.124546016135635, 5e-14); EXPECT_NEAR(z.g(0), 2.06610501430439, 5e-14); EXPECT_EQ("", debug.str()); EXPECT_EQ("", info.str()); EXPECT_EQ("", warn.str()); EXPECT_EQ("", error.str()); EXPECT_EQ("", fatal.str()); }
TEST_F(McmcHmcIntegratorsExplLeapfrogF, evolve_6) { // setup z stan::mcmc::unit_e_point z(1); z.V = 1.99974742955684; z.q(0) = 1.99987371079118; z.p(0) = 0.0942249427134016; z.g(0) = 1.99987371079118; EXPECT_NEAR(z.V, 1.99974742955684, 1e-15); EXPECT_NEAR(z.q(0), 1.99987371079118, 1e-15); EXPECT_NEAR(z.p(0), 0.0942249427134016, 1e-15); EXPECT_NEAR(z.g(0), 1.99987371079118, 1e-15); // setup hamiltonian stan::mcmc::unit_e_metric<command_namespace::command, rng_t> hamiltonian(*model, &std::cout); // setup epsilon double epsilon = 1.6; unit_e_integrator.evolve(z, hamiltonian, epsilon); EXPECT_NEAR(z.V, 0.0837242558054816, 5e-14); EXPECT_NEAR(z.q(0), -0.409204730680088, 5e-14); EXPECT_NEAR(z.p(0), -1.17831024137547, 5e-14); EXPECT_NEAR(z.g(0), -0.409204730680088, 5e-14); }
TEST_F(McmcHmcIntegratorsExplLeapfrogF, evolve_9) { // setup z stan::mcmc::diag_e_point z(1); z.V = 0.807684865121721; z.q(0) = 1.27097196280777; z.p(0) = -0.159996782671291; z.g(0) = 1.27097196280777; z.mInv(0) = 0.733184698671436; EXPECT_NEAR(z.V, 0.807684865121721, 1e-15); EXPECT_NEAR(z.q(0), 1.27097196280777, 1e-15); EXPECT_NEAR(z.p(0), -0.159996782671291, 1e-15); EXPECT_NEAR(z.g(0), 1.27097196280777, 1e-15); // setup hamiltonian stan::mcmc::diag_e_metric<command_namespace::command, rng_t> hamiltonian(*model, &std::cout); // setup epsilon double epsilon = 2.40769920051673; diag_e_integrator.evolve(z, hamiltonian, epsilon); EXPECT_NEAR(z.V, 1.46626604258356, 5e-14); EXPECT_NEAR(z.q(0), -1.71246374711032, 5e-14); EXPECT_NEAR(z.p(0), 0.371492925378682, 5e-14); EXPECT_NEAR(z.g(0), -1.71246374711032, 5e-14); }
TEST_F(McmcHmcIntegratorsExplLeapfrogF, evolve_7) { // setup z stan::mcmc::unit_e_point z(1); z.V = 1.99974742955684; z.q(0) = 1.99987371079118; z.p(0) = 1.01936184962275; z.g(0) = 1.99987371079118; EXPECT_NEAR(z.V, 1.99974742955684, 1e-15); EXPECT_NEAR(z.q(0), 1.99987371079118, 1e-15); EXPECT_NEAR(z.p(0), 1.01936184962275, 1e-15); EXPECT_NEAR(z.g(0), 1.99987371079118, 1e-15); // setup hamiltonian stan::mcmc::unit_e_metric<command_namespace::command, rng_t> hamiltonian(*model, &std::cout); // setup epsilon double epsilon = 3.2; unit_e_integrator.evolve(z, hamiltonian, epsilon); EXPECT_NEAR(z.V, 12.3878614837537, 5e-13); EXPECT_NEAR(z.q(0), -4.97752176966686, 5e-14); EXPECT_NEAR(z.p(0), 5.78359874382383, 5e-14); EXPECT_NEAR(z.g(0), -4.97752176966686, 5e-14); }
TEST_F(McmcHmcIntegratorsExplLeapfrogF, evolve_4) { // setup z stan::mcmc::unit_e_point z(1); z.V = 1.99974742955684; z.q(0) = 1.99987371079118; z.p(0) = -1.01150787313287; z.g(0) = 1.99987371079118; EXPECT_NEAR(z.V, 1.99974742955684, 1e-15); EXPECT_NEAR(z.q(0), 1.99987371079118, 1e-15); EXPECT_NEAR(z.p(0), -1.01150787313287, 1e-15); EXPECT_NEAR(z.g(0), 1.99987371079118, 1e-15); // setup hamiltonian stan::mcmc::unit_e_metric<command_namespace::command, rng_t> hamiltonian(*model, &std::cout); // setup epsilon double epsilon = 0.4; unit_e_integrator.evolve(z, hamiltonian, epsilon); EXPECT_NEAR(z.V, 1.03001529319458, 5e-14); EXPECT_NEAR(z.q(0), 1.43528066467474, 5e-14); EXPECT_NEAR(z.p(0), -1.69853874822605, 5e-14); EXPECT_NEAR(z.g(0), 1.43528066467474, 5e-14); }
TEST_F(McmcHmcIntegratorsExplLeapfrogF, evolve_5) { // setup z stan::mcmc::unit_e_point z(1); z.V = 1.99974742955684; z.q(0) = 1.99987371079118; z.p(0) = -0.141638464197442; z.g(0) = 1.99987371079118; EXPECT_NEAR(z.V, 1.99974742955684, 1e-15); EXPECT_NEAR(z.q(0), 1.99987371079118, 1e-15); EXPECT_NEAR(z.p(0), -0.141638464197442, 1e-15); EXPECT_NEAR(z.g(0), 1.99987371079118, 1e-15); // setup hamiltonian stan::mcmc::unit_e_metric<command_namespace::command, rng_t> hamiltonian(*model, &std::cout); // setup epsilon double epsilon = 0.8; unit_e_integrator.evolve(z, hamiltonian, epsilon); EXPECT_NEAR(z.V, 0.777009958583946, 5e-14); EXPECT_NEAR(z.q(0), 1.24660335198005, 5e-14); EXPECT_NEAR(z.p(0), -1.44022928930593, 5e-14); EXPECT_NEAR(z.g(0), 1.24660335198005, 5e-14); }
int main(int argc, char *argv[]) { //typedef rokko::matrix_row_major matrix_major; typedef rokko::matrix_col_major matrix_major; if (argc <= 1) { std::cerr << "error: " << argv[0] << " solver_name alps_parameter_file_name" << std::endl; exit(34); } rokko::solver solver(solver_name); solver.initialize(argc, argv); std::string solver_name(argv[1]); std::ifstream ifs(argv[2]); alps::Parameters params(ifs); barista::Hamiltonian<> hamiltonian(params); int dim = hamiltonian.dimension(); std::cout << "dim=" << dim << std::endl; rokko::distributed_matrix<matrix_major> mat(dim, dim, g, solver); hamiltonian.fill(mat); mat.print(); rokko::localized_matrix<matrix_major> lmat1(dim, dim); rokko::gather(mat, lmat1, root); rokko::localized_matrix<rokko::matrix_col_major> lmat2(dim, dim); hamiltonian.fill<double>(lmat2); std::cout << "lmat1=" << std::endl << lmat1 << std::endl; std::cout << "lmat2=" << std::endl << lmat2 << std::endl; return 0; }
TEST_F(McmcHmcIntegratorsExplLeapfrogF, begin_update_p) { // setup z stan::mcmc::unit_e_point z(1); z.V = 1.99974742955684; z.q(0) = 1.99987371079118; z.p(0) = -1.58612292129732; z.g(0) = 1.99987371079118; EXPECT_NEAR(z.V, 1.99974742955684, 1e-15); EXPECT_NEAR(z.q(0), 1.99987371079118, 1e-15); EXPECT_NEAR(z.p(0), -1.58612292129732, 1e-15); EXPECT_NEAR(z.g(0), 1.99987371079118, 1e-15); // setup hamiltonian stan::mcmc::unit_e_metric<command_model_namespace::command_model, rng_t> hamiltonian(*model, &output); // setup epsilon double epsilon = 0.1; unit_e_integrator.begin_update_p(z, hamiltonian, 0.5 * epsilon); EXPECT_NEAR(z.V, 1.99974742955684, 5e-14); EXPECT_NEAR(z.q(0), 1.99987371079118, 5e-14); EXPECT_NEAR(z.p(0), -1.68611660683688, 5e-14); EXPECT_NEAR(z.g(0), 1.99987371079118, 5e-14); EXPECT_EQ("", output.str()); }
TEST_F(McmcHmcIntegratorsExplLeapfrogF, evolve_1) { // setup z stan::mcmc::unit_e_point z(1); z.V = 1.99974742955684; z.q(0) = 1.99987371079118; z.p(0) = -1.58612292129732; z.g(0) = 1.99987371079118; EXPECT_NEAR(z.V, 1.99974742955684, 1e-15); EXPECT_NEAR(z.q(0), 1.99987371079118, 1e-15); EXPECT_NEAR(z.p(0), -1.58612292129732, 1e-15); EXPECT_NEAR(z.g(0), 1.99987371079118, 1e-15); // setup hamiltonian stan::mcmc::unit_e_metric<command_model_namespace::command_model, rng_t> hamiltonian(*model); // setup epsilon double epsilon = 0.1; unit_e_integrator.evolve(z, hamiltonian, epsilon, logger); EXPECT_NEAR(z.V, 1.67676034808195, 5e-14); EXPECT_NEAR(z.q(0), 1.83126205010749, 5e-14); EXPECT_NEAR(z.p(0), -1.77767970934226, 5e-14); EXPECT_NEAR(z.g(0), 1.83126205010749, 5e-14); EXPECT_EQ("", debug.str()); EXPECT_EQ("", info.str()); EXPECT_EQ("", warn.str()); EXPECT_EQ("", error.str()); EXPECT_EQ("", fatal.str()); }
TEST_F(McmcHmcIntegratorsExplLeapfrogF, evolve_8) { // setup z stan::mcmc::unit_e_point z(1); z.V = 1.99974742955684; z.q(0) = 1.99987371079118; z.p(0) = -2.73131279771964; z.g(0) = 1.99987371079118; EXPECT_NEAR(z.V, 1.99974742955684, 1e-15); EXPECT_NEAR(z.q(0), 1.99987371079118, 1e-15); EXPECT_NEAR(z.p(0), -2.73131279771964, 1e-15); EXPECT_NEAR(z.g(0), 1.99987371079118, 1e-15); // setup hamiltonian stan::mcmc::unit_e_metric<command_namespace::command, rng_t> hamiltonian(*model, &std::cout); // setup epsilon double epsilon = -1; unit_e_integrator.evolve(z, hamiltonian, epsilon); EXPECT_NEAR(z.V, 6.96111198693627, 5e-14); EXPECT_NEAR(z.q(0), 3.73124965311523, 5e-14); EXPECT_NEAR(z.p(0), 0.134248884233563, 5e-14); EXPECT_NEAR(z.g(0), 3.73124965311523, 5e-14); }
int main () { create_graph(5); print_graph(); path = malloc(n * sizeof (int)); set = calloc(n, sizeof (int)); hamiltonian(0); return 0; }
int printLattice(int lattice[N][N]) { for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { //std::cout << ((lattice[i][j] == 1) ? "+" : "-") << " "; std::cout << ((lattice[i][j] == 1) ? "\033[0;35mU\033[0m" : "\033[0;34mD\033[0m") << " "; //std::cout << ((lattice[i][j] == 1) ? "+" : "-") << " "; } std::cout << std::endl; } std::cout << "Energy: " << hamiltonian(lattice) << std::endl;; }
int flipSpinR(int lattice[N][N], double *T, int k) { int i=(N*1.0*random()/(RAND_MAX+1.)); int j=(N*1.0*random()/(RAND_MAX+1.)); double oldHam=hamiltonian(lattice); double newHam, deltaH; deltaH=oldHam; lattice[i][j]*=-1; newHam=hamiltonian(lattice); deltaH=newHam-deltaH; //std::cout << "Flipped, Delta H: " << deltaH << std::endl; double p=std::min(1.,exp(-deltaH / (*T))); //std::cout << "p=" << p << std::endl; if ((1.0*random()/(RAND_MAX+1.)) <= p) { //std::cout << "Taken." << std::endl; (*T)=1/log(k); deltaH=newHam; } else { //std::cout << "Not taken." << std::endl; lattice[i][j]*=-1; deltaH=oldHam; } }
void theory_t::get_force(vector<matr_t> &F,conf_t &conf,double t) { //used for the mass*mass const double mass2=mass*mass; //compute force for(auto &Fi : F) Fi.setZero(); //first piece for X and Y for(int i=0;i<nX;i++) F[i]=-mass2*conf.X[i]-3.0*I*mass*comm(conf.X[(i+1)%nX],conf.X[(i+2)%nX]); for(int a=nX;a<glb_N;a++) F[a]=-0.25*mass2*conf.X[a]; //second piece, same for all of them for(int i=0;i<glb_N;i++) for(int a=0;a<glb_N;a++) F[i]+=comm(comm(conf.X[a],conf.X[i]),conf.X[a]); //#define DEBUG #ifdef DEBUG int icheck=1; double e_bef=hamiltonian(); double eps=1e-6; X[icheck](1,0)+=eps/2; X[icheck](0,1)+=eps/2; double e_aft=hamiltonian(); double f_num=-(e_aft-e_bef)/eps; double f_exa=(F[icheck](1,0)+F[icheck](0,1)).real()/2.0; cout<<"Exa: "<<f_exa<<", num: "<<f_num<<endl; X[icheck](1,0)-=eps; X[icheck](0,1)-=eps; #endif //print the force //double n=0; //for(auto &Fi : F) n+=Fi.norm(); //cout<<n<<endl; }
int main() { printf("\n"); result_array[1] = 1; // start from first node hamiltonian( 1 ); // start to find all Hamiltonian cycles recursively if( hamiltonian_cycle_amount == 0 ) // no Hamiltonian cycles found printf("\nThere is no Hamiltonian cycles in this graph\n\n"); printf("\n"); system("PAUSE"); return 0; }
int main(int argc, char *argv[]) { //typedef rokko::matrix_row_major matrix_major; typedef rokko::matrix_col_major matrix_major; MPI_Init(&argc, &argv); if (argc <= 2) { std::cerr << "error: " << argv[0] << " solver_name alps_parameter_file_name" << std::endl; exit(34); } std::string solver_name(argv[1]); std::ifstream ifs(argv[2]); alps::Parameters params(ifs); barista::Hamiltonian<> hamiltonian(params); int dim = hamiltonian.dimension(); std::cout << "dim=" << dim << std::endl; rokko::parallel_dense_solver solver(solver_name); solver.initialize(argc, argv); MPI_Comm comm = MPI_COMM_WORLD; rokko::grid g(comm, rokko::grid_col_major); int myrank = g.get_myrank(); const int root = 0; rokko::distributed_matrix<matrix_major> mat(dim, dim, g, solver); hamiltonian.fill(mat); mat.print(); rokko::localized_matrix<rokko::matrix_col_major> lmat1; //(dim, dim); rokko::gather(mat, lmat1, root); if (myrank == root) { rokko::localized_matrix<matrix_major> lmat2(dim, dim); hamiltonian.fill(lmat2); if (lmat1 != lmat2) { MPI_Abort(MPI_COMM_WORLD, 22); } std::cout << "OK" << std::endl; //std::cout << "lmat1=" << std::endl << lmat1 << std::endl; //std::cout << "lmat2=" << std::endl << lmat2 << std::endl; } solver.finalize(); MPI_Finalize(); return 0; }
/* finds hamiltonian paths in the graph if there are any */ void hamiltonian (int i) { int j; if (i == n) { printf("hamiltonian path: "); for (j = 0; j < n; j++) { printf("%3d", path[j]); } printf("\n"); return; } for (j = 0; j < n; j++) { if (!set[j]) { if (!i || graph[path[i - 1]][j]) { set[j] = 1; path[i] = j; hamiltonian(i + 1); set[j] = 0; } } } }
void hamiltonian ( int node_index ) { int j, k; if( promising(node_index) ) // current node is valid in a Hamiltonian cycles { if( node_index == (node_amount) ) // at latest node { hamiltonian_cycle_amount = 1; // increase hamiltonian_cycle_amount printf(" v%d", result_array[1]); // print out the result for( k = 2;k <= node_amount;k ++ ) printf(" ¡÷ v%d", result_array[k]); printf("\n\n"); } else // not latest node { for( j = 2;j <= node_amount;j ++ ) // recursively scan from second node to latest node { result_array[node_index+1] = j; // store result hamiltonian( node_index+1 ); // call function recursively } } } }
int main(int argc, char *argv[]) { MPI_Init(&argc, &argv); //typedef rokko::matrix_row_major matrix_major; typedef rokko::matrix_col_major matrix_major; if (argc <= 2) { std::cerr << "error: " << argv[0] << " solver_name alps_parameter_file_name" << std::endl; MPI_Abort(MPI_COMM_WORLD, 34); } std::string solver_name(argv[1]); std::ifstream ifs(argv[2]); alps::Parameters params(ifs); std::ostream& out = std::cout; barista::Hamiltonian<> hamiltonian(params); int dim = hamiltonian.dimension(); rokko::parallel_dense_solver solver(solver_name); solver.initialize(argc, argv); MPI_Comm comm = MPI_COMM_WORLD; rokko::grid g(comm, rokko::grid_col_major); int myrank = g.get_myrank(); const int root = 0; rokko::distributed_matrix<matrix_major> mat(dim, dim, g, solver); hamiltonian.fill(mat); mat.print(); rokko::localized_matrix<matrix_major> lmat(dim, dim); rokko::gather(mat, lmat, root); if (myrank == root) std::cout << "lmat:" << std::endl << lmat << std::endl; rokko::localized_vector w(dim); rokko::distributed_matrix<matrix_major> Z(dim, dim, g, solver); try { solver.diagonalize(mat, w, Z); } catch (const char *e) { std::cout << "Exception : " << e << std::endl; MPI_Abort(MPI_COMM_WORLD, 22); } // gather of eigenvectors rokko::localized_matrix<matrix_major> eigvec_global; rokko::localized_matrix<matrix_major> eigvec_sorted(dim, dim); rokko::localized_vector eigval_sorted(dim); rokko::gather(Z, eigvec_global, root); Z.print(); if (myrank == root) { std::cout << "eigvec:" << std::endl << eigvec_global << std::endl; } std::cout.precision(20); /* std::cout << "w=" << std::endl; for (int i=0; i<dim; ++i) { std::cout << w[i] << " "; } std::cout << std::endl; */ if (myrank == root) { rokko::sort_eigenpairs(w, eigvec_global, eigval_sorted, eigvec_sorted); std::cout << "Computed Eigenvalues= " << eigval_sorted.transpose() << std::endl; std::cout.precision(3); std::cout << "Check the orthogonality of Eigenvectors:" << std::endl << eigvec_sorted * eigvec_sorted.transpose() << std::endl; // Is it equal to indentity matrix? //<< eigvec_global.transpose() * eigvec_global << std::endl; // Is it equal to indentity matrix? std::cout << "residual := A x - lambda x = " << std::endl << lmat * eigvec_sorted.col(1) - eigval_sorted(1) * eigvec_sorted.col(1) << std::endl; std::cout << "Are all the following values equal to some eigenvalue = " << std::endl << (lmat * eigvec_sorted.col(0)).array() / eigvec_sorted.col(0).array() << std::endl; //cout << "lmat=" << std::endl << lmat << std::endl; } solver.finalize(); MPI_Finalize(); return 0; }
int main(int argc, char **argv) { double lattice[N][N]; int iter=10000; int printstep=5; int drawstep=1; int nanoseconds=100000; timespec sleepPeriod = { 0, nanoseconds }; timespec unusedPeriod; srandom(time(NULL)); // Open files: std::ofstream magfile; magfile.open("magnetisation.dat"); // Initialize SDL: SDL_Surface *screen; if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1; if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, SDL_HWSURFACE))) { SDL_Quit(); return 1; } // Key to break it SDL_Event event; // Simple Pixel Uint32 simplepix = SDL_MapRGB( screen->format, 0, 0xFF, 0xFF ); // Surface to paint on: SDL_Surface *paintme = NULL; paintme = SDL_CreateRGBSurface( SDL_HWSURFACE, screen->w, screen->h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, 0 ); // Start xymodel: double T=100.0; randLattice(lattice); for (int k = 0; k < iter; ++k) { if (k%printstep==0) { //nanosleep(&sleepPeriod, &unusedPeriod); //system("clear"); std::cout << "Iteration: " << k << std::endl; //printLattice(lattice); std::cout << "Energy: " << hamiltonian(lattice) << std::endl;; magfile << k << "\t" << magnetisation(lattice) << std::endl; } if (k%drawstep==0) { SDLdrawLattice(lattice,paintme,screen); SDL_WM_SetCaption("xy Model", NULL); } turnSpinS(lattice,&T,k*N*N); //turnSpinR(lattice,&T,k*N*N); if (k%printstep==0) std::cout << "Temperature: " << T << std::endl; // Shall we quit? while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { // Quit! SDL_Quit(); magfile.close(); return 0; } } } magfile.close(); return 0; }
/* * Use DIIS to help SCF */ void calculateSCFDIIS(molecule_t *molecule) { #define EPS 0.0000000000001 #define DEL 0.0000000000001 double **fs[6], **es[6], **b, *c; int **piv; hamiltonian(molecule); sqrtMolecule(molecule); int n = molecule->orbitals; //So that the same thing does not need to be typed repeatedly. int count = 0; double elec, energy = 0, elast, rms; double **f0, **f1, **f2, **c0, **c1, **d0, **d1, **work1, **work2, **work3, **ham, **shalf, **s; double **sort; f0 = calloc_contiguous(2, sizeof(double), n, n); f1 = calloc_contiguous(2, sizeof(double), n, n); f2 = calloc_contiguous(2, sizeof(double), n, n); c0 = calloc_contiguous(2, sizeof(double), n, n); c1 = calloc_contiguous(2, sizeof(double), n, n); d0 = calloc_contiguous(2, sizeof(double), n, n); d1 = calloc_contiguous(2, sizeof(double), n, n); work1 = calloc_contiguous(2, sizeof(double), n, n); work2 = calloc_contiguous(2, sizeof(double), n, n); work3 = calloc_contiguous(2, sizeof(double), n, n); ham = calloc_contiguous(2, sizeof(double), n, n); shalf = calloc_contiguous(2, sizeof(double), n, n); sort = calloc_contiguous(2, sizeof(double), n, n); b = calloc_contiguous(2, sizeof(double), 7, 7); c = calloc(7, sizeof(double)); s = calloc_contiguous(2, sizeof(double), n, n); piv = calloc_contiguous(2, sizeof(double), 7, 7); for(int i = 0; i < 6; i++) { fs[i] = calloc_contiguous(2, sizeof(double), n, n); es[i] = calloc_contiguous(2, sizeof(double), n, n); } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { s[i][j] = molecule->overlap[i][j]; shalf[i][j] = molecule->symmetric[i][j]; } } printf("\nElec\t\tEnergy\t\tDiff\t\tRMS\n"); do { elast = energy; if(count == 0) { for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { //Find the initial Fock guess. f0[i][j] = ham[i][j] = molecule->hamiltonian[i][j]; } } } else { memcpy(*d1, *d0, n * n * sizeof(double)); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { f0[i][j] = ham[i][j]; for(int k = 0; k < n; k++) { for(int l = 0; l < n; l++) { f0[i][j] += d0[k][l] * (2 * molecule->two_electron[TEI(i, j, k, l)] - molecule->two_electron[TEI(i, k, j, l)]); } } } } } //DIIS extrapolation. memcpy(*(fs[count % 6]), *f0, n * n * sizeof(double)); cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, n, n, n, 1.0, *s, n, *d0, n, 0, *work1, n); cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, n, n, n, 1.0, *work1, n, *f0, n, 0, *work2, n); cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, n, n, n, 1.0, *f0, n, *d0, n, 0, *work1, n); cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, n, n, n, 1.0, *work1, n, *s, n, 0, *work3, n); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { es[count % 6][i][j] = work3[i][j] - work2[i][j]; } } if(count >= 6) { for(int i = 0; i < ((count > 6)? 6: count); i++) { for(int j = 0; j < ((count > 6)? 6: count); j++) { b[i][j] = 0; for(int k = 0; k < n; k++) { for(int l = 0; l < n; l++) { b[i][j] += es[i][k][l] * es[j][k][l]; } } } } if(count < 6) { for(int i = 0; i < 6; i++) { for(int j = 0; j < 6; j++) { if(i < count && j < count) { continue; } if(i == j) { b[i][j] = 1; } else { b[i][j] = 0; } } } } for(int i = 0; i < 6; i++) { b[6][i] = -1; b[i][6] = -1; c[i] = 0; } b[6][6] = 0; c[6] = -1; LAPACKE_dgesv(LAPACK_ROW_MAJOR, 7, 1, *b, 7, *piv, c, 1); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { f2[i][j] = 0; for(int m = 0; m < 6; m++) { f2[i][j] += c[m] * fs[m][i][j]; } } } cblas_dgemm(CblasRowMajor, CblasTrans, CblasNoTrans, n, n, n, 1.0, *shalf, n, *f2, n, 0, *work1, n); cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, n, n, n, 1.0, *work1, n, *shalf, n, 0, *f1, n); } else { cblas_dgemm(CblasRowMajor, CblasTrans, CblasNoTrans, n, n, n, 1.0, *shalf, n, *f0, n, 0, *work1, n); cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, n, n, n, 1.0, *work1, n, *shalf, n, 0, *f1, n); } memset(work1[0], 0, n * n * sizeof(double)); memset(work2[0], 0, n * n * sizeof(double)); memset(work3[0], 0, n * n * sizeof(double)); LAPACKE_dgeev(LAPACK_ROW_MAJOR, 'N', 'V', n, *f1, n, *work1, *work2, *work3, n, *c1, n); //Prepare for sorting. for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { work2[i][j] = c1[i][j]; } } //Sort for(int i = 0; i < n; i++) { sort[i] = work1[0] + i; } qsort(sort, n, sizeof(double *), comparedd); //Sift through data. for(int i = 0; i < n; i++) { unsigned long off = ((unsigned long) sort[i] - (unsigned long) work1[0]); off /= sizeof(double); for(int j = 0; j < n; j++) { c1[j][i] = work2[j][off]; } } cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, n, n, n, 1.0, *shalf, n, *c1, n, 0, *c0, n); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { d0[i][j] = 0; for(int k = 0; k < molecule->electrons / 2; k++) { d0[i][j] += c0[i][k] * c0[j][k]; } } } elec = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { elec += d0[i][j] * (ham[i][j] + f0[i][j]); } } energy = elec + molecule->enuc; rms = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { rms += (d0[i][j] - d1[i][j]) * (d0[i][j] - d1[i][j]); } } rms = sqrt(rms); count++; printf("%d\t%.15f\t%.15f\t%.15f\t%.15f\n", count, elec, energy, fabs(elast - energy), rms); } while(count < 100 && (fabs(elast - energy) > EPS && rms > DEL)); molecule->scf_energy = energy; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { molecule->density[i][j] = d0[i][j]; molecule->fock[i][j] = f0[i][j]; molecule->molecular_orbitals[i][j] = c0[i][j]; molecule->molecular_eigs[i][j] = ((i == j)? sort[i][0]: 0); } } free_mult_contig(16, c0, c1, d0, d1, f0, f1, f2, ham, shalf, work1, work2, work3, sort, b, c, s); for(int i = 0; i < 6; i++) { free(fs[i]); free(es[i]); } }
int main(int argc, char* argv[]) { int program = 0; // Get input file from first command line argument if(argc < 3){ // No input file given std::cerr << "Usage: ./drudeh [input_file] [basis_file]\n"; program = -1; } else { std::string ifname = argv[1]; // Input filename std::string bfname = argv[2]; // Basis filename std::string ofname = ifname; // Output file prefix std::size_t pos = ofname.find('.'); if (pos != std::string::npos) { // Cut off extension ofname.erase(pos, ofname.length()); } ofname += ".output"; // Open the input file std::ifstream input(ifname); // Check it opened successfully if (!input.is_open()){ std::cerr << "Failed to open input file.\n"; program = -1; } else { // Declare and read parameters int N; std::vector<double> mu, omega, q; N = readParams(input, mu, omega, q); // Make the zeta vector std::vector<double> zeta(N); for (int i = 0; i < N; i++) zeta[i] = 0.5*mu[i]*omega[i]; // Geometry Eigen::MatrixXd R(N-1, 3); // Rewind input file input.clear(); input.seekg(0, std::ios::beg); // Read in geometry readGeom(input, R, N); // Open basis file std::ifstream basis(bfname); // Check if opened successfully if (!basis.is_open()){ std::cerr << "Failed to open basis file.\n"; program = -1; } else { // Initialise array of basis functions std::vector<BasisFunction> bfs; // Read in the basis functions int nbfs = readBasis(basis, bfs, N, zeta, R); // Form and diagonalise hamiltonian matrix Eigen::MatrixXd D = hamiltonian(N, nbfs, bfs, R, mu, omega, q); // Find lowest non-zero eigenvalue //int i = 0; double lowest_eig = D(0); // while ( D(i) < 0.1 ) i++; // if ( i < nbfs ) //lowest_eig = D(i); // Open output file std::ofstream output(ofname); if (!output.is_open()){ std::cout << "Couldn't open output file.\n"; std::cout << "Total number of basis functions = " << nbfs << "\n"; std::cout << "Lowest eigenvalue = " << std::setprecision(15) << lowest_eig << "\n"; } else { output << "Total number of basis functions = " << nbfs << "\n"; output << "Lowest eigenvalue = "<< std::setprecision(15) << lowest_eig << "\n"; } } } } return program; }
int main(int argc, char *argv[]){ int i,j,k, c, N; int dflag=0, eflag=0, gflag=0, vflag=0, hflag=0; float w; /* frec */ //char *lvalue=NULL; double **M, // XYZ coordinates dos, lambda=0; while((c = getopt (argc, argv, "degvhl:")) != -1){ switch (c){ case 'd': dflag = 1; break; case 'e': eflag = 1; break; case 'g': gflag = 1; break; case 'v': vflag = 1; break; case 'h': hflag = 1; break; case 'l': lambda = atof(optarg); break; } } scanf("%d",&N); M = (double **) malloc (N*sizeof(double *)); // coordinate matrix // read coordinates (XYZ format file) for (int i=0; i<N; i++){ char null[5]; // discard element double *tmp = (double *) malloc (3 * sizeof(double)); // 3 coordinates scanf("%s%lf%lf%lf", null, &tmp[0], &tmp[1], &tmp[2]); M[i] = tmp; // printf("- %.2f %.2f\n",M[i][0], M[i][1]); // DEBUG } /* M: coordinate matrix, N: number of atoms, l: spin-orbit parameter (set to 0 to tight-binding)*/ gsl_matrix_complex * Hso = hamiltonian(M, N, lambda); /* print hamiltonial */ if (hflag){ printComMat(Hso,N*SPIN*ORB); return 0; } /* eigenvalues */ gsl_matrix_complex * evec = gsl_matrix_complex_alloc(N*SPIN*ORB, N*SPIN*ORB); gsl_vector * eval = gsl_vector_alloc(N*SPIN*ORB); gsl_eigen_hermv_workspace * ws = gsl_eigen_hermv_alloc(N*SPIN*ORB); gsl_matrix_complex * A = Hso; // gsl_eigen_hermv() destroys Hso matrix, use a copy instead gsl_eigen_hermv (A, eval, evec, ws); gsl_eigen_hermv_sort (eval, evec, GSL_EIGEN_SORT_VAL_ASC); gsl_eigen_hermv_free(ws); if (eflag){ for (int i=0; i<N*SPIN*ORB; i++) printf("%d %.4g \n", i, gsl_vector_get(eval,i)); return 0; } if (vflag){ printComMat(evec, N*SPIN*ORB); return 0; } /* calculate DoS * __ __ * \ \ Hij Hji * DOS(E) = -imag /_ /_ ---------------- * i j E - En + i*eta * * where H is the hamiltonian, and n is the state. * NOTE: i and j 0-indexed list. i*eta */ double eval_min = gsl_vector_min (eval), /* lower bound */ eval_max = gsl_vector_max (eval); /* upper bound */ if (dflag) for (w = eval_min; w < eval_max; w += 1e-3){ dos = 0; #pragma omp parallel num_threads(4) { int tid = omp_get_thread_num(); #pragma omp for private(i,k) reduction (+:dos) for (i=0; i<N*SPIN*ORB; i++) for (k=0; k<N*SPIN*ORB; k++){ gsl_complex h = gsl_matrix_complex_get (Hso, i, k); double l = gsl_vector_get (eval ,k); gsl_complex z = gsl_complex_rect(0,5e-3); /* parte imaginaria */ gsl_complex num = gsl_complex_mul(h,gsl_complex_conjugate(h)); /* numerador */ gsl_complex den = gsl_complex_add_real(z, w-l); /* denominador */ gsl_complex g = gsl_complex_div(num,den); dos += GSL_IMAG(g); } if (dflag && tid==0) printf("%.3g %g \n", w, -dos/PI); } } /* Green's function * * <i|n> <n|j> * Gij(E) = ---------------- * E - En + i*eta * * where i and j are atoms, and n is the state. * NOTE: i and j 0-indexed list. */ int list[]={0,1,2,5,6,7}; /* atoms to get conductance */ int NL = (int) sizeof(list)/sizeof(list[0]); gsl_matrix_complex * G = gsl_matrix_complex_alloc(NL*SPIN*ORB, NL*SPIN*ORB); // Green if (gflag) for (double E = eval_min; E < eval_max; E += 1e-3){ // energy gsl_matrix_complex_set_all(G, GSL_COMPLEX_ZERO); // init for (int n=0; n<N*SPIN*ORB; n++) // states for (i=0; i<NL; i++) // atoms for (j=0; j<NL; j++) // atoms for (int k0=0; k0<SPIN*ORB; k0++){ // orbitals for (int k1=0; k1<SPIN*ORB; k1++){ // orbitals gsl_complex in = gsl_matrix_complex_get (evec, n, list[i]*SPIN*ORB+k0); gsl_complex nj = gsl_matrix_complex_get (evec, n, list[j]*SPIN*ORB+k1); double En = gsl_vector_get (eval ,n); gsl_complex eta = gsl_complex_rect(0,5e-3); /* delta */ gsl_complex num = gsl_complex_mul(in, gsl_complex_conjugate(nj)); /* num */ gsl_complex den = gsl_complex_add_real(eta, E - En); /* den */ gsl_complex Gij = gsl_complex_div(num,den); gsl_complex tmp = gsl_matrix_complex_get(G, i*SPIN*ORB+k0, j*SPIN*ORB+k1); gsl_complex sum = gsl_complex_add(tmp, Gij); gsl_matrix_complex_set(G, i*SPIN*ORB+k0, j*SPIN*ORB+k1, sum); } } dos = 0 ; for(int i=0; i<NL*SPIN*ORB; i++) dos += GSL_IMAG( gsl_matrix_complex_get(G, i, i) ); printf("%.3g %g\n", E, -dos/PI); // printComMat(G, NL*SPIN*ORB); } gsl_matrix_complex_free(G); gsl_vector_free(eval); gsl_matrix_complex_free(evec); return 0; }