Note: This discussion is about an older version of the COMSOL Multiphysics® software. The information provided may be out of date.

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.

Automatically export integrated data between parametric sweep

Please login with a confirmed email address before reporting spam

Hello,

I am using COMSOL 4.2a, on a 64-bit windows 7 machine.

I am solving the "Heat Transfer" and several "Diluted Specie" concentrations. I want to run a parametric sweep and export a specie inventory vs time for each parameter in the sweep.

I have tried finding discussions online, however, I cannot seem to find a solution.

When I choose "keep solutions in memory" to be "all", the tables in the results node only seem to have the solution from the last parameter. Also, when I try to use the "derived values" in the results node, again, it seems the only solution saved is one from the last parameter.

If necessary, am also trying to familiarize myself with the Java API, so if there is some work-around with using that then I am interested to hear solutions from that end.

Any help or feedback would be greatly appreciated. Thank you!

1 Reply Last Post 3 oct. 2013, 00:47 UTC−4

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 3 oct. 2013, 00:47 UTC−4
I figured out a way to do this, in a seemingly unconventional way (but it works for me), and thought that I should share...

I am using the COMSOL Java API and running through Eclipse. The rest of my specs are listed in the question.
Instead of modifying a comsol java file, I created a new class and created a comsol model object
(so I using composition instead of inheriting from the comsol class, which I had trouble figuring out how to do).
From there I just made my own class member variables and functions to separate and automate the process by which the model is made.
Finally, I created member functions for creating a study, batch and my own "parametricStudy" function (which I included).
This essentially runs individual cases but loops through each of the parameters. The difference is that the export command follows each
iteration, so all of the data can be exported between each run... Here is the code:

import com.comsol.model.Model;
import com.comsol.model.util.ModelUtil;

//And define the class to my model:

public class myModel {

Model m; // so that the model can be manipulated
int iteration; // other member functions are included after..

public static void main(String[] args) {
run();
}

public static Model run() {
Model model = ModelUtil.create("Model");
myModel m = new myModel(model);
m.defineParams(model);

// this is where the model was constructed...

m.runParametricStudy(model);
return model;
}


public void runParametricStudy(Model model) {
this.iteration = 0;
while (this.iteration < this.paramValues.length) {
// Update
model.study("std1").feature("param").set("plist", this.paramValues[this.iteration]);
model.batch("p1").feature("saDef").set("filename", this.simulationName + "," + this.paramName + "=" + this.paramValues[this.iteration] + ".mph");

// Run Batch:
model.batch("p1").run();
// Evaluate tables (created earlier):
model.result().numerical("int1").setResult();
model.result().numerical("av1").setResult();
model.result().numerical("av2").setResult();
// Run plot:
model.result(this.inv).run();
model.result(this.dfvt).run();
model.result(this.pfvt).run();
// Export plot data:
this.exportResults(model);
this.iteration += 1;
}
}


public void exportResults(Model model) {
model.result().export(this.inv).set("filename", this.dataExportPath + this.inv + "," + this.paramName + "=" + this.paramValues[this.iteration]);
model.result().export(this.dfvt).set("filename", this.dataExportPath + this.dfvt + "," + this.paramName + "=" + this.paramValues[this.iteration]);
model.result().export(this.pfvt).set("filename", this.dataExportPath + this.pfvt + "," + this.paramName + "=" + this.paramValues[this.iteration]);
model.result().export().run();
model.result().export(this.inv).run();
model.result().export(this.dfvt).run();
model.result().export(this.dfvt).run();
}

What I found helpful with this approach (using batch().run(); on a parametric sweep instead of sol().runAll();) was
that each of the cases are saved in the directory I specified (there aren't too many so I can look at each individual solution),
and the data I want is automatically exported (with a unique name as not to overwrite the previous cases) between each parametric case.
I imagine that using the sol().runAll(); would also work too if you don't care to see any solutions but only the exported data.

I'm sorry if this wasn't presented in a clean way (time restraints), but I hope that this was helpful and I would appreciate any comments.

Regards,

Charlie
I figured out a way to do this, in a seemingly unconventional way (but it works for me), and thought that I should share... I am using the COMSOL Java API and running through Eclipse. The rest of my specs are listed in the question. Instead of modifying a comsol java file, I created a new class and created a comsol model object (so I using composition instead of inheriting from the comsol class, which I had trouble figuring out how to do). From there I just made my own class member variables and functions to separate and automate the process by which the model is made. Finally, I created member functions for creating a study, batch and my own "parametricStudy" function (which I included). This essentially runs individual cases but loops through each of the parameters. The difference is that the export command follows each iteration, so all of the data can be exported between each run... Here is the code: import com.comsol.model.Model; import com.comsol.model.util.ModelUtil; //And define the class to my model: public class myModel { Model m; // so that the model can be manipulated int iteration; // other member functions are included after.. public static void main(String[] args) { run(); } public static Model run() { Model model = ModelUtil.create("Model"); myModel m = new myModel(model); m.defineParams(model); // this is where the model was constructed... m.runParametricStudy(model); return model; } public void runParametricStudy(Model model) { this.iteration = 0; while (this.iteration < this.paramValues.length) { // Update model.study("std1").feature("param").set("plist", this.paramValues[this.iteration]); model.batch("p1").feature("saDef").set("filename", this.simulationName + "," + this.paramName + "=" + this.paramValues[this.iteration] + ".mph"); // Run Batch: model.batch("p1").run(); // Evaluate tables (created earlier): model.result().numerical("int1").setResult(); model.result().numerical("av1").setResult(); model.result().numerical("av2").setResult(); // Run plot: model.result(this.inv).run(); model.result(this.dfvt).run(); model.result(this.pfvt).run(); // Export plot data: this.exportResults(model); this.iteration += 1; } } public void exportResults(Model model) { model.result().export(this.inv).set("filename", this.dataExportPath + this.inv + "," + this.paramName + "=" + this.paramValues[this.iteration]); model.result().export(this.dfvt).set("filename", this.dataExportPath + this.dfvt + "," + this.paramName + "=" + this.paramValues[this.iteration]); model.result().export(this.pfvt).set("filename", this.dataExportPath + this.pfvt + "," + this.paramName + "=" + this.paramValues[this.iteration]); model.result().export().run(); model.result().export(this.inv).run(); model.result().export(this.dfvt).run(); model.result().export(this.dfvt).run(); } What I found helpful with this approach (using batch().run(); on a parametric sweep instead of sol().runAll();) was that each of the cases are saved in the directory I specified (there aren't too many so I can look at each individual solution), and the data I want is automatically exported (with a unique name as not to overwrite the previous cases) between each parametric case. I imagine that using the sol().runAll(); would also work too if you don't care to see any solutions but only the exported data. I'm sorry if this wasn't presented in a clean way (time restraints), but I hope that this was helpful and I would appreciate any comments. Regards, Charlie

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.