ElmerCSC / elmer_circuitbuilder

Python module for creating the circuit simulation definitions for Elmer FEM
GNU Lesser General Public License v3.0
10 stars 6 forks source link

Complex source values not handled properly #6

Closed gijswl closed 5 months ago

gijswl commented 5 months ago

When using complex-valued current or voltage sources, the assigned body force will not have the correct magnitude and phase. It took quite some doubting myself and the simulation results before I traced this mistake to the circuit.definition.

Recreating the issue

I define three-phase currents I1, I2, I3, with identical magnitude and phase (0, +120, -120 deg).

phase = np.radians(0)
I1 = I("I1", 1, 2, Iph*np.exp(phase*1j))
I2 = I("I2", 1, 3, Iph*np.exp((phase + np.radians(120))*1j))
I3 = I("I3", 1, 4, Iph*np.exp((phase - np.radians(120))*1j))

In the resulting circuit definition, the general parameters look OK (although the absolute value of the real and imaginary part is given, which is an odd choice in my opinion).

! General Parameters 
! I1 = re_I1+ j im_I1, phase_I1 = 0.0(Deg)
$ re_I1 = 1000.0
$ im_I1 = 0.0
$ phase_I1 = 0.0
! I2 = re_I2+ j im_I2, phase_I2 = 119.99999999999999(Deg)
$ re_I2 = 499.9999999999998
$ im_I2 = 866.0254037844387
$ phase_I2 = 2.0943951023931953
! I3 = re_I3+ j im_I3, phase_I3 = -119.99999999999999(Deg)
$ re_I3 = 499.9999999999998
$ im_I3 = 866.0254037844387
$ phase_I3 = -2.0943951023931953

The issue occurs in the body force definition, where sin(phase) and cos(phase) are again applied to the real and imaginary parts, instead of to the magnitude.

Body Force 1
  I1_Source re = Real $ re_I1*cos(phase_I1)          => 1000.0 [correct]
  I1_Source im = Real $ im_I1*sin(phase_I1)          => 0.0 [correct]
  I2_Source re = Real $ re_I2*cos(phase_I2)          => -250.0 [incorrect, should be -500.0]
  I2_Source im = Real $ im_I2*sin(phase_I2)          => 750.0 [incorrect, should be 866.0254..]
  I3_Source re = Real $ re_I3*cos(phase_I3)          => -250.0 [incorrect, should be -500.0]
  I3_Source im = Real $ im_I3*sin(phase_I3)          => -750.0 [incorrect, should be -866.0254..]
End

Solutions

The code responsible for this is write_sif_additions for the body force, and write_parameters for the parameters. Possible solutions:

Until then, the easiest is to manually modify the generated circuit.definitions.

jvela018 commented 5 months ago

Hi @gijswl

Let me know if this resolves the issue.

/Jon

gijswl commented 5 months ago

Hi Jon, thanks for the quick patch. Body force is now generated as it should be. However, I see that the real and imaginary parts are still set to their absolute value.

! I2 = re_I2+ j im_I2, phase_I2 = 119.99999999999999(Deg)
$ re_I2 = 499.9999999999998
$ im_I2 = 866.0254037844387
! I3 = re_I3+ j im_I3, phase_I3 = -119.99999999999999(Deg)
$ re_I3 = 499.9999999999998
$ im_I3 = 866.0254037844387

The parameters re_I2, re_I3, and im_I3 should be negative. The issue is probably in lines 1977-1978 where the absolute value of the real and imaginary part is taken.

jvela018 commented 5 months ago

@gijswl ,

Please, upgrade your circuit builder version once again. Do you mind testing your actual simulation? I did remove the signs, however' I do remember that I did something hacky, with the signs, because on the finite element side one of them - the voltage or the current, have a different sign convention. Now that I pushed it, I'm not completely sure if this will have a negative effect on the FEM side.

BR,

/Jon

gijswl commented 5 months ago

Thanks Jon. I ran the simulation again, and it seems to work perfectly fine now. The phases of the three currents look good relative to each other. Absolute phase of the currents also seems OK, but I am unsure of the effects this could have if specifying, e.g., a voltage and a current source at the same time. Perhaps this sign convention is something that should be handled while writing the Body Force to the circuit.definitions, rather than in the real/imag part calculation?

Kind regards, Gijs

jvela018 commented 5 months ago

@gijswl ,

I have no control on how the Body force is handled on the FEM side. I just know it takes the real and imagination part as is. That's why I created the circuit builder. My guess, is that by eliminating the unnecessary sin/cos the sign convention was fixed by taking the values as hey are.

As of this, I'll consider this ticket resolved. Don't hesitate to reopen it if other issues appear.

/Jon

jvela018 commented 5 months ago

@gijswl,

After digging further, I remembered that the Voltage values are the ones that are reversed, and I handled it by adding the variable val_sign when I'm writing the Body Force (which Is the method I think you were trying to explain). So indeed, I don't handle the signs at the actual source value, but the parameter that goes into the Body Force. I just did a test with a three-phase voltage source and it seems to work just fine.

Upgrade your circuit builder once again - and thank you for your patience. /Jon