PView *GMSH_MathEvalPlugin::execute(PView *view) { int timeStep = (int)MathEvalOptions_Number[0].def; int iView = (int)MathEvalOptions_Number[1].def; int otherTimeStep = (int)MathEvalOptions_Number[2].def; int iOtherView = (int)MathEvalOptions_Number[3].def; int forceInterpolation = (int)MathEvalOptions_Number[4].def; int physicalRegion = (int)MathEvalOptions_Number[5].def; std::vector<std::string> expr(9); for(int i = 0; i < 9; i++) expr[i] = MathEvalOptions_String[i].def; PView *v1 = getView(iView, view); if(!v1) return view; PViewData *data1 = getPossiblyAdaptiveData(v1); if(data1->hasMultipleMeshes()){ Msg::Error("MathEval plugin cannot be applied to multi-mesh views"); return view; } PView *otherView = v1; if(iOtherView >= 0){ otherView = getView(iOtherView, view); if(!otherView){ Msg::Error("MathEval plugin could not find other view %i", iOtherView); return view; } } PViewData *otherData = getPossiblyAdaptiveData(otherView); if(otherData->hasMultipleMeshes()){ Msg::Error("MathEval plugin cannot be applied to multi-mesh views"); return view; } if(otherTimeStep < 0 && otherData->getNumTimeSteps() != data1->getNumTimeSteps()){ Msg::Error("Number of time steps don't match: using step 0"); otherTimeStep = 0; } else if(otherTimeStep > otherData->getNumTimeSteps() - 1){ Msg::Error("Invalid time step (%d) in View[%d]: using step 0 instead", otherTimeStep, otherView->getIndex()); otherTimeStep = 0; } int numComp2; if(expr[3].size() || expr[4].size() || expr[5].size() || expr[6].size() || expr[7].size() || expr[8].size()){ numComp2 = 9; for(int i = 0; i < 9; i++) if(expr[i].empty()) expr[i] = "0"; } else if(expr[1].size() || expr[2].size()){ numComp2 = 3; for(int i = 0; i < 3; i++) if(expr[i].empty()) expr[i] = "0"; } else{ numComp2 = 1; } expr.resize(numComp2); const char *names[] = { "x", "y", "z", "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "w0", "w1", "w2", "w3", "w4", "w5", "w6", "w7", "w8" }; unsigned int numVariables = sizeof(names) / sizeof(names[0]); std::vector<std::string> variables(numVariables); for(unsigned int i = 0; i < numVariables; i++) variables[i] = names[i]; mathEvaluator f(expr, variables); if(expr.empty()) return view; std::vector<double> values(numVariables), res(numComp2); OctreePost *octree = 0; if(forceInterpolation || (data1->getNumEntities() != otherData->getNumEntities()) || (data1->getNumElements() != otherData->getNumElements())){ Msg::Info("Other view based on different grid: interpolating..."); octree = new OctreePost(otherView); } PView *v2 = new PView(); PViewDataList *data2 = getDataList(v2); if(timeStep < 0){ timeStep = - data1->getNumTimeSteps(); } else if(timeStep > data1->getNumTimeSteps() - 1){ Msg::Error("Invalid time step (%d) in View[%d]: using all steps instead", timeStep, v1->getIndex()); timeStep = - data1->getNumTimeSteps(); } int firstNonEmptyStep = data1->getFirstNonEmptyTimeStep(); int timeBeg = (timeStep < 0) ? firstNonEmptyStep : timeStep; int timeEnd = (timeStep < 0) ? -timeStep : timeStep + 1; for(int ent = 0; ent < data1->getNumEntities(timeBeg); ent++){ bool ok = (physicalRegion <= 0); if(physicalRegion > 0){ GEntity *ge = data1->getEntity(timeBeg, ent); if(ge){ std::vector<int>::iterator it = std::find (ge->physicals.begin(), ge->physicals.end(), physicalRegion); ok = (it != ge->physicals.end()); } } if(!ok) continue; for(int ele = 0; ele < data1->getNumElements(timeBeg, ent); ele++){ if(data1->skipElement(timeBeg, ent, ele)) continue; int numNodes = data1->getNumNodes(timeBeg, ent, ele); int type = data1->getType(timeBeg, ent, ele); int numComp = data1->getNumComponents(timeBeg, ent, ele); int otherNumComp = (!otherData || octree) ? 9 : otherData->getNumComponents(timeBeg, ent, ele); std::vector<double> *out = data2->incrementList(numComp2, type, numNodes); std::vector<double> v(std::max(9, numComp), 0.); std::vector<double> w(std::max(9, otherNumComp), 0.); std::vector<double> x(numNodes), y(numNodes), z(numNodes); for(int nod = 0; nod < numNodes; nod++) data1->getNode(timeBeg, ent, ele, nod, x[nod], y[nod], z[nod]); for(int nod = 0; nod < numNodes; nod++) out->push_back(x[nod]); for(int nod = 0; nod < numNodes; nod++) out->push_back(y[nod]); for(int nod = 0; nod < numNodes; nod++) out->push_back(z[nod]); for(int step = timeBeg; step < timeEnd; step++){ if(!data1->hasTimeStep(step)) continue; int step2 = (otherTimeStep < 0) ? step : otherTimeStep; for(int nod = 0; nod < numNodes; nod++){ for(int comp = 0; comp < numComp; comp++) data1->getValue(step, ent, ele, nod, comp, v[comp]); if(otherData){ if(octree){ int qn = forceInterpolation ? numNodes : 0; if(!octree->searchScalar(x[nod], y[nod], z[nod], &w[0], step2, 0, qn, &x[0], &y[0], &z[0])) if(!octree->searchVector(x[nod], y[nod], z[nod], &w[0], step2, 0, qn, &x[0], &y[0], &z[0])) octree->searchTensor(x[nod], y[nod], z[nod], &w[0], step2, 0, qn, &x[0], &y[0], &z[0]); } else for(int comp = 0; comp < otherNumComp; comp++) otherData->getValue(step2, ent, ele, nod, comp, w[comp]); } values[0] = x[nod]; values[1] = y[nod]; values[2] = z[nod]; for(int i = 0; i < 9; i++) values[3 + i] = v[i]; for(int i = 0; i < 9; i++) values[12 + i] = w[i]; if(f.eval(values, res)){ for(int i = 0; i < numComp2; i++) out->push_back(res[i]); } else{ goto end; } } } } } end: if(octree) delete octree; if(timeStep < 0){ for(int i = firstNonEmptyStep; i < data1->getNumTimeSteps(); i++) { if(!data1->hasTimeStep(i)) continue; data2->Time.push_back(data1->getTime(i)); } } else data2->Time.push_back(data1->getTime(timeStep)); data2->setName(data1->getName() + "_MathEval"); data2->setFileName(data1->getName() + "_MathEval.pos"); data2->finalize(); return v2; }
PView *GMSH_ModifyComponentPlugin::execute(PView *view) { int component = (int)ModifyComponentOptions_Number[0].def; int timeStep = (int)ModifyComponentOptions_Number[1].def; int iView = (int)ModifyComponentOptions_Number[2].def; int otherTimeStep = (int)ModifyComponentOptions_Number[3].def; int otherView = (int)ModifyComponentOptions_Number[4].def; int forceInterpolation = (int)ModifyComponentOptions_Number[5].def; PView *v1 = getView(iView, view); if(!v1) return view; PViewData *data1 = v1->getData(); if(timeStep > data1->getNumTimeSteps() - 1){ Msg::Error("Invalid time step (%d) in View[%d]: using step 0 instead", timeStep, v1->getIndex()); timeStep = 0; } PView *v2 = v1; if(otherView >= 0){ if(otherView < (int)PView::list.size()) v2 = PView::list[otherView]; else Msg::Error("View[%d] does not exist: using self", otherView); } PViewData *data2 = getPossiblyAdaptiveData(v2); if(otherTimeStep < 0 && data2->getNumTimeSteps() != data1->getNumTimeSteps()){ Msg::Error("Number of time steps don't match: using step 0"); otherTimeStep = 0; } else if(otherTimeStep > data2->getNumTimeSteps() - 1){ Msg::Error("Invalid time step (%d) in View[%d]: using step 0 instead", otherTimeStep, v2->getIndex()); otherTimeStep = 0; } const char *names[] = {"x", "y", "z", "Time", "TimeStep", "v", "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "w", "w0", "w1", "w2", "w3", "w4", "w5", "w6", "w7", "w8"}; unsigned int numVariables = sizeof(names) / sizeof(names[0]); std::vector<std::string> expressions(1), variables(numVariables); expressions[0] = ModifyComponentOptions_String[0].def; for(unsigned int i = 0; i < numVariables; i++) variables[i] = names[i]; mathEvaluator f(expressions, variables); if(expressions.empty()) return view; std::vector<double> values(numVariables), res(1); OctreePost *octree = 0; if(forceInterpolation || (data1->getNumEntities() != data2->getNumEntities()) || (data1->getNumElements() != data2->getNumElements())){ Msg::Info("Other view based on different grid: interpolating..."); octree = new OctreePost(v2); } for(int step = 0; step < data1->getNumTimeSteps(); step++){ if(timeStep >= 0 && timeStep != step) continue; double time = data1->getTime(step); int step2 = (otherTimeStep < 0) ? step : otherTimeStep; // tag all the nodes with "0" (the default tag) for(int ent = 0; ent < data1->getNumEntities(step); ent++){ for(int ele = 0; ele < data1->getNumElements(step, ent); ele++){ if(data1->skipElement(step, ent, ele)) continue; for(int nod = 0; nod < data1->getNumNodes(step, ent, ele); nod++) data1->tagNode(step, ent, ele, nod, 0); } } for(int ent = 0; ent < data1->getNumEntities(step); ent++){ for(int ele = 0; ele < data1->getNumElements(step, ent); ele++){ if(data1->skipElement(step, ent, ele)) continue; int numComp = data1->getNumComponents(step, ent, ele); int numComp2 = octree ? 9 : data2->getNumComponents(step2, ent, ele); int numNodes = data1->getNumNodes(step, ent, ele); std::vector<int> tag(numNodes); std::vector<double> x(numNodes), y(numNodes), z(numNodes); for(int nod = 0; nod < numNodes; nod++) tag[nod] = data1->getNode(step, ent, ele, nod, x[nod], y[nod], z[nod]); for(int nod = 0; nod < numNodes; nod++){ if(tag[nod]) continue; // node has already been modified std::vector<double> v(std::max(9, numComp), 0.); for(int comp = 0; comp < numComp; comp++) data1->getValue(step, ent, ele, nod, comp, v[comp]); std::vector<double> w(std::max(9, numComp2), 0.); if(octree){ int qn = forceInterpolation ? numNodes : 0; if(!octree->searchScalar(x[nod], y[nod], z[nod], &w[0], step2, 0, qn, &x[0], &y[0], &z[0])) if(!octree->searchVector(x[nod], y[nod], z[nod], &w[0], step2, 0, qn, &x[0], &y[0], &z[0])) octree->searchTensor(x[nod], y[nod], z[nod], &w[0], step2, 0, qn, &x[0], &y[0], &z[0]); } else for(int comp = 0; comp < numComp2; comp++) data2->getValue(step2, ent, ele, nod, comp, w[comp]); for(int comp = 0; comp < numComp; comp++){ if(component >= 0 && component != comp) continue; values[0] = x[nod]; values[1] = y[nod]; values[2] = z[nod]; values[3] = time; values[4] = step; values[5] = v[comp]; for(int i = 0; i < 9; i++) values[6 + i] = v[i]; values[15] = w[comp]; for(int i = 0; i < 9; i++) values[16 + i] = w[i]; if(f.eval(values, res)) data1->setValue(step, ent, ele, nod, comp, res[0]); data1->tagNode(step, ent, ele, nod, 1); } } } } } if(octree) delete octree; data1->finalize(); v1->setChanged(true); return v1; }