CoulombViscousFriction.java

/*
 * $Id: CoulombViscousFriction.java,v 1.14 2008/07/15 15:27:15 koga Exp $
 *
 * 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.ComplexNumericalMatrix;
import org.mklab.nfc.matrix.RealNumericalMatrix;
import org.mklab.nfc.ode.PiecewiseContinuousAlgebraicSystem;
import org.mklab.nfc.ode.PiecewiseUtil;
import org.mklab.nfc.scalar.ComplexNumericalScalar;
import org.mklab.nfc.scalar.RealNumericalScalar;
import org.mklab.tool.control.system.continuous.BaseContinuousStaticSystem;
import org.mklab.tool.control.system.parameter.Parameter;
import org.mklab.tool.control.system.parameter.StringExternalizable;


/**
 * クーロン(静的)摩擦と粘性(動的)摩擦システムを表すクラスです。
 * 
 * @author koga
 * @version $Revision: 1.14 $
  * @param <RS> type of real scalar
 * @param <RM> type of real matrix
 * @param <CS> type of complex scalar
 * @param <CM> type of complex matrix
*/
public class CoulombViscousFriction<RS extends RealNumericalScalar<RS, RM, CS, CM>, RM extends RealNumericalMatrix<RS, RM, CS, CM>, CS extends ComplexNumericalScalar<RS, RM, CS, CM>, CM extends ComplexNumericalMatrix<RS, RM, CS, CM>> extends BaseContinuousStaticSystem<RS,RM,CS,CM> implements PiecewiseContinuousAlgebraicSystem<RS,RM,CS,CM>, StringExternalizable {

  /** ゲイン */
  @Parameter(name = "gain", description = "CoulombViscousFriction.1", internationalization = true)
  private RS gain;

  /** オフセット */
  @Parameter(name = "offset", description = "CoulombViscousFriction.3", internationalization = true)
  private RS offset;

  /**
   * コンストラクター
   * @param sunit unit of scalar
   */
  public CoulombViscousFriction(RS sunit) {
    this(sunit.create(1), sunit.create(1), sunit);
  }

  /**
   * 新しく生成された<code>Saturation</code>オブジェクトを初期化します。
   * 
   * @param gain ゲイン
   * @param offset オフセット
   * @param sunit unit of scalar
   */
  public CoulombViscousFriction(final RS gain, final RS offset, RS sunit) {
    super(-1, -1, sunit);
    setAutoSize(true);
    setHasDirectFeedthrough(true);
    setGain(gain);
    setOffset(offset);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public RM outputEquation( final RS t, final RM u) {
    return u.signumElementWise().multiply(u.absElementWise().multiply(this.gain).addElementWise(this.offset));
  }

  /**
   * ゲインを設定します。
   * 
   * @param gain ゲイン
   */
  public void setGain(final RS gain) {
    this.gain = gain;
  }

  /**
   * オフセットを設定します。
   * 
   * @param offset オフセット
   */
  public void setOffset(final RS offset) {
    this.offset = offset;
  }

  /**
   * ゲインを返します。
   * 
   * @return ゲイン
   */
  public RS getGain() {
    return this.gain;
  }

  /**
   * オフセットを返します。
   * 
   * @return オフセット
   */
  public RS getOffset() {
    return this.offset;
  }

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

    for (int i = 1; i <= size; i++) {
      if (u.getElement(i).isLessThan(0)) {
        pieces.add(0);
      } else {
        pieces.add(1);
      }
    }

    return pieces;
  }

  /**
   * {@inheritDoc}
   */
  @SuppressWarnings("boxing")
  public RS getDiscontinuousPoint(final RS t1, final RM u1, final RS t2, final RM u2) {
    final List<Integer> pieces1 = getPiece(t1, u1);
    final List<Integer> pieces2 = getPiece(t2, u2);

    if (pieces1.equals(pieces2)) {
      return this.sunit.getNaN();
    }

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

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

    final RS uu1 = u1.getElement(number);
    final RS uu2 = u2.getElement(number);

    if (fromZeroToOne || fromOneToZero) {
      return t1.add(uu1.unaryMinus().divide(uu2.subtract(uu1)).multiply(t2.subtract(t1)));
    }

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

    return this.sunit.getNaN();
  }

  /**
   * @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;
    }
    CoulombViscousFriction<RS,RM,CS,CM> castedObj = (CoulombViscousFriction<RS,RM,CS,CM>)o;
    return ((this.gain == castedObj.gain) && (this.offset == castedObj.offset));
  }

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