int CellmlModelDefinition::getVariableIndex(const std::string& variableId, unsigned char variableType) { unsigned char vt = getVariableType(variableId); if (vt == csim::UndefinedType) { std::cerr << "CellML Model Definition::getVariableIndex: unable to get the variable type for: " << variableId << std::endl; return csim::UNDEFINED_VARIABLE_TYPE; } // can now assume everything set up for use if (vt & variableType) { ObjRef<iface::cellml_api::CellMLVariable> sv = findLocalVariable(mCapi, variableId); return mVariableIndices[getVariableUniqueId(sv)][variableType]; } std::cerr << "CellML Model Definition::getVariableIndex: no computation target of matching type." << std::endl; return csim::MISMATCHED_COMPUTATION_TARGET; }
int CellMLModelDefinition::getVariableType(const char* name,int* type) { int StateType = 1; int KnownType = 2; int WantedType = 3; int IndependentType = 4; int code = -1; iface::cellml_api::Model* model = static_cast<iface::cellml_api::Model*>(mModel); if (!model && !mCodeInformation && !mAnnotations) { std::cerr << "CellMLModelDefinition::getVariableType -- missing model?" << std::endl; return -2; } RETURN_INTO_OBJREF(var,iface::cellml_api::CellMLVariable,findLocalVariable(model,name)); if (!var) { std::cerr << "CellMLModelDefinition::getVariableType -- unable to find named variable: " << name << std::endl; return -3; } iface::cellml_services::AnnotationSet* as = static_cast<iface::cellml_services::AnnotationSet*>(mAnnotations); RETURN_INTO_WSTRING(flag,as->getStringAnnotation(var,L"flag")); if (!flag.empty()) { code = 0; if (flag == L"STATE") *type = StateType; else if (flag == L"KNOWN") *type = KnownType; else if (flag == L"WANTED") *type = WantedType; else if (flag == L"INDEPENDENT") *type = IndependentType; else { std::cerr << "CellMLModelDefinition::getVariableType -- variable: " << name << "; has an unknown type: "; std::wcerr << flag.c_str(); std::cerr << std::endl; code = -5; } } else { std::cerr << "CellMLModelDefinition::getVariableType -- unable to find named variable's type: " << name << std::endl; return -4; } return code; }
int CellMLModelDefinition::getInitialValue(const char* name,double* value) { int code = -1; iface::cellml_api::Model* model = static_cast<iface::cellml_api::Model*>(mModel); if (!model) { std::cerr << "CellMLModelDefinition::getInitialValue -- missing model?" << std::endl; return -2; } RETURN_INTO_OBJREF(var,iface::cellml_api::CellMLVariable,findLocalVariable(model,name)); if (!var) { std::cerr << "CellMLModelDefinition::getInitialValue -- unable to find named variable: " << name << std::endl; return -3; } RETURN_INTO_WSTRING(iv,var->initialValue()); if (iv != L"") { wchar_t* end; double v = wcstod(iv.c_str(),&end); if (end == NULL || *end != 0) { std::wcerr << "CellMLModelDefinition::getInitialValue -- variable " << name << " has non-numeric initial value: " << iv.c_str() << std::endl; code = -5; } else { *value = v; code = 0; } } else { std::cerr << "CellMLModelDefinition::getInitialValue -- variable " << name << " has no initial value." << std::endl; code = -4; } return code; }
int CellMLModelDefinition::getVariableIndex(const char* name,int* index) { int code = -1; iface::cellml_api::Model* model = static_cast<iface::cellml_api::Model*>(mModel); if (!model && !mCodeInformation && !mAnnotations) { std::cerr << "CellMLModelDefinition::getVariableIndex -- missing model?" << std::endl; return -2; } RETURN_INTO_OBJREF(var,iface::cellml_api::CellMLVariable,findLocalVariable(model,name)); if (!var) { std::cerr << "CellMLModelDefinition::getVariableIndex -- unable to find named variable: " << name << std::endl; return -3; } iface::cellml_services::AnnotationSet* as = static_cast<iface::cellml_services::AnnotationSet*>(mAnnotations); RETURN_INTO_WSTRING(str,as->getStringAnnotation(var,L"array_index")); if (!str.empty()) { code = 0; wchar_t* tail = NULL; int i = wcstol(str.c_str(),&tail,/*base 10*/10); if (tail != str.c_str()) *index = i; else { std::cerr << "CellMLModelDefinition::getVariableIndex -- variable: " << name << "; has an invalid index: "; std::wcerr << str.c_str(); std::cerr << std::endl; code = -5; } } else { std::cerr << "CellMLModelDefinition::getVariableIndex -- unable to find named variable's index: " << name << std::endl; return -4; } return code; }
unsigned char CellmlModelDefinition::getVariableType(const std::string& variableId) { if (! mCapi->codeInformation) { std::cerr << "CellML Model Definition::getVariableType: missing model implementation?" << std::endl; return csim::UndefinedType; } ObjRef<iface::cellml_api::CellMLVariable> sv = findLocalVariable(mCapi, variableId); if (!sv) { std::cerr << "CellML Model Definition::getVariableType: unable to find source variable for: " << variableId << std::endl; return csim::UndefinedType; } std::map<std::string, unsigned char>::iterator variableTypeIt = mVariableTypes.find(getVariableUniqueId(sv)); unsigned char currentTypes = csim::UndefinedType; if (variableTypeIt != mVariableTypes.end()) { currentTypes = variableTypeIt->second; } return currentTypes; }
static int flagVariable(CellMLModelDefinition* d,const char* name, const wchar_t* typeString, std::vector<iface::cellml_services::VariableEvaluationType> vets,int& count, int& specificCount) { if (! d->mCodeInformation) { std::cerr << "CellMLModelDefinition::flagVariable -- missing model?" << std::endl; return -1; } //std::cout << "setting variable '" << cv.second.c_str() << "' from component '" << cv.first.c_str() << "' as KNOWN" << std::endl; iface::cellml_api::Model* model = static_cast<iface::cellml_api::Model*>(d->mModel); iface::cellml_services::CodeInformation* cci= static_cast<iface::cellml_services::CodeInformation*>(d->mCodeInformation); iface::cellml_services::AnnotationSet* as = static_cast<iface::cellml_services::AnnotationSet*>(d->mAnnotations); RETURN_INTO_OBJREF(sv,iface::cellml_api::CellMLVariable,findLocalVariable(model,name)); // check if source is already marked as known - what to do? safe to continue with no error RETURN_INTO_WSTRING(currentAnnotation,as->getStringAnnotation(sv,L"flag")); if (!currentAnnotation.empty()) { if (currentAnnotation == typeString) { std::cout << "Already flagged same type, nothing to do." << std::endl; return 0; } else { std::cerr << "CellMLModelDefinition::flagVariable -- variable already flagged something else: " << name << std::endl; return -5; } } // find corresponding computation target RETURN_INTO_OBJREF(cti,iface::cellml_services::ComputationTargetIterator,cci->iterateTargets()); iface::cellml_services::ComputationTarget* ct = NULL; while(1) { ct = cti->nextComputationTarget(); if (ct == NULL) break; if (ct->variable() == sv) { // std::cout << "found a computation target for the source variable of the variable: " // << cv.first.c_str() << " / " << cv.second.c_str() << std::endl; break; } else { ct->release_ref(); ct = NULL; } } if (!ct) { std::cerr << "CellMLModelDefinition::flagVariable -- unable get computation target for the source of variable: " << name << std::endl; return -5; } // check type of computation target and make sure compatible unsigned int i,compatible = 0; for (i=0;i<vets.size();i++) { if (ct->type() == vets[i]) { compatible = 1; break; } } if (compatible) { as->setStringAnnotation(sv,L"flag",typeString); std::wstring ename(L"OC_"); ename += typeString; as->setStringAnnotation(sv,L"array",ename.c_str()); as->setStringAnnotation(ct->variable(),L"array_index",formatNumber(specificCount).c_str()); specificCount++; count++; } else { std::cerr << "CellMLModelDefinition::flagVariable -- computation target for variable: " << name << "; is the wrong type to be flagged" << std::endl; std::cerr << "Computation target for this source variable is: "; switch (ct->type()) { case iface::cellml_services::CONSTANT: std::cerr << "CONSTANT"; break; case iface::cellml_services::VARIABLE_OF_INTEGRATION: std::cerr << "VARIABLE_OF_INTEGRATION"; break; case iface::cellml_services::STATE_VARIABLE: std::cerr << "STATE_VARIABLE"; break; case iface::cellml_services::PSEUDOSTATE_VARIABLE: std::cerr << "PSEUDOSTATE_VARIABLE"; break; case iface::cellml_services::ALGEBRAIC: std::cerr << "ALGEBRAIC"; break; case iface::cellml_services::LOCALLY_BOUND: std::cerr << "LOCALLY_BOUND"; break; case iface::cellml_services::FLOATING: std::cerr << "FLOATING"; break; default: std::cerr << "Invalid"; } std::cerr << std::endl; ct->release_ref(); return -5; } ct->release_ref(); return 0; }
int flagVariable(const std::string& variableId, unsigned char type, std::vector<iface::cellml_services::VariableEvaluationType> vets, int& count, CellmlApiObjects* capi, std::map<std::string, unsigned char>& variableTypes, std::map<std::string, std::map<unsigned char, int> >& variableIndices) { if (! capi->codeInformation) { std::cerr << "CellML Model Definition::flagVariable: missing model implementation?" << std::endl; return csim::UNABLE_TO_FLAG_VARIABLE; } ObjRef<iface::cellml_api::CellMLVariable> sv = findLocalVariable(capi, variableId); if (!sv) { std::cerr << "CellML Model Definition::flagVariable: unable to find source variable for: " << variableId << std::endl; return csim::UNABLE_TO_FLAG_VARIABLE; } // FIXME: here we simply accept any flag combination. Code generation is the place // where flags will be checked for consistency? // check if source is already flagged with the specified type. std::map<std::string, unsigned char>::iterator currentAnnotation = variableTypes.find(getVariableUniqueId(sv)); unsigned char currentTypes; if (currentAnnotation != variableTypes.end()) { currentTypes = currentAnnotation->second; if (currentTypes & type) { std::cout << "Already flagged same type, nothing to do." << std::endl; return variableIndices[getVariableUniqueId(sv)][type]; } } // find corresponding computation target ObjRef<iface::cellml_services::ComputationTargetIterator> cti = capi->codeInformation->iterateTargets(); ObjRef<iface::cellml_services::ComputationTarget> ct(NULL); while(true) { ct = cti->nextComputationTarget(); if (ct == NULL) break; if (ct->variable() == sv) break; } if (!ct) { std::cerr << "CellMLModelDefinition::flagVariable -- unable get computation target for the source of variable: " << variableId << std::endl; return csim::NO_MATCHING_COMPUTATION_TARGET; } // check type of computation target and make sure compatible unsigned int i,compatible = 0; for (i=0; i<vets.size(); i++) { if (ct->type() == vets[i]) { compatible = 1; break; } } if (!compatible) { std::cerr << "CellMLModelDefinition::flagVariable -- computation target for variable: " << variableId << "; is the wrong type to be flagged" << std::endl; std::cerr << "Computation target for this source variable is: "; switch (ct->type()) { case iface::cellml_services::CONSTANT: std::cerr << "CONSTANT"; break; case iface::cellml_services::VARIABLE_OF_INTEGRATION: std::cerr << "VARIABLE_OF_INTEGRATION"; break; case iface::cellml_services::STATE_VARIABLE: std::cerr << "STATE_VARIABLE"; break; case iface::cellml_services::PSEUDOSTATE_VARIABLE: std::cerr << "PSEUDOSTATE_VARIABLE"; break; case iface::cellml_services::ALGEBRAIC: std::cerr << "ALGEBRAIC"; break; case iface::cellml_services::LOCALLY_BOUND: std::cerr << "LOCALLY_BOUND"; break; case iface::cellml_services::FLOATING: std::cerr << "FLOATING"; break; default: std::cerr << "Invalid"; } std::cerr << std::endl; return csim::MISMATCHED_COMPUTATION_TARGET; } variableTypes[getVariableUniqueId(sv)] = currentTypes | type; variableIndices[getVariableUniqueId(sv)][type] = count++; return variableIndices[getVariableUniqueId(sv)][type]; }