Working with Input and Output
You can reference a version in the database as an input source or an output target from a model. The referenced version itself is identified by an item version location URI.
A referenced file version is loaded on demand from the database to a temporary working copy directory located on the computer running COMSOL Multiphysics. Any input read by the model, and any output written by the model, goes via files in this directory. You save the contents of the working copy directory as a new file version in the database by specifying the model tag identifier of the model and the location URI of the file version.
A location URI for a file version is written using the so-called
dbfile
file scheme. A location URI for a model version does not use a file scheme.
•
Copying Model and File Locations
•
Loading and Saving Auxiliary Data Files Stored in Databases
Examples
Set a file version containing a single file resource as an input source for an interpolation function feature.
SearchBranchItemResult result = branch
.searchItems("my_interpolation.txt")
.single();
FileItemVersion version = database
.fileVersionByKey(result.itemVersionKey());
String fileLocationUri = version
.firstFileResourceOrNull()
.fileLocationUri();
FunctionFeature f = model.func("int1");
f.set("source", "file").set("filename", fileLocationUri);
f.importData();
Set a fileset version containing a CAD assembly as an input source for a geometry feature.
SearchBranchItemResult result = branch
.searchItems("My Cad Assembly")
.single();
FileItemVersion version = database
.fileVersionByKey(result.itemVersionKey());
// Find the file resource corresponding to the main assembly file
// in the fileset.
String fileLocationUri = "";
for (FileResourceResult resource : version.fileResources()) {
if (resource.path().filename().endsWith(".asm")) {
fileLocationUri = resource.fileLocationUri();
break;
}
}
GeomSequence gs = model.component("comp1").geom("geom1");
GeomFeature f = gs.create("imp1", "Import");
f.set("filename", fileLocationUri);
f.importData();
Set a model version as an input source for a geometry part.
SearchBranchItemResult result = branch
.searchItems("My Geometry Part @part:geometry").single();
ModelItemVersion version = database
.modelVersionByKey(result.itemVersionKey());
String modelLocationUri = version.modelLocationUri();
// We assume that the geometry part has the tag identifier "part1".
String[] partTags = new String[]{"part1"};
model.geom().load(partTags, modelLocationUri, partTags);
Set a new file as the output target for a mesh export.
// Declare a file location URI for a new, but not yet created, file.
String locationUri = branch.newFileLocationUri("my_mesh.mphbin");
// Export the mesh as a working copy.
MeshSequence m = model.component("comp1").mesh("mesh1");
m.export().set("filename", locationUri);
m.export(location);
// Save the working copy to the branch as a new file version.
SaveFileItemParamGenerator p = DatabaseApiUtil.param()
.forSaveFile()
.withSourceItemVersionWorkingCopy(model.tag(), locationUri);
FileItemVersion version = branch
.saveFile(p, "Saved an exported mesh.");
Set an existing file as the output target for an HTML report.
SearchBranchItemResult result = branch
.searchItems("my_report.html")
.single();
FileItemVersion version1 = database
.fileVersionByKey(result.itemVersionKey());
String location = version1
.firstFileResourceOrNull()
.fileLocationUri();
ReportFeature r = model.result().report("rpt1");
r.set("filename", location);
r.run();
SaveFileItemParamGenerator p = DatabaseApiUtil.param()
.forSaveFile()
.withSourceItemVersionWorkingCopy(model.tag(), location);
FileItemVersion version2 = branch
.saveFile(p, "Updated a report.");