Docs/Custom Components/Uploading MTK Component Files

Uploading MTK Component Files

Applies to: CoModel cloud modeling & simulation platform (v1.7+)

1. Overview

CoModel offers three ways to create custom MTK components:

Approach Entry point Use case
Upload Library This article You have already written a .jl file locally with Julia/ModelingToolkit and want to import it in bulk
Graphical Component Graphical Editor Drag existing components together into a composite component — see Creating MTK Components Graphically
New Component Online Component Editor Write code directly in the browser — see Online Component Editor

All three converge on the same result: the platform parses out the component definitions (name, ports, parameters) and mounts them under the User Components group in the Component Browser, where they can be searched, dragged onto the canvas, and used in simulations just like standard library components.

This article first introduces the My MTK Components page, then walks through the complete upload workflow.


2. The My MTK Components page

Creation and management of custom MTK components are concentrated on the My MTK Components page:

  1. Click the Components group in the left navigation bar.
  2. Click MTK Components in the expanded submenu.

My MTK Components page

2.1 Page layout

From top to bottom the page has three areas:

Area Position Description
Title and actions Top Page title and description on the left; Refresh, New Component, Graphical Component, and Upload Library buttons on the right
Category filter Below the actions Rounded tags for All Categories, Uncategorized, and each administrator-created category; click one to filter the list
Library file list Body A table of all your library files, with sorting and pagination

2.2 Top action buttons

Button Purpose
Refresh Reloads the file list
New Component Opens the online editor to create a component (see Online Component Editor)
Graphical Component Opens the Graphical Editor to create a composite component (see Creating MTK Components Graphically)
Upload Library Opens the upload dialog to import a local .jl file (see below)

2.3 Library file list

Each row in the list is one library file — an uploaded .jl file, a component written online, or a graphical component:

Column Description
ID Numeric ID of the file
Name Display name of the file or component (official components carry an Official badge)
Category The custom category it belongs to; shows Uncategorized when not assigned
Source Upload / Online / Graphical
Parse Status Pending / Parsing / Parsed / Failed
Components Number of component definitions parsed out
Size Size of the original .jl file
Uploaded / Updated Creation time and last modification time
Actions Edit (online components only), Graphical Editor (graphical components only), Meta Info, Delete

Click any row to expand its details and see every component parsed from that file: component name, port count, parameter count, description, plus the name lists of inputs / outputs / parameters; when parsing failed, the specific error message is shown as well.

The list supports sorting by column header (click the same column again to toggle ascending/descending) and paginates automatically beyond 20 rows.


3. Uploading component files

3.1 Opening the upload dialog

Click the Upload Library button at the top right of the page to open the upload dialog.

3.2 Selecting a file and entering a name

Field Description
Name Display name of the library; if left blank, it is derived automatically from the file name (without the .jl suffix)
File Click the dashed area and pick a local .jl file in the file dialog

The front end performs basic validation first and reports immediately when a file does not comply:

Message Cause
Only .jl files are supported The extension is not .jl
File exceeds the 1 MB size limit A single file > 1 MB
File content is empty The file has no content

After a successful Upload submission, the dialog closes with the message "Uploaded, parsing in background", the file appears in the list with parse status Pending or Parsing.

Upload component dialog

3.3 File content requirements

Requirement Description
Extension Must be .jl
Single file size ≤ 1 MB (configurable by the administrator via an environment variable)
Total storage per user ≤ 50 MB (configurable by the administrator via an environment variable)
Content At least one @component function definition (@mtkmodel is no longer supported; see the note below)
Ports Components intended for modeling must expose external ports; see below and Julia Component Code Conventions
Naming Top-level component names must not collide with MTK standard library components (see §6)

A valid upload file may contain multiple component definitions. The following example file contains two components with electrical ports; after uploading, they can be dragged onto the canvas for modeling right away:

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
using ModelingToolkitStandardLibrary.Electrical: OnePort

@component function MyResistor(; name, R = 1.0)
    @named oneport = OnePort()
    @unpack v, i = oneport
    pars = @parameters begin
        R = R, [description = "Resistance"]
    end
    eqs = [
        v ~ R * i,
    ]
    return extend(System(eqs, t, [], pars; name), oneport)
end

@component function MyCapacitor(; name, C = 1.0e-6)
    @named oneport = OnePort()
    @unpack v, i = oneport
    pars = @parameters begin
        C = C, [description = "Capacitance"]
    end
    eqs = [
        D(v) ~ i / C,
    ]
    return extend(System(eqs, t, [], pars; name), oneport)
end

Multi-component files are parsed in batch; each component gets its own row in the database. The @mtkmodel macro has been removed in the MTK version of the current environment — components must be defined with @component function. For the full coding conventions, see Julia Component Code Conventions.


4. Parse status polling

After a successful upload the back end returns 202 { library_file_id } and starts an asynchronous parsing job. While any file in the list is unfinished, the page polls and refreshes the status automatically every 3 seconds until it completes or fails.

Status Meaning Presentation
Pending Waiting to be processed Gray badge + clock icon
Parsing Being parsed Blue badge + spinner icon
Parsed Parsing completed Green badge listing the component count
Failed Parsing error Red badge; expand the row to see the error details

5. Managing uploaded libraries

5.1 Viewing component details

Library file list

Click a file row in the list to expand its details, showing all components parsed from that file:

Field Description
Component name The type name of the @component function
Port / parameter count Total number of parsed ports and parameters
Description The component's description (if any)
Inputs / Outputs / Parameters The name label list of each

5.2 Editing meta info

Click the pencil icon on a file row (Meta Info) to modify:

  • Name: the display name of the library;
  • Category: assign it to an administrator-created category, or keep it Uncategorized;
  • Official marking (visible to administrators only): mark the whole library file as Official; the marking cascades to all components in the file, makes them visible to every user, and survives re-parsing.

Editing library meta info

5.3 Updating component definitions

How you update a library depends on its source:

Source How to update
Online Click Edit in the Actions column to open the online editor; modify and save to overwrite the original definition
Graphical Click the Graphical Editor icon in the Actions column, adjust, and save
Upload The platform provides no in-place editing; modify the local file, then delete the old library and upload again (deletion is refused while its components are referenced — release the references first, see 5.4), or rebuild and overwrite it in the Online Component Editor

After an overwrite update, saved models do not automatically pick up the new definition. You must reopen and re-save the model for the new component definition to be inlined into the model file.

5.4 Deleting a file

  1. Click Delete on a file row; a confirmation dialog appears:

Delete library "{name}"? All components parsed from it will also be deleted. This action cannot be undone.

  1. Once confirmed, the system cascades the deletion to the database records of the file and its components, and to the original .jl file on disk.

If any component of this library is in use by another composite component or system model, deletion is refused (409) and the names of the referencing objects are listed. Release the references first (replace or delete the models / composite components that use them), then delete.


6. Naming conflicts

6.1 Name conflicts with the standard library

If a top-level component name (mtk_name) collides with an MTK standard library component, e.g. Resistor or Capacitor, the upload is rejected outright (422), and the error message lists the conflicting names:

Component names conflict with ModelingToolkitStandardLibrary: Resistor. Please rename them or wrap them in a module block.

Two ways out:

  • Rename: change the component names in the source and upload again (recommended);
  • Wrap in a module: wrap the definitions in a module ... end block. Components inside a module are addressed through module-qualified names and do not conflict with the standard library.

6.2 Name conflicts with your own components

Two components with the same name cannot coexist under one user (uploader_id + mtk_name is unique). When the upload collides with a component in one of your existing libraries, the back end returns 409 Conflict listing the conflicting names. The upload dialog shows the error; to overwrite, open the corresponding library file in the Online Component Editor and modify it, or delete the old library first and upload again.

Components of different users are fully isolated and never conflict.


7. Extra dependencies

If your component source contains using SomePackage and SomePackage is not installed in the CoModel simulation environment (the service/core repository), simulation fails with compile_error. Recommendations:

  • Stick to preinstalled packages such as ModelingToolkit and ModelingToolkitStandardLibrary whenever possible.
  • If a third-party package is unavoidable, contact the administrator in advance to confirm it is installed.
  • When semantic parsing fails, the file status is set to Failed; expand the row to see the details.

8. FAQ

Q1: Why does the parse status stay on "Parsing" after upload?

The semantic stage of parsing actually loads the components in a separate MTK subprocess, which takes seconds; while unfinished files exist, the page refreshes automatically every 3 seconds. If it does not complete for a long time, refresh the page or ask the administrator to check the back-end job status.

Q2: Why is the file still listed after parsing failed?

If the synchronous validation at upload time (size, syntax, definition detection, standard-library name conflicts) fails, the file is not saved. If the asynchronous semantic parsing fails, the file is kept with status Failed; expand the row to see the error details.

Q3: Can components be spread across multiple files?

Yes. Each .jl file is uploaded and parsed independently. Within one user, mtk_name must not repeat across files. Cross-file references between components are allowed (e.g. extend a type defined in another file); parsing automatically loads the source of the other components you can see.


9. Next steps