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.

JAVA parallel code in Model Builder

Please login with a confirmed email address before reporting spam

Is there a way to run parallel code in Model builder? Here is example:

package builder;

import com.comsol.api.*;
import com.comsol.model.*;
import com.comsol.model.physics.*;
import com.comsol.model.application.*;
import java.util.*;

public class method11 extends ApplicationMethod {

  public void execute() {
    List<Integer> mylist = Arrays.asList(1, 2, 3);
    mylist.parallelStream().map(i-> debugLog(i));
    mylist.stream().map(i-> debugLog(i));
    // 'i' cannot be resolved to a variable.
  }
}


6 Replies Last Post 21 août 2023, 11:01 UTC−4
Edgar J. Kaiser Certified Consultant

Please login with a confirmed email address before reporting spam

Posted: 1 year ago 3 août 2023, 13:24 UTC−4

Hi,

I am not exactly sure what you mean by parallel code. It is possible to set up external functions and external material models. For this you need to make a DLL in your preferred development system and COMSOL will call the DLL at each solver call, e.g. at each time step. You can run any code in such a DLL. This is for Windows, I think the Linux version has similar mechanisms. The model builder allows model methods in JAVA, but I don't think they can be called during computation. I am sometimes using them to set parameters and for post processing. Check the documentation.

Cheers, Edgar

-------------------
Edgar J. Kaiser
emPhys Physical Technology
www.emphys.com
Hi, I am not exactly sure what you mean by parallel code. It is possible to set up external functions and external material models. For this you need to make a DLL in your preferred development system and COMSOL will call the DLL at each solver call, e.g. at each time step. You can run any code in such a DLL. This is for Windows, I think the Linux version has similar mechanisms. The model builder allows model methods in JAVA, but I don't think they can be called during computation. I am sometimes using them to set parameters and for post processing. Check the documentation. Cheers, Edgar

Please login with a confirmed email address before reporting spam

Posted: 1 year ago 4 août 2023, 02:54 UTC−4

Hi,

I am not exactly sure what you mean by parallel code. It is possible to set up external functions and external material models. For this you need to make a DLL in your preferred development system and COMSOL will call the DLL at each solver call, e.g. at each time step. You can run any code in such a DLL. This is for Windows, I think the Linux version has similar mechanisms. The model builder allows model methods in JAVA, but I don't think they can be called during computation. I am sometimes using them to set parameters and for post processing. Check the documentation.

Cheers, Edgar

HI Edgar, thanks for answer. There is way to use methods during computatin but basically I use JAVA methods for post processing.

The example I provided is legit JAVA code which works but not in Comsol. Thanks for the idea with external DLL. It's great and useful tool but it does not fit what I want to try.

For example we have parametric sweep with 3 steps, where 1, 2, 3 are indeces of those steps. In postprocessing we need to perform Global Evaluation for each step independently. I will omit imports, class name and execute method of the class method11 and show simple example:

int[] indeces = new int[]{1, 2, 3};
for (int i: indeces)
{
    with(model.result().numerical("sol2"));
        set("outersolnumindices", i);
    endwith();
    model.result().numerical("gev1").setIndex("expr", "2+" + toString(i), 0);
    model.result().numerical("gev1").setResult();
}
double[][] table = model.result().table("tbl1").getReal();

Previous code is a sequence. Next example suppose to be legit equivalent of previous.

int[] indeces = new int[]{1, 2, 3};
List<Integer> mylist = Arrays.asList(indeces);
mylist.parallelStream().map(i-> {
    with(model.result().numerical("sol2"));
        set("outersolnumindices", i);
    endwith();
    model.result().numerical("gev1").setIndex("expr", "2+" + toString(i), 0);
    model.result().numerical("gev1").setResult();
});
double[][] table = model.result().table("tbl1").getReal();
// 'i' cannot be resolved to a variable.

But this code should run for each index i an independent subprogram in the same time.

I suspect that it's impossible to do in the way I want but at least to have an option to run parallel JAVA code would be nice.

Best Regards, Kostiantyn Vasko

>Hi, > >I am not exactly sure what you mean by parallel code. It is possible to set up external functions and external material models. For this you need to make a DLL in your preferred development system and COMSOL will call the DLL at each solver call, e.g. at each time step. You can run any code in such a DLL. This is for Windows, I think the Linux version has similar mechanisms. >The model builder allows model methods in JAVA, but I don't think they can be called during computation. I am sometimes using them to set parameters and for post processing. >Check the documentation. > >Cheers, >Edgar HI Edgar, thanks for answer. There is way to use methods during computatin but basically I use JAVA methods for post processing. The example I provided is legit JAVA code which works but not in Comsol. Thanks for the idea with external DLL. It's great and useful tool but it does not fit what I want to try. For example we have parametric sweep with 3 steps, where 1, 2, 3 are indeces of those steps. In postprocessing we need to perform `Global Evaluation` for each step independently. I will omit imports, class name and `execute` method of the class `method11` and show simple example: int[] indeces = new int[]{1, 2, 3}; for (int i: indeces) { with(model.result().numerical("sol2")); set("outersolnumindices", i); endwith(); model.result().numerical("gev1").setIndex("expr", "2+" + toString(i), 0); model.result().numerical("gev1").setResult(); } double[][] table = model.result().table("tbl1").getReal(); Previous code is a sequence. Next example suppose to be legit equivalent of previous. int[] indeces = new int[]{1, 2, 3}; List mylist = Arrays.asList(indeces); mylist.parallelStream().map(i-> { with(model.result().numerical("sol2")); set("outersolnumindices", i); endwith(); model.result().numerical("gev1").setIndex("expr", "2+" + toString(i), 0); model.result().numerical("gev1").setResult(); }); double[][] table = model.result().table("tbl1").getReal(); // 'i' cannot be resolved to a variable. But this code should run for each index `i` an independent subprogram in the same time. I suspect that it's impossible to do in the way I want but at least to have an option to run parallel JAVA code would be nice. Best Regards, Kostiantyn Vasko

Edgar J. Kaiser Certified Consultant

Please login with a confirmed email address before reporting spam

Posted: 1 year ago 4 août 2023, 05:02 UTC−4

You can submit model parameters to an external material and receive states from it.

-------------------
Edgar J. Kaiser
emPhys Physical Technology
www.emphys.com
You can submit model parameters to an external material and receive states from it.

Please login with a confirmed email address before reporting spam

Posted: 1 year ago 18 août 2023, 07:37 UTC−4

You can submit model parameters to an external material and receive states from it.

Do you have a simple example of what you describing?

>You can submit model parameters to an external material and receive states from it. Do you have a simple example of what you describing?

Edgar J. Kaiser Certified Consultant

Please login with a confirmed email address before reporting spam

Posted: 1 year ago 20 août 2023, 17:15 UTC−4

There are examples in the documentation. Unfortunately all I have is protected by confidentiality agreements and it is not exactly simple as well.

-------------------
Edgar J. Kaiser
emPhys Physical Technology
www.emphys.com
There are examples in the documentation. Unfortunately all I have is protected by confidentiality agreements and it is not exactly simple as well.

Edgar J. Kaiser Certified Consultant

Please login with a confirmed email address before reporting spam

Posted: 1 year ago 21 août 2023, 11:01 UTC−4

I would also advise to contact COMSOL support. I got examples from them when I started to look into external materials, but I don't think I am allowed to share this with other users.

-------------------
Edgar J. Kaiser
emPhys Physical Technology
www.emphys.com
I would also advise to contact COMSOL support. I got examples from them when I started to look into external materials, but I don't think I am allowed to share this with other users.

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.