OpenModelica (ci-après dénommé OM) peut être insatisfaisant en raison de sa faible fonction d'étude des paramètres et de ses fonctions de traitement, d'analyse et de visualisation des résultats. Étant donné que OM a une fonction à lier avec Python, il peut être possible de compenser le manque d'OM en exécutant des calculs et en traitant les résultats en Python. Pour confirmer cela, vérifiez d'abord le fonctionnement de la fonction de liaison Python-OM et traitez les résultats du calcul avec une bibliothèque d'analyse de données appelée Pandas. Python est presque inexpérimenté, veuillez donc signaler toute erreur.
OM a trois fonctions pour la liaison avec Python. Il exécute principalement des commandes OM à partir de Python.
omc.sendExpression("simulate(BouncingBall, stopTime=3.0)")
mod.simulate()
Nous vérifierons le fonctionnement de l'OMPython le plus basique. L'exécution côté Python se fait dans Jupyter Lab.
Windows10 Home 1809 conda version : 4.5.4 conda-build version : 3.10.5 python version : 3.6.5.final.0 JupyterLab : 1.2.6 OpenModelica : 1.13.2 64bit
Installez selon le Lisez-moi ci-dessous. https://github.com/OpenModelica/OMPython
On suppose que OM et Anaconda sont installés à l'avance.
①. Démarrez Anaconda Prompt et accédez au dossier suivant Ici, "% OPENMODELICAHOME%" est le chemin directement sous le dossier où OM est installé.
cd %OPENMODELICAHOME%\share\omc\scripts\PythonInterface
②. Installez avec la commande suivante
python -m pip install -U .
J'ai reçu ce message.
Could not install packages due to an EnvironmentError: [WinError 5]Accès refusé.: 'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Anaconda3_64\\Lib\\site-packages\\future'
Consider using the `--user` option or check the permissions.
Le message d'erreur est très gentil. Réexécutez avec --user
comme indiqué. terminé.
python -m pip install --user -U .
<détails> <résumé> Message </ résumé>
③ Démarrez Jupyter Lab pour vérifier s'il est installé. Pour le démarrer, démarrez Anaconda Navigator à partir du menu Démarrer et démarrez "Jupyter Lab". Sélectionnez Python pour Notebook.
④. Exécutez les
import OMPython
help(OMPython.ModelicaSystem)
L'image de Jupyter Lab ressemble à ceci. Intuitif et facile à utiliser. Exécutez avec Ctrl + Entrée. Insérez la cellule b et supprimez d deux fois.
Help on class ModelicaSystem in module OMPython:
class ModelicaSystem(builtins.object)
| Methods defined here:
|
| del(self)
|
| init(self, fileName=None, modelName=None, lmodel=[], useCorba=False)
| "constructor"
| It initializes to load file and build a model, generating object, exe, xml, mat, and json files. etc. It can be called :
| •without any arguments: In this case it neither loads a file nor build a model. This is useful when a FMU needed to convert to Modelica model
| •with two arguments as file name with ".mo" extension and the model name respectively
| •with three arguments, the first and second are file name and model name respectively and the third arguments is Modelica standard library to load a model, which is common in such models where the model is based on the standard library. For example, here is a model named "dcmotor.mo" below table 4-2, which is located in the directory of OpenModelica at "C:\OpenModelica1.9.4-dev.beta2\share\doc\omc estmodels".
| Note: If the model file is not in the current working directory, then the path where file is located must be included together with file name. Besides, if the Modelica model contains several different models within the same package, then in order to build the specific model, in second argument, user must put the package name with dot(.) followed by specific model name.
| ex: myModel = ModelicaSystem("ModelicaModel.mo", "modelName")
|
| convertFmu2Mo(self, fmuName)
| In order to load FMU, at first it needs to be translated into Modelica model. This method is used to generate Modelica model from the given FMU. It generates "fmuName_me_FMU.mo". It can be called:
| •only without any arguments
| Currently, it only supports Model Exchange conversion.
|
| - Input arguments: s1
| * s1: name of FMU file, including extension .fmu
|
| convertMo2Fmu(self)
| This method is used to generate FMU from the given Modelica model. It creates "modelName.fmu" in the current working directory. It can be called:
| •only without any arguments
|
| getContinuous(self, *names)
| This method returns dict. The key is continuous names and value is corresponding continuous value.
| If *name is None then the function will return dict which contain all continuous names as key and value as corresponding values. eg., getContinuous()
| Otherwise variable number of arguments can be passed as continuous name in string format separated by commas. eg., getContinuous('cName1', 'cName2')
|
| getInputs(self, *names)
| This method returns dict. The key is input names and value is corresponding input value.
| If *name is None then the function will return dict which contain all input names as key and value as corresponding values. eg., getInputs()
| Otherwise variable number of arguments can be passed as input name in string format separated by commas. eg., getInputs('iName1', 'iName2')
|
| getLinearInputs(self)
|
| getLinearOutputs(self)
|
| getLinearQuantityInformation(self)
|
| getLinearStates(self)
|
| getLinearizationOptions(self, *names)
| This method returns dict. The key is linearize option names and value is corresponding linearize option value.
| If *name is None then the function will return dict which contain all linearize option names as key and value as corresponding values. eg., getLinearizationOptions()
| Otherwise variable number of arguments can be passed as simulation option name in string format separated by commas. eg., getLinearizationOptions('linName1', 'linName2')
|
| getOptimizationOptions(self, *names)
|
| getOutputs(self, *names)
| This method returns dict. The key is output names and value is corresponding output value.
| If *name is None then the function will return dict which contain all output names as key and value as corresponding values. eg., getOutputs()
| Otherwise variable number of arguments can be passed as output name in string format separated by commas. eg., getOutputs(opName1', 'opName2')
|
| getParameters(self, *names)
| This method returns dict. The key is parameter names and value is corresponding parameter value.
| If *name is None then the function will return dict which contain all parameter names as key and value as corresponding values. eg., getParameters()
| Otherwise variable number of arguments can be passed as parameter name in string format separated by commas. eg., getParameters('paraName1', 'paraName2')
|
| getQuantities(self, names=None)
| This method returns list of dictionaries. It displays details of quantities such as name, value, changeable, and description, where changeable means if value for corresponding quantity name is changeable or not. It can be called :
| •without argument: it returns list of dictionaries of all quantities
| •with a single argument as list of quantities name in string format: it returns list of dictionaries of only particular quantities name
| •a single argument as a single quantity name (or in list) in string format: it returns list of dictionaries of the particular quantity name
|
| getSimulationOptions(self, *names)
| This method returns dict. The key is simulation option names and value is corresponding simulation option value.
| If *name is None then the function will return dict which contain all simulation option names as key and value as corresponding values. eg., getSimulationOptions()
| Otherwise variable number of arguments can be passed as simulation option name in string format separated by commas. eg., getSimulationOptions('simName1', 'simName2')
|
| getSolutions(self, *varList)
| This method returns tuple of numpy arrays. It can be called:
| •with a list of quantities name in string format as argument: it returns the simulation results of the corresponding names in the same order. Here it supports Python unpacking depending upon the number of variables assigned.
|
| linearize(self)
| This method linearizes model according to the linearized options. This will generate a linear model that consists of matrices A, B, C and D. It can be called:
| •only without any arguments
|
| optimize(self)
| This method optimizes model according to the optimized options. It can be called:
| •only without any arguments
|
| requestApi(self, apiName, entity=None, properties=None)
| # request to OMC
|
| setContinuous(self, **cvals)
| This method is used to set continuous values. It can be called:
| •with a sequence of continuous name and assigning corresponding values as arguments as show in the example below:
| setContinuousValues(cName1 = 10.9, cName2 = 0.066)
|
| setInputs(self, **nameVal)
| This method is used to set input values. It can be called:
| •with a sequence of input name and assigning corresponding values as arguments as show in the example below:
| setParameterValues(iName = [(t0, v0), (t1, v0), (t1, v2), (t3, v2)...]), where tj<=tj+1
|
| setLinearizationOptions(self, **linearizationOptions)
| This method is used to set linearization options. It can be called:
| •with a sequence of linearization options name and assigning corresponding value as arguments as show in the example below
| setLinearizationOptions(stopTime=0, stepSize = 10)
|
| setOptimizationOptions(self, **optimizationOptions)
| This method is used to set optimization options. It can be called:
| •with a sequence of optimization options name and assigning corresponding values as arguments as show in the example below:
| setOptimizationOptions(stopTime = 10,simflags = '-lv LOG_IPOPT -optimizerNP 1')
|
| setParameters(self, **pvals)
| This method is used to set parameter values. It can be called:
| •with a sequence of parameter name and assigning corresponding value as arguments as show in the example below:
| setParameterValues(pName1 = 10.9, pName2 = 0.066)
|
| setSimulationOptions(self, **simOptions)
| This method is used to set simulation options. It can be called:
| •with a sequence of simulation options name and assigning corresponding values as arguments as show in the example below:
| setSimulationOptions(stopTime = 100, solver = 'euler')
|
| simulate(self)
| This method simulates model according to the simulation options. It can be called:
| •only without any arguments: simulate the model
Data descriptors defined here: |
dict |
dictionary for instance variables (if defined) |
weakref |
list of weak references to the object (if defined) |
Suivez le tutoriel officiel ci-dessous. La cible du calcul est le comportement de la balle qui rebondit. https://www.openmodelica.org/doc/OpenModelicaUsersGuide/OpenModelicaUsersGuide-v1.13.2.pdf
①. Chargez le modèle, déplacez le dossier, etc. pour exécuter le calcul
②. Exécution du calcul Sortie avec csv pour le traitement des résultats avec Pandas plus tard
③. Acquisition de la valeur calculée
④. Graphique des résultats
Python et Jupyter Lab sont intuitifs et faciles à utiliser. Si vous vous habituez aux Pandas, ce n'est probablement pas grave. J'étais convaincu que c'était un outil standard pour l'analyse des données avec une opérabilité facile. Cependant, puisque la confirmation et le traitement du résultat sont basés sur des commandes, comme MATLAB et Excel Je serais heureux si je pouvais créer un graphique en sélectionnant les données plus graphiquement avec la souris et en cliquant sur les boutons. Les traitements tels que l'exécution des calculs et le traitement des données (extraction des résultats nécessaires, moyenne et différence) sont effectués par Python. Je pense que vous pouvez utiliser Excel, etc. correctement pour la création de graphiques et la création de rapports pour la soumission des valeurs numériques traitées.
Postscript Dans l'article suivant, il y avait une considération très facile à comprendre sur la façon d'utiliser correctement Excel et Python. https://qiita.com/taka000826/items/ca8fa4a1a392728f688c
OMPython a bien fonctionné et a fait du bon travail. Après tout, je viens d'appuyer sur la commande OMShell telle quelle C'est très utile car Python couvre le comportement dans lequel OMShell n'est pas bon. Je continuerai à l'utiliser à l'avenir.
Recommended Posts