Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed invalid s-parameters from wave port. #1904

Merged
merged 1 commit into from
Sep 19, 2024
Merged

Conversation

QimingFlex
Copy link
Contributor

@QimingFlex QimingFlex commented Aug 15, 2024

S-matrix is defined by:
CodeCogsEqn

where I is the current flows into the port, so I here introduced a invalidator to throw an error if the integral sign of CurrentIntegralAxisAligned doesn't match the port's direction. In the subsequent S-matrix computation, if the voltage path is inversely chosen, it results in a negative Z_R, leading to invalid S-matrix. To address this, we now flip the sign where a negative Z_R occurs, which corresponds to where a negative voltage is given. This yields the correct S-matrix.

@QimingFlex QimingFlex marked this pull request as draft August 15, 2024 20:35
@QimingFlex QimingFlex force-pushed the qiming/wave_port branch 2 times, most recently from 4d602ab to b626689 Compare August 15, 2024 20:45
@QimingFlex QimingFlex marked this pull request as ready for review August 15, 2024 20:46
@QimingFlex QimingFlex force-pushed the qiming/wave_port branch 2 times, most recently from dd6c50b to 63daa4e Compare August 15, 2024 21:00
Copy link
Collaborator

@momchil-flex momchil-flex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense but @weiliangjin2021 or ideally @dmarek-flex should know better.

@dmarek-flex
Copy link
Contributor

Thanks, this fix will work and help reverse user error. The only problem I find is that it kind of changes the Port setup behind the scenes. I much prefer the Validator approach that you did for the current integral, then how the sign of the voltage is changed in the port.

I think a better fix for this problem is to add another validator to the port for the voltage integral that ensures the positive terminal (and not the negative terminal) of the voltage integral is inside the current loop. This should be easy for the AxisAligned versions of the integrals. More work for the Custom versions. It would also be nice if you could extend your current integral validator to handle the case of the CustomCurrentIntegral2D (you just need to figure out if the vertices are ordered CW or CCW).

Setting up these integrals correctly is a bit of a pain, which is also why we are going to try and automate these steps.

@QimingFlex
Copy link
Contributor Author

@dmarek-flex I'm thinking the same way but I haven't figured out how we know if the terminal is positive. That's why I try to change it behind the scenes as the port impedance will tell us if the integral is correct or not.

@weiliangjin2021
Copy link
Collaborator

I much prefer the Validator approach that you did for the current integral, then how the sign of the voltage is changed in the port.

I'm thinking the same way but I haven't figured out how we know if the terminal is positive. That's why I try to change it behind the scenes as the port impedance will tell us if the integral is correct or not.

If pursuing that approach, I think you can just throw an error where you currently flip the sign.

But as @dmarek-flex also mentioned, before we have the automatic approach, there will be lots of pain setting up those path. I'm thinking if flipping the sign of voltage (and consequently the impedance) is all we need when voltage path is of the wrong direction, we can just silently handle it internally?

@QimingFlex
Copy link
Contributor Author

@weiliangjin2021 Yeah, agree it is a lot of pain setting these paths if seeking automation. I went through the formulations and am pretty sure that flipping the sign is all we need. Throwing an error where I flip the sign wouldn't be ideal I guess as this is in post-processing and would ask users to re-run the simulation.

@dmarek-flex
Copy link
Contributor

dmarek-flex commented Aug 19, 2024

@dmarek-flex I'm thinking the same way but I haven't figured out how we know if the terminal is positive. That's why I try to change it behind the scenes as the port impedance will tell us if the integral is correct or not.

Determining which terminal is the positive one is straightforward using the sign parameter. For the VoltageIntegralAxisAligned, when sign="+" the positive terminal is the terminal with the larger coordinate (the path is a simple straight line along one dimension). You can see the logic in the plotting function to double check.

@dmarek-flex
Copy link
Contributor

I'm thinking if flipping the sign of voltage (and consequently the impedance) is all we need when voltage path is of the wrong direction, we can just silently handle it internally?

That should work as a temporary fix until we improve other parts.

Comment on lines +241 to +248
negative_real_Z = np.real(Z_numpy) < 0
V_numpy = np.where(negative_real_Z, -V_numpy, V_numpy)
Z_numpy = np.where(negative_real_Z, -Z_numpy, Z_numpy)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a sanity check to make sure that the sign choice is consistent over all frequencies. The impedance should not change sign as the frequency changes (at least for typical transmission lines)

name = values.get("name")
if val.sign != direction:
raise ValidationError(
f"'current_integral' sign must match the '{name}' direction ({direction})."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add missing quotes around direction

@pd.validator("current_integral", always=True)
def validate_current_integral_sign(cls, val, values):
"""
Validate that the sign of current_integral matches the direction.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add double single quotes around current_integral

@dmarek-flex
Copy link
Contributor

After further discussion, let's keep it simple and just silently swap the voltage sign when the real part of the line impedance is found to be negative.

Let's just at least implement the sanity check to make sure the sign is the same for all frequencies.

@QimingFlex QimingFlex force-pushed the qiming/wave_port branch 3 times, most recently from 2be935c to a5d961c Compare September 12, 2024 15:39
@dmarek-flex
Copy link
Contributor

@QimingFlex please take a look at the changes I made and do a final check.

@@ -396,3 +397,16 @@ def plot(
arrowprops=ARROW_CURRENT,
)
return ax

@cached_property
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the ordering of vertices alone is not enough to determine the current flow. For example, consider two ports placed in the x-y plane. If we have the port direction as '+', then the ccw orientation represents '+' and there is no problem. While if we have the port direction as '-', then the sign of the current should be '-' as well. But with this approach, it will be incorrectly determined as '+'.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function only determines the sign of the current integral. The port direction and current integral sign are still compared in the WavePort's validator. In your example, the validator will correctly determine that there is a mismatch.

Copy link
Contributor

@dmarek-flex dmarek-flex Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just generalized the code you had before to include the CustomCurrentIntegral2D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, so this is a sign of the integral rather than the sign of current flow. The rest looks good, thanks!

@dmarek-flex
Copy link
Contributor

@QimingFlex ok I think you can squash, rebase, and merge now!

@QimingFlex QimingFlex merged commit 0ea2e3f into pre/2.8 Sep 19, 2024
15 checks passed
@QimingFlex QimingFlex deleted the qiming/wave_port branch September 19, 2024 03:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants