⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testeso1doc.cpp

📁 利用渐进结构优化算法(ESO)实现矩形板的第一阶频率的最大化。用VC实现界面
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	CString strBDF;
	strBDF.Format(_T("%s\\CurrentStruct.bdf"),GetModulePath());
	CString strLocationOfNastran(_T("C:\\MSC.Software\\MSC.Nastran\\bin\\nastran.exe"));
	CString strCmd;

	strCmd.Format(_T("\"%s\" \"%s\""),strLocationOfNastran, strBDF);

	STARTUPINFO si;
	::ZeroMemory (&si, sizeof (STARTUPINFO));
	si.cb = sizeof (STARTUPINFO);
	PROCESS_INFORMATION pi;

	if (::CreateProcess (NULL, (TCHAR*)(LPCTSTR)strCmd,	NULL, NULL,	FALSE,
		NORMAL_PRIORITY_CLASS, NULL, (TCHAR*)(LPCTSTR)strPathUnder, &si, &pi))
	{
		::CloseHandle (pi.hThread);
		::WaitForSingleObject (pi.hProcess, INFINITE);
		::CloseHandle (pi.hProcess);
	}
}

CString CTestESO1Doc::GetModulePath()
{
	TCHAR lpFilename[MAX_PATH];

	GetModuleFileName(AfxGetApp()->m_hInstance, lpFilename, MAX_PATH);
	CString strModuleFilePath(lpFilename);
	int i = strModuleFilePath.ReverseFind('\\');
	strModuleFilePath = strModuleFilePath.Left(i);
	return strModuleFilePath;

}

void CTestESO1Doc::DeleteAllIterationSubDir()
{
	int i;
	for(i=0; i<=m_nIteration; i++)
	{
		CString strIterationSubDir;
		strIterationSubDir.Format(_T("%s\\Iteration %d"), GetModulePath(), i);
		CString strFile;
		HANDLE  hFind;
		WIN32_FIND_DATA fd;
		strFile.Format(_T("%s\\*.*"), strIterationSubDir);

		
		if((hFind=::FindFirstFile(strFile, &fd)) != INVALID_HANDLE_VALUE)
		{
			CString strFileToDelete;
			if(!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
			{
				strFileToDelete.Format(_T("%s\\%s"),strIterationSubDir,fd.cFileName);
				CFile::Remove(strFileToDelete);
			}
			
			while(::FindNextFile(hFind, &fd))
			{
				if(!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
				{
					strFileToDelete.Format(_T("%s\\%s"),strIterationSubDir,fd.cFileName);
					CFile::Remove(strFileToDelete);
				}
			}
			::FindClose(hFind);
		}
		::RemoveDirectory(strIterationSubDir);
	}

}

void CTestESO1Doc::GetSettings()
{
	CString strFileSettings;
	strFileSettings.Format(_T("%s\\Settings.set"),GetModulePath());
	CFile  FileSettings;

	if(!FileSettings.Open(strFileSettings, CFile::modeRead))
	{
		AfxMessageBox(_T("无法获取设置!"));
		return;
	}
	CArchive archive(&FileSettings, CArchive::load);
	archive>>m_nIteration;
	archive.Close();

}

void CTestESO1Doc::PutSettings()
{
	CString strFileSettings;
	strFileSettings.Format(_T("%s\\Settings.set"),GetModulePath());

	HANDLE hFile = CreateFile(strFileSettings, GENERIC_WRITE, FILE_SHARE_READ,
		NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

	CFile  FileSettings((int)hFile);
	CArchive archive(&FileSettings, CArchive::store);
	archive<<m_nIteration;
	archive.Close();
}

void CTestESO1Doc::GetNodesDisplacements(CString strCurrentPath)
{
	//Bind f06 to a CArchive object

	CString strFilef06;
	strFilef06.Format(_T("%s\\currentstruct.f06"),strCurrentPath);
	CFile  Filef06;

	if(!Filef06.Open(strFilef06, CFile::modeRead))
	{
		AfxMessageBox(_T("无法获取f06文件!"));
		return;
	}
	CArchive arf06(&Filef06, CArchive::load);

	CString strOneLine;
	while(arf06.ReadString(strOneLine))
	{
		strOneLine.TrimLeft();
		strOneLine.TrimRight();

		// Find eigenvalues
		if(strOneLine==_T("R E A L   E I G E N V A L U E S"))
		{
			// Eat two lines
			arf06.ReadString(strOneLine);
			arf06.ReadString(strOneLine);

			// Get the third value
			int nIndex;
			arf06.ReadString(strOneLine);
			for(int i=0; i<2;i++)
			{
				strOneLine.TrimLeft();
				nIndex=strOneLine.Find(' ');
				strOneLine=strOneLine.Mid(nIndex);
			}
			strOneLine.TrimLeft();
			nIndex=strOneLine.Find(' ');
			CString strEigen;
			strEigen=strOneLine.Mid(0,nIndex);
			m_dCurrentEigen=::atof(strEigen);
			break;
		}
	}

	while(arf06.ReadString(strOneLine))
	{
		if(strOneLine.Find(_T("POINT ID."))!=-1)
		{
			while(arf06.ReadString(strOneLine))
			{
				if(strOneLine.Find(_T("PAGE"))!=-1)
					break;
								
				int nIndex;
				strOneLine.TrimLeft();
				nIndex=strOneLine.Find(' ');
				CString strNodeID;
				strNodeID=strOneLine.Mid(0,nIndex);
				strOneLine=strOneLine.Mid(nIndex);

				CObject* pObject;
				m_mapNodeIDToNode.Lookup(WORD(::atoi(strNodeID)), pObject);
				CNode* pNode=(CNode*)pObject;

				// Get rid of TYPE
				strOneLine.TrimLeft();
				nIndex=strOneLine.Find(' ');
				strOneLine=strOneLine.Mid(nIndex);

				// Get T1
				strOneLine.TrimLeft();
				nIndex=strOneLine.Find(' ');
				CString strT1;
				strT1=strOneLine.Mid(0,nIndex);
				pNode->m_dXDisplacement=::atof(strT1);
				strOneLine=strOneLine.Mid(nIndex);

				// Get T2
				strOneLine.TrimLeft();
				nIndex=strOneLine.Find(' ');
				CString strT2;
				strT2=strOneLine.Mid(0,nIndex);
				pNode->m_dYDisplacement=::atof(strT2);
			}
			// Start a new page
		}
	}
}

void CTestESO1Doc::ManipulateSensitivity()
{
	CString strCmd;
	POSITION  pos;
	pos=m_mapElementIDToElement.GetStartPosition();

	strCmd.Format(_T("ES = zeros(%d,2);"), m_mapElementIDToElement.GetCount()-m_nTotNumElementNotDeletable);
	engEvalString(m_ep, strCmd);
	int i, j=0;

	while (pos !=NULL)
	{
		WORD nIndex;
		CObject* pObject;
		CElement* pEle;
		m_mapElementIDToElement.GetNextAssoc(pos, nIndex, pObject);
		pEle=(CElement*)pObject;
		if(!pEle->m_bCanbeDeleted)
			continue;

		double dsensitivity;
		CaculateSpecifiedEleSensitivity((int)nIndex, &dsensitivity);
		j++;
		strCmd.Format(_T("ES(%d,1)=%d;"), j,(int)nIndex);
		engEvalString(m_ep, strCmd);
		strCmd.Format(_T("ES(%d,2)=%f;"), j,dsensitivity);
		engEvalString(m_ep, strCmd);
	}



//	for(i=1; i<=m_mapElementIDToElement.GetCount(); i++)
//	{
//		if(!CaculateSpecifiedEleSensitivity(i, &dsensitivity))
//			continue;
//	}

	ASSERT(j+m_nTotNumElementNotDeletable==m_mapElementIDToElement.GetCount());
	strCmd.Format(_T("SES=SORTROWS(ES,[-2]);SESCOL1=SES(:,1)';SESCOL2=SES(:,2)';"));
	engEvalString(m_ep, strCmd);

	mxArray *T1,*T2;
	int nCols;
	T1=engGetVariable(m_ep,_T("SESCOL1"));
	T2=engGetVariable(m_ep,_T("SESCOL2"));
	nCols=mxGetN(T1);
	double *pd1, *pd2;
	pd1=mxGetPr(T1);
	pd2=mxGetPr(T2);

	m_strInfo.Empty();
	// To control the number of elements to be deleted each time
	for(i=0; i<NUMBER_OF_ELEMENTS_TO_DELETE && i<nCols;i++)
	{
		int nEleID=(int)pd1[i];
		RemoveSpecifiedElement(nEleID);
		m_mapElementIDToElement.RemoveKey((WORD)nEleID);
	}
	mxDestroyArray(T1);
	mxDestroyArray(T2);
	engEvalString(m_ep, _T("clear ES;"));
	engEvalString(m_ep, _T("clear SES;"));
	engEvalString(m_ep, _T("clear SESCOL1;"));
	engEvalString(m_ep, _T("clear SESCOL2;"));

}

void CTestESO1Doc::PutKMMatrix()
{
	double dStiffnessMx[8][8]={
	40.63644617,24.03846169,-2.174909767,4.807693481,-31.47893697,-24.03846169,-6.982599434,-4.807693481,
    24.03846169,90.43040393,-4.807693481,44.18498303,-24.03846169,-53.80036713,4.807693481,-80.81501983,
    -2.174909767,-4.807693481,40.63644617,-24.03846169,-6.982599434,4.807693481,-31.47893697,24.03846169,
    4.807693481,44.18498303,-24.03846169,90.43040393,-4.807693481,-80.81501983,24.03846169,-53.80036713,
    -31.47893697,-24.03846169,-6.982599434,-4.807693481,40.63644617,24.03846169,-2.174909767,4.807693481,
    -24.03846169,-53.80036713,4.807693481,-80.81501983,24.03846169,90.43040393,-4.807693481,44.18498303,
    -6.982599434,4.807693481,-31.47893697,24.03846169,-2.174909767,-4.807693481,40.63644617,-24.03846169,
    -4.807693481,-80.81501983,24.03846169,-53.80036713,4.807693481,44.18498303,-24.03846169,90.43040393};

	mxStiffness=mxCreateDoubleMatrix(8, 8, mxREAL);
	memcpy((void *)mxGetPr(mxStiffness), (void *)dStiffnessMx, sizeof(dStiffnessMx));
	engPutVariable(m_ep, "K", mxStiffness);

	double dMassMx[8][8]={
		2e-011,0,0,0,0,0,0,0,
		0,2e-011,0,0,0,0,0,0,
		0,0,2e-011,0,0,0,0,0,
		0,0,0,2e-011,0,0,0,0,
		0,0,0,0,2e-011,0,0,0,
		0,0,0,0,0,2e-011,0,0,
		0,0,0,0,0,0,2e-011,0,
		0,0,0,0,0,0,0,2e-011};

	mxMass=mxCreateDoubleMatrix(8, 8, mxREAL);
	memcpy((void *)mxGetPr(mxMass), (void *)dMassMx, sizeof(dMassMx));
	engPutVariable(m_ep, "M", mxMass);

/*	engEvalString(m_ep,
	_T("K=["
	"40.63644617 24.03846169 -2.174909767 4.807693481 -31.47893697 -24.03846169	-6.982599434 -4.807693481;"
    "24.03846169 90.43040393 -4.807693481 44.18498303 -24.03846169 -53.80036713	4.807693481	-80.81501983;"
    "-2.174909767 -4.807693481 40.63644617 -24.03846169	-6.982599434 4.807693481 -31.47893697 24.03846169;"
    "4.807693481 44.18498303 -24.03846169 90.43040393 -4.807693481 -80.81501983	24.03846169 -53.80036713;"
    "-31.47893697 -24.03846169 -6.982599434	-4.807693481 40.63644617 24.03846169 -2.174909767 4.807693481;"
    "-24.03846169 -53.80036713 4.807693481 -80.81501983	24.03846169	90.43040393	-4.807693481 44.18498303;"
    "-6.982599434 4.807693481 -31.47893697 24.03846169 -2.174909767	-4.807693481 40.63644617 -24.03846169;"
    "-4.807693481 -80.81501983 24.03846169 -53.80036713	4.807693481	44.18498303	-24.03846169 90.43040393"
	"];"));
*/	
}

BOOL CTestESO1Doc::CaculateSpecifiedEleSensitivity(int ElementID,double* pdSensitivity)
{
	double dElementK, dElementM;
	CObject  *pObject;
	CElement *pElement;
	mxArray *Vector = NULL, *ValueIK, *ValueIM;
	m_mapElementIDToElement.Lookup(WORD(ElementID), pObject);
	pElement=(CElement*)pObject;
	if(!pElement->m_bCanbeDeleted)
	{
		*pdSensitivity=0;
		return FALSE;
	}

	Vector = mxCreateDoubleMatrix(1, pElement->DimofDisplacementVector(), mxREAL);





/*	double time[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
	memcpy((void *)mxGetPr(Vector), (void *)time, pElement->DimofDisplacementVector()*sizeof(double));
	engPutVariable(m_ep, "ELEDISP", Vector);
	engEvalString(m_ep, _T("IK=ELEDISP*K*ELEDISP';"));
	engEvalString(m_ep, _T("IM=ELEDISP*M*ELEDISP';"));
	Value=engGetVariable(m_ep,_T("IK"));
	dElementK=*mxGetPr(Value);
	Value=engGetVariable(m_ep,_T("IM"));
	dElementM=*mxGetPr(Value);
*/

	memcpy((void *)mxGetPr(Vector), (void *)pElement->GetDisplacementVector(), pElement->DimofDisplacementVector()*sizeof(double));
	engPutVariable(m_ep, "ELEDISP", Vector);
	engEvalString(m_ep, _T("IK=ELEDISP*K*ELEDISP';"));
	engEvalString(m_ep, _T("IM=ELEDISP*M*ELEDISP';"));

	ValueIK=engGetVariable(m_ep,_T("IK"));
	dElementK=*mxGetPr(ValueIK);
	pElement->m_dModuleK=dElementK;

	ValueIM=engGetVariable(m_ep,_T("IM"));
	dElementM=*mxGetPr(ValueIM);
	pElement->m_dModuleM=dElementM;

	pElement->m_dSensitivity=m_dCurrentEigen*dElementM-dElementK;

	mxDestroyArray(Vector);
	mxDestroyArray(ValueIK);
	mxDestroyArray(ValueIM);
	engEvalString(m_ep,_T("clear ELEDISP"));
	engEvalString(m_ep,_T("clear IK"));
	engEvalString(m_ep,_T("clear IM"));

	*pdSensitivity=pElement->m_dSensitivity;
	return TRUE;




//	double p, *dp;
/*   T = mxCreateDoubleMatrix(1, 8, mxREAL);
   Result=engGetVariable(m_ep,_T("S"));

   dp=mxGetPr(Result);
   for(int i=0; i<8; i++)
   {
	   int j;
	   j++;
	   p=dp[i];
   }
*/

}
/*
void CTestESO1Doc::CaculateModuleMass()
{
	double p[1122];
	CObject  *pObject;
	CNode *pNode;
	mxArray *Vector = NULL;
	Vector = mxCreateDoubleMatrix(1, 1122, mxREAL);

	for(int i=0; i<561; i++)
	{
		m_mapNodeIDToNode.Lookup(WORD(i+1), pObject);
		pNode=(CNode*)pObject;
		p[2*i]=pNode->m_dXDisplacement;
		p[2*i+1]=pNode->m_dYDisplacement;
	}
	memcpy((void *)mxGetPr(Vector), (void *)p, sizeof(p));
	 

}
*/

void CTestESO1Doc::WriteInfomation(CString strCurrentPath)
{
	CString strCmd;
	POSITION  pos;
	pos=m_mapElementIDToElement.GetStartPosition();

	strCmd.Format(_T("ES = zeros(%d,2);"), m_mapElementIDToElement.GetCount()-m_nTotNumElementNotDeletable);
	engEvalString(m_ep, strCmd);
	int i, j=0;

	while (pos !=NULL)
	{
		WORD nIndex;
		CObject* pObject;
		CElement* pEle;
		m_mapElementIDToElement.GetNextAssoc(pos, nIndex, pObject);
		pEle=(CElement*)pObject;
		if(!pEle->m_bCanbeDeleted)
			continue;

		double dsensitivity;
		CaculateSpecifiedEleSensitivity((int)nIndex, &dsensitivity);
		j++;
		strCmd.Format(_T("ES(%d,1)=%d;"), j,(int)nIndex);
		engEvalString(m_ep, strCmd);
		strCmd.Format(_T("ES(%d,2)=%f;"), j,dsensitivity);
		engEvalString(m_ep, strCmd);
	}

	ASSERT(j+m_nTotNumElementNotDeletable==m_mapElementIDToElement.GetCount());
	strCmd.Format(_T("SES=SORTROWS(ES,[-2]);SESCOL1=SES(:,1)';SESCOL2=SES(:,2)';"));
	engEvalString(m_ep, strCmd);

	mxArray *T1,*T2;
	int nCols;
	T1=engGetVariable(m_ep,_T("SESCOL1"));
	T2=engGetVariable(m_ep,_T("SESCOL2"));
	nCols=mxGetN(T1);
	double *pd1, *pd2;
	pd1=mxGetPr(T1);
	pd2=mxGetPr(T2);

	CString strFile;
	strFile.Format(_T("%s\\Infomation.txt"),strCurrentPath);
	CFile  FileInfo;

	if(!FileInfo.Open(strFile, CFile::modeCreate|CFile::modeWrite))
	{
		AfxMessageBox(_T("无法创建信息文件!"));
		return;
	}
	CArchive ar(&FileInfo, CArchive::store);

	CString strOneLine;
	strOneLine.Format(_T("EigenValue=%e\r\n"),m_dCurrentEigen);
	ar.WriteString(strOneLine);

	// To control the number of elements to be deleted each time
	for(i=0; i<nCols;i++)
	{
		int nEleID=(int)pd1[i];
		double dEleSensitivity=pd2[i];
		strOneLine.Format(_T("ElementID=%d\t\t\tSensitivity=%e\r\n"), nEleID, dEleSensitivity);
		ar.WriteString(strOneLine);
	}
	ar.Close();
	mxDestroyArray(T1);
	mxDestroyArray(T2);
	engEvalString(m_ep, _T("clear ES;"));
	engEvalString(m_ep, _T("clear SES;"));
	engEvalString(m_ep, _T("clear SESCOL1;"));
	engEvalString(m_ep, _T("clear SESCOL2;"));
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -