Found an issue with the book? Report it on Github.

물리적 자료형 가져오기(Importing Physical Types)

물리적 자료형 가져오기(Importing Physical Types)

이전 섹션에서 다른 패키지에 정의된 자료형을 참조하는 방법을 배웠습니다.이렇게 하면 개발자가 로컬 모델에서 항목을 지속적으로 정의하지 않아도 되며, 패키지에 정의를 배치한 다음 해당 패키지를 참조할 수 있습니다.

그러나 정규화되어 있는 이름이 긴 참조는 반복해서 입력하기에 지루할 수 있습니다.이러한 이유로 모델리카에는 로컬에서 정의된 것처럼 사용할 수 있는 import 문이 존재 합니다.

물리적 자료형(Physical Types) 에 대한 이전에 다루었던 이 예를 다시 생각해 보겠습니다.

within ModelicaByExample.BasicEquations.CoolingExample;
model NewtonCoolingWithTypes "Cooling example with physical types"
  // Types
  type Temperature=Real(unit="K", min=0);
  type ConvectionCoefficient=Real(unit="W/(m2.K)", min=0);
  type Area=Real(unit="m2", min=0);
  type Mass=Real(unit="kg", min=0);
  type SpecificHeat=Real(unit="J/(K.kg)", min=0);

  // Parameters
  parameter Temperature T_inf=298.15 "Ambient temperature";
  parameter Temperature T0=363.15 "Initial temperature";
  parameter ConvectionCoefficient h=0.7 "Convective cooling coefficient";
  parameter Area A=1.0 "Surface area";
  parameter Mass m=0.1 "Mass of thermal capacitance";
  parameter SpecificHeat c_p=1.2 "Specific heat";

  // Variables
  Temperature T "Temperature";
initial equation
  T = T0 "Specify initial value for T";
equation
  m*c_p*der(T) = h*A*(T_inf-T) "Newton's law of cooling";
end NewtonCoolingWithTypes;

이전 섹션에서는 모델리카 표준 라이브러리(Modelica Standard Library) 의 자료형을 사용하여 로컬에서 이러한 자료형을 정의하지 않고 기존의 자료형을 사용하는 방법을 설명했습니다. 그러나 import 명령을 사용하여 모델리카 표준 라이브러리에서 해당 자료형을 한 번 가져온 다음 정규화된 이름을 지정하지 않고도 사용할 수 있습니다.이에 대한 결과 코드는 다음과 같습니다.

within ModelicaByExample.PackageExamples;
model NewtonCooling
  "Cooling example importing physical types from the Modelica Standard Library"
  import Modelica.SIunits.Temperature;
  import Modelica.SIunits.Mass;
  import Modelica.SIunits.Area;
  import ConvectionCoefficient = Modelica.SIunits.CoefficientOfHeatTransfer;
  import SpecificHeat = Modelica.SIunits.SpecificHeatCapacity;

  // Parameters
  parameter Temperature T_inf=300.0 "Ambient temperature";
  parameter Temperature T0=280.0 "Initial temperature";
  parameter ConvectionCoefficient h=0.7 "Convective cooling coefficient";
  parameter Area A=1.0 "Surface area";
  parameter Mass m=0.1 "Mass of thermal capacitance";
  parameter SpecificHeat c_p=1.2 "Specific heat";

  // Variables
  Temperature T "Temperature";
initial equation
  T = T0 "Specify initial value for T";
equation
  m*c_p*der(T) = h*A*(T_inf-T) "Newton's law of cooling";
end NewtonCooling;

자료형 정의를 import 문으로 대체했는데, 강조 표시된 줄이 이전 코드와 어떻게 동일한지 확인 해보겠습니다.이러한 import 문은 두가지 사용 방법이 존재하는데, 이를 자세히 살펴보고 모델에 미치는 영향을 이해해야 합니다.첫번째 방식은 다음과 같습니다.

  import Modelica.SIunits.Temperature;

이것은 Modelica.SIunits.Temperature 자료형을 현재 모델로 가져옵니다. 기본적으로 이 가져온 자료형의 이름은 정규화된 마지막 이름이 됩니다.(예: * Temperature). 즉, 이 import 문이 있으면 단순히 Temperature 자료형 이름을 사용하는 경우, 자동으로 Modelica.SIunits.Temperature 를 다시 참조 합니다.

이제 다른 import 문을 살펴보겠습니다.

  import ConvectionCoefficient = Modelica.SIunits.CoefficientOfHeatTransfer;

여기에서의 구문은 약간 다릅니다. 이 경우 가져오는 자료형은 Modelica.SIunits.CoefficientOfHeatTransfer 인데, 정규화된 전체 이름중 마지막 이름을 기반으로 로컬 자료형을 만드는 대신 다른 이름으로 선언합니다.*즉,* CoefficientOfHeatTransfer 로컬 자료형이 ConvectionCoefficient 으로 연결되어야 한다고 지정하는 것입니다.이 경우 초기 예제에서 사용했던 것과 같이 원하는 이름을 사용할 수 있습니다.이러한 방식을 통해, 이전에 지정한 것과 동일한 이름을 사용하는 경우를 피하도록 코드 리팩토링을 할 수 있습니다.대체 이름(모델리카 컴파일러가 일반적으로 할당하는 기본 이름 이외)을 지정하는 또 다른 이유는 이름사이의 충돌을 피하기 위한 것입니다.두 개의 서로 다른 패키지에서 두 가지 자료형을 가져오고 싶다고 상상해 보겠습니다. 를 들어 아래와 같습니다.

import Modelica.SIunits.Temperature; // Celsius
import ImperialUnits.Temperature;    // Fahrenheit

이렇게 하면 Temperature 라는 두 가지 자료형이 남게 됩니다. 로컬 별칭(alternative name)에 대한 대체 이름을 정의하면 다음과 같이 할 수 있습니다.

import DegK = Modelica.SIunits.Temperature; // Kelvin
import DegR = ImperialUnits.Temperature;    // Rankine

논의할 가치가 있는 import 문의 마지막 형태가 있는데, 그것은 와일드카드 import 문입니다. 한 번에 한 단위씩 단위를 가져오는 것은 지루한 작업일 수 있지만, 와일드카드 가져오기를 사용하면 주어진 패키지에서 모든 자료형을 한 번에 가져올 수 있습니다. 이전의 예제를 다시 기억 해보겠습니다.

within ModelicaByExample.BasicEquations.RotationalSMD;
model SecondOrderSystem "A second order rotational system"
  type Angle=Real(unit="rad");
  type AngularVelocity=Real(unit="rad/s");
  type Inertia=Real(unit="kg.m2");
  type Stiffness=Real(unit="N.m/rad");
  type Damping=Real(unit="N.m.s/rad");
  parameter Inertia J1=0.4 "Moment of inertia for inertia 1";
  parameter Inertia J2=1.0 "Moment of inertia for inertia 2";
  parameter Stiffness c1=11 "Spring constant for spring 1";
  parameter Stiffness c2=5 "Spring constant for spring 2";
  parameter Damping d1=0.2 "Damping for damper 1";
  parameter Damping d2=1.0 "Damping for damper 2";
  Angle phi1 "Angle for inertia 1";
  Angle phi2 "Angle for inertia 2";
  AngularVelocity omega1 "Velocity of inertia 1";
  AngularVelocity omega2 "Velocity of inertia 2";
initial equation
  phi1 = 0;
  phi2 = 1;
  omega1 = 0;
  omega2 = 0;
equation
  // Equations for inertia 1
  omega1 = der(phi1);
  J1*der(omega1) = c1*(phi2-phi1)+d1*der(phi2-phi1);
  // Equations for inertia 2
  omega2 = der(phi2);
  J2*der(omega2) = c1*(phi1-phi2)+d1*der(phi1-phi2)-c2*phi2-d2*der(phi2);
end SecondOrderSystem;

이러한 자료형 정의를 예시 와 같은 import 문으로 대체할 수 있습니다.

import Modelica.SIunits.Angle;
import Modelica.SIunits.AngularVelocity;
import Modelica.SIunits.Inertia;
import Stiffness = Modelica.SIunits.RotationalSpringConstant;
import Damping = Modelica.SIunits.RotationalDampingConstant;

그러나 더 많은 자료형을 가져올수록 더 많은 import 문을 추가해야 합니다.대신 다음과 같이 모델을 작성할 수 있습니다.

within ModelicaByExample.PackageExamples;
model SecondOrderSystem
  "A second order rotational system importing types from Modelica Standard Library"
  import Modelica.SIunits.*;
  parameter Angle phi1_init = 0;
  parameter Angle phi2_init = 1;
  parameter AngularVelocity omega1_init = 0;
  parameter AngularVelocity omega2_init = 0;
  parameter Inertia J1=0.4;
  parameter Inertia J2=1.0;
  parameter RotationalSpringConstant c1=11;
  parameter RotationalSpringConstant c2=5;
  parameter RotationalDampingConstant d1=0.2;
  parameter RotationalDampingConstant d2=1.0;
  Angle phi1;
  Angle phi2;
  AngularVelocity omega1;
  AngularVelocity omega2;
initial equation
  phi1 = phi1_init;
  phi2 = phi2_init;
  omega1 = omega1_init;
  omega2 = omega2_init;
equation
  omega1 = der(phi1);
  omega2 = der(phi2);
  J1*der(omega1) = c1*(phi2-phi1)+d1*der(phi2-phi1);
  J2*der(omega2) = c1*(phi1-phi2)+d1*der(phi1-phi2)-c2*phi2-d2*der(phi2);
end SecondOrderSystem;

강조 표시된 import 문에 유의 해야 합니다.이 단일(와일드카드) 가져오기 구문은 Modelica.SIunits 의 모든 정의를 현재 모델로 가져오는데, 자료형의 "이름 변경" 옵션이 없습니다.따라서 명명된 패키지에 있는 것과 동일한 이름을 로컬에서 갖게 됩니다.

와일드카드 가져오기를 사용하기 전에 이 주의사항 을 읽어보세요.

이 장에서 import 문을 사용하여 다른 패키지에서 자료형을 가져오는 방법을 살펴보았습니다만, 결과적으로 import 문이 항상 그렇게 유용한 것은 아닙니다.그래픽 모델링 환경 내에서 모델을 개발할 때 시뮬레이션 소프트웨어는 일반적으로 참조 자료형에 대해 가장 모호하지 않고 가장 명시적인 방법인 정규화된 이름을 사용 하는 것이 좋습니다.결국, 그래픽 기반의 시뮬레이션 소프트웨어를 사용할 때 각각 입력할 필요가 없기 때문에 이름의 길이는 문제가 되지 않습니다.전체 경로를 사용하는 것은 또한 이름 조회, 이름 충돌 등의 문제를 방지할 수 있기 때문에 장점을 가지고 있습니다.