Categories
Blog COMSOL COMSOL General Articles FEA Research

Java method version tracker in COMSOL Application Builder

Bookmark (0)
Please login to bookmark Close

Auto-Increment Version and Date Comments in COMSOL Application Builder

Have you ever wished COMSOL could automatically track version numbers or update timestamps when working in the Application Builder? If you're building simulation apps or managing complex models, having a simple built-in versioning system can go a long way in documenting changes — even if it's just for your future self. In this post, I’ll show you how to implement a lightweight method in Java (COMSOL’s scripting language inside the Application Builder) that automatically increments a hidden version counter every time you run it, inserts the current date, and displays both in the property panel of the Global Parameters node.

Full Video

What You'll Get

Every time you run the method, the property panel will show:

Last updated: 2025-04-11   Version: 5

And a hidden parameter named autoversion will store the current version count.

The Code

Here's the complete method:

String versionTag = "autoversion";
int version = 1;
try {
  version = Integer.parseInt(model.param().get(versionTag)) + 1;
} catch (Exception e) {
  version = 1;
}
model.param().set(versionTag, Integer.toString(version));

long ms = java.lang.System.currentTimeMillis();
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setTimeInMillis(ms);

int year = cal.get(java.util.Calendar.YEAR);
int month = cal.get(java.util.Calendar.MONTH) + 1;
int day = cal.get(java.util.Calendar.DAY_OF_MONTH);

String comment = "<s>Last updated: " + year + "-" + month + "-" + day + "</s> Version: </cb> " + version + "<cb>";
model.param().comments(comment);

what the method does

This Java method runs entirely inside COMSOL’s built-in Method Editor. It first defines a parameter tag named autoversion, then tries to fetch and increment the value. If the parameter doesn’t yet exist, it defaults to version 1. After updating the version count, it uses Java’s Calendar class to get the current year, month, and day. These values are then formatted into a comment string, using internal COMSOL formatting tags like <s> and <cb> which visually style the comment in the property panel. Finally, the method sets that comment using model.param().comments(), updating the Settings window for the Parameters node.

why scripting with java matters in comsol

COMSOL’s integration with Java makes it far more than a drag-and-drop GUI simulation tool. Through scripting, you can build logic-driven workflows, respond to UI events, pre-process or post-process data, run iterative simulations, validate inputs, export reports, manipulate geometry, control mesh settings, or even generate fully interactive applications using the Application Builder. Unlike external automation tools, COMSOL Java methods are embedded and have full access to the model object, which means they can manipulate nearly every part of the simulation—from parameter values to solver configurations to graphical outputs.

how to use this versioning method

You can trigger this method using a custom button in your application, assign it as a pre-execution step before a study runs, or simply call it manually whenever you're saving or exporting your model. COMSOL does not currently expose a native event hook for save events, so running this method explicitly is still the best approach for automated version tagging. It’s a lightweight but powerful way to ensure you always know when the model was last changed and what version you're on. You could extend this method further to include the author name, append comments from a text input field, or even store a change log in a file or parameter array.

This example is a small window into what’s possible with Java scripting in COMSOL. With just a few lines of code, you can embed intelligence, traceability, and versioning directly into your models. Whether you’re building internal engineering tools, sharing apps with customers, or managing complex simulation workflows, Java scripting lets you take full control of the modeling process. It’s one of the most underrated and underutilized features of COMSOL—and the best part is, once you start using it, you’ll keep finding new ways to streamline and automate your simulation logic.

Note that this article and this technique is completely my personal experience and it has nothing to do with official documentation or methodologies. Please try at your own curiosity. This article is not affiliated with anyone else.

Discussions? let's talk here

Check out YouTube channel, published research

All product names, trademarks, and registered trademarks mentioned in this article are the property of their respective owners. Use of these names does not imply any affiliation, endorsement, or sponsorship. The views expressed are those of the author only. COMSOL, COMSOL Multiphysics, and LiveLink are either registered trademarks or trademarks of COMSOL AB.