#include "nmCalculationSolver.h" #include #include #include #include #include nmCalculationSolver::nmCalculationSolver() { } bool nmCalculationSolver::execSolve(QString vtkFilePath, QString reservoirParamFilePath, QString wellsParamFilePath, QString dllDir, QString outputFile) { // 读取点、cell信息 std::vector points; std::vector cells; this->readMesh(vtkFilePath.toStdString(), points, cells); // 读取油藏信息 double *pBaseData; pBaseData = new double[8]; this->readReservoirParamters(reservoirParamFilePath.toStdString(), pBaseData); // 读取井的属性信息 int nWellNum; CWell* wWell = NULL; this->readWells(wellsParamFilePath.toStdString(), nWellNum, wWell); QString dllPath = dllDir + "singlePhaseSolverDll.dll"; //LPCWSTR name; //wchar_t* str2 = new wchar_t[dllPath.size() + 1]; //int len1 = dllPath.toWCharArray(str2); //str2[len1] = '\0';//不添加这个,会有乱码 //name = str2; // HMODULE hMod = LoadLibrary(name); //HMODULE hMod = LoadLibrary(L"E:\01-Projects\16-CNPC\16-nmWTAI-gitea\Bin\Temp\Nm\Solver\singlePhaseSolverDll.dll"); //int err = GetLastError(); // if (NULL == hMod) { // std::cout << "singlePhaseSolver.dll加载失败!\n"; // delete[] pBaseData; // delete[] wWell; // return false; // } // typedef int(*Solver)(double*, std::vector, std::vector, int, CWell*, // std::vector> &, std::vector>> &); // // Get the address of the trinomial function // Solver solverFun = (Solver)GetProcAddress(hMod, "singlePhaseSolver"); // if (NULL == solverFun) { // FreeLibrary(hMod); // std::cout << "singlePhaseSolver文件加载函数地址获取失败!\n"; // delete[] pBaseData; // delete[] wWell; // return false; // } QLibrary library(dllPath); // 加载动态库: if (library.load()) { qDebug() << "Library loaded successfully."; } else { qDebug() << "Failed to load library:" << library.errorString(); } // 获取函数指针并调用函数: typedef int(*Solver)(double*, std::vector, std::vector, int, CWell*, std::vector> &, std::vector>> &); Solver solverFun = (Solver)library.resolve("singlePhaseSolver"); if (solverFun) { qDebug() << "Success to resolve function"; } else { qDebug() << "Failed to resolve function:" << library.errorString(); } std::vector> vecBP; std::vector>> vecFieldPre; // 调用求解器求解 solverFun(pBaseData, points, cells, nWellNum, wWell, vecBP, vecFieldPre); // FreeLibrary(hMod); delete[] pBaseData; delete[] wWell; return true; } bool nmCalculationSolver::readMesh(std::string fname, std::vector &points, std::vector &cells) { std::ifstream file(fname); if (!file.is_open()) { return false; } // Skip header std::string line; for (int i = 0; i < 4; ++i) { std::getline(file, line); } // Read points std::getline(file, line); std::istringstream iss(line); std::string keyword; int pointCount; iss >> keyword >> pointCount; points.resize(pointCount); for (int i = 0; i < points.size(); i++) { Point& point = points[i]; file >> point.x >> point.y >> point.z; } // Read cells std::getline(file, line); // Skip empty line std::getline(file, line); std::getline(file, line); iss = std::istringstream(line); int cellCount, totalListSize; iss >> keyword >> cellCount >> totalListSize; cells.resize(cellCount); // for (Cell& cell : cells) { for (int i = 0; i < cells.size(); i++) { Cell& cell = cells[i]; int cellPointSize; file >> cellPointSize; cell.pointIndices.resize(cellPointSize); // for (int& index : cell.pointIndices) { std::cout << cellPointSize; for (int j = 0; j < cell.pointIndices.size(); j++) { int& index = cell.pointIndices[j]; file >> index; } } // Read cell types std::getline(file, line); // Skip empty line std::getline(file, line); std::getline(file, line); iss = std::istringstream(line); iss >> keyword >> cellCount; // for (Cell& cell : cells) { for (int i = 0; i < cells.size(); i++) { Cell& cell = cells[i]; file >> cell.type; } // Read point data std::getline(file, line); // Skip empty line std::getline(file, line); std::getline(file, line); iss = std::istringstream(line); int dataCount; iss >> keyword >> dataCount; std::getline(file, line); std::getline(file, line); // for (Point& point : points) { for (int i = 0; i < points.size(); i++) { Point& point = points[i]; point.pointData.resize(1); file >> point.pointData[0]; } return 1; } bool nmCalculationSolver::readReservoirParamters(std::string fname, double* pBaseData) { std::ifstream file(fname); if (!file.is_open()) { return false; } for (int i = 0; i < 8; ++i) { file >> pBaseData[i]; } return true; } bool nmCalculationSolver::readWells(std::string fname, int& numWell, CWell* well) { std::fstream f(fname); if (!f.is_open()) { return false; } f >> numWell; well = new CWell[numWell]; for (int i = 0; i < numWell; ++i) { f >> well[i].dRc >> well[i].dSkin >> well[i].dC >> well[i].nTimeNumQ; if (well[i].nTimeNumQ > 1) { well[i].bisMultFlow = true; } for (int j = 0; j < well[i].nTimeNumQ; ++j) { f >> well[i].pdTimeQ[j] >> well[i].pdQ[j]; } } return true; }