ApertureSlice.java

  1. // @formatter:off
  2.  /*******************************************************************************
  3.  *
  4.  * This file is part of JMad.
  5.  *
  6.  * Copyright (c) 2008-2011, CERN. All rights reserved.
  7.  *
  8.  * Licensed under the Apache License, Version 2.0 (the "License");
  9.  * you may not use this file except in compliance with the License.
  10.  * You may obtain a copy of the License at
  11.  *
  12.  *     http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing, software
  15.  * distributed under the License is distributed on an "AS IS" BASIS,
  16.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17.  * See the License for the specific language governing permissions and
  18.  * limitations under the License.
  19.  *
  20.  ******************************************************************************/
  21. // @formatter:on

  22. package cern.accsoft.steering.jmad.domain.aperture;

  23. import cern.accsoft.steering.jmad.domain.types.enums.JMadPlane;

  24. /**
  25.  * Represents one slice of aperture in the aperture model. Such a slice is not necessarily equivalent with one element.
  26.  * In the contrary on element might have many slices.
  27.  * <p>
  28.  * This class contains the original values as well as the calculated min/max values for the both planes. Additionaly the
  29.  * positions for a so-called reference trajectory are stored, around which the aperture can later be centered.
  30.  *
  31.  * @author Kajetan Fuchsberger (kajetan.fuchsberger at cern.ch)
  32.  */
  33. public class ApertureSlice {

  34.     private static final double MAX_APERTURE = 0.2;

  35.     private AperType type;
  36.     private double xMin;
  37.     private double xMax;
  38.     private double yMin;
  39.     private double yMax;
  40.     private double aper1;
  41.     private double aper2;
  42.     private double aper3;
  43.     private double aper4;
  44.     private double posX;
  45.     private double posY;
  46.     private double posS;

  47.     /**
  48.      * Create a new ApertureSlice and calculate the Aperture Extrema
  49.      *
  50.      * @param type {@link AperType} of this Element
  51.      * @param aper1 Aperture Value 1
  52.      * @param aper2 Aperture Value 2
  53.      * @param aper3 Aperture Value 3
  54.      * @param aper4 Aperture Value 4
  55.      * @param pos Position of this Element in the Sequence
  56.      */
  57.     public ApertureSlice(AperType type, double aper1, double aper2, double aper3, double aper4, double pos) {
  58.         this.posS = pos;
  59.         this.type = type;

  60.         this.setApertureValues(aper1, aper2, aper3, aper4);
  61.     }

  62.     /**
  63.      * Create a new ApertureSlice only defined by its Type and Position
  64.      *
  65.      * @param type {@link AperType} of this Element
  66.      * @param pos Position of this Element in the Sequence
  67.      */
  68.     public ApertureSlice(AperType type, double pos) {
  69.         this.posS = pos;
  70.         this.type = type;
  71.     }

  72.     public double getAper1() {
  73.         return aper1;
  74.     }

  75.     public double getAper2() {
  76.         return aper2;
  77.     }

  78.     public double getAper3() {
  79.         return aper3;
  80.     }

  81.     public double getAper4() {
  82.         return aper4;
  83.     }

  84.     /**
  85.      * Calculate the Aperture Extrema for the Aperture Slice according to the type and the 4 Aperture Values
  86.      *
  87.      * @param aper1 the value of the madx-variable aper1
  88.      * @param aper2 the value of the madx-variable aper2
  89.      * @param aper3 the value of the madx-variable aper3
  90.      * @param aper4 the value of the madx-variable aper4
  91.      */
  92.     public final void setApertureValues(double aper1, double aper2, double aper3, double aper4) {

  93.         // Ensure visibility in Plots
  94.         this.aper1 = (aper1 > MAX_APERTURE ? MAX_APERTURE : aper1);
  95.         this.aper2 = (aper2 > MAX_APERTURE ? MAX_APERTURE : aper2);
  96.         this.aper3 = (aper3 > MAX_APERTURE ? MAX_APERTURE : aper3);
  97.         this.aper4 = (aper4 > MAX_APERTURE ? MAX_APERTURE : aper4);

  98.         switch (this.type) {
  99.         case RACETRACK:
  100.             this.xMin = -aper1 - aper3;
  101.             this.xMax = +aper1 + aper3;
  102.             this.yMin = -aper2 - aper3;
  103.             this.yMax = +aper2 + aper3;
  104.             break;
  105.         case RECTELLIPSE:
  106.             this.xMin = (-1.0) * Math.min(aper1, aper3);
  107.             this.xMax = Math.min(aper1, aper3);
  108.             this.yMin = (-1.0) * Math.min(aper2, aper4);
  109.             this.yMax = Math.min(aper2, aper4);
  110.             break;
  111.         case CIRCLE:
  112.             this.xMin = (-1.0) * aper1;
  113.             this.xMax = aper1;
  114.             this.yMin = (-1.0) * aper1;
  115.             this.yMax = aper1;
  116.             break;
  117.         case RECTANGLE:
  118.             this.xMin = (-1.0) * aper1;
  119.             this.xMax = aper1;
  120.             this.yMin = (-1.0) * aper2;
  121.             this.yMax = aper2;
  122.             break;
  123.         default:
  124.         }
  125.     }

  126.     /**
  127.      * Compares two aperture slices
  128.      *
  129.      * @param actSlice aperture slice to compare to this one
  130.      * @return <code>true</code> when type/position and all 4 ApertureValues are equal
  131.      */
  132.     public boolean sameAperInfo(ApertureSlice actSlice) {
  133.         if (actSlice.type.equals(this.type) && actSlice.posS == this.posS && actSlice.aper1 == this.aper1
  134.                 && actSlice.aper2 == this.aper2 && actSlice.aper3 == this.aper3 && actSlice.aper4 == this.aper4) {
  135.             return true;
  136.         }

  137.         return false;
  138.     }

  139.     public AperType getType() {
  140.         return type;
  141.     }

  142.     public void setType(AperType type) {
  143.         this.type = type;
  144.     }

  145.     public double getXmin() {
  146.         return xMin;
  147.     }

  148.     public void setXmin(double xMin) {
  149.         this.xMin = xMin;
  150.     }

  151.     public double getXmax() {
  152.         return xMax;
  153.     }

  154.     public void setXmax(double xMax) {
  155.         this.xMax = xMax;
  156.     }

  157.     public double getS() {
  158.         return posS;
  159.     }

  160.     public void setS(double posS) {
  161.         this.posS = posS;
  162.     }

  163.     public double getYmin() {
  164.         return yMin;
  165.     }

  166.     public void setYmin(double yMin) {
  167.         this.yMin = yMin;
  168.     }

  169.     public double getYmax() {
  170.         return yMax;
  171.     }

  172.     public void setYmax(double yMax) {
  173.         this.yMax = yMax;
  174.     }

  175.     public double getX() {
  176.         return posX;
  177.     }

  178.     public void setX(double x) {
  179.         this.posX = x;
  180.     }

  181.     public double getY() {
  182.         return posY;
  183.     }

  184.     public void setY(double y) {
  185.         this.posY = y;
  186.     }

  187.     /**
  188.      * retrieves the trajectory-position for the given plane. This is the position around which the aperture can be
  189.      * centered.
  190.      *
  191.      * @param plane the plane for which to get the position
  192.      * @return the position in the given plane
  193.      */
  194.     public double getPos(JMadPlane plane) {
  195.         switch (plane) {
  196.         case H:
  197.             return getX();
  198.         case V:
  199.             return getY();
  200.         default:
  201.             throw new IllegalArgumentException("Unknown plane '" + plane + "'.");
  202.         }
  203.     }

  204.     /**
  205.      * returns max value of the aperture in the given plane.
  206.      *
  207.      * @param plane the plane for which to get the value
  208.      * @return the max aperture value in the given plane
  209.      */
  210.     public double getMax(JMadPlane plane) {
  211.         switch (plane) {
  212.         case H:
  213.             return getXmax();
  214.         case V:
  215.             return getYmax();
  216.         default:
  217.             throw new IllegalArgumentException("Unknown plane '" + plane + "'.");
  218.         }
  219.     }

  220.     /**
  221.      * returns the aperture minimum value in the given plane
  222.      *
  223.      * @param plane the plane for which to retrieve the minimum value
  224.      * @return the aperture minimum value in the given plane
  225.      */
  226.     public double getMin(JMadPlane plane) {
  227.         switch (plane) {
  228.         case H:
  229.             return getXmin();
  230.         case V:
  231.             return getYmin();
  232.         default:
  233.             throw new IllegalArgumentException("Unknown plane '" + plane + "'.");
  234.         }
  235.     }

  236. }