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

아키텍처 드리븐 접근방법(Architecture Driven Approach)

아키텍처 드리븐 접근방법(Architecture Driven Approach)

지금까지 이 섹션에서는 플랫 접근 방식으로 시작하여 모델리카의 아키텍처 기능을 사용하도록 점진적으로 조정했습니다.이제는, 먼저 시스템의 구조를 정의한 다음 특정 하위 시스템 구현을 추가하는 구조적 접근 방식을 사용하여 위에서 아래로 시스템을 구축해 보겠습니다.

인터페이스(Interfaces)

가지고 있는 하위 시스템과 이들이 어떻게 연결되어 있는지 설명하는 최상위 아키텍처 모델을 구축하는 것으로 시작하고 싶습니다. 하위 시스템을 나타내기 위해 이 아키텍처에 넣을 무언가 가 필요합니다.그러나 (아직) 모든 하위 시스템 모델에 대한 구현을 생성하는 작업에 착수하고 싶지 않습니다. 그럼 어디서부터 시작해야 할까요?

이에 대한 대답은 하위 시스템의 인터페이스를 설명하는 것으로 시작한다는 것입니다. 이 섹션의 앞부분에서 재선언은 제한 자료형에 의존하고 모든 구현은 해당 제한 자료형을 준수해야 한다고 언급한 것을 기억하십니까? 인터페이스는 기본적으로 제약 자료형(예: 예상되는 공용 인터페이스)의 공식적인 정의이지만 구현에 대한 세부 정보는 없습니다.구현 세부 사항이 없기 때문에(즉, 방정식이나 하위 구성 요소가 없음) 결국 partial 모델이 되지만, 그것은 아키텍처를 만들려고 하는 지금의 목적에 적합합니다.

센서 하위 시스템의 인터페이스를 고려하는 것부터 시작하겠습니다.이미 공용 인터페이스에 대해 자세히 논의했습니다. 다음은 해당 인터페이스에 대한 모델리카 정의 및 아이콘입니다.

within ModelicaByExample.Architectures.SensorComparison.Interfaces;
partial model Sensor "Interface for sensor"
  Modelica.Mechanics.Rotational.Interfaces.Flange_a shaft
    "Flange of shaft from which sensor information shall be measured"
    annotation ...
  Modelica.Blocks.Interfaces.RealOutput w "Absolute angular velocity of flange"
    annotation ...
  annotation ...
end Sensor;
Sensor interface

모델 정의에 대해 몇 가지 유의해야 할 사항이 있습니다. 첫 번째는 조금 전에 언급했듯이 이 모델이 partial 이라는 것입니다. 이는 커넥터가 있지만 해당 커넥터의 변수를 해결하는 데 도움이 되는 방정식이 없기 때문입니다.주목할 가치가 있는 또 다른 사항은 주석이 포함되어 있다는 사실입니다. 커넥터 선언과 관련된 주석은 해당 커넥터가 렌더링되는 방법을 지정합니다. 이러한 주석은 이 모델에서 확장 되는 모든 모델에 의해 상속되므로, 모든 구현에서 의미가 있는 위치를 선택하는 것이 중요합니다. 또한 Icon 주석도 포함되어 있으며, 이것은 기본적으로 최종 구현 아이콘에 대한 "배경"을 정의합니다. 즉, 이 모델에서 확장된 모든 모델은 Sensor 모델에 의해 정의된 것 위에 추가 그래픽을 그릴 수 있습니다.

액추에이터 모델의 인터페이스 정의는 두 개의 회전 커넥터(하나는 명령된 토크를 적용하고 다른 하나는 반응 토크를 처리함)를 포함한다는 점을 제외하면 거의 동일합니다. 예를 들어 액추에이터 모델이 전기 모터인 경우 전자는 회전자용 커넥터이고 후자는 고정자용 커넥터입니다. 그렇지 않으면 Sensor 정의와 매우 유사합니다.

within ModelicaByExample.Architectures.SensorComparison.Interfaces;
partial model Actuator "Interface for actuator"
  Modelica.Mechanics.Rotational.Interfaces.Flange_b shaft "Output shaft"
    annotation ...
  Modelica.Mechanics.Rotational.Interfaces.Support housing
    "Connection to housing"
    annotation ...
  Modelica.Blocks.Interfaces.RealInput tau "Input torque command"
    annotation ...
  annotation ...
end Actuator;
Actuator interface

Plant 인터페이스에는 3개의 회전 커넥터가 있습니다. 하나는 "입력" 쪽(액추에이터가 연결될 위치), 하나는 "출력" 쪽(센서가 연결될 위치), 마지막 하나는 "지지(support)" 쪽 입니다. 그래서 "장착(mounted)"될 수 있습니다.

within ModelicaByExample.Architectures.SensorComparison.Interfaces;
partial model Plant "Interface for plant model"
  Modelica.Mechanics.Rotational.Interfaces.Flange_b flange_b
    "Output shaft of plant"
    annotation ...
  Modelica.Mechanics.Rotational.Interfaces.Flange_a flange_a
    "Input shaft for plant"
    annotation ...
  Modelica.Mechanics.Rotational.Interfaces.Support housing
    "Connection to mounting"
    annotation ...
  annotation ...
end Plant;
Plant interface

마지막으로 Controller 인터페이스 정의가 있습니다.

within ModelicaByExample.Architectures.SensorComparison.Interfaces;
partial model Controller "Interface for controller subsystem"
  Modelica.Blocks.Interfaces.RealInput setpoint "Desired system response"
    annotation ...
  Modelica.Blocks.Interfaces.RealInput measured "Actual system response"
    annotation ...
  Modelica.Blocks.Interfaces.RealOutput command "Command to send to actuator"
    annotation ...
  annotation ...
end Controller;
Controller interface

구현 방식(예: 비례 제어, PID 제어)에 관계없이 Controller 에는 원하는 설정값에 대한 입력 커넥터, setpoint 에 대한 또 다른 입력 커넥터,측정된 속도 measured 및 명령된 토크를 액추에이터로 보내는 출력 커넥터 command 이 필요합니다.

아키텍처(Architecture)

인터페이스를 지정하면 아키텍처 모델을 다음과 같이 작성할 수 있습니다.

within ModelicaByExample.Architectures.SensorComparison.Examples;
partial model SystemArchitecture
  "A system architecture built from subsystem interfaces"

  replaceable Interfaces.Plant plant
    annotation ...
  replaceable Interfaces.Actuator actuator
    annotation ...
  replaceable Interfaces.Sensor sensor
    annotation ...
  replaceable Interfaces.Controller controller
    annotation ...
  Modelica.Blocks.Sources.Trapezoid setpoint(period=1.0)
    annotation ...
equation
  connect(actuator.shaft, plant.flange_a) annotation ...
  connect(actuator.housing, plant.housing) annotation ...
  connect(plant.flange_b, sensor.shaft) annotation ...
  connect(controller.measured, sensor.w) annotation ...
  connect(controller.command, actuator.tau) annotation ...
  connect(setpoint.y, controller.setpoint) annotation ...
end SystemArchitecture;

모델리카 코드에서 아키텍처가 plant, actuator, sensorsetpoint 의 4가지 replaceable 하위 시스템으로 구성되어 있음을 알 수 있습니다.이러한 각 선언에는 하나의 자료형만 포함됩니다.이 섹션의 앞부분에서 배운 것처럼 해당 자료형은 기본 자료형과 제한 자료형(원하는 자료형)으로 모두 사용하며, 이 모델에는 하위 시스템 간의 모든 연결도 포함합니다.이러한 방식으로 하위 시스템과 하위 시스템 간의 연결에 대한 완전한 설명이 가능합니다.**누락된 것은 각 하위 시스템에 대한 구현 선택 사항입니다**.

SystemArchitecture 모델은 그 자체로 partial 입니다. 이는 모든 하위 시스템의 기본 자료형이 partial 이고 아직 구현을 지정하지 않았기 때문입니다. 즉, 이 모델에는 구현이 없으므로 (아직) 시뮬레이션할 수 없는데, 이러한 모델을 partial 로 표시합니다.

인터페이스 사양과 마찬가지로 이 모델에는 그래픽 주석도 포함되어 있습니다.이는 하위 시스템을 지정하는 것 외에도 하위 시스템의 위치와 연결 경로도 지정하기 때문입니다. 시스템 아키텍처를 그리면 아래와 같습니다.

Top down architecture

구현(Implementations)

이제 인터페이스와 아키텍처가 있으므로 아키텍처에 "주입"할 수 있는 몇 가지 구현을 만들어야 합니다.이러한 구현은 기존 인터페이스에서 상속(따라서 중복 코드 방지)하거나 단순히 구현의 선언이 인터페이스와 플러그 호환되게 만들도록 선택할 수 있습니다.분명히 일반적으로 인터페이스에서 상속하는 것이 훨씬 더 나은 접근 방식입니다.그러나 인터페이스에서 상속하는 것이 엄격하게 요구되지 않는다는 것을 보여주기 위해 두 가지 예를 모두 포함했습니다.

다음은 이 섹션의 나머지 부분에서 사용할 구현 중 일부입니다.이러한 모델에는 여러 줄의 모델리카 소스 코드가 포함되어 있지만 그래픽 모델리카 환경에서 매우 빠르게 구현할 수 있다는 점을 염두에 두는 것이 중요합니다(즉, 이러한 모델은 일반적으로 손으로 입력하지 않습니다).

플랜트 모델(Plant Models)

within ModelicaByExample.Architectures.SensorComparison.Implementation;
model BasicPlant "Implementation of the basic plant model"
  parameter Modelica.SIunits.Inertia J_a=0.1 "Moment of inertia";
  parameter Modelica.SIunits.Inertia J_b=0.3 "Moment of inertia";
  parameter Modelica.SIunits.RotationalSpringConstant c=100 "Spring constant";
  parameter Modelica.SIunits.RotationalDampingConstant d_shaft=3
    "Shaft damping constant";
  parameter Modelica.SIunits.RotationalDampingConstant d_load=4
    "Load damping constant";

  Modelica.Mechanics.Rotational.Interfaces.Support housing
    "Connection to mounting"
    annotation ...
  Modelica.Mechanics.Rotational.Interfaces.Flange_a flange_a
    "Input shaft for plant"
    annotation ...
  Modelica.Mechanics.Rotational.Interfaces.Flange_b flange_b
    "Output shaft of plant"
    annotation ...
protected
  Modelica.Mechanics.Rotational.Components.Fixed fixed
    annotation ...
  Modelica.Mechanics.Rotational.Components.Inertia inertia(J=J_a)
    annotation ...
  Modelica.Mechanics.Rotational.Components.Inertia inertia1(J=J_b)
    annotation ...
  Modelica.Mechanics.Rotational.Components.SpringDamper springDamper(c=c, d=
        d_shaft)
    annotation ...
  Modelica.Mechanics.Rotational.Components.Damper damper(d=d_load)
    annotation ...
equation
  connect(springDamper.flange_a, inertia.flange_b) annotation ...
  connect(springDamper.flange_b, inertia1.flange_a) annotation ...
  connect(damper.flange_b, inertia1.flange_b) annotation ...
  connect(damper.flange_a, fixed.flange) annotation ...
  connect(inertia1.flange_b, flange_b) annotation ...
  connect(inertia.flange_a, flange_a) annotation ...
  connect(fixed.flange, housing) annotation ...
end BasicPlant;
Basic Plant

엑츄에이터 모델(Actuator Models)

within ModelicaByExample.Architectures.SensorComparison.Implementation;
model IdealActuator "An implementation of an ideal actuator"
  Modelica.Mechanics.Rotational.Interfaces.Flange_b shaft "Output shaft"
    annotation ...
  Modelica.Mechanics.Rotational.Interfaces.Support housing
    "Connection to housing"
    annotation ...
  Modelica.Blocks.Interfaces.RealInput tau "Input torque command"
    annotation ...
protected
  Modelica.Mechanics.Rotational.Sources.Torque torque(useSupport=true)
    annotation ...
equation
  connect(torque.flange, shaft) annotation ...
  connect(torque.support, housing) annotation ...
  connect(torque.tau, tau) annotation ...
end IdealActuator;
Basic Plant
within ModelicaByExample.Architectures.SensorComparison.Implementation;
model LimitedActuator "An actuator with lag and saturation"
  extends Interfaces.Actuator;
  parameter Modelica.SIunits.Time delayTime
    "Delay time of output with respect to input signal";
  parameter Real uMax "Upper limits of input signals";
protected
  Modelica.Mechanics.Rotational.Sources.Torque torque(useSupport=true)
    annotation ...
  Modelica.Blocks.Nonlinear.Limiter limiter(uMax=uMax)
    annotation ...
  Modelica.Blocks.Nonlinear.FixedDelay lag(delayTime=delayTime)
    annotation ...
equation
  connect(torque.flange, shaft) annotation ...
  connect(torque.support, housing) annotation ...
  connect(limiter.y, torque.tau) annotation ...
  connect(lag.u, tau) annotation ...
  connect(lag.y, limiter.u) annotation ...
end LimitedActuator;
Limited Actuator

컨트롤러 모델(Controller Models)

within ModelicaByExample.Architectures.SensorComparison.Implementation;
model ProportionalController "Implementation of a proportional controller"
  parameter Real k=20 "Controller gain";
  Modelica.Blocks.Interfaces.RealInput setpoint "Desired system response"
    annotation ...
  Modelica.Blocks.Interfaces.RealInput measured "Actual system response"
    annotation ...
  Modelica.Blocks.Interfaces.RealOutput command "Command to send to actuator"
    annotation ...
protected
  Modelica.Blocks.Math.Gain gain(k=k)
    annotation ...
  Modelica.Blocks.Math.Feedback feedback
    annotation ...
equation
  connect(feedback.y, gain.u) annotation ...
  connect(feedback.u1, setpoint) annotation ...
  connect(gain.y, command) annotation ...
  connect(measured, feedback.u2) annotation ...
end ProportionalController;
Proportional Controller
within ModelicaByExample.Architectures.SensorComparison.Implementation;
model PID_Controller "Controller subsystem implemented using a PID controller"
  extends Interfaces.Controller;
  parameter Real k "Gain of controller";
  parameter Modelica.SIunits.Time Ti "Time constant of Integrator block";
  parameter Modelica.SIunits.Time Td "Time constant of Derivative block";
  parameter Real yMax "Upper limit of output";
protected
  Modelica.Blocks.Continuous.LimPID PID(k=k, Ti=Ti, Td=Td, yMax=yMax)
    annotation ...
equation
  connect(setpoint, PID.u_s) annotation ...
  connect(measured, PID.u_m) annotation ...
  connect(PID.y, command) annotation ...
end PID_Controller;
PID Controller

센서 모델(Sensor Models)

within ModelicaByExample.Architectures.SensorComparison.Implementation;
model IdealSensor "Implementation of an ideal sensor"
  Modelica.Mechanics.Rotational.Interfaces.Flange_a shaft
    "Flange of shaft from which sensor information shall be measured"
    annotation ...
  Modelica.Blocks.Interfaces.RealOutput w "Absolute angular velocity of flange"
    annotation ...
protected
  Modelica.Mechanics.Rotational.Sensors.SpeedSensor idealSpeedSensor
    "An ideal speed sensor" annotation ...
equation
  connect(idealSpeedSensor.flange, shaft) annotation ...
  connect(idealSpeedSensor.w, w) annotation ...
end IdealSensor;
Ideal Sensor
within ModelicaByExample.Architectures.SensorComparison.Implementation;
model SampleHoldSensor "Implementation of a sample hold sensor"
  parameter Modelica.SIunits.Time sample_time(min=Modelica.Constants.eps);
  Modelica.Mechanics.Rotational.Interfaces.Flange_a shaft
    "Flange of shaft from which sensor information shall be measured"
    annotation ...
  Modelica.Blocks.Interfaces.RealOutput w "Absolute angular velocity of flange"
    annotation ...
protected
  Components.SpeedMeasurement.Components.SampleHold sampleHoldSensor(
      sample_time=sample_time)
    annotation ...
equation
  connect(sampleHoldSensor.w, w) annotation ...
  connect(sampleHoldSensor.flange, shaft) annotation ...
end SampleHoldSensor;
Sample Hold Sensor

검증(Variations)

기본 구성(Baseline Configuration)

이러한 구현을 통해 전체 시스템의 다양한 구현을 만들 수 있습니다.예를 들어 원래 FlatSystem 모델의 동작을 구현하려면 단순히 SystemArchitecture 모델에서 확장하고 구현이 FlatSystem 의 하위 시스템 구현과 일치하도록 각 하위 시스템을 다시 선언합니다.*즉.,*

within ModelicaByExample.Architectures.SensorComparison.Examples;
model BaseSystem "System architecture with base implementations"
  extends SystemArchitecture(
    redeclare replaceable Implementation.ProportionalController controller,
    redeclare replaceable Implementation.IdealActuator actuator,
    redeclare replaceable Implementation.BasicPlant plant,
    redeclare replaceable Implementation.IdealSensor sensor);
end BaseSystem;

여기에서 구성 지정에 대한 모델리카의 진정한 힘을 볼 수 있습니다. 각 재선언에 replaceable 한정자가 어떻게 포함되어 있는지 확인함으로써 후속 재선언이 가능하도록 합니다.

SystemArchitecture 모델이 이러한 구현을 기본값으로 지정하지만 여전히 인터페이스를 제약 자료형으로 사용하기를 원했다면, 다음과 같이 SystemArchitecture 에서 하위 시스템을 선언할 수 있습니다.

  replaceable Implementation.BasicPlant plant constrainedby Interfaces.Plant
    annotation ...
  replaceable Implementation.IdealActuator actuator constrainedby
    Interfaces.Actuator
    annotation ...
  replaceable Implementation.IdealSensor sensor constrainedby Interfaces.Sensor
    annotation ...
  replaceable Implementation.ProportionalController controller constrainedby
    Interfaces.Controller
    annotation ...
  replaceable Modelica.Blocks.Sources.Trapezoid setpoint(period=1.0) constrainedby
    Modelica.Blocks.Interfaces.SO
    annotation ...

Variation1

BaseSystem 모델의 변형을 생성하려면 상속과 수정자를 사용하여 생성할 수 있습니다. 를 들어 보겠습니다.

within ModelicaByExample.Architectures.SensorComparison.Examples;
model Variant1 "Creating sample-hold variant using system architecture"
  extends BaseSystem(redeclare replaceable
      Implementation.SampleHoldSensor sensor(sample_time=0.01));
end Variant1;

이 모델이 어떻게 BaseSystem 구성에서 확장되지만 sensor 모델 변경하는지에 집중해 보겠습니다.이 시스템을 시뮬레이션하면 성능이 원래 평형 시스템(Flat System) 의 성능과 일치하는 것을 볼 수 있습니다.

/static/_images/SV1.svg

그러나 대기 시간이 더 긴 구성을 대신 생성하면 시스템이 불안정해집니다(정확히 평형 시스템(Flat System) 에서도 마찬가지임)

/static/_images/SV1U.svg

Variation2

적절한 센서가 있어도 Variant1 구성의 컨트롤러가 잘못된 정상 상태 속도로 수렴하는 것처럼 보입니다.비례 게인 컨트롤러만 사용하고 있기 때문입니다.그러나 Variant1 모델에서 확장하여 PID 컨트롤러와 공급할 수 있는 토크의 양에 대한 제한이 있는 보다 현실적인 액추에이터를 추가하면 아래과 같습니다.

within ModelicaByExample.Architectures.SensorComparison.Examples;
model Variant2 "Adds PID control and realistic actuator subsystems"
  extends Variant1(
      redeclare replaceable Implementation.PID_Controller controller(
        yMax=15, Td=0.1, k=20, Ti=0.1),
      redeclare replaceable Implementation.LimitedActuator actuator(
        delayTime=0.005, uMax=10));
end Variant2;

다음과 같은 시뮬레이션 결과를 얻게 됩니다.

/static/_images/SV2.svg

또한 PID 컨트롤러에서 게인을 조정하는 데 약간의 시간을 사용하면 아래와 같고,

within ModelicaByExample.Architectures.SensorComparison.Examples;
model Variant2_tuned "A tuned up version of variant 2"
  extends Variant2(
    controller(yMax=50, Ti=0.07, Td=0.01, k=4),
    actuator(uMax=50),
    sensor(sample_time=0.01));
end Variant2_tuned;

그러면 더 나은 시뮬레이션 결과를 얻을 수 있습니다.

/static/_images/SV2T.svg

결론(Conclusion)

이것으로 이 특정 아키텍처에 대한 논의를 마칩니다.이 예의 핵심은 아키텍처를 사용하여 시스템의 대체 구성을 매우 쉽게 탐색할 수 있다는 것입니다.그러나 더 쉬울 뿐만 아니라(이러한 작업을 매우 빠르게 수행할 수 있다는 점에서) 각 구성에 추가 연결이 필요하지 않기 때문에 어느 정도의 정확성도 보장할 수 있습니다.대신 사용자는 각 하위 시스템에 사용하려는 구현을 지정하기만 하면 되며, 그런 다음에도 모델리카 컴파일러는 선택 사항이 아키텍처를 지정하는 데 사용하는 제약 자료형과 플러그 호환되는지 확인할 수 있습니다.