Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.

Troubles with operations on vectors/arrays

Please login with a confirmed email address before reporting spam

Hi, I am currently trying to do shape optimization for an acoustic waveguide. The approach that I take is heavily based on the following library from the COMSOL Application Gallery: https://www.comsol.com/model/tweeter-dome-and-waveguide-shape-optimization-89631.

One difference that I have is that I have different optimization criteria. I want the on-axis frequency response to be smooth. For this reason I want to minimize the variance of the frequency response at a particular point in space.

The things I have tried:

  1. Using the average function in COMSOL - this doesn’t work because it averages over a geometric entity (as opposed to averaging over frequency, even when using a point).
  2. Manually create the mean function and variance function using the following tutorial: https://www.comsol.com/blogs/getting-the-stats-computing-standard-deviations-and-other-statistical-quantities/ - So far this doesn’t work. I think this is because it now computes the mean/variance of each individual index of the vector.
  3. Manually compute mean/variance using the integrate/sum functionality – This doesn’t work for the same reason as (2).

After being stuck on this, seemingly trivial, issue. I figured I’d search the COMSOL forums. This person seems to have the EXACT same issue as I have, but no answers were given unfortunately: https://www.comsol.com/forum/thread/47147/Averaging-over-frequencies---into-a-variable

In general I find it quite difficult to understand how to do operations over an array/vector. For instance how can filter such a vector? How can I find the min/max values of it? I also don't understand how the optimization criteria deals with this. For instance, if I compute the SPL at a point in space using a probe and want to optimize for a certain target SPL. Then I can define my optimization criteria as (SPL_measured - SPL_target)^2, but the SPL_measured is frequency dependant. How does the optimization criteria deal with this? It becomes even more confusing if you have an objective function that is not just a point in space! Many thanks in advance!


5 Replies Last Post Aug 22, 2023, 7:46 a.m. EDT
Kristian Ejlebjærg Jensen COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 8 months ago Aug 17, 2023, 3:28 a.m. EDT
Updated: 8 months ago Aug 17, 2023, 7:01 a.m. EDT

Dear Hidde de Wit

The Frequency Domain and Wavelength Domain study step create a solution for every frequency/wavelength. The "solution" option in the Objective Function section of the (Shape/Topology) Optimization study step determines how the objectives for the different solutions are combined. By default the objectives are summed, but you can also choose to minimize the maximum if you use the MMA optimization method. You cannot make an objective function that combines global variables evaluated at different solutions arbitrarily.

A consequence of this is that the last objective you mention is easy to express in the COMSOL GUI , while the variance minimization is more difficult. You will essentially have to avoid creating different solutions. This is possible by repeating the geometry and/or having several physics interfaces. Ref 1. demonstrates the latter, but in practice you probably also care about the mean value and therefore the objective with a fixed target (instead of the mean) is probably a better choice for most engineering applications.

Ref 1: https://www.comsol.com/model/14513

Best regards,
Kristian E. Jensen
Technical Product Manager, Optimization

Dear Hidde de Wit The Frequency Domain and Wavelength Domain study step create a solution for every frequency/wavelength. The "solution" option in the Objective Function section of the (Shape/Topology) Optimization study step determines how the objectives for the different solutions are combined. By default the objectives are summed, but you can also choose to minimize the maximum if you use the MMA optimization method. You cannot make an objective function that combines global variables evaluated at different solutions arbitrarily. A consequence of this is that the last objective you mention is easy to express in the COMSOL GUI , while the variance minimization is more difficult. You will essentially have to avoid creating different solutions. This is possible by repeating the geometry and/or having several physics interfaces. Ref 1. demonstrates the latter, but in practice you probably also care about the mean value and therefore the objective with a fixed target (instead of the mean) is probably a better choice for most engineering applications. Ref 1: Best regards, Kristian E. Jensen Technical Product Manager, Optimization

Please login with a confirmed email address before reporting spam

Posted: 8 months ago Aug 18, 2023, 6:39 a.m. EDT

Hi Kristian E. Jensen, Thank you for your elaborate response. It saddens me that operations w.r.t. a set of solutions is not possible. However it is nice to know that I no longer need to search for solutions in this area I guess.

The issue now changes however from not being able able to use operations over various solutions to defining smoothness of the curve w.r.t. a solution set. The cost function (SPL_measured - SPL_target)^2 would essentially be a scaled mean deviation. However this could lead to a result that is really close on many frequencies and has very large deviation at one frequency ( a very spikey frequency response) or it could result in a frequency response that smoothly deviates from the target frequency response. It is perfectly fine if the loudspeaker deviates from the target SPL, provided that it does so smoothly.

The SPL is only part of the entire cost function. The other parameters would be the directivity index and beamwidth. However the same issues arise there that I can't optimize frequency dependant behaviour.

Thanks in advance! Hidde

Hi Kristian E. Jensen, Thank you for your elaborate response. It saddens me that operations w.r.t. a set of solutions is not possible. However it is nice to know that I no longer need to search for solutions in this area I guess. The issue now changes however from not being able able to use operations over various solutions to defining smoothness of the curve w.r.t. a solution set. The cost function (SPL_measured - SPL_target)^2 would essentially be a scaled mean deviation. However this could lead to a result that is really close on many frequencies and has very large deviation at one frequency ( a very spikey frequency response) or it could result in a frequency response that smoothly deviates from the target frequency response. It is perfectly fine if the loudspeaker deviates from the target SPL, provided that it does so smoothly. The SPL is only part of the entire cost function. The other parameters would be the directivity index and beamwidth. However the same issues arise there that I can't optimize frequency dependant behaviour. Thanks in advance! Hidde

Acculution ApS Certified Consultant

Please login with a confirmed email address before reporting spam

Posted: 8 months ago Aug 22, 2023, 3:45 a.m. EDT
Updated: 8 months ago Aug 22, 2023, 3:46 a.m. EDT

"Then I can define my optimization criteria as (SPL_measured - SPL_target)^2, but the SPL_measured is frequency dependant."

It seems that you think that the SPL_target has to a constant, but that is not the case, it too will be frequency dependent, or at least can be. Or am I misunderstanding you problem? This does not seem as a COMSOL issue, but a general issue about how to define targets across a frequency range, and no matter how you define them, you always risk getting a result that fits with the mathematical targets, but is still not what you really wanted if you just put the target down in words. So yes, you can get dips or peaks in the final response, but there are typically ways to avoid that, or at least lessen the problem.

The other link you have to a question seems different from what you are looking for. They want to optimize for the actual average value across all frequencies as a single number, whereas you want smoothness across the frequency response. Looking only at the average like that would not guarantee smoothness at all.

-------------------
René Christensen, PhD
Acculution ApS
www.acculution.com
info@acculution.com
"Then I can define my optimization criteria as (SPL_measured - SPL_target)^2, but the SPL_measured is frequency dependant." It seems that you think that the SPL_target has to a constant, but that is not the case, it too will be frequency dependent, or at least can be. Or am I misunderstanding you problem? This does not seem as a COMSOL issue, but a general issue about how to define targets across a frequency range, and no matter how you define them, you always risk getting a result that fits with the mathematical targets, but is still not what you really wanted if you just put the target down in words. So yes, you can get dips or peaks in the final response, but there are typically ways to avoid that, or at least lessen the problem. The other link you have to a question seems different from what you are looking for. They want to optimize for the actual average value across all frequencies as a single number, whereas you want smoothness across the frequency response. Looking only at the average like that would not guarantee smoothness at all.

Please login with a confirmed email address before reporting spam

Posted: 8 months ago Aug 22, 2023, 5:13 a.m. EDT

It seems that you think that the SPL_target has to a constant, but that is not the case, it too will be frequency dependent, or at least can be. Or am I misunderstanding you problem?

I understand that I can define an arbitrary frequency response in the objective (I refer to it as SPL_target in my previous comment). Similar to how we define the transfer function of a linear system, say 1/(s+1) we can very simply define such a curve in COMSOL by substituting s = j* omega). While the target is then clear it, like you said, doesn't guarantee smoothness of the curve.

Preferably we want to objective function to punish the behaviour that we do not want and reward the behaviour that we do want. Intuitively this would suggest that we want to minimize the variance (perhaps even convolute with a high pass filter and then minimize the variance). Kristian Ejlebjærg Jensen's comment suggests that it is difficult to compute the variance of frequency response in the objective function. Then the question arises, if we can't use operators in a loss function w.r.t. the simulation data, then how can we define a function that constraints our optimization problem such that only smooth solutions are considered?

The other link you have to a question seems different from what you are looking for. They want to optimize for the actual average value across all frequencies as a single number, whereas you want smoothness across the frequency response. Looking only at the average like that would not guarantee smoothness at all.

It is true that the question only considers the mean. However since Var(X) = E( (X-mean(X))^2), it seems fairly simple to compute the variance once you have the mean.

>It seems that you think that the SPL_target has to a constant, but that is not the case, it too will be frequency dependent, or at least can be. Or am I misunderstanding you problem? I understand that I can define an arbitrary frequency response in the objective (I refer to it as SPL_target in my previous comment). Similar to how we define the transfer function of a linear system, say 1/(s+1) we can very simply define such a curve in COMSOL by substituting s = j* omega). While the target is then clear it, like you said, doesn't guarantee smoothness of the curve. Preferably we want to objective function to punish the behaviour that we do not want and reward the behaviour that we do want. Intuitively this would suggest that we want to minimize the variance (perhaps even convolute with a high pass filter and then minimize the variance). Kristian Ejlebjærg Jensen's comment suggests that it is difficult to compute the variance of frequency response in the objective function. Then the question arises, if we can't use operators in a loss function w.r.t. the simulation data, then how can we define a function that constraints our optimization problem such that only smooth solutions are considered? >The other link you have to a question seems different from what you are looking for. They want to optimize for the actual average value across all frequencies as a single number, whereas you want smoothness across the frequency response. Looking only at the average like that would not guarantee smoothness at all. It is true that the question only considers the mean. However since Var(X) = E( (X-mean(X))^2), it seems fairly simple to compute the variance once you have the mean.

Acculution ApS Certified Consultant

Please login with a confirmed email address before reporting spam

Posted: 8 months ago Aug 22, 2023, 7:46 a.m. EDT

I have had similar challenges, only with topology optimization. I have some work-arounds that are proprietary for now involving dynamically changing targets, but for now I think you should try with the standard approach first just with some target 'filter' function and see if the issues mentioned occur or not. It becomes particular problematic when both on-axis and off-axis is desired smooth but with no particular target curves easily defined, so I fully understand what you are trying to achieve.

-------------------
René Christensen, PhD
Acculution ApS
www.acculution.com
info@acculution.com
I have had similar challenges, only with topology optimization. I have some work-arounds that are proprietary for now involving dynamically changing targets, but for now I think you should try with the standard approach first just with some target 'filter' function and see if the issues mentioned occur or not. It becomes particular problematic when both on-axis and off-axis is desired smooth but with no particular target curves easily defined, so I fully understand what you are trying to achieve.

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.