double Geo_Constraint_Bond(unsigned n, const double *x, double *grad, void *data)
{
	int ia, ib, iPos;
	double r, r0, g[2][3];
	CMol* pMol;
	GEO_FIX_BOND *Geo_Fix_r;

	Geo_Fix_r = (GEO_FIX_BOND*)data;
	pMol = (CMol*)(Geo_Fix_r->pMol);
	ia = Geo_Fix_r->ia;
	ib = Geo_Fix_r->ib;
	r0 = Geo_Fix_r->r0;

	if(grad)	{
		r = pMol->Query_Distance(ia, ib, 1, g);
		memset(grad, 0, sizeof(double)*n);

		iPos = 3 * ia;
		grad[iPos  ] = g[0][0];
		grad[iPos+1] = g[0][1];
		grad[iPos+2] = g[0][2];

		iPos = 3 * ib;
		grad[iPos  ] = g[1][0];
		grad[iPos+1] = g[1][1];
		grad[iPos+2] = g[1][2];
	}
	else	{
		r = pMol->Query_Distance(ia, ib, 0, g);
	}

	return (r - r0);
}
void Get_Representative_14_Atoms(int iDih)
{
	int ia, ib, ic, id, i, Idx, nAtom_Connected, nAtom_Connected_Max=0;

	ib = Dih_Bond[iDih][1];
	ic = Dih_Bond[iDih][2];

	//start	to find the representative atom for the left side, ia
	nAtom_Connected_Max=-100;
	ia = -1;
	for(i=0; i<Bond_Count[ib]; i++)	{
		Idx = Bond_List[ib][i];
		if(Idx == ic)	{
			continue;
		}
		nAtom_Connected = Mol.Count_All_Atoms_Connected(Idx, ib);
		if(nAtom_Connected > nAtom_Connected_Max)	{
			nAtom_Connected_Max = nAtom_Connected;
			ia = Idx;
		}
		else if( (i!=0) && (nAtom_Connected == nAtom_Connected_Max) && (Mol.mass[Idx] > Mol.mass[ia]) )	{
			ia = Idx;
		}
	}
	if(ia>=0)	{
		Dih_Bond[iDih][0] = ia;
	}
	else	{
		Quit_With_Error_Msg("Fail to find the representative atom (heavy atom), ia, in dihedral.\nQuit\n");
	}
	//end	to find the representative atom for the left side, ia


	//start	to find the representative atom for the left side, ia
	nAtom_Connected_Max=-100;
	id = -1;
	for(i=0; i<Bond_Count[ic]; i++)	{
		Idx = Bond_List[ic][i];
		if(Idx == ib)	{
			continue;
		}
		nAtom_Connected = Mol.Count_All_Atoms_Connected(Idx, ic);
		if(nAtom_Connected > nAtom_Connected_Max)	{
			nAtom_Connected_Max = nAtom_Connected;
			id = Idx;
		}
		else if( (i!=0) && (nAtom_Connected == nAtom_Connected_Max) && (Mol.mass[Idx] > Mol.mass[id]) )	{
			id = Idx;
		}
	}
	if(id>=0)	{
		Dih_Bond[iDih][3] = id;
	}
	else	{
		Quit_With_Error_Msg("Fail to find the representative atom (heavy atom), id, in dihedral.\nQuit\n");
	}
	//end	to find the representative atom for the left side, ia

	return;
}
void SaveOptPdb(char szName[])
{
	int i;
	for(i=0; i<n_Phi; i++)	{
		Mol.QueryDihedral(IdxDihSelect[i]);
		Mol.Edit_Dihedral(IdxDihSelect[i], Phi_To_Set_Scan[i]);
	}
	Mol.SavePdb(szName);
}
Exemple #4
0
void To_Setup_All_Dihedral_Constraint(void)
{
	int i;

	Mol_ESP.nPhi_Fixed = 0;

	for(i=0; i<n_Phi; i++)	{
		Mol_ESP.To_Fix_Dihedral(DihList[i][0], DihList[i][1], DihList[i][2], DihList[i][3], 
			Mol_ESP.Query_Dihedral(DihList[i][0], DihList[i][1], DihList[i][2], DihList[i][3], 0, NULL));
	}
}
double Geo_Constraint_Dihedral(unsigned n, const double *x, double *grad, void *data)
{
	int ia, ib, ic, id, iPos;
	double phi, phi0, d_phi, g[4][3];
	CMol* pMol;
	GEO_FIX_DIHEDRAL *Geo_Fix_phi;

	Geo_Fix_phi = (GEO_FIX_DIHEDRAL*)data;
	pMol = (CMol*)(Geo_Fix_phi->pMol);
	ia = Geo_Fix_phi->ia;
	ib = Geo_Fix_phi->ib;
	ic = Geo_Fix_phi->ic;
	id = Geo_Fix_phi->id;
	phi0 = Geo_Fix_phi->phi0;

	if(grad)	{
		phi = pMol->Query_Dihedral(ia, ib, ic, id, 1, g);
		memset(grad, 0, sizeof(double)*n);

		iPos = 3 * ia;
		grad[iPos  ] = g[0][0];
		grad[iPos+1] = g[0][1];
		grad[iPos+2] = g[0][2];

		iPos = 3 * ib;
		grad[iPos  ] = g[1][0];
		grad[iPos+1] = g[1][1];
		grad[iPos+2] = g[1][2];

		iPos = 3 * ic;
		grad[iPos  ] = g[2][0];
		grad[iPos+1] = g[2][1];
		grad[iPos+2] = g[2][2];

		iPos = 3 * id;
		grad[iPos  ] = g[3][0];
		grad[iPos+1] = g[3][1];
		grad[iPos+2] = g[3][2];
	}
	else	{
		phi = pMol->Query_Dihedral(ia, ib, ic, id, 0, g);
	}

	d_phi = phi - phi0;
	if(d_phi < -PI)	{
		d_phi += PI2;
	}
	else if(d_phi > PI)	{
		d_phi -= PI2;
	}

	return d_phi;
}
void Gen_Soft_Dihedral_List_MD_High_T(void)
{
	int Step, Idx, RecIdx, iPos, ia, ib, ic, id;

	Mol.Init_LangevinDynamics(T_Sim);

	nDihedral = Mol.nDihedral;

	for(Step=1; Step<=MAX_STEP; Step++)	{
		Mol.LangevinDynamics(Step);
		if(Step%GAP==0)	{
			RecIdx = Step/GAP - 1;
			for(Idx=0; Idx<nDihedral; Idx++)	{
				dih_List[RecIdx][Idx] = Mol.dih_Phi_List[Idx];
			}
		}
	}

	Cal_Sig();
	Cal_Sig_Shift_PI2();

	n_Soft_Dih = 0;
	for(Idx=0; Idx<nDihedral; Idx++)	{
		if(IsRigidDih[Idx] == 0)	{
			iPos = Idx*4;
			ia = Mol.DihedralList[iPos  ];
			ib = Mol.DihedralList[iPos+1];
			ic = Mol.DihedralList[iPos+2];
			id = Mol.DihedralList[iPos+3];
			if(ib <= ic)	{
				if(!Is_In_Soft_Dihedral_List(ib, ic))	{
					Dih_Bond[n_Soft_Dih][0] = ia;
					Dih_Bond[n_Soft_Dih][1] = ib;
					Dih_Bond[n_Soft_Dih][2] = ic;
					Dih_Bond[n_Soft_Dih][3] = id;
					n_Soft_Dih++;
					printf("%3d %3d %3d %3d \n", Mol.DihedralList[iPos]+1, Mol.DihedralList[iPos+1]+1, Mol.DihedralList[iPos+2]+1, Mol.DihedralList[iPos+3]+1);
				}
			}
			else	{
				if(!Is_In_Soft_Dihedral_List(ic, ib))	{
					Dih_Bond[n_Soft_Dih][0] = id;
					Dih_Bond[n_Soft_Dih][1] = ic;
					Dih_Bond[n_Soft_Dih][2] = ib;
					Dih_Bond[n_Soft_Dih][3] = ia;
					n_Soft_Dih++;
					printf("%3d %3d %3d %3d \n", Mol.DihedralList[iPos]+1, Mol.DihedralList[iPos+1]+1, Mol.DihedralList[iPos+2]+1, Mol.DihedralList[iPos+3]+1);
				}
			}
		}
	}
}
int Read_Soft_DihedralList(void)
{
	FILE *fIn;
	int ReadItem;
	char szLine[256], *ReadLine;

	n_Phi = 0;

	fIn = fopen(szPhiToScan, "r");

	while(1)	{
		if(feof(fIn))	{
			break;
		}
		ReadLine = fgets(szLine, 128, fIn);
		if(ReadLine == NULL)	{
			break;
		}

		ReadItem = sscanf(szLine, "%d %d %d %d %lf %lf %lf %lf %lf %lf", 
			&(DihList[n_Phi][0]), &(DihList[n_Phi][1]), &(DihList[n_Phi][2]), &(DihList[n_Phi][3]), 
			&(Phi_Set[n_Phi][0]), &(Phi_Set[n_Phi][1]), &(Phi_Set[n_Phi][2]), &(Phi_Set[n_Phi][3]), &(Phi_Set[n_Phi][4]), &(Phi_Set[n_Phi][5]));

		if(ReadItem > 4)	{
			DihList[n_Phi][0]--;
			DihList[n_Phi][1]--;
			DihList[n_Phi][2]--;
			DihList[n_Phi][3]--;
			IdxDihSelect[n_Phi] = Mol.Query_Dihedral_Index(DihList[n_Phi][0], DihList[n_Phi][1], DihList[n_Phi][2], DihList[n_Phi][3]);

			if(IdxDihSelect[n_Phi] < 0)	{
				Quit_With_Error_Msg("Fail to identify the index of one soft dihedral.\n");
			}
			Mol.BuildSegmentList_Dihedrals(IdxDihSelect[n_Phi]);

			n_State_List[n_Phi] = ReadItem - 4;
			n_Phi++;
		}
		else	{
			break;
		}
	}

	fclose(fIn);

	memcpy(Phi_Set_Save, Phi_Set, sizeof(double)*N_MAX_DIH*MAX_N_STATE);
	memcpy(n_State_List_Save, n_State_List, sizeof(int)*N_MAX_DIH);

	return n_Phi;
}
void Output_Gaussian_File(void)
{
	FILE *fOut, *fIn;
	int i, nAtom, IdxScan;
	char szNameGjf[256], szCmd[256], szName_Output[256];

	Determine_QM_Scan_Part();

	nAtom = Mol.nAtom;
	for(IdxScan=0; IdxScan<n_QM_Part; IdxScan++)	{
		Mol.QueryDihedral(IdxDihSelect[Active_Phi]);
		Mol.Edit_Dihedral(IdxDihSelect[Active_Phi], Phi_QM_Start[IdxScan]);

		sprintf(szNameGjf, "qm-scan-%d-%d.gjf", Active_Phi+1, IdxScan+1);
		fOut = fopen(szNameGjf, "w");
		fprintf(fOut, "%s", szQM_Level_1D_Scan_Cmd);
		for(i=0; i<nAtom; i++)	{
//			fprintf(fOut, "%c  %9.5lf %9.5lf %9.5lf\n", Mol.AtomName[i][0], Mol.x[i], Mol.y[i], Mol.z[i]);
			fprintf(fOut, "%s  %9.5lf %9.5lf %9.5lf\n", szElemName[i], Mol.x[i], Mol.y[i], Mol.z[i]);
		}
		fprintf(fOut, "\n");
		
		for(i=0; i<n_Phi; i++)	{
			if(i == Active_Phi)	{
				fprintf(fOut, "%s", szQM_Scan[IdxScan]);
			}
			else	{
				fprintf(fOut, "%d %d %d %d F\n", DihList[i][0]+1, DihList[i][1]+1, DihList[i][2]+1, DihList[i][3]+1);
			}
		}
		fprintf(fOut, "\n");
		fclose(fOut);

		sprintf(szName_Output, "qm-1d-phi-%d-p%d.out", Active_Phi+1, IdxScan+1);
		sprintf(szCmd, "%s < %s > %s", szExe_G09, szNameGjf, szName_Output);


		fIn = fopen(szName_Output, "r");
		if(fIn == NULL)	{
			system(szCmd);
		}
		else	{
			fclose(fIn);
		}

		Extract_Coord_E(szName_Output, Active_Phi, IdxScan);
	}
}
Exemple #9
0
void Cal_E_MM_Scaned(void)
{
	int i, j, nAtom, Count=0;
	double E_Min_Local_QM;

	E_Scan_MM_Mean = 0.0;
	E_Scan_QM_Mean = 0.0;
	nAtom = Mol_ESP.nAtom;

	for(i=0; i<n_Phi; i++)	{
		E_Min_Local_QM = 1.0E100;
		for(j=0; j<nScan_List[i]; j++)	{
			if(E_Scan_QM[i][j] < E_Min_Local_QM)	{	// to find the lowest QM energy
				E_Min_Local_QM = E_Scan_QM[i][j];
			}
		}

		for(j=0; j<nScan_List[i]; j++)	{
			memcpy(Mol_ESP.x, x_Scan_list[i][j], sizeof(double)*nAtom);
			memcpy(Mol_ESP.y, y_Scan_list[i][j], sizeof(double)*nAtom);
			memcpy(Mol_ESP.z, z_Scan_list[i][j], sizeof(double)*nAtom);
			E_Scan_MM[i][j] = Mol_ESP.Cal_E(0);

			if( E_Scan_QM[i][j] < (E_Min_Local_QM+6.0) )	{	// skip configuration with much higher energies
				E_Scan_QM_Mean += E_Scan_QM[i][j];
				E_Scan_MM_Mean += E_Scan_MM[i][j];
				Count ++;
			}
		}
	}
	E_Scan_QM_Mean /= Count;
	E_Scan_MM_Mean /= Count;
}
Exemple #10
0
int main(int argc, char *argv[])
{
	if(argc < 4)	{
		printf("Usage: add-tip3 your-rtf your-prm your-psf E14FAC\nQuit\n");
		return 1;
	}

	strcpy(szRtfFile, argv[1]);
	strcpy(szPrmFile, argv[2]);
	strcpy(szXpsfName, argv[3]);

	if(argc == 5)	{
		E14FAC = atof(argv[4]);
	}

	fFile_Run_Log = fopen("log-modify-rtf.txt", "w");
	Mol.ReadPSF(szXpsfName, 0);
	GetAtomTypeFromMass();
	Count_Bond_and_H();
	Assign_Alpha_Miller();

	Add_Tip3_Rtf_File();

	Add_Tip3_Prm_File();

	fclose(fFile_Run_Log);

	return 0;
}
int main(int argc, char *argv[])
{
	if(argc == 2)	{
		To_Exclude_Methyl_Torsion = 0;
	}
	else	{
		To_Exclude_Methyl_Torsion = 1;
	}

	memset(IsRigidDih, 0, sizeof(int)*MAX_DIHEDRAL_LIST);

	fFile_Run_Log = fopen("identify-soft.log", "w");

	ForceField.ReadForceField(szForceFiled);
	Mol.ReadPSF(szXpsfFile, 0);
	Mol.AssignForceFieldParameters(&ForceField);
	Mol.ReadCRD(szCrdFile);
	Setup_Bond_List();

//	Gen_Soft_Dihedral_List_MD_High_T();
//	printf("\n\n");

	Gen_Soft_Dihedral_List_Excluding_Ring();
	printf("\n\n");

	To_Refine_Soft_Dihedral_List();

/*	
	fData = fopen("dihedral.txt", "w");
	for(Step=0; Step<MAX_STEP/GAP; Step++)	{
		fprintf(fData, "%8d ", Step);
		for(Idx=0; Idx<nDihedral; Idx++)	{
			if(IsRigidDih[Idx] == 0)	{
				fprintf(fData, " %7.2lf", dih_List[Step][Idx]);
			}
		}
		fprintf(fData, "\n");
	}
	fclose(fData);
*/
	fclose(fFile_Run_Log);


	return 0;
}
int Read_Soft_DihedralList(void)
{
    FILE *fIn;
    int ReadItem;
    char szLine[256], *ReadLine, ErrorMsg[256], szFlag[256];

    n_Phi = 0;

    fIn = fopen(szPhiToScan, "r");

    while(1)	{
        if(feof(fIn))	{
            sprintf(ErrorMsg, "Fail to open file: %s\nQuit\n", szPhiToScan);
            Quit_With_Error_Msg(ErrorMsg);
        }
        ReadLine = fgets(szLine, 128, fIn);
        if(ReadLine == NULL)	{
            break;
        }

        ReadItem = sscanf(szLine, "%d %d %d %d %s",
                          &(DihList[n_Phi][0]), &(DihList[n_Phi][1]), &(DihList[n_Phi][2]), &(DihList[n_Phi][3]), szFlag);

        if(ReadItem >= 4)	{
            DihList[n_Phi][0]--;
            DihList[n_Phi][1]--;
            DihList[n_Phi][2]--;
            DihList[n_Phi][3]--;
            IdxDihSelect[n_Phi] = Mol_ESP.Query_Dihedral_Index(DihList[n_Phi][0], DihList[n_Phi][1], DihList[n_Phi][2], DihList[n_Phi][3]);

            if(IdxDihSelect[n_Phi] < 0)	{
                Quit_With_Error_Msg("Fail to identify the index of one soft dihedral.\n");
            }

            Is_Dih_Fixed[n_Phi] = 0;
            if(ReadItem==5)	{
                if( (strcmp(szFlag, "F")==0) || (strcmp(szFlag, "f")==0) )	{
                    Is_Dih_Fixed[n_Phi] = 1;
                }
            }

            n_Phi++;
        }
        else	{
            break;
        }
    }

    fclose(fIn);

    Setup_Phi_Constraint_List();

    return n_Phi;
}
void MM_Fixed_1D_Scan(void)
{
	int i, Phi, Count;

	RestoreCoordinates();
	for(i=0; i<n_Phi; i++)	{
		Mol.QueryDihedral(IdxDihSelect[i]);
		Mol.Edit_Dihedral(IdxDihSelect[i], Phi_To_Set[i]);
	}
	
	Count = 0;
	for(Phi=-180; Phi<=180; Phi+=BIN_SIZE)	{	// 1D scan for this rotamer
		Phi_To_Set[Active_Phi] = Phi*1.0;
		Mol.QueryDihedral(IdxDihSelect[Active_Phi]);
		Mol.Edit_Dihedral(IdxDihSelect[Active_Phi], Phi_To_Set[Active_Phi]);
		E_Phi[Count] = Mol.Cal_E(0);
		Count++;
	}

	return;
}
double func_Geo_Opt_Constraint(unsigned n, const double *x, double *grad, void *my_func_data)
{
	int i, iPos, nAtom;
	double E_Total, *gx, *gy, *gz, *xx, *yy, *zz;
	CMol* pMol;

	pMol = (CMol*)my_func_data;
	nAtom = pMol->nAtom;
	gx = pMol->grad_x;
	gy = pMol->grad_y;
	gz = pMol->grad_z;
	xx = pMol->x;
	yy = pMol->y;
	zz = pMol->z;

	if(grad)	{
		Iter_Opt_Constrained ++;
		for(i=0; i<nAtom; i++)	{	// update the coordinate
			iPos = 3*i;
			xx[i] = x[iPos  ];
			yy[i] = x[iPos+1];
			zz[i] = x[iPos+2];
		}

		E_Total = pMol->Cal_E(0);

		for(i=0; i<nAtom; i++)	{	// get the gradient
			iPos = 3*i;
			grad[iPos  ] = gx[i];
			grad[iPos+1] = gy[i];
			grad[iPos+2] = gz[i];
		}
	}
	else	{
		E_Total = pMol->Cal_E(0);
	}

//  printf( "TT: %f\n", E_Total );
	return E_Total;
}
double Geo_Constraint_Angle(unsigned n, const double *x, double *grad, void *data)
{
	int ia, ib, ic, iPos;
	double theta, theta0, g[3][3];
	CMol* pMol;
	GEO_FIX_ANGLE *Geo_Fix_theta;

	Geo_Fix_theta = (GEO_FIX_ANGLE*)data;
	pMol = (CMol*)(Geo_Fix_theta->pMol);
	ia = Geo_Fix_theta->ia;
	ib = Geo_Fix_theta->ib;
	ic = Geo_Fix_theta->ic;
	theta0 = Geo_Fix_theta->theta0;

	if(grad)	{
		theta = pMol->Query_Angle(ia, ib, ic, 1, g);
		memset(grad, 0, sizeof(double)*n);

		iPos = 3 * ia;
		grad[iPos  ] = g[0][0];
		grad[iPos+1] = g[0][1];
		grad[iPos+2] = g[0][2];

		iPos = 3 * ib;
		grad[iPos  ] = g[1][0];
		grad[iPos+1] = g[1][1];
		grad[iPos+2] = g[1][2];

		iPos = 3 * ic;
		grad[iPos  ] = g[2][0];
		grad[iPos+1] = g[2][1];
		grad[iPos+2] = g[2][2];
	}
	else	{
		theta = pMol->Query_Angle(ia, ib, ic, 0, g);
	}

	return (theta - theta0);
}
Exemple #16
0
int main(int argc, char **argv)
{
	int i;
	char ErrorMsg[256];

	fFile_Run_Log = fopen(szName_Run_Log, "w");
	if(fFile_Run_Log==NULL)	{
		sprintf(ErrorMsg, "Fail to create the log file.\n\n");
		Quit_With_Error_Msg(ErrorMsg);
	}

	ForceField.ReadForceField("org-mol.prm");
//	ForceField.ReadForceField("mol.prm");
	
	Mol_ESP.ReadPSF("org-mol.xpsf", 0);
//	Mol_ESP.ReadPSF("mol.xpsf", 0);
	Read_Rtf_File();
	
	strcpy(Mol_ESP.MolName, Mol_ESP.ResName[0]);
	Mol_ESP.AssignForceFieldParameters(&ForceField);

	Read_Soft_DihedralList();

	Mol_ESP.Is_Phi_Psi_Constrained = 0;
	Mol_ESP.E_CMap_On = 0;

//	i = 3;
	for(i=0; i<n_Phi; i++)	{
		Cal_E_MM_QM_Diff(i);
		Fit_Torsion_Parameters(i);
	}

	fflush(fFile_Run_Log);
	

	fclose(fFile_Run_Log);
	return 0;
}
int Read_Soft_DihedralList(void)
{
	FILE *fIn;
	int ReadItem;
	char szLine[256], *ReadLine;

	n_Phi = 0;

	fIn = fopen(szPhiToScan, "r");

	while(1)	{
		if(feof(fIn))	{
			break;
		}
		ReadLine = fgets(szLine, 128, fIn);
		if(ReadLine == NULL)	{
			break;
		}

		ReadItem = sscanf(szLine, "%d %d %d %d", 
			&(DihList[n_Phi][0]), &(DihList[n_Phi][1]), &(DihList[n_Phi][2]), &(DihList[n_Phi][3]));

		if(ReadItem == 4)	{
			DihList[n_Phi][0]--;
			DihList[n_Phi][1]--;
			DihList[n_Phi][2]--;
			DihList[n_Phi][3]--;
			IdxDihSelect[n_Phi] = Mol_ESP.Query_Dihedral_Index(DihList[n_Phi][0], DihList[n_Phi][1], DihList[n_Phi][2], DihList[n_Phi][3]);

			if(IdxDihSelect[n_Phi] < 0)	{
				Quit_With_Error_Msg("Fail to identify the index of one soft dihedral.\n");
			}
			n_Phi++;
		}
		else	{
			break;
		}
	}

	fclose(fIn);

	Setup_Phi_Constraint_List();

	return n_Phi;
}
Exemple #18
0
void Output_1D_QM_MM(void)
{
	FILE *fOut;
	char szName[256];
	int i, j, nAtom;
	double E_Min_1D_QM, E_Min_1D_MM_Mean, E_Min_1D_QM_Mean, E_Min_1D_QM_MM_Shift, Weight_Sum;

	if(ProgID != 0)	{
		return;
	}

	nAtom = Mol_ESP.nAtom;
	for(i=0; i<n_Phi; i++)	{
		sprintf(szName, "1d-qm-mm-%d.dat", i+1);
		fOut = fopen(szName, "w");

		E_Min_1D_QM = 1.0E100;
		for(j=0; j<nScan_List[i]; j++)	{
			memcpy(Mol_ESP.x, x_Scan_list[i][j], sizeof(double)*nAtom);
			memcpy(Mol_ESP.y, y_Scan_list[i][j], sizeof(double)*nAtom);
			memcpy(Mol_ESP.z, z_Scan_list[i][j], sizeof(double)*nAtom);
			E_Scan_MM[i][j] = Mol_ESP.Cal_E(0);

			if(E_Min_1D_QM > E_Scan_QM[i][j])	{
				E_Min_1D_QM = E_Scan_QM[i][j];
			}
		}

		E_Min_1D_MM_Mean = E_Min_1D_QM_Mean = 0.0;
		Weight_Sum = 0.0;
		for(j=0; j<nScan_List[i]; j++)	{
			Weight_Sum += w_Scan_List[i][j];
			E_Min_1D_MM_Mean += (w_Scan_List[i][j] * E_Scan_MM[i][j] );
			E_Min_1D_QM_Mean += (w_Scan_List[i][j] * E_Scan_QM[i][j] );
		}
		E_Min_1D_QM_MM_Shift = (E_Min_1D_QM_Mean - E_Min_1D_MM_Mean)/Weight_Sum;


		for(j=0; j<nScan_List[i]; j++)	{
			fprintf(fOut, "%.1lf %.5lf %.5lf\n", Phi_Scan_List[i][j], E_Scan_QM[i][j]-E_Min_1D_QM, E_Scan_MM[i][j]+E_Min_1D_QM_MM_Shift-E_Min_1D_QM);	// the relative 1D energy
		}

		fclose(fOut);
	}
}
void Gen_Soft_Dihedral_List_Excluding_Ring(void)
{
	int i, nDih, iPos, ia, ib, ic, id;

	nDih = Mol.nDihedral;
	n_Soft_Dih = 0;

	for(i=0; i<nDih; i++)	{
		if(Mol.Is_A_Dihedrals_In_A_Ring(i) == 0)	{
			iPos = i*4;
			ia = Mol.DihedralList[iPos  ];
			ib = Mol.DihedralList[iPos+1];
			ic = Mol.DihedralList[iPos+2];
			id = Mol.DihedralList[iPos+3];
			if(ib <= ic)	{
				if(!Is_In_Soft_Dihedral_List(ib, ic))	{
					Dih_Bond[n_Soft_Dih][0] = ia;
					Dih_Bond[n_Soft_Dih][1] = ib;
					Dih_Bond[n_Soft_Dih][2] = ic;
					Dih_Bond[n_Soft_Dih][3] = id;
					n_Soft_Dih++;
					printf("%3d %3d %3d %3d \n", Mol.DihedralList[iPos]+1, Mol.DihedralList[iPos+1]+1, Mol.DihedralList[iPos+2]+1, Mol.DihedralList[iPos+3]+1);
				}
			}
			else	{
				if(!Is_In_Soft_Dihedral_List(ic, ib))	{
					Dih_Bond[n_Soft_Dih][0] = id;
					Dih_Bond[n_Soft_Dih][1] = ic;
					Dih_Bond[n_Soft_Dih][2] = ib;
					Dih_Bond[n_Soft_Dih][3] = ia;
					n_Soft_Dih++;
					printf("%3d %3d %3d %3d \n", Mol.DihedralList[iPos]+1, Mol.DihedralList[iPos+1]+1, Mol.DihedralList[iPos+2]+1, Mol.DihedralList[iPos+3]+1);
				}
			}
		}
	}
}
void Extract_Coord_E(char szName[], int Idx_Phi, int Idx_Part)
{
	FILE *fIn, *fOut;
	int i, nAtom, ToRead=1, ReadItem, n_Rec=0;
	char szOutput[256], szErrorMsg[256], szLine[256], *ReadLine, szTag[256], ErrorMsg[256];
	double E_QM, E_MM, Phi, Phi_Set, x_Save[MAX_ATOM], y_Save[MAX_ATOM], z_Save[MAX_ATOM];

	nAtom = Mol.nAtom;
	memcpy(x_Save, Mol.x, sizeof(double)*nAtom);
	memcpy(y_Save, Mol.y, sizeof(double)*nAtom);
	memcpy(z_Save, Mol.z, sizeof(double)*nAtom);


	sprintf(szOutput, "tor-1D-idx-%d.dat", Idx_Phi+1);
	fOut = fopen(szOutput, "a+");
	fseek(fOut, 0, SEEK_END);

	fIn = fopen(szName, "r");
	if(fIn == NULL)	{
		sprintf(szErrorMsg, "Fail to open %s\nQuit\n", szName);
		Quit_With_Error_Msg(szErrorMsg);
	}


	while(ToRead)	{
		if(feof(fIn))	{
			break;
		}
		ReadLine = fgets(szLine, 256, fIn);
		if(ReadLine == NULL)	{
			break;
		}
		else	{
//			if(FindString(szLine, " Center     Atomic      Atomic")>=0)	{	// to extract the coordinate
//				Skip_N_Line(fIn, szName, 2);
//			if(FindString(szLine, "     Input orientation:")>=0)	{	// to extract the coordinate
			if(FindString(szLine, " orientation:")>=0)	{	// to extract the coordinate
				Skip_N_Line(fIn, szName, 4);
				for(i=0; i<nAtom; i++)	{
					ReadLine = fgets(szLine, 256, fIn);
					if(ReadLine == NULL)	{
						break;
					}
					ReadItem = sscanf(szLine+31, "%lf %lf %lf", &(Mol.x[i]), &(Mol.y[i]), &(Mol.z[i]));
					if(ReadItem != 3)	{
						ToRead = 0;
						break;
					}
				}
			}
			else if( (FindString(szLine, " SCF Done: ")>=0) && (QM_Level == QM_LEVEL_HF) )	{	// HF
				E_QM = Get_Energy(szLine);
			}
			else if( (FindString(szLine, "EUMP2 =")>=0) && (QM_Level == QM_LEVEL_MP2) )	{
				E_QM = Get_Energy(szLine+27);
			}
			else if(FindString(szLine, " Optimization completed")>=0)	{
				sprintf(szTag, "  D(%d,%d,%d,%d)", DihList[Idx_Phi][0]+1, DihList[Idx_Phi][1]+1, DihList[Idx_Phi][2]+1, DihList[Idx_Phi][3]+1);
				To_Find_Tag(fIn, szName, szTag, szLine);
				ReadItem = sscanf(szLine+28, "%lf", &Phi_Set);	// previous, modredundant
				if(ReadItem != 1)	{
					sprintf(ErrorMsg, "Error in extracting the dihedral.\n%s\nQuit\n", szLine);
					Quit_With_Error_Msg(ErrorMsg);
				}

				Phi = Mol.QueryDihedral(IdxDihSelect[Idx_Phi]);
				E_MM = Mol.Cal_E(0);

				fprintf(fOut, "E_Scan %.13E %.13E  Phi  %.1lf\n", E_QM, E_MM, Phi);
				fprintf(fOut, "Coordinate\n");
				for(i=0; i<nAtom; i++)	{
					fprintf(fOut, "%12.6lf %12.6lf %12.6lf\n", Mol.x[i], Mol.y[i], Mol.z[i]);
				}

				n_Rec++;
			}
		}
	}




	fclose(fOut);
	fclose(fIn);

	memcpy(Mol.x, x_Save, sizeof(double)*nAtom);
	memcpy(Mol.y, y_Save, sizeof(double)*nAtom);
	memcpy(Mol.z, z_Save, sizeof(double)*nAtom);
}
int main(int argc, char **argv)
{
	int i;
	FILE *fOut;
	char szName[256];


	fFile_Run_Log = fopen("qm-1d-scan.log", "w");

//	MPI_Init(&argc, &argv);
//	MPI_Comm_rank(MPI_COMM_WORLD, &ProgID);
//	MPI_Comm_size(MPI_COMM_WORLD, &nProc);

	Get_EXE_Path("G09_EXE_PATH", szExe_G09);

	n_Bins = 360/BIN_SIZE+1;


	ForceField.ReadForceField(szForceFiled);
	Mol.ReadPSF(szXpsfFile, 0);
	Get_Netcharge_From_Xpsf();
	Setup_QM_Level();
	ReadElementList();

	Mol.AssignForceFieldParameters(&ForceField);
	Mol.ReadCRD(szCrdFile);
	BackupCoordinates();

	Read_Soft_DihedralList();

	for(Active_Phi=0; Active_Phi<n_Phi; Active_Phi++)	{
		RestoreCoordinates();
		for(i=0; i<n_Phi; i++)	{
			Phi_To_Set[i] = Mol.QueryDihedral(IdxDihSelect[i]);
			Mol.Edit_Dihedral(IdxDihSelect[i], Phi_To_Set[i]);
		}

		MM_Fixed_1D_Scan();
		E_Barrier_Lowest = Get_Barrier(E_Phi, n_Bins, E_Min, Phi_Save);
		E_Min_Save = E_Min;
		E_Min_Org = E_Min + E_RANGE;
		memcpy(E_Scan, E_Phi, sizeof(double)*n_Bins);
		memcpy(Phi_To_Set_Scan, Phi_To_Set, sizeof(double)*n_Phi);
		Phi_To_Set_Scan[Active_Phi] = Phi_Save;

		sprintf(szName, "ini-%d.dat", Active_Phi+1);
		fOut = fopen(szName, "w");
		for(i=0; i<n_Bins; i++)	{
			fprintf(fOut, "%5d %8.5E\n", -180+i*BIN_SIZE, E_Scan[i]-E_Min);
		}
		fclose(fOut);

		sprintf(szName, "ini-%d.pdb", Active_Phi+1);
		SaveOptPdb(szName);

		n_State_List[Active_Phi] = 1;
		Enumerate_Dihedrals(0);
		n_State_List[Active_Phi] = n_State_List_Save[Active_Phi];

		sprintf(szName, "end-%d.dat", Active_Phi+1);
		fOut = fopen(szName, "w");
		for(i=0; i<n_Bins; i++)	{
			fprintf(fOut, "%5d %8.5E\n", -180+i*BIN_SIZE, E_Scan[i]-E_Min_Save);
		}
		fclose(fOut);

		sprintf(szName, "end-%d.pdb", Active_Phi+1);
		SaveOptPdb(szName);

		sprintf(szName, "phi-%d.dat", Active_Phi+1);
		fOut = fopen(szName, "w");
		for(i=0; i<n_Phi; i++)	{
			fprintf(fOut, "%lf\n", Phi_To_Set_Scan[i]);
		}
		fclose(fOut);

	
		Output_Gaussian_File();
	}


	Reorganize_QM_1D_Scan_Data();

	fclose(fFile_Run_Log);

//	MPI_Barrier(MPI_COMM_WORLD);
//	MPI_Finalize();

	return 0;
}
void Cal_E_MM_QM_Diff(int Idx)
{
	FILE *fIn, *fOut;
	char szName[256], szLine[256], *ReadLine, szTmp[256], ErrorMsg[256];
	double E_QM_Min, E_MM_Min, E_MM_Read, E_MM_Cal;
	double Para_k_Dih_Save[6], Para_phi_Dih_Save[6];
	double x_Save[MAX_NUM_ATOM], y_Save[MAX_NUM_ATOM], z_Save[MAX_NUM_ATOM];
	int i, j, ReadItem, nAtom;

	memcpy(Para_k_Dih_Save, Mol_ESP.Para_k_Dih[IdxDihSelect[Idx]]+1, sizeof(double)*6);
	memcpy(Para_phi_Dih_Save, Mol_ESP.Para_phi[IdxDihSelect[Idx]]+1, sizeof(double)*6);
	memset(Mol_ESP.Para_k_Dih[IdxDihSelect[Idx]]+1, 0, sizeof(double)*6);	// turn of this dihedral
	memset(Mol_ESP.Para_phi[IdxDihSelect[Idx]]+1, 0, sizeof(double)*6);

	sprintf(szName, "tor-1D-idx-%d.dat", Idx+1);
	fIn = fopen(szName, "r");
	if(fIn == NULL)	{
		sprintf(ErrorMsg, "Fail to open file: %s\nQuit\n", szName);
		printf("%s", ErrorMsg);
		Quit_With_Error_Msg(ErrorMsg);
	}

	sprintf(szName, "mm-tor-1D-idx-%d.dat", Idx+1);	// the one with optimized geometry in MM force field
	fOut = fopen(szName, "w");

	nAtom = Mol_ESP.nAtom;
	nScan = 0;
	while(1)	{
		if(feof(fIn))	{
			break;
		}
		ReadLine = fgets(szLine, 256, fIn);
		if(ReadLine == NULL)	{
			break;
		}
		if(strncmp(szLine, "E_Scan", 6)==0)	{
			ReadItem = sscanf(szLine, "%s %lf %lf %s %lf", szTmp, &(E_QM_Scaned[nScan]), &E_MM_Read, szTmp, &(Phi_Scaned[nScan]));


			if(ReadItem != 5)	{
				sprintf(ErrorMsg, "Error in reading file: %s\nQuit\n", szName);
				Quit_With_Error_Msg(ErrorMsg);
			}

			ReadLine = fgets(szLine, 256, fIn);	// skip one line, "Coordinate"

			for(i=0; i<nAtom; i++)	{	// read one snapshot
				ReadLine = fgets(szLine, 256, fIn);
				ReadItem = sscanf(szLine, "%lf %lf %lf", &(Mol_ESP.x[i]), &(Mol_ESP.y[i]), &(Mol_ESP.z[i]));
			}
			E_MM_Cal = Mol_ESP.Cal_E(0);	// the nergy without optimization and dihedral term is turned off

			To_Setup_All_Dihedral_Constraint();
			E_MM_Scaned[nScan] = Geoometry_Optimization_With_Constraint(&Mol_ESP);	// do restrained geometry optimizatio. soft restraint on all dihedrals. Rigid restraints on soft dihedrals

			if(E_MM_Scaned[nScan] > (1.0E3+E_MM_Cal))	{	// not a valid configuration. Something wrong in the constrained optimization
        printf(" Bad configuration: %d %f %f\n", nScan, E_MM_Scaned[nScan], E_MM_Cal );
				continue;
			}

			memcpy(x_Save, Mol_ESP.x, sizeof(double)*nAtom);
			memcpy(y_Save, Mol_ESP.y, sizeof(double)*nAtom);
			memcpy(z_Save, Mol_ESP.z, sizeof(double)*nAtom);
			
			fprintf(fOut, "E_Scan %.13E %.13E Phi %.1lf\n", E_QM_Scaned[nScan], E_MM_Scaned[nScan], Phi_Scaned[nScan]);
			fprintf(fOut, "Coordinate\n");
			for(j=0; j<nAtom; j++)	{
				fprintf(fOut, "%14.6lf%14.6lf%14.6lf\n", x_Save[j], y_Save[j], z_Save[j]);
			}
			//		fprintf(fOut, "%.1lf %.6lf %.6lf %.6lf\n", Phi_Scaned[i], E_QM_Scaned[i], E_MM_Scaned[i], E_QM_Scaned[i] - E_MM_Scaned[i]);
			

			nScan++;
		}

	}


	fclose(fOut);

	fclose(fIn);

	E_QM_Min = E_MM_Min = 1.0E100;
	for(i=0; i<nScan; i++)	{
		if(E_QM_Min > E_QM_Scaned[i])	{
			E_QM_Min = E_QM_Scaned[i];
		}
		if(E_MM_Min > E_MM_Scaned[i])	{
			E_MM_Min = E_MM_Scaned[i];
		}
	}

	for(i=0; i<nScan; i++)	{
		E_QM_Scaned[i] = (E_QM_Scaned[i]-E_QM_Min)*HARTREE_To_KCAL;
		E_MM_Scaned[i] -= E_MM_Min;
		E_QM_MM_Diff[i] = E_QM_Scaned[i]-E_MM_Scaned[i];

		if(E_MM_Scaned[i] > 60.0)	{	// an unusual large MM energy
			w_Point[i] = 0.0;
		}
		else	{
			w_Point[i] = 1.0;
		}
	}

	printf(" There are %d conformers\n", nScan );
	for(i=0;i<nScan;i++) {
		printf("%5d %8.3f %8.3f %8.3f %8.3f %d\n", i, Phi_Scaned[i], E_QM_Scaned[i], E_MM_Scaned[i], E_QM_MM_Diff[i], w_Point[i]==1.0 );
	}


/*
	sprintf(szName, "qm-mm-diff-%d.dat", Idx+1);
	fOut = fopen(szName, "w");
	for(i=0; i<nScan; i++)	{
		E_QM_Scaned[i] = (E_QM_Scaned[i]-E_QM_Min)*HARTREE_To_KCAL;
		E_MM_Scaned[i] -= E_MM_Min;
		fprintf(fOut, "%.1lf %.6lf %.6lf\n", Phi_Scaned[i], E_QM_Scaned[i], E_QM_Scaned[i]-E_MM_Scaned[i]);
//		fprintf(fOut, "%.1lf %.12e %.6lf %.6lf\n", Phi_Scaned[i], E_QM_Scaned[i]);
//		fprintf(fOut, "%.1lf %.6lf %.6lf %.6lf\n", Phi_Scaned[i], E_QM_Scaned[i], E_MM_Scaned[i], E_QM_Scaned[i] - E_MM_Scaned[i]);
	}
	fclose(fOut);
*/


	memcpy(Mol_ESP.Para_k_Dih[IdxDihSelect[Idx]]+1, Para_k_Dih_Save, sizeof(double)*6);
	memcpy(Mol_ESP.Para_phi[IdxDihSelect[Idx]]+1, Para_phi_Dih_Save, sizeof(double)*6);
}
void Reorganize_QM_1D_Scan_Data(void)
{
	FILE *fIn, *fOut, *fQMStates;
	int i, j, i_Left, i_Right, Idx_Phi, nScan, nScan_Mapped, ReadItem, nAtom, To_Output[MAX_SCAN];
	char szName_Input[256], szName_Output[256], ErrorMsg[256], szLine[256], *ReadLine, szTmp[256];
	double x_List[MAX_SCAN][MAX_ATOM], y_List[MAX_SCAN][MAX_ATOM], z_List[MAX_SCAN][MAX_ATOM];
	double E_QM[MAX_SCAN], E_MM[MAX_SCAN], Phi[MAX_SCAN], Phi_Mapped[MAX_SCAN], E_QM_Mapped[MAX_SCAN], E_Min, E_Max, Phi_Ini, fTmp=0.0;
	int N_Rotamer_Total=1, nState, DoubleBond;

	RestoreCoordinates();

	nAtom = Mol.nAtom;
	fQMStates = fopen("qm-1d-states.dat", "w");
	for(Idx_Phi=0; Idx_Phi<n_Phi; Idx_Phi++)	{
		sprintf(szName_Input, "tor-1D-idx-%d.dat", Idx_Phi+1);
		sprintf(szName_Output, "new-tor-1D-idx-%d.dat", Idx_Phi+1);

		E_Min = 1.0E100;
		E_Max = -1.0E100;
		Phi_Ini = Mol.QueryDihedral(IdxDihSelect[Idx_Phi]);
		DoubleBond = Is_This_A_Double_Bond(DihList[Idx_Phi][1], DihList[Idx_Phi][2]);

		nScan = 0;
		fIn = fopen(szName_Input, "r");
		if(fIn == NULL)	{
			sprintf(ErrorMsg, "Fail to open file for read: %s\nQuit\n", szName_Input);
			Quit_With_Error_Msg(ErrorMsg);
		}
		while(1)	{
			if(feof(fIn))	{
				break;
			}
			ReadLine = fgets(szLine, 256, fIn);
			if(ReadLine == NULL)	{
				break;
			}
			if(strncmp(szLine, "E_Scan", 6) == 0)	{
				ReadItem = sscanf(szLine, "%s %lf %lf %s %lf", szTmp, &(E_QM[nScan]), &(E_MM[nScan]), szTmp, &(Phi[nScan]));
				if(ReadItem == 5)	{
					fgets(szLine, 256, fIn);
					for(i=0; i<nAtom; i++)	{
						fgets(szLine, 256, fIn);
						ReadItem = sscanf(szLine, "%lf %lf %lf", &(x_List[nScan][i]), &(y_List[nScan][i]), &(z_List[nScan][i]));
						if(ReadItem != 3)	{
							fclose(fIn);
							sprintf(ErrorMsg, "Error in readubg file: %s\nQuit\n", szName_Input);
							Quit_With_Error_Msg(ErrorMsg);
						}
					}
					if(E_QM[nScan] < E_Min)	{
						E_Min = E_QM[nScan];
					}
					if(E_QM[nScan] > E_Max)	{
						E_Max = E_QM[nScan];
					}


					nScan++;
				}
				else	{
					break;
				}
			}
		}
		fclose(fIn);


		for(i=0; i<nScan; i++)	{
			To_Output[i] = 1;
		}

		if( (Phi[0] > 0.0) && (Cal_Phi_Dist(Phi[0], -180.0) < 1.0) )	{	// +180.0 at the first point
			Phi[0] -= 360.0;
		}

		//start	to delete those redundant entries
		for(i=0; i<nScan; i++)	{
			for(j=i+1; j<nScan; j++)	{
				if(Cal_Phi_Dist(Phi[i], Phi[j]) < 1.0)	{	// the same dihedral
					if(Cal_Phi_Dist(Phi[j], -180.0) > 1.0)	{	// only -180, 180 can exist, others will be removed
						To_Output[j] = 0;
					}
				}
			}
		}
		//end	to delete those redundant entries

		//start	to set the correct sign of +180 if it exists as -180
		if(Cal_Phi_Dist(Phi[0], -180.0) < 1.0)	{	// starting with -180
			for(i=nScan-1; i>0; i--)	{
				if(To_Output[i] == 0)	{
					continue;
				}
				
				if( (Phi[i] < 0.0) && (Cal_Phi_Dist(Phi[i], -180.0) < 1.0) )	{
					Phi[i] += 360.0;
//					break;
				}
			}
		}
		//end	to set the correct sign of +180 if it exists as -180

//		fOut = fopen(szName_Output, "w");
		fOut = fopen(szName_Input, "w");	// to overwrite 
		for(j=0; j<nScan; j++)	{
			if(To_Output[j]==0)	{
				continue;
			}
			fprintf(fOut, "E_Scan %.13E %.13E  Phi  %.1lf\n", E_QM[j], E_MM[j], Phi[j]);
			fprintf(fOut, "Coordinate\n");
			for(i=0; i<nAtom; i++)	{
				fprintf(fOut, "%12.6lf %12.6lf %12.6lf\n", x_List[j][i], y_List[j][i], z_List[j][i]);
			}
		}
		
		fclose(fOut);

		//start	to identify QM local minima of each dihedral
		nScan_Mapped = 0;
		for(i=0; i<nScan; i++)	{
			if(To_Output[i])	{
				if(fabs(Phi[i]-180.0) > 3.0)	{	// +180 is excluded
					Phi_Mapped[nScan_Mapped] = Phi[i];
					E_QM_Mapped[nScan_Mapped] = E_QM[i];
					nScan_Mapped++;
				}
			}
		}
		if(nScan_Mapped < 3)	{
			sprintf(ErrorMsg, "nScan_Mapped < 3 for dihedral %d \nQuit\n", Idx_Phi+1);
			Quit_With_Error_Msg(ErrorMsg);
		}
		fprintf(fQMStates, "%3d %3d %3d %3d ", DihList[Idx_Phi][0]+1, DihList[Idx_Phi][1]+1, DihList[Idx_Phi][2]+1, DihList[Idx_Phi][3]+1);
		nState = 0;
		for(i=0; i<nScan_Mapped; i++)	{
			i_Left = (i+nScan_Mapped-1)%nScan_Mapped;
			i_Right = (i+nScan_Mapped+1)%nScan_Mapped;

			if( (DoubleBond) && ( ((E_Max-E_Min)*HARTREE_KCAL) > E_BARRIER_DOUBLE_BOND ) )	{
				fprintf(fQMStates, "  %.0lf", Phi_Ini);
				nState = 1;
				break;
			}

			if( (E_QM_Mapped[i] <= E_QM_Mapped[i_Left]) && (E_QM_Mapped[i] <= E_QM_Mapped[i_Right]) )	{	// a local minimum along QM 1D profile
				fprintf(fQMStates, "  %.0lf", Phi_Mapped[i]);
				nState++;
			}
		}
		fprintf(fQMStates, "\n");
		N_Rotamer_Total *= nState;
		//end	to identify QM local minima of each dihedral
	
	}

	fclose(fQMStates);

	fOut = fopen("n_rotamer.txt", "w");
	fprintf(fOut, "%d", N_Rotamer_Total);
	fclose(fOut);
}
Exemple #24
0
double CalObjectiveFunction(double Para_List[], int Get_E_Rotamer)
{
	int i, j, nAtom, time_1, time_2;
	double d_E, E_Shift, w_1D;
	double Chi_SQ_Rotamer_Local;
	double E_Rotamer_MM_Local[MAX_ROTAMER];
	double E_Min_1D_MM_Mean, E_Min_1D_QM_Mean, E_Min_1D_QM_MM_Shift, Weight_Sum;

	Chi_SQ = Chi_SQ_1D = Chi_SQ_Rotamer = 0.0;
	Chi_SQ_Rotamer_Local = 0.0;

	w_1D = 1.0 - w_Rotamer;

	time_1 = time(NULL);

	Distribute_Torsion_Parameters();
	Assign_Torsion_Parameters();

	E_Shift = Para_List[10*n_Phi];

	nAtom = Mol_ESP.nAtom;
	for(i=0; i<n_Phi; i++)	{
		for(j=0; j<nScan_List[i]; j++)	{
			memcpy(Mol_ESP.x, x_Scan_list[i][j], sizeof(double)*nAtom);
			memcpy(Mol_ESP.y, y_Scan_list[i][j], sizeof(double)*nAtom);
			memcpy(Mol_ESP.z, z_Scan_list[i][j], sizeof(double)*nAtom);
			E_Scan_MM[i][j] = Mol_ESP.Cal_E(0);
		}

		E_Min_1D_MM_Mean = E_Min_1D_QM_Mean = 0.0;
		Weight_Sum = 0.0;
		for(j=0; j<nScan_List[i]; j++)	{
			Weight_Sum += w_Scan_List[i][j];
			E_Min_1D_MM_Mean += (w_Scan_List[i][j] * E_Scan_MM[i][j] );
			E_Min_1D_QM_Mean += (w_Scan_List[i][j] * E_Scan_QM[i][j] );
		}
		E_Min_1D_QM_MM_Shift = (E_Min_1D_QM_Mean - E_Min_1D_MM_Mean)/Weight_Sum;

		for(j=0; j<nScan_List[i]; j++)	{
			d_E = (E_Scan_QM[i][j] - E_Scan_MM[i][j] - E_Min_1D_QM_MM_Shift);	// fit the relative 1D energy profile
			Chi_SQ_1D += (d_E*d_E*w_Scan_List[i][j]);
		}
	}

	
	Chi_SQ_1D *= (w_1D/w_Scan_Sum);


	for(i=0; i<nRotamer; i++)	{
		E_Rotamer_MM[i] = E_Rotamer_MM_Local[i] = 0.0;
		if(i%nProc != ProgID)	{	//only proceed when i%nProc == ProgID, do job decomposition
			continue;
		}
		memcpy(Mol_ESP.x, x_Rotamer_Opt[i], sizeof(double)*nAtom);
		memcpy(Mol_ESP.y, y_Rotamer_Opt[i], sizeof(double)*nAtom);
		memcpy(Mol_ESP.z, z_Rotamer_Opt[i], sizeof(double)*nAtom);
		Mol_ESP.FullGeometryOptimization_LBFGS_step(1);
		E_Rotamer_MM_Local[i] = Mol_ESP.Cal_E(0);
		d_E = E_Rotamer_QM[i] - E_Rotamer_MM_Local[i] - E_Shift;
		Chi_SQ_Rotamer_Local += (d_E*d_E*w_rotamer_List[i]);
	}
	if(Get_E_Rotamer)	{
//eliot (from Matt Harvey)
	for( int i = 0 ; i < nRotamer; i++ ) { E_Rotamer_MM[i] += E_Rotamer_MM_Local[i]; }
                Chi_SQ_Rotamer = Chi_SQ_Rotamer_Local;

//eliot		MPI_Allreduce(E_Rotamer_MM_Local, E_Rotamer_MM, nRotamer, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
	}
//eliot	MPI_Allreduce(&Chi_SQ_Rotamer_Local, &Chi_SQ_Rotamer, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
//	MPI_Bcast(&Chi_SQ_Rotamer, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);

	printf(" w_rotamer_Sum = %f\n", w_rotamer_Sum );

	Chi_SQ_Rotamer *= (w_Rotamer/w_rotamer_Sum);

	Chi_SQ = Chi_SQ_1D + Chi_SQ_Rotamer;

//	MPI_Barrier(MPI_COMM_WORLD);
//	printf("%lf\n", Chi_SQ);
//	MPI_Finalize();

	time_2 = time(NULL);
/*
	if(ProgID == 0)	{
		FILE *fOut;
		fOut = fopen("time-rec.txt", "a+");
		fseek(fOut, 0, SEEK_END);
		fprintf(fOut, "%d\n", time_2-time_1);
		fclose(fOut);
	}
*/
	printf("Chi_SQ=%f\n", Chi_SQ );
	return Chi_SQ;
}
double Geoometry_Optimization_With_Constraint(CMol *pMol)
{

	return (pMol->Cal_E(0));


	nlopt_opt opt, opt_AugLag;
	int Return_Opt, nAtom, nDim, i, iPos;
	double E_min, x[MAX_NUM_ATOM*3];

	Mol_ESP.Restrain_All_Torsions(1);	// apply soft restraints on all soft dihedrals

	nAtom = pMol->nAtom;
	nDim = 3 * nAtom;

	printf("\nMM before:\t");
	for(i=0; i<n_Phi; i++)	{
		printf("%3d %3d %3d %3d %8.4lf\n", DihList[i][0], DihList[i][1], DihList[i][2], DihList[i][3], 
			Mol_ESP.Query_Dihedral(DihList[i][0], DihList[i][1], DihList[i][2], DihList[i][3], 0, NULL));
	}

	double AUG_LAG_ICM_TOL = 1.0E-17;
	opt_AugLag = nlopt_create(NLOPT_LD_LBFGS_GEO, nDim);
	nlopt_set_min_objective(opt_AugLag, func_Geo_Opt_Constraint, pMol);

//	pMol->SavePdb("opt-free.pdb");

	for(i=0; i<pMol->nBond_Fixed; i++)	{
		int ret=0;
		pMol->Geo_Fix_r0[i].pMol = pMol;
		ret =nlopt_add_equality_constraint(opt_AugLag, Geo_Constraint_Bond, &(pMol->Geo_Fix_r0[i]), 2.0E-6);
		printf(" Fixing bond %d\n", i );
	}

	for(i=0; i<pMol->nAngle_Fixed; i++)	{
		int ret=0;
		pMol->Geo_Fix_theta0[i].pMol = pMol;
		ret=nlopt_add_equality_constraint(opt_AugLag, Geo_Constraint_Angle, &(pMol->Geo_Fix_theta0[i]), 2.0E-6);
		printf(" Fixing angle %d\n", i );
	}

	for(i=0; i<pMol->nPhi_Fixed; i++)	{
		int ret=0;
		pMol->Geo_Fix_phi0[i].pMol = pMol;
		ret=nlopt_add_equality_constraint(opt_AugLag, Geo_Constraint_Dihedral, &(pMol->Geo_Fix_phi0[i]), 2.0E-6);
		printf(" Fixing phi %d : %d\n", i, ret );
	}


	nlopt_set_xtol_rel(opt_AugLag, 2E-7);
	
	
//	opt = nlopt_create(NLOPT_LD_LBFGS_GEO, nDim);
//	opt = nlopt_create(NLOPT_LN_COBYLA, nDim);
//	nlopt_set_min_objective(opt, func_Geo_Opt_Constraint, pMol);
//	nlopt_set_xtol_rel(opt, 1E-7);

//	nlopt_set_local_optimizer(opt_AugLag, opt);
//	nlopt_destroy(opt);

	for(i=0; i<nAtom; i++)	{
		iPos = 3*i;
		x[iPos  ] = pMol->x[i];
		x[iPos+1] = pMol->y[i];
		x[iPos+2] = pMol->z[i];
	}
	
	Iter_Opt_Constrained = 0;
	Return_Opt = nlopt_optimize(opt_AugLag, x, &E_min);
	if (Return_Opt < 0) {
		printf("nlopt failed : %d\n", Return_Opt);
	}
//	else {
//		printf("After constrained optimization, E = %12.6lf\n", E_min);
//	}

	nlopt_destroy(opt_AugLag);

	printf("MM after:\t");
	for(i=0; i<n_Phi; i++)	{
		printf("%3d %3d %3d %3d %8.4lf\n", DihList[i][0], DihList[i][1], DihList[i][2], DihList[i][3], 
			Mol_ESP.Query_Dihedral(DihList[i][0], DihList[i][1], DihList[i][2], DihList[i][3], 0, NULL));
	}

	Mol_ESP.Restrain_All_Torsions(0);	// lift soft restraints on all soft dihedrals

	return (pMol->Cal_E(0));
}
Exemple #26
0
int main(int argc, char **argv)
{
	char ErrorMsg[256];

  timebomb();

	fFile_Run_Log = fopen(szName_Run_Log, "w");
	if(fFile_Run_Log==NULL)	{
		sprintf(ErrorMsg, "Fail to create the log file.\n\n");
		Quit_With_Error_Msg(ErrorMsg);
	}

	strcpy(szName_Conf_File, argv[1]);
	ReadConfFile(szName_Conf_File);

//eliot	MPI_Init(&argc, &argv);
//	MPI_Comm_rank(MPI_COMM_WORLD, &ProgID);
//	MPI_Comm_size(MPI_COMM_WORLD, &nProc);
	

	ForceField.ReadForceField(szName_Force_Field);
	
	Mol_ESP.ReadPSF(szName_XPSF, 0);
	Read_Rtf_File();
	
	strcpy(Mol_ESP.MolName, Mol_ESP.ResName[0]);
	Mol_ESP.AssignForceFieldParameters(&ForceField);

	Read_Soft_DihedralList();

	Mol_ESP.Is_Phi_Psi_Constrained = 0;
	Mol_ESP.E_CMap_On = 0;

	Read_QM_Rotamer_Data();

  if( nRotamer == 0 ) {
    printf("No rotamers found. The should be at least one in all-rotamer.dat. Something has gone wrong in the previous step\n" );
		exit(0);
  }

	Read_Tor_Para_1D_Fitting();
	Assign_Torsion_Parameters();

	Read_1D_Scan_QM_Data();

//	Cal_E_MM_Scaned();
//	Cal_E_MM_Rotamer();

	fFitting = fopen("fitting.dat", "w");
	Fitting_Torsion_Parameters();
	fclose(fFitting);

//	FILE *fOut;
//	fOut = fopen("rotamer-E.dat", "w");
//	for(i=0; i<nRotamer; i++)	{
//		fprintf(fOut, "%d %lf %lf %lf\n", i+1, E_Rotamer_QM[i], E_Rotamer_MM[i], rmsd_Rotamer[i]);
//	}
//	fclose(fOut);

//	Cal_E_MM_QM_Diff(7);
	
//	for(i=0; i<n_Phi; i++)	{
//		Cal_E_MM_QM_Diff(i);
//	}

//	Fit_Torsion_Parameters(7);

//	fPara = fopen("torsion-para.dat", "w");
//	for(i=0; i<n_Phi; i++)	{
//		Fit_Torsion_Parameters(i);
//	}
//	fclose(fPara);


//	Geoometry_Optimization_With_Constraint(&Mol_ESP);

	fflush(fFile_Run_Log);
	

	fclose(fFile_Run_Log);

//	MPI_Finalize();
	
	return 0;
}
int main(int argc, char **argv)
{
	int i;
	FILE *fOut;
	char szName[256], *szEnv;

//	SetEnvironmentVariable(szGAUSS_SCRDIR, "c:\\1");

	fFile_Run_Log = fopen("qm-1d-scan.log", "w");
	
	szEnv = getenv(szGAUSS_SCRDIR);
	if (szEnv == NULL) {
		Quit_With_Error_Msg("Environment variable $GAUSS_SCRDIR is NOT set.\nQuit\n");
	}
	strcpy(szGAUSS_SCRDIR_Base, szEnv);
	


	Get_EXE_Path("G09_EXE_PATH", szExe_G09);

	n_Bins = 360/BIN_SIZE+1;


	ForceField.ReadForceField(szForceFiled);
	Mol.ReadPSF(szXpsfFile, 0);
	Get_Netcharge_From_Xpsf();
	Setup_QM_Level();
	ReadElementList();

	Mol.AssignForceFieldParameters(&ForceField);
	Mol.ReadCRD(szCrdFile);
	BackupCoordinates();

	Read_Soft_DihedralList();

	nJob = nJobDone = 0;

	for(Active_Phi=0; Active_Phi<n_Phi; Active_Phi++)	{
		RestoreCoordinates();
		for(i=0; i<n_Phi; i++)	{
			Phi_To_Set[i] = Mol.QueryDihedral(IdxDihSelect[i]);
			Mol.Edit_Dihedral(IdxDihSelect[i], Phi_To_Set[i]);
		}

		MM_Fixed_1D_Scan();
		E_Barrier_Lowest = Get_Barrier(E_Phi, n_Bins, E_Min, Phi_Save);
		E_Min_Save = E_Min;
		E_Min_Org = E_Min + E_RANGE;
		memcpy(E_Scan, E_Phi, sizeof(double)*n_Bins);
		memcpy(Phi_To_Set_Scan, Phi_To_Set, sizeof(double)*n_Phi);
		Phi_To_Set_Scan[Active_Phi] = Phi_Save;

		sprintf(szName, "ini-%d.dat", Active_Phi+1);
		fOut = fopen(szName, "w");
		for(i=0; i<n_Bins; i++)	{
			fprintf(fOut, "%5d %8.5E\n", -180+i*BIN_SIZE, E_Scan[i]-E_Min);
		}
		fclose(fOut);

		sprintf(szName, "ini-%d.pdb", Active_Phi+1);
		SaveOptPdb(szName);

		n_State_List[Active_Phi] = 1;
		Enumerate_Dihedrals(0);
		n_State_List[Active_Phi] = n_State_List_Save[Active_Phi];

		sprintf(szName, "end-%d.dat", Active_Phi+1);
		fOut = fopen(szName, "w");
		for(i=0; i<n_Bins; i++)	{
			fprintf(fOut, "%5d %8.5E\n", -180+i*BIN_SIZE, E_Scan[i]-E_Min_Save);
		}
		fclose(fOut);

		sprintf(szName, "end-%d.pdb", Active_Phi+1);
		SaveOptPdb(szName);

		sprintf(szName, "phi-%d.dat", Active_Phi+1);
		fOut = fopen(szName, "w");
		for(i=0; i<n_Phi; i++)	{
			fprintf(fOut, "%lf\n", Phi_To_Set_Scan[i]);
		}
		fclose(fOut);

	
		Output_Gaussian_File();
	}

	if(nJob > 0)	{
		RunAllJobs();
		Exatract_All_Info_QM_1D_Scan();
		Reorganize_QM_1D_Scan_Data();
	}

	fclose(fFile_Run_Log);

	return 0;
}