Migration Guide
Use this guide to update code from the legacy Python interface to jcmsuite. The
jcmsuite package uses a clean-break redesign. It does not provide a compatibility
package or the legacy API shape.
Read Installation first. Then use the Quickstart to verify the local setup. Use the API Reference to check the current signatures.
Change the imports
Replace the legacy package import with the new package import:
# Legacy
import jcmwave
# jcmsuite
import jcmsuite
Configure the context
jcmsuite uses a context to manage the JCMsuite installation and connections to the
installation. The package manages a default context automatically. In many cases, you do
not need to manage a context.
A context is different from a daemon.
The default context uses the JCMsuite installation identified by JCMROOT. Before the
first solve, set JCMROOT to the JCMsuite installation directory. Alternatively, create a
context with create_context(jcm_root=...). Use the
default context for a local solve:
import jcmsuite
jcmsuite.info()
result = jcmsuite.solve("project.jcmp")
Create and use an explicit context with create_context when a script needs different installation or resource
settings:
import jcmsuite
context = jcmsuite.context.create_context(num_threads=4)
with context:
result = jcmsuite.solve("project.jcmp")
A context controls installation and runtime settings. A context is not a replacement for a project or a set of project keys.
See Contexts and the Context API for details.
Update solve calls
The project path is positional. All solve options, including keys=, are keyword-only:
# Legacy positional style
result = jcmwave.solve("project.jcmp", {"radius": 0.5})
# jcmsuite
result = jcmsuite.solve("project.jcmp", keys={"radius": 0.5})
See Solving Projects and the solve
reference for solve options and project inputs.
Batched Project Keys
A single set of project keys returns a SolveResult. A sequence of project keys returns a
BatchSolveResult:
batch = jcmsuite.solve(
"project.jcmp",
keys=[{"radius": 0.3}, {"radius": 0.4}],
)
This lets you solve one project with many sets of project keys without a loop. The returned value provides individual and batched results.
See Batched Results for batch-result access.
Access results
jcmwave provided all data through result-list entries. jcmsuite provides result data
through result attributes, for example
result.costs.
result = jcmsuite.solve("project.jcmp")
print(result.keys)
print(result.costs)
print(result.eigenvalues)
print(result.postprocesses.titles())
postprocess = result.postprocesses[0]
An optional property can be None when the project does not produce a value for the
property. Select a postprocess before you use its export methods. Use the typed loaders
for data in result files:
table = result.load_table("table.jcm")
fields = result.load_cartesian_fields("fieldbag.jcm")
Use jcmsuite.results.open to reopen a result directory or
a project result file.
See SolveResult Access for result attributes and loaders.
Result data export
jcmwave provided exporting to a dictionary. jcmsuite provides exports of result data
to dictionaries via to_dict(), to a
pandas DataFrame via to_df(), and to an
xarray DataTree via to_xarray().
The exact availability depends on the type of result data.
See Data Exporting for supported export methods.
Field export handling
When a postprocess exports field data, you can select, transform, and export the data.
import jcmsuite
result = jcmsuite.solve("project.jcmp")
field_export_postprocess = result.postprocesses[0]
fields = field_export_postprocess.fields
abs_transform_export = fields.transform("abs").to_xarray()
See Field Export Postprocess for field selection, transforms, and export methods.
Use a Resultbag
Pass a Resultbag to solve
to store and reuse results:
import jcmsuite
resultbag = jcmsuite.results.Resultbag("results.jcmrb")
result = jcmsuite.solve("project.jcmp", resultbag=resultbag)
Use get_results with keys= to load one
exact set of project keys or a sequence of exact sets. For a sequence, the returned batch
uses the same order as the supplied project keys:
one_result = resultbag.get_results(
keys={"material": "Si", "radius": 0.5},
)
batch = resultbag.get_results(
keys=[
{"material": "Si", "radius": 0.3},
{"material": "Si", "radius": 0.5},
],
)
Use filter to select Resultbag entries by
project keys when the selection is a range or another condition. Then use
get_results to load the selected results.
from jcmsuite.results.resultbag import between
resultbag = jcmsuite.results.Resultbag("results.jcmrb")
selected = resultbag.filter(radius=between(0.3, 0.5))
materialized = selected.get_results()
Advanced Resultbag methods are in the management namespace:
resultbag = jcmsuite.results.Resultbag("results.jcmrb")
rb_management = resultbag.management
See Resultbag for filtering and result reuse.
Use daemon execution
Use a daemon handler to control the daemon. Activate the handler with a with daemon:
statement. You can configure multiple resource types for different parts of the solve
process.
Add resources with daemon.resources.add.
import jcmsuite
daemon = jcmsuite.daemon.handler()
daemon.resources.add(
jcmsuite.daemon.Workstation(
hostname="localhost",
multiplicity=2,
n_threads=1
)
)
with daemon:
result = jcmsuite.solve("project.jcmp")
Note
When the application makes many solve calls, place the daemon context manager around all of them. Do not open and close a daemon context for each solve call because each context startup and shutdown adds lifecycle overhead. The same context can cover solve calls for projects in different files.
Waiting for results is optional. By default, the solve call returns after it collects all results, even when it uses the daemon.
To explicitly wait on results, use the dispatch keyword.
with daemon:
submission = jcmsuite.solve("project.jcmp", dispatch="submit")
result = submission.wait()
Use handler.lifecycle for startup
and shutdown. Use handler.resources to
configure compute resources. Use
handler.jobs to collect and manage Submissions. Preserve API
identifiers such as daemon.jobs and
submission.job_id when you update the
code.
See Daemon Execution and the Daemon API for handler and resource details.
Use asynchronous calls
When an event loop is running, pass asynchronous=True to a supported operation such as
solve. The operation returns a coroutine. Await the coroutine to
obtain the result:
import asyncio
import jcmsuite
async def main() -> None:
result = await jcmsuite.solve(
"project.jcmp",
asynchronous=True,
)
print(result.project_file)
asyncio.run(main())
In a script entry point, use asyncio.run(...). In a notebook or another running event
loop, await the operation in the existing event loop.
See Async Execution for asynchronous solve and submission handling.
Next steps
Core Functionality for solve inputs and project tools.
Working With Results for navigation and data export.
Resultbag for persistence and selection.
Runtime And Execution for contexts, daemon, and async use.
API Reference for current names and signatures.