001/* An actor that outputs a random sequence with a BreitWigner distribution.
002
003 Copyright (c) 2004-2016 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.actor.lib.colt;
029
030import cern.jet.random.BreitWigner;
031import ptolemy.actor.parameters.PortParameter;
032import ptolemy.data.BooleanToken;
033import ptolemy.data.DoubleToken;
034import ptolemy.data.expr.SingletonParameter;
035import ptolemy.data.type.BaseType;
036import ptolemy.kernel.CompositeEntity;
037import ptolemy.kernel.util.IllegalActionException;
038import ptolemy.kernel.util.NameDuplicationException;
039
040///////////////////////////////////////////////////////////////////
041//// BreitWigner
042
043/**
044 Produce a random sequence with a BreitWigner distribution.  On each
045 iteration, a new random number is produced.  The output port is of
046 type DoubleToken.  The values that are generated are independent
047 and identically distributed with the mean and the standard
048 deviation given by parameters.  In addition, the seed can be
049 specified as a parameter to control the sequence that is generated.
050
051 <p> This actor instantiates a
052 <a href="http://hoschek.home.cern.ch/hoschek/colt/V1.0.3/doc/cern/jet/random/BreitWigner.html">cern.jet.random.BreitWigner</a> object with
053 mean, gamma and cut set to 1.0.
054
055 <p>Breit-Wigner is a also know as the Lorentz distribution.
056 The Breit-Wigner distribution is a more generate form of the
057 Cauchy distribution.
058
059 <p>A definition of the Breit-Wigner distribution can be found at
060 <a href="http://rd11.web.cern.ch/RD11/rkb/AN16pp/node23.html#SECTION000230000000000000000"><code>http://rd11.web.cern.ch/RD11/rkb/AN16pp/node23.html#SECTION000230000000000000000</code></a>
061
062 @author David Bauer and Kostas Oikonomou
063 @version $Id$
064 @since Ptolemy II 4.1
065 @Pt.ProposedRating Red (cxh)
066 @Pt.AcceptedRating Red (cxh)
067 */
068public class ColtBreitWigner extends ColtRandomSource {
069    /** Construct an actor with the given container and name.
070     *  @param container The container.
071     *  @param name The name of this actor.
072     *  @exception IllegalActionException If the actor cannot be contained
073     *   by the proposed container.
074     *  @exception NameDuplicationException If the container already has an
075     *   actor with this name.
076     */
077    public ColtBreitWigner(CompositeEntity container, String name)
078            throws NameDuplicationException, IllegalActionException {
079        super(container, name);
080
081        output.setTypeEquals(BaseType.DOUBLE);
082
083        mean = new PortParameter(this, "mean", new DoubleToken(1.0));
084        mean.setTypeEquals(BaseType.DOUBLE);
085        new SingletonParameter(mean.getPort(), "_showName")
086                .setToken(BooleanToken.TRUE);
087
088        gamma = new PortParameter(this, "gamma", new DoubleToken(1.0));
089        gamma.setTypeEquals(BaseType.DOUBLE);
090        new SingletonParameter(gamma.getPort(), "_showName")
091                .setToken(BooleanToken.TRUE);
092
093        cut = new PortParameter(this, "cut", new DoubleToken(1.0));
094        cut.setTypeEquals(BaseType.DOUBLE);
095        new SingletonParameter(cut.getPort(), "_showName")
096                .setToken(BooleanToken.TRUE);
097
098        cut.moveToFirst();
099        gamma.moveToFirst();
100        mean.moveToFirst();
101    }
102
103    ///////////////////////////////////////////////////////////////////
104    ////                     ports and parameters                  ////
105
106    /** mean.
107     *  This is a double with default 1.0.
108     */
109    public PortParameter mean;
110
111    /** gamma.
112     *  This is a double with default 1.0.
113     */
114    public PortParameter gamma;
115
116    /** cut.
117     *  This is a double with default 1.0.
118     */
119    public PortParameter cut;
120
121    ///////////////////////////////////////////////////////////////////
122    ////                         public methods                    ////
123
124    /** Send a random number with a BreitWigner distribution to the output.
125     *  This number is only changed in the prefire() method, so it will
126     *  remain constant throughout an iteration.
127     *  @exception IllegalActionException If there is no director.
128     */
129    @Override
130    public void fire() throws IllegalActionException {
131        mean.update();
132        gamma.update();
133        cut.update();
134        super.fire();
135        output.send(0, new DoubleToken(_current));
136    }
137
138    ///////////////////////////////////////////////////////////////////
139    ////                         protected methods                 ////
140
141    /** Method that is called after _randomNumberGenerator is changed.
142     */
143    @Override
144    protected void _createdNewRandomNumberGenerator() {
145        _generator = new BreitWigner(1.0, 1.0, 1.0, _randomNumberGenerator);
146    }
147
148    /** Generate a new random number.
149     *  @exception IllegalActionException If parameter values are incorrect.
150     */
151    @Override
152    protected void _generateRandomNumber() throws IllegalActionException {
153        double meanValue = ((DoubleToken) mean.getToken()).doubleValue();
154        double gammaValue = ((DoubleToken) gamma.getToken()).doubleValue();
155        double cutValue = ((DoubleToken) cut.getToken()).doubleValue();
156
157        _current = _generator.nextDouble(meanValue, gammaValue, cutValue);
158    }
159
160    ///////////////////////////////////////////////////////////////////
161    ////                         private variables                 ////
162
163    /** The random number for the current iteration. */
164    private double _current;
165
166    /** The random number generator. */
167    private BreitWigner _generator;
168}