i_component as argument for MATC function

Numerical methods and mathematical models of Elmer
MartinHoeijmakers
Posts: 37
Joined: 22 Oct 2017, 21:03
Antispam: Yes

i_component as argument for MATC function

Post by MartinHoeijmakers »

Hi all,

I want to control a variable in a Body Force by means of a MATH function. If I use "Variable time" as argument, it works fine. However, I want to use a current in a circuit as a control argument. So I would like to do something like

Code: Select all

Body Force 1
  v2 = Variable i_component(1)
    Real MATC "test_function(tx)"
End
This doesn't work. Does anyone have a solution?
In fact, I want to control a voltage source in the circuit by means of a current in that circuit. This current doesn't have to be the current through that voltage source.

Kind regards,
Martin Hoeijmakers
raback
Site Admin
Posts: 4812
Joined: 22 Aug 2009, 11:57
Antispam: Yes
Location: Espoo, Finland
Contact:

Re: i_component as argument for MATC function

Post by raback »

Hi

This is tough one. I don't think the circuit variables have been created as variables inside Elmer. They can be accessed from a field called "Lagrangemultiplier" that is created because we are solving a linear system with some additional lines for constraints.

So in principle this could work as

Code: Select all

Body Force 1
  v2 = Variable "Lagrangemultiplier 13"
    Real MATC "test_function(tx)"
End
Even this would have two challenges. 1) What happens in the 1st iteration when the lagrange multipliers have not be created. 2) What index does your current of interest actually correspond to.

It would be not a big thing to have currents and voltages available as variables. Another thing is whether this is enough to have any kind of dependence within the circuit simulator as there may be some underlying assumptions of linearity.

-Peter
MartinHoeijmakers
Posts: 37
Joined: 22 Oct 2017, 21:03
Antispam: Yes

Re: i_component as argument for MATC function

Post by MartinHoeijmakers »

Hello Peter,

Thank you for the suggestion. I will just try what happens ;-)

Kind regards,
Martin
MartinHoeijmakers
Posts: 37
Joined: 22 Oct 2017, 21:03
Antispam: Yes

Re: i_component as argument for MATC function

Post by MartinHoeijmakers »

Hello Peter,

I tried

Code: Select all

   v_ac_source = Variable "Lagrangemultiplier 1"
    Real MATC "test_function(tx)"
and got the message:

ListParseStrToVars: Parsed variable 1 of lagrangemultiplier 1
ERROR:: ListParseStrToVars: Can't find independent variable:[lagrangemultiplier 1] for dependent variable:[v_ac_source]
STOP 1

Is this what you expected?


Kind regards,
Martin
raback
Site Admin
Posts: 4812
Joined: 22 Aug 2009, 11:57
Antispam: Yes
Location: Espoo, Finland
Contact:

Re: i_component as argument for MATC function

Post by raback »

Hi Martin,

Yes, that's what I meant. There is now functionality to export variables. It is not automated but you have to give keyword "Export Circuit Variables = Logical True". Basically it should be given in "CircuitsOutput" solver but you could also give it in the primary circuit variable if you need functional dependence. I think the variable name should be "crt i 1 (2,3,...) and for complex "crt i re 1" (2,3,...) and "crt i im 1". For voltage "crt v...".

-Peter
MartinHoeijmakers
Posts: 37
Joined: 22 Oct 2017, 21:03
Antispam: Yes

Re: i_component as argument for MATC function

Post by MartinHoeijmakers »

Hello Peter,

I saw that you added the subroutine Circuits_ToMeshVariable and I downloaded the fresh binaries. Thank you very much!

I did not add "Export Circuit Variables = Logical True" to the solver "CircuitsOutput" (this didn't work), but to the "main" solver "CircuitsAndDynamics".

Further, I have 2 circuits with each 6 currents (6 components). Looking at the source code, I changed the variable name "crt i 1" into "crt 1 i 1" (necessary for more than 1 circuit). So, the 6th current in the first circuit is "crt 1 i 6". That works well.
However, "crt 2 i 1" to "crt 2 i 6" for the currents in the 2nd circuit resulted in zeros?!

Was my interpretation of the source code not correct?

Kind regards,
Martin
raback
Site Admin
Posts: 4812
Joined: 22 Aug 2009, 11:57
Antispam: Yes
Location: Espoo, Finland
Contact:

Re: i_component as argument for MATC function

Post by raback »

Hi

Try to add the keyword to both solvers. The 1st one it is called it will be all zeros. The CircuitsOutput add the variable values. The circuit solver is needed probably since otherwise you can not have the initial dependence.

I had a look at the code but didn't find anything funny. We can still change the names if we invent some better ones. There are not too many users for this yet...

-Peter
MartinHoeijmakers
Posts: 37
Joined: 22 Oct 2017, 21:03
Antispam: Yes

Re: i_component as argument for MATC function

Post by MartinHoeijmakers »

Hello Peter,

Adding the keyword to both solvers didn't work.

I made a simple model with 2 circuits and everything went well. Next, I tried my big model with the function

Code: Select all

$ function test_function(c, circuitNumber)\
{\
   fprintf(stdout, "\n circuit: %g; c(0): %g\n\n", circuitNumber c(0));\
   _test_function = 0.0\
}
If I use

Code: Select all

Body Force 1    ! circuit
  v_ac_source = Variable "crt 2 i 1"
    Real MATC "test_function(tx, 1)"
  v_xz_source = Variable "crt 2 i 1"
    Real MATC "test_function(tx, 2)"
I get zeros for the first function call, but the right values for the second call, for all time steps.

If I use

Code: Select all

Body Force 1    ! circuit
  v_ac_source = Variable "crt 1 i 1"
    Real MATC "test_function(tx, 1)"
  v_xz_source = Variable "crt 2 i 1"
    Real MATC "test_function(tx, 2)"
everything works well!

Code: Select all

Body Force 1    ! circuit
  v_ac_source = Variable "crt 1 i 1"
    Real MATC "test_function(tx, 1)"
  v_xz_source = Variable "crt 1 i 1"
    Real MATC "test_function(tx, 2)"
also gives correct results.

Further, in all experiments, I noticed that all values are either the correct ones or zero. Knowing this, I can work with the second Body Force shown above.

I can imagine that there are not too many users for this, because it is quite complicated. I stopped trying about 4 years ago, but with the retry this year, I got a better understanding and I get very nice results with a synchronous generator with an external circuit. If I have some time, I will write some examples for learning working with circuits to help other people.

Thank you again,
Kind regards,
Martin
raback
Site Admin
Posts: 4812
Joined: 22 Aug 2009, 11:57
Antispam: Yes
Location: Espoo, Finland
Contact:

Re: i_component as argument for MATC function

Post by raback »

Hi

Yes, this is a lagging value only updated when the new subroutine is called. But if you are ok with values from previous timestep this should work ok for you.

I guess you meant that you have zero values as they have not been initialized and once they are everything is ok?

Is the naming convention ideal? I was thinking that the entities could also simply be named after the components. I.e. there would be "comp 1 i", "comp 2 i Re", "comp 4 v Im" etc. This could be easier since then just one number would suffice in choosing the correct field. It could be even "component 1 i" etc. making it perhaps more readable.

Also with this logic one could create other variables that could be associated to the components. So to follow the ciruits or the components?

-Peter
MartinHoeijmakers
Posts: 37
Joined: 22 Oct 2017, 21:03
Antispam: Yes

Re: i_component as argument for MATC function

Post by MartinHoeijmakers »

Hello Peter,

Sorry, I wasn't clear. In the first example of Body Force, the values for "crt 2 i 1" for v_ac_source were zero for all time steps. The values for "crt 2 i 1" for v_xz_source were all correct for all time steps (with a lagging). A bit strange isn't it?

However, I can use the second example of Body Force.

Kind regards,
Martin
Post Reply