Skip to content

Add commonly used Blocks and Interfaces #4518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

m-kormann
Copy link
Contributor

@m-kormann m-kormann commented Jan 17, 2025

During development of our Modelica libraries, we centralize commonly used models and interfaces in the "DymolaModels" library, which is shipped with Dymola. Some of these models are so common that we want to give them back for the MSL.

Summary by CodeRabbit

  • New Features
    • Introduced multiple new control blocks offering varied input/output configurations for continuous systems.
    • Added an intelligent switching component to selectively route integer signals based on conditions.
    • Enhanced mathematical operations with blocks for computing scalar products and normalizing vectors.
    • Expanded nonlinear processing with components for output protection and smooth interpolation transitions.
    • Updated test models and reference data files to support and validate these new capabilities.

@beutlich beutlich added enhancement New feature or enhancement L: Blocks Issue addresses Modelica.Blocks labels Jan 17, 2025
@beutlich
Copy link
Member

Thanks for the PR. Can you please resolve the few issues reported by the static checks, see https://github.com/modelica/ModelicaStandardLibrary/pull/4518/files.

@m-kormann
Copy link
Contributor Author

Sure! All checks should pass now and I rebased the branch on current master.

@m-kormann m-kormann force-pushed the add-common-blocks branch 2 times, most recently from 2207c14 to ce9eaa2 Compare January 21, 2025 14:23
@beutlich beutlich force-pushed the add-common-blocks branch from 9375ce3 to e418a61 Compare April 13, 2025 16:03
Copy link

coderabbitai bot commented Apr 13, 2025

Walkthrough

This pull request introduces several new blocks and models across the Modelica library. In the Interfaces, Logical, Math, and Nonlinear packages, new blocks are added to support various continuous control interfaces, integer signal switching, vector operations, and nonlinear signal shifting. Additionally, new test models are added to ModelicaTest to demonstrate and verify the functionality of these blocks, along with reference comparison files for simulation validation. The changes extend the modeling capabilities and testing framework without modifying existing interfaces or logic.

Changes

File(s) Change Summary
Modelica/Blocks/Interfaces.mo Added partial blocks: SI2SO2, SI2MO, and MI2SO defining new continuous control interfaces with multiple inputs and outputs.
Modelica/Blocks/Logical.mo Added IntegerSwitch block with two integer inputs, one boolean input, and one integer output, implementing conditional switching logic with icon annotations.
Modelica/Blocks/Math.mo Added ScalarProduct block for dot product of two vectors and NormalizeVector block for vector normalization with parameters, equations, and icons.
Modelica/Blocks/Nonlinear.mo Added ZeroProtection, LinearShifter, SinSquareShifter, and VariableLinearShifter blocks to provide nonlinear and linear signal shifting and protection functionalities.
ModelicaTest/Blocks.mo Added test models Shifters, Vectors, and IntegerSwitch demonstrating usage of new blocks with experiment annotations.
ModelicaTest/Resources/Reference/.../comparisonSignals.txt Added new reference files listing expected output signals for IntegerSwitch, Shifters, and Vectors test models to support simulation result comparisons.

Sequence Diagram(s)

sequenceDiagram
    participant BoolStep as BooleanStep
    participant IntStep as IntegerStep
    participant Clock as ContinuousClock
    participant IntSwitch as IntegerSwitch
    participant R2I as RealToInteger

    BoolStep->>IntSwitch: Provide boolean input (u2)
    IntStep->>IntSwitch: Provide integer input (u3)
    Clock->>R2I: Provide timing signal
    R2I->>IntSwitch: Provide integer input (u1)
    IntSwitch->>IntSwitch: Switch output y based on u2 condition
Loading
sequenceDiagram
    participant Sine as Sine Generator
    participant ConstPos as Constant (positive)
    participant ConstNeg as Constant (negative)
    participant ZeroProt as ZeroProtection
    participant LinShift as LinearShifter
    participant SinSqShift as SinSquareShifter
    participant VarLinShift as VariableLinearShifter

    Sine->>ZeroProt: Input signal u
    Sine->>LinShift: Input signal u
    Sine->>SinSqShift: Input signal u
    Sine->>VarLinShift: Normalized input u
    ConstPos->>VarLinShift: Input u1
    ConstNeg->>VarLinShift: Input u2
Loading
sequenceDiagram
    participant Sine as Sine Generator
    participant Const1 as Constant 1
    participant Const2 as Constant 2
    participant Mux3 as Multiplex3
    participant Mux2 as Multiplex3 (second)
    participant Scalar as ScalarProduct
    participant Norm as NormalizeVector

    Sine->>Mux3: Input vector component
    Const1->>Mux3: Input vector component
    Sine->>Mux2: Input vector component
    Const2->>Mux2: Input vector component
    Mux3->>Scalar: Vector input u1
    Mux2->>Scalar: Vector input u2
    Scalar->>Norm: Vector output y
Loading

Poem

🐇 In fields of code where signals play,
New blocks arise to light the way.
Switches flip and vectors glide,
Shifters dance with graceful stride.
Protection guards the zero line,
Math and logic all align.
Hop along, the future’s fine! 🌟


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@beutlich beutlich force-pushed the add-common-blocks branch from e418a61 to 48f6f9d Compare April 22, 2025 04:55
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
Modelica/Blocks/Nonlinear.mo (2)

558-560: Fix broken hyperlink in the documentation

The HTML doc points to modelica://DymolaModels.Blocks.NonLinear.LinearShifter, which does not exist in the MSL and will yield a 404 in most browsers.
Replace by modelica://Modelica.Blocks.Nonlinear.LinearShifter to keep intra‑library navigation intact.


589-591: Typo: “sumOfWheightedInputs” → “sumOfWeightedInputs”

The misspelling does not affect simulation, but it shows up in the icon layer and auto‑generated diagrams. Renaming the component improves readability and keeps naming consistent with Modelica guidelines.

-    Modelica.Blocks.Math.Add sumOfWheightedInputs
+    Modelica.Blocks.Math.Add sumOfWeightedInputs
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e418a61 and 48f6f9d.

📒 Files selected for processing (8)
  • Modelica/Blocks/Interfaces.mo (3 hunks)
  • Modelica/Blocks/Logical.mo (1 hunks)
  • Modelica/Blocks/Math.mo (2 hunks)
  • Modelica/Blocks/Nonlinear.mo (2 hunks)
  • ModelicaTest/Blocks.mo (2 hunks)
  • ModelicaTest/Resources/Reference/ModelicaTest/Blocks/IntegerSwitch/comparisonSignals.txt (1 hunks)
  • ModelicaTest/Resources/Reference/ModelicaTest/Blocks/Shifters/comparisonSignals.txt (1 hunks)
  • ModelicaTest/Resources/Reference/ModelicaTest/Blocks/Vectors/comparisonSignals.txt (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (7)
  • ModelicaTest/Resources/Reference/ModelicaTest/Blocks/Vectors/comparisonSignals.txt
  • ModelicaTest/Resources/Reference/ModelicaTest/Blocks/IntegerSwitch/comparisonSignals.txt
  • ModelicaTest/Resources/Reference/ModelicaTest/Blocks/Shifters/comparisonSignals.txt
  • Modelica/Blocks/Logical.mo
  • Modelica/Blocks/Interfaces.mo
  • ModelicaTest/Blocks.mo
  • Modelica/Blocks/Math.mo
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: external_c_checks_cmake_windows-msvc
  • GitHub Check: external_c_checks_cmake_windows-cygwin
🔇 Additional comments (1)
Modelica/Blocks/Nonlinear.mo (1)

385-393: LinearShifter parameters look good – thanks for the built‑in sanity check

k and y0 are pre‑computed, final, and the initial equation already asserts u1 < u2, eliminating the divide‑by‑zero case. The block also forwards strict to the internal Limiter, giving users fine‑grained event control.
No additional findings here.

Comment on lines +490 to +507
parameter Real u1(start = 1)
"Lower boundary of input u, switching from constant value y1 to sin^2 interpolation";
parameter Real u2(start = 2)
"Upper boundary of input u, switching from sin^2 interpolation to constant value y2";
parameter Real y1(start = 0)
"Minimum value for y";
parameter Real y2(start = 1)
"Maximum value for y";

equation

if noEvent(u <= u1) then
y = y1;
elseif noEvent(u > u1 and u < u2) then
y = y1 + (y2 - y1) * sin((u - u1) / (u2 - u1) * pi / 2) ^ 2;
else
y = y2;
end if;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Missing guard for u2 – u1 ≠ 0 risks a division‑by‑zero

SinSquareShifter divides by (u2-u1). If the user accidentally sets u1 ≈ u2, the simulation fails hard.
Add an assert(u1 < u2, …) in initial equation (analogous to LinearShifter) to detect the configuration early.

+initial equation
+  assert(u1 < u2,
+    "SinSquareShifter: parameters must satisfy u1 < u2 to avoid division by zero.");
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
parameter Real u1(start = 1)
"Lower boundary of input u, switching from constant value y1 to sin^2 interpolation";
parameter Real u2(start = 2)
"Upper boundary of input u, switching from sin^2 interpolation to constant value y2";
parameter Real y1(start = 0)
"Minimum value for y";
parameter Real y2(start = 1)
"Maximum value for y";
equation
if noEvent(u <= u1) then
y = y1;
elseif noEvent(u > u1 and u < u2) then
y = y1 + (y2 - y1) * sin((u - u1) / (u2 - u1) * pi / 2) ^ 2;
else
y = y2;
end if;
parameter Real u1(start = 1)
"Lower boundary of input u, switching from constant value y1 to sin^2 interpolation";
parameter Real u2(start = 2)
"Upper boundary of input u, switching from sin^2 interpolation to constant value y2";
parameter Real y1(start = 0)
"Minimum value for y";
parameter Real y2(start = 1)
"Maximum value for y";
initial equation
assert(u1 < u2,
"SinSquareShifter: parameters must satisfy u1 < u2 to avoid division by zero.");
equation
if noEvent(u <= u1) then
y = y1;
elseif noEvent(u > u1 and u < u2) then
y = y1 + (y2 - y1) * sin((u - u1) / (u2 - u1) * pi / 2) ^ 2;
else
y = y2;
end if;

Comment on lines +258 to +270
// Parameter
parameter Real threshold(min = 0, start = Modelica.Constants.eps)
"Smallest value around zero, which the output can have";

equation

y =
if noEvent(u >= threshold or u <= -threshold) then
u
elseif noEvent(u >= 0) then
threshold
else
-threshold;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enforce a strictly positive threshold and guard against an ineffective block

threshold is declared with min = 0, which still permits the value zero.
If a user sets threshold = 0 (or is tempted by the default dialog spin‑box to do so), the block degenerates to y = u, defeating its stated purpose “avoid a value of zero”.
Adding an assert(threshold > 0, …) (or changing min = Modelica.Constants.eps) makes the intent explicit and prevents silent misuse.

parameter Real threshold(min = 0, start = Modelica.Constants.eps)
      "Smallest value around zero, which the output can have";

+initial equation
+  assert(threshold > 0,
+    "ZeroProtection: parameter 'threshold' must be strictly positive to be effective.");
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Parameter
parameter Real threshold(min = 0, start = Modelica.Constants.eps)
"Smallest value around zero, which the output can have";
equation
y =
if noEvent(u >= threshold or u <= -threshold) then
u
elseif noEvent(u >= 0) then
threshold
else
-threshold;
// Parameter
parameter Real threshold(min = 0, start = Modelica.Constants.eps)
"Smallest value around zero, which the output can have";
initial equation
assert(threshold > 0,
"ZeroProtection: parameter 'threshold' must be strictly positive to be effective.");
equation
y =
if noEvent(u >= threshold or u <= -threshold) then
u
elseif noEvent(u >= 0) then
threshold
else
-threshold;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or enhancement L: Blocks Issue addresses Modelica.Blocks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants