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
Posted 2023年8月3日 GMT+8 17:46 General 6 Replies
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.
}
}
Attachments:
Please login with a confirmed email address before reporting spam
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
Please login with a confirmed email address before reporting spam
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
Please login with a confirmed email address before reporting spam
You can submit model parameters to an external material and receive states from it.
-------------------Edgar J. Kaiser
emPhys Physical Technology
www.emphys.com
Please login with a confirmed email address before reporting spam
You can submit model parameters to an external material and receive states from it.
Do you have a simple example of what you describing?
Please login with a confirmed email address before reporting spam
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
Please login with a confirmed email address before reporting spam
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
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.
Suggested Content
- BLOG Using the Chatbot Window in COMSOL Multiphysics®
- KNOWLEDGE BASE Running parametric sweeps, batch sweeps, and cluster sweeps from the command line
- FORUM Java data manipulation in app builder
- FORUM How to save the COMSOL App Builder as java code?
- FORUM small problem with code in Application Builder
