bool GwfObjectInfoReader::parseBus(const QDomElement &element) { std::auto_ptr<SCgBusInfo> busInfo(new SCgBusInfo()); if(!parseObject(element,busInfo.get())) return false; if (!getAttributeString(element, "owner", busInfo->ownerIdRef())) return false; double bx, by; if (!getAttributeDouble(element, "b_x", bx) || !getAttributeDouble(element, "b_y", by)) return false; double ex, ey; if (!getAttributeDouble(element, "e_x", ex) || !getAttributeDouble(element, "e_y", ey)) return false; busInfo->pointsRef().append(QPointF(bx, by)); if (!getElementPoints(element, busInfo->pointsRef())) return false; busInfo->pointsRef().append(QPointF(ex, ey)); mObjectsInfo[SCgBus::Type].append(busInfo.release()); return true; }
bool GwfObjectInfoReader::parseNode(const QDomElement &element) { std::auto_ptr<SCgNodeInfo> nodeInfo(new SCgNodeInfo()); if(!parseObject(element,nodeInfo.get())) return false; qreal& x = nodeInfo->posRef().rx(); qreal& y = nodeInfo->posRef().ry(); if (!getAttributeDouble(element, "x", x) || !getAttributeDouble(element, "y", y)) return false; // get content element QDomElement contEl = element.firstChildElement("content"); if (contEl.isNull()) { errorHaventContent(element.tagName()); return false; } // get content type int& cType = nodeInfo->contentTypeRef(); if (!getAttributeInt(contEl, "type", cType)) return false; // get mime type if (!getAttributeString(contEl, "mime_type", nodeInfo->contentMimeTypeRef())) return false; // set content to nodeInfo if (cType > 0 && cType < 5) { if (cType == 1 || cType == 2 || cType == 3) nodeInfo->contentDataRef() = QVariant(contEl.firstChild().nodeValue()); else if (cType == 4) { // get file name getAttributeString(contEl, "file_name", nodeInfo->contentFilenameRef()); QString cData = contEl.firstChild().nodeValue(); QByteArray arr = QByteArray::fromBase64(cData.toLocal8Bit()); nodeInfo->contentDataRef() = QVariant(arr); } else { mLastError = QObject::tr("Content type '%1' doesn't supported for now").arg(cType); return false; } } else if (cType != 0) { mLastError = QObject::tr("Unknown content type '%1'").arg(cType); return false; } mObjectsInfo[SCgNode::Type].append(nodeInfo.release()); return true; }
bool GwfObjectInfoReader::getElementPoints(const QDomElement &element, QVector<QPointF> &result) { QDomElement points = element.firstChildElement("points"); if (points.isNull()) { mLastError = QObject::tr("There are no points data for element '%1'").arg(element.tagName()); return false; } QDomElement point = points.firstChildElement("point"); while (!point.isNull()) { double x, y; if (!getAttributeDouble(point, "x", x) || !getAttributeDouble(point, "y", y)) return false; result.push_back(QPointF(x, y)); point = point.nextSiblingElement("point"); } return true; }
bool GwfObjectInfoReader::parsePair(const QDomElement &element) { std::auto_ptr<SCgPairInfo> pairInfo(new SCgPairInfo()); if(!parseObject(element,pairInfo.get())) return false; if (!getAttributeString(element, "id_b", pairInfo->beginObjectIdRef()) || !getAttributeString(element, "id_e", pairInfo->endObjectIdRef())) return false; if (!getAttributeDouble(element, "dotBBalance", pairInfo->beginDotRef()) || !getAttributeDouble(element, "dotEBalance", pairInfo->endDotRef())) return false; pairInfo->pointsRef().push_back(QPointF()); if (!getElementPoints(element, pairInfo->pointsRef())) return false; pairInfo->pointsRef().push_back(QPointF()); mObjectsInfo[SCgPair::Type].append(pairInfo.release()); return true; }
// simulate the given FMU from tStart = 0 to tEnd. static int simulate(FMU* fmu, double tEnd, double h, fmi2Boolean loggingOn, char separator, int nCategories, const fmi2String categories[]) { double time; double tStart = 0; // start time const char *guid; // global unique id of the fmu const char *instanceName; // instance name fmi2Component c; // instance of the fmu fmi2Status fmi2Flag; // return code of the fmu functions char *fmuResourceLocation = getTempResourcesLocation(); // path to the fmu resources as URL, "file://C:\QTronic\sales" fmi2Boolean visible = fmi2False; // no simulator user interface fmi2CallbackFunctions callbacks = {fmuLogger, calloc, free, NULL, fmu}; // called by the model during simulation ModelDescription* md; // handle to the parsed XML file fmi2Boolean toleranceDefined = fmi2False; // true if model description define tolerance fmi2Real tolerance = 0; // used in setting up the experiment ValueStatus vs = 0; int nSteps = 0; Element *defaultExp; FILE* file; // instantiate the fmu md = fmu->modelDescription; guid = getAttributeValue((Element *)md, att_guid); instanceName = getAttributeValue((Element *)getCoSimulation(md), att_modelIdentifier); c = fmu->instantiate(instanceName, fmi2CoSimulation, guid, fmuResourceLocation, &callbacks, visible, loggingOn); free(fmuResourceLocation); if (!c) return error("could not instantiate model"); if (nCategories > 0) { fmi2Flag = fmu->setDebugLogging(c, fmi2True, nCategories, categories); if (fmi2Flag > fmi2Warning) { return error("could not initialize model; failed FMI set debug logging"); } } defaultExp = getDefaultExperiment(md); if (defaultExp) tolerance = getAttributeDouble(defaultExp, att_tolerance, &vs); if (vs == valueDefined) { toleranceDefined = fmi2True; } fmi2Flag = fmu->setupExperiment(c, toleranceDefined, tolerance, tStart, fmi2True, tEnd); if (fmi2Flag > fmi2Warning) { return error("could not initialize model; failed FMI setup experiment"); } fmi2Flag = fmu->enterInitializationMode(c); if (fmi2Flag > fmi2Warning) { return error("could not initialize model; failed FMI enter initialization mode"); } fmi2Flag = fmu->exitInitializationMode(c); if (fmi2Flag > fmi2Warning) { return error("could not initialize model; failed FMI exit initialization mode"); } // open result file if (!(file = fopen(RESULT_FILE, "w"))) { printf("could not write %s because:\n", RESULT_FILE); printf(" %s\n", strerror(errno)); return 0; // failure } // output solution for time t0 outputRow(fmu, c, tStart, file, separator, fmi2True); // output column names outputRow(fmu, c, tStart, file, separator, fmi2False); // output values // enter the simulation loop time = tStart; while (time < tEnd) { fmi2Flag = fmu->doStep(c, time, h, fmi2True); if (fmi2Flag == fmi2Discard) { fmi2Boolean b; // check if model requests to end simulation if (fmi2OK != fmu->getBooleanStatus(c, fmi2Terminated, &b)) { return error("could not complete simulation of the model. getBooleanStatus return other than fmi2OK"); } if (b == fmi2True) { return error("the model requested to end the simulation"); } return error("could not complete simulation of the model"); } if (fmi2Flag != fmi2OK) return error("could not complete simulation of the model"); time += h; outputRow(fmu, c, time, file, separator, fmi2False); // output values for this step nSteps++; } // end simulation fmu->terminate(c); fmu->freeInstance(c); fclose(file); // print simulation summary printf("Simulation from %g to %g terminated successful\n", tStart, tEnd); printf(" steps ............ %d\n", nSteps); printf(" fixed step size .. %g\n", h); return 1; // success }