modelicaopenmodelicafmi

How to Implement a Causal Adaptor for Electrical Components in OpenModelica?


I am a beginner in Modelica and recently I have been working on exporting some electrical components as Functional Mock-up Units (FMUs). Based on the information I found in this article, it appears that Modelica connectors are acausal, and therefore, they cannot be directly converted into FMUs. The article suggests using an electrical adaptor as a bridge to achieve this.

However, I am unsure how to implement this in practice. Specifically, I am looking for guidance on how to create a causal adaptor in OpenModelica.

Additionally, I would like to understand the working principle of the causal adaptor described in the article. Specifically, why is it necessary to extract voltage (V) and current (I) in the process? How does this help in converting the acausal Modelica connectors into a causal form suitable for FMU export?

I expect to have the text view(in Modelica language) of the causal adaptor


Thanks for the answer from Markus that Modelica.Electrical.Analog.Examples.GenerationOfFMUs gives an example of transferring a electrical component into FMU. Based on this, I am trying to build a simple circuit with 1 voltage source and 1 resistor with GeneralCurrentToVoltageAdaptor and GeneralVoltageToCurrentAdaptor as below:

enter image description here

After that, I am trying to split this circuit into 3 parts (Voltage Source/Resistor/GND) and then export each part into FMU like below (please ignore the difference of voltage signal):

enter image description here

Eventually, I import these FMUs and connect them to do simulation, however, it failed during simulation and the log shows some error message about the solver I think

kinsol failed for system 122 [module] KINSOL | [function] KINSol | [error_code] -5 The line search algorithm was unable to find an iterate sufficiently distinct from the current iterate. KINSol finished with errorCode -5. kinsols line search did not convergence. Try without. kinsol failed for system 122 [module] KINSOL | [function] KINSol | [error_code] -7 Five consecutive steps have been taken that satisfy a scaled step length test. KINSol finished with errorCode -7. Newton step exceed the maximum step size several times. Try again after increasing maximum step size. kinsol failed for system 122 [module] KINSOL | [function] KINSol | [error_code] -11 The linear solver's setup function failed in an unrecoverable manner. KINSol finished with errorCode -11. KINSOL: The kinls setup routine (lsetup) encountered an error. Retry with numerical Jacobian. KINSOL: Trying to switch to numeric Jacobian for sparse solver KLU, but no sparsity pattern is available. Solution status: FAILED

If I only use 1 FMU to replace the corresponding part in Figure 1, it can do simulate successful and the result is correct, but I encounter this issue when I use more than 2 FMUs like below: enter image description here

What I want to ask:

  1. Is is possible to use multiple FMUs to represent each components in a electrical circuit?
  2. What is the reason that cause simulation fail when connecting multiple FMUs?
  3. How to solve problem 2?

Current Solution :

I reviewed the log file recording the simulation process information and found the following statement at the top of the content: initialize non-linear system solvers 2 non-linear systems Using sparse solver kinsol for nonlinear system 0 (106), because density of 0.04 remains under threshold of 0.10. KINSOL: log level 1 KINSOL: Using linear solver method klu Using sparse solver kinsol for nonlinear system 1 (221), because density of 0.04 remains under threshold of 0.10. KINSOL: log level 1 KINSOL: Using linear solver method klu The maximum density for using sparse solvers can be specified using the runtime flag '<-nlssMaxDensity=value>'.

Based on this description, I added -nlssMaxDensity=value (according to the error message, the value needs to be less than 0.10, so I set it to 0.05) in OpenModelica → Simulation Setup → Simulation Flags → Additional Simulation Flags (Optional):. After this, the simulation ran normally. I speculate that the cause of this issue is that the system detects density is smaller than the default value of -nlssMaxDensity, thus attempting to use the sparse solver instead of the dense solver. However, for some reason, the sparse solver might be unable to solve this non-linear system, leading to the termination of the simulation. Although the specific root cause is still unclear to me, this method indeed resolves the issue.


Solution

  • There is one example that should contain what you need to do: Modelica.Electrical.Analog.Examples.GenerationOfFMUs:

    Example of applying adapters to causal variables in Modelica

    Basically, adapters (Modelica.Electrical.Analog.Basic.GeneralCurrentToVoltageAdaptor and GeneralVoltageToCurrentAdaptor) are used to generate causal signals from the physical electrical pins defined in the Modelica Standard library. This is the recommended way to generate FMUs from physical models.

    Also, note that potentially you will need to set the parameters use_pder and/or use_fder to true when the FMU you create contains energy storing, which could be capacitors or inductors. If this needs to be done, the simulation environment should tell you add derivatives when you generate the FMU.