Please login with a confirmed email address before reporting spam
Posted:
1 decade ago
03.10.2013, 00:47 GMT-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