DoubleSaturation.java

/*
 * $Id$
 *
 * Copyright (C) 2004 Masanobu Koga. All rights reserved.
 */
package org.mklab.tool.control.system.discontinuous;

import java.util.ArrayList;
import java.util.List;

import org.mklab.nfc.matrix.DoubleMatrix;
import org.mklab.nfc.ode.DoublePiecewiseContinuousAlgebraicSystem;
import org.mklab.nfc.ode.PiecewiseUtil;
import org.mklab.tool.control.system.continuous.DoubleBaseContinuousStaticSystem;
import org.mklab.tool.control.system.parameter.Parameter;
import org.mklab.tool.control.system.parameter.StringExternalizable;


/**
 * 飽和システムを表すクラスです。
 * 
 * @author koga
 * @version $Revision$
 */
public class DoubleSaturation extends DoubleBaseContinuousStaticSystem implements DoublePiecewiseContinuousAlgebraicSystem, StringExternalizable {

  /** 出力の最小値 */
  @Parameter(name = "minimum", description = "Saturation.1", internationalization = true)
  private double minimum;

  /** 出力の最大値 */
  @Parameter(name = "maximum", description = "Saturation.3", internationalization = true)
  private double maximum;

  /**
   * コンストラクター
   */
  public DoubleSaturation() {
    this(-1, 1);
  }

  /**
   * 新しく生成された<code>Saturation</code>オブジェクトを初期化します。
   * 
   * @param minimum 出力の最小値
   * @param maximum 出力の最大値
   */
  public DoubleSaturation(final double minimum, final double maximum) {
    super(-1, -1);
    setAutoSize(true);
    setHasDirectFeedthrough(true);
    setMinimum(minimum);
    setMaximum(maximum);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public DoubleMatrix outputEquation( final double t, final DoubleMatrix u) {
    final int size = u.getRowSize();
    final DoubleMatrix y = new DoubleMatrix(size, 1);

    for (int i = 1; i <= size; i++) {
      final double uu = u.getDoubleElement(i);

      if (uu < this.minimum) {
        y.setElement(i, 1, this.minimum);
      } else if (uu > this.maximum) {
        y.setElement(i, 1, this.maximum);
      } else {
        y.setElement(i, 1, uu);
      }
    }

    return y;
  }

  /**
   * 最小値を設定します。
   * 
   * @param mininum 最小値
   */
  public void setMinimum(final double mininum) {
    this.minimum = mininum;
  }

  /**
   * 最大値を設定します。
   * 
   * @param maximum 最大値
   */
  public void setMaximum(final double maximum) {
    this.maximum = maximum;
  }

  /**
   * 最小値を返します。
   * 
   * @return 最小値
   */
  public double getMinimum() {
    return this.minimum;
  }

  /**
   * 最大値を返します。
   * 
   * @return 最大値
   */
  public double getMaximum() {
    return this.maximum;
  }

  /**
   * {@inheritDoc}
   */
  @SuppressWarnings("boxing")
  public List<Integer> getPiece( final double t, final DoubleMatrix u) {
    final int size = u.getRowSize();
    final List<Integer> pieces = new ArrayList<>(size);

    for (int i = 1; i <= size; i++) {
      final double uu = u.getDoubleElement(i);

      if (uu < this.minimum) {
        pieces.add(0);
      } else if (uu < this.maximum) {
        pieces.add(1);
      } else {
        pieces.add(2);
      }
    }

    return pieces;
  }

  /**
   * {@inheritDoc}
   */
  @SuppressWarnings("boxing")
  public double getDiscontinuousPoint(final double t1, final DoubleMatrix u1, final double t2, final DoubleMatrix u2) {
    final List<Integer> pieces1 = getPiece(t1, u1);
    final List<Integer> pieces2 = getPiece(t2, u2);
    if (pieces1.equals(pieces2)) {
      return Double.NaN;
    }

    final int number = PiecewiseUtil.getDistinctPiece(pieces1, pieces2);
    final int piece1 = pieces1.get(number - 1);
    final int piece2 = pieces2.get(number - 1);

    final double uu1 = u1.getDoubleElement(number);
    final double uu2 = u2.getDoubleElement(number);

    final boolean fromOneToTwo = piece1 == 1 && piece2 == 2;
    final boolean fromTwoToOne = piece1 == 2 && piece2 == 1;

    if (fromOneToTwo || fromTwoToOne) {
      return t1 + (this.maximum - uu1) / (uu2 - uu1) * (t2 - t1);
    }

    final boolean fromOneToZero = piece1 == 1 && piece2 == 0;
    final boolean fromZeroToOne = piece1 == 0 && piece2 == 1;

    if (fromOneToZero || fromZeroToOne) {
      return t1 + (uu1 - this.minimum) / (uu1 - uu2) * (t2 - t1);
    }

    assert false : "never reached"; //$NON-NLS-1$

    return (t1 + t2) / 2;
  }

  /**
   * @see org.mklab.tool.control.system.SystemOperator#setInputSize(int)
   */
  @Override
  public void setInputSize(final int size) {
    super.setInputSize(size);
    super.setOutputSize(size);
  }

  /**
   * @see org.mklab.tool.control.system.SystemOperator#setOutputSize(int)
   */
  @Override
  public void setOutputSize(final int size) {
    super.setInputSize(size);
    super.setOutputSize(size);
  }

  /**
   * @see org.mklab.tool.control.system.parameter.StringExternalizable#getString(java.lang.String)
   */
  public String getString(String key) {
    return Messages.getString(key);
  }

  /**
   * @see org.mklab.tool.control.system.SystemOperator#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!super.equals(o)) {
      return false;
    }
    if (o == null) {
      return false;
    }
    if (o.getClass() != getClass()) {
      return false;
    }
    DoubleSaturation castedObj = (DoubleSaturation)o;
    return ((this.minimum == castedObj.minimum) && (this.maximum == castedObj.maximum));
  }

  /**
   * @see org.mklab.tool.control.system.SystemOperator#hashCode()
   */
  @Override
  public int hashCode() {
    int hashCode = super.hashCode();
    hashCode = 31 * hashCode + (int)(Double.doubleToLongBits(this.minimum) ^ (Double.doubleToLongBits(this.minimum) >>> 32));
    hashCode = 31 * hashCode + (int)(Double.doubleToLongBits(this.maximum) ^ (Double.doubleToLongBits(this.maximum) >>> 32));
    return hashCode;
  }
}