# Modelica コンポーネント設計・コーディングルール案 ## 汎用版:1D物理システムモデル向け ## 1. 基本方針 Modelicaモデルは、再利用性・交換性・保守性を高めるため、各コンポーネントの役割を明確に分離して作成する。 | 種類 | 役割 ||---|---|| `connector` | 物理量・信号量の接続仕様 || `partial model` | 共通構造を持つ基底モデル || `model` | 実際に使用する物理コンポーネント || `record` / `model System` | 共通パラメータ、基準値、設定値 || `function` | 再利用可能な計算式 || `package` | コンポーネント群の整理単位 | 基本方針は次の通り。 > **トップモデルは接続関係だけを記述する** > **物理式は各コンポーネント内部に閉じ込める** > **共通設定は `inner/outer System` で参照する** > **部品交換を想定する部分は `replaceable` にする** --- ## 2. 推奨パッケージ構造 ```modelicapackage GenericPhysicalSystem package Interfaces connector FluidPort connector ThermalPort connector MechanicalPort connector SignalPort end Interfaces; package Common model System partial model TwoPortBase partial model VolumeBase partial model LossBase partial model ActuatorBase end Common; package Components model Boundary model FlowResistance model FlowSource model ControlVolume model ValveLikeElement model RotatingElement model TranslatingElement end Components; package LossModels model NoLoss model ConstantLoss model QuadraticLoss model TableBasedLoss end LossModels; package Functions function smoothStep function pressureLossQuadratic function limitedSqrt end Functions; package Examples model TestSingleComponent model TestNetwork end Examples; end GenericPhysicalSystem; --- 3. コンポーネントの標準テンプレート model ComponentName // ============================================================ // 1. Imports / Replaceable packages // ============================================================ replaceable package Medium = Modelica.Media.Interfaces.PartialMedium; // ============================================================ // 2. Outer system reference // ============================================================ outer GenericPhysicalSystem.Common.System system; // ============================================================ // 3. Parameters // ============================================================ parameter Real A = 1.0e-6 "Representative area [m2]"; parameter Real C = 1.0 "Model coefficient [-]"; parameter Boolean useLoss = true "Enable loss calculation"; // ============================================================ // 4. Connectors // ============================================================ GenericPhysicalSystem.Interfaces.FluidPort inlet "Inlet port"; GenericPhysicalSystem.Interfaces.FluidPort outlet "Outlet port"; // ============================================================ // 5. Replaceable submodels // ============================================================ replaceable GenericPhysicalSystem.LossModels.NoLoss lossModel constrainedby GenericPhysicalSystem.Common.LossBase "Loss calculation model"; // ============================================================ // 6. Public variables // ============================================================ Real dp "Pressure difference [Pa]"; Real m_flow "Mass flow rate [kg/s]"; Real lossPower "Loss power [W]"; protected // ============================================================ // 7. Protected variables // ============================================================ Real rho "Representative density [kg/m3]"; initial equation // ============================================================ // 8. Initial equations // ============================================================ equation // ============================================================ // 9. Main equations // ============================================================ dp = inlet.p - outlet.p; rho = max(inlet.d, system.rho_min); m_flow = C*A*dp / sqrt(abs(dp) + system.eps); inlet.m_flow + outlet.m_flow = 0; inlet.m_flow = m_flow; outlet.m_flow = -m_flow; lossModel.dp = dp; lossModel.m_flow = m_flow; lossPower = lossModel.lossPower; // ============================================================ // 10. Validity checks // ============================================================ assert(A > 0, "Representative area A must be positive."); assert(C >= 0, "Model coefficient C must be non-negative."); annotation( Documentation(info=" <html> <p>Generic component template for 1D physical system modeling.</p> </html>"));end ComponentName; --- 4. モデル内部の記述順序 各コンポーネントでは、原則として次の順序に統一する。 1. replaceable package2. outer System3. parameter4. connector5. replaceable submodel6. public variable7. protected variable8. initial equation9. equation10. algorithm11. assert12. annotation この順序に統一することで、以下が容易になる。 モデルレビュー チーム内での保守 AIやスクリプトによる仕様抽出 1コンポーネント1枚の仕様スライド化 モデル間の差分確認 --- 5. コネクタ設計ルール 5.1 物理的に接続される量は connector を使う 流体系、熱系、機械系、信号系はそれぞれ専用のコネクタを用意する。 例: connector FluidPort Real p "Pressure [Pa]"; flow Real m_flow "Mass flow rate [kg/s]"; Real h "Specific enthalpy [J/kg]"; Real T "Temperature [K]"; Real d "Density [kg/m3]";end FluidPort; 5.2 単なる参照値は connector にしない 以下のような値はコネクタではなく、共通設定として扱う。 基準圧力 基準温度 最小許容値 数値安定化用の微小量 共通の計算設定 代表的な運転条件 モデル全体で共有する定数 これらは System モデルにまとめる。 model System parameter Real p_start = 1.0e5 "Initial pressure [Pa]"; parameter Real T_start = 300 "Initial temperature [K]"; parameter Real rho_min = 1.0e-3 "Minimum density [kg/m3]"; parameter Real eps = 1.0e-9 "Small value for regularization"; annotation(defaultComponentName="system");end System; --- 6. コンポーネント間の値の受け渡しルール トップモデルで内部変数を直接代入しない。 避けたい例: componentB.x = componentA.x;componentC.y = componentB.y; 推奨する書き方: connect(componentA.outlet, componentB.inlet);connect(componentB.outlet, componentC.inlet); 値の受け渡しは、次の基準で分類する。 渡したい量 推奨方法 圧力・流量・温度・エンタルピー 物理コネクタ力・変位・速度・トルク・角速度 機械コネクタ熱流・温度 熱コネクタ制御信号・開度・指令値 信号コネクタまたは input系全体の設定値 outer System部品内部だけの補助量 protected 変数複数部品で共有する状態 独立した共有コンポーネントとしてモデル化 --- 7. input / output の使用ルール input / output は、物理的な保存則を持たない信号的な受け渡しに限定する。 使ってよい例: input Real command "Command signal [-]";input Real speed "Prescribed speed [rad/s]";output Real sensorValue "Sensor output";output Real lossPower "Loss power [W]"; 避けたい例: input Real p "Pressure [Pa]";input Real m_flow "Mass flow rate [kg/s]";output Real T "Temperature [K]"; 圧力・流量・温度など、物理的接続として意味を持つ量は、原則としてコネクタで扱う。 --- 8. 状態量の配置ルール 状態量は、原則として「蓄積を持つコンポーネント」に置く。 状態量 置くべき場所 質量 流体の蓄積要素圧力 境界条件または蓄積要素温度 熱容量または蓄積要素内部エネルギー 熱・流体の蓄積要素体積 幾何を表す要素変位 機械運動要素速度 機械運動要素回転角 回転機械要素角速度 回転機械要素 --- 9. 保存則の書き方 9.1 質量保存 蓄積を持つ流体要素では、質量保存を明示する。 der(m) = inlet.m_flow + outlet.m_flow; 複数ポートの場合: der(m) = port1.m_flow + port2.m_flow + port3.m_flow; 9.2 エネルギー保存 必要に応じて、エネルギー保存を記述する。 der(U) = inlet.m_flow*inlet.h + outlet.m_flow*outlet.h + Q_flow - W_flow; ただし、目的に応じて以下のような簡略化を認める。 等温モデル 断熱モデル ポリトロープ近似 固定物性モデル テーブル参照モデル --- 10. 損失モデルのルール 損失計算は、コンポーネント内部に固定で直書きせず、交換可能なサブモデルとして扱う。 partial model LossBase input Real m_flow "Mass flow rate [kg/s]"; input Real dp "Pressure difference [Pa]"; output Real lossPower "Loss power [W]";equationend LossBase; 損失なし: model NoLoss extends LossBase;equation lossPower = 0;end NoLoss; 二乗損失: model QuadraticLoss extends LossBase; parameter Real K = 1.0 "Loss coefficient";equation lossPower = K*m_flow*abs(m_flow);end QuadraticLoss; コンポーネント側: replaceable LossModels.NoLoss lossModel constrainedby Common.LossBase; lossModel.m_flow = m_flow;lossModel.dp = dp;lossPower = lossModel.lossPower; この構造により、次のような差し替えが可能になる。 損失なし 一定損失 流量比例損失 流量二乗損失 テーブル補間損失 実測合わせ損失 詳細物理モデルによる損失 --- 11. equation と algorithm の使い分け 11.1 基本は equation Modelicaでは、物理式は原則として equation に書く。 適しているもの: 保存則 構成方程式 圧力と流量の関係式 力と変位の関係式 熱流と温度差の関係式 接続関係 11.2 algorithm を使ってよい場合 algorithm は手順的な処理に限定する。 適しているもの: テーブル探索 離散的な状態遷移 複雑な条件分岐 補助計算 最大値・最小値の更新 関数内部の逐次計算 単純な条件分岐は、なるべく equation 側に書く。 m_flow = if dp > 0 then C*sqrt(dp) else 0; --- 12. 不連続性の扱い 不連続な if、max、min、sign は、収束性やイベント発生に影響するため注意して使う。 避けたい例: m_flow = if dp > 0 then C*sqrt(dp) else 0; 改善例: m_flow = C*dp / sqrt(abs(dp) + system.eps); または、用途に応じて滑らかな近似関数を用意する。 function limitedSqrt input Real x; input Real eps; output Real y;algorithm y := sqrt(sqrt(x*x + eps*eps));end limitedSqrt; --- 13. 初期値設定ルール 状態量には、原則として初期値を与える。 Real p(start=system.p_start);Real T(start=system.T_start);Real m(start=system.m_start); 必要に応じて initial equation を使う。 initial equation p = system.p_start; T = system.T_start; ただし、初期方程式を入れすぎると過拘束になるため注意する。 --- 14. 妥当性チェック パラメータや状態量には assert を入れる。 assert(A > 0, "Representative area must be positive.");assert(T > 0, "Temperature became non-positive.");assert(p > 0, "Pressure became non-positive."); 特に確認すべき量: 量 チェック 面積 A > 0体積 V > 0密度 d > 0温度 T > 0圧力 p > 0係数 想定範囲内周波数 f > 0粘度 mu > 0時定数 tau > 0 --- 15. 命名ルール 15.1 モデル名 モデル名は、具体的な製品名ではなく、物理的役割で命名する。 ControlVolumeFlowResistanceFlowSourceBoundaryValveLikeElementHeatCapacityThermalResistanceRotatingInertiaTranslatingMassActuatorSensor 15.2 変数名 意味 推奨名 圧力 p温度 T密度 d または rho質量 m質量流量 m_flow体積流量 V_flow比エンタルピー h内部エネルギー U圧力差 dp変位 x速度 v力 f回転角 theta角速度 omegaトルク tau損失動力 lossPower 15.3 ポート名 2ポート系は、 inletoutlet または、 port_aport_b に統一する。 方向性を強く持つ部品: inletoutlet 方向性が弱い部品: port_aport_b 複数ポートの場合: mainPortbranchPortleakPortcontrolPortthermalPortmechanicalPort --- 16. コメント・ドキュメントルール 各モデルには、最低限以下を記載する。 annotation( Documentation(info=" <html> <h4>概要</h4> <p>このモデルの役割を記述する。</p> <h4>仮定</h4> <ul> <li>一次元集中定数モデル</li> <li>空間分布は考慮しない</li> <li>係数は一定値として扱う</li> </ul> <h4>主な式</h4> <p>代表的な支配式を記述する。</p> <h4>注意点</h4> <p>適用範囲や制限事項を記述する。</p> </html>")); --- 17. 1コンポーネント1枚仕様書を意識した記述 各モデルから、次の情報を抽出できるようにしておく。 - モデル名- 役割- 入力ポート- 出力ポート- 状態量- パラメータ- 主な式- 仮定- 適用範囲- 注意点 モデル先頭にコメントブロックを置いてもよい。 /*Component: ControlVolume Purpose: Lumped volume element for 1D physical system simulation. Ports: inlet : upstream fluid port outlet : downstream fluid port States: m, U, p, T Assumptions: - Lumped parameter model - Uniform pressure and temperature - No spatial distribution Main equations: - Mass conservation - Energy conservation - Equation of state*/ --- 18. トップモデルのルール トップモデルには、原則として次だけを書く。 1. inner System2. コンポーネント配置3. connect文4. 実験条件 annotation 例: model TestNetwork inner Common.System system; Components.Boundary boundary1; Components.FlowSource source; Components.FlowResistance resistance1; Components.ControlVolume volume1; Components.FlowResistance resistance2; Components.Boundary boundary2; equation connect(boundary1.outlet, source.inlet); connect(source.outlet, resistance1.inlet); connect(resistance1.outlet, volume1.inlet); connect(volume1.outlet, resistance2.inlet); connect(resistance2.outlet, boundary2.inlet); annotation( experiment(StartTime=0, StopTime=1.0, Interval=1e-4));end TestNetwork; トップモデルには、以下のような内部変数の直接代入を書かない。 componentB.x = componentA.x;componentC.y = componentB.y; --- 19. 汎用化した議論用の表現 具体的な対象を隠したい場合は、次のように表現する。 具体的な表現 汎用的な表現 圧縮室 可変容積要素吸入室 上流側容積要素吐出弁 一方向流れ要素給油回路 流体ネットワーク軸受 流体抵抗要素/漏れ流路要素排油室 蓄積容積要素クランクシャフト 回転機械要素ロータリー圧縮機 回転機械を含む1D物理システム冷媒 作動流体油 液体作動媒体漏れ バイパス流れ/副流路損失 散逸項/損失項 --- 20. 議論用の短い説明文 本資料では、Modelicaによる1D物理システムモデルの作成において、コンポーネントごとの記述方法を平準化するためのコーディングルール案を整理する。 対象は、流体、熱、機械、信号を含む集中定数系の物理モデルとし、特定の製品や装置に依存しない汎用的な構造を目指す。 基本方針は、トップモデルを接続関係の記述に限定し、物理式は各コンポーネント内部に閉じ込めることである。 また、共通設定は System モデルで管理し、部品交換を想定する部分は replaceable として実装する。