Package ca.nengo.model

Examples of ca.nengo.model.SimulationException


    float[] values = new float[origins.length];
   
    for (int i = 0; i < origins.length; i++) {
      InstantaneousOutput o = origins[i].getValues();
      if ( !(o instanceof PreciseSpikeOutput) ) {
        throw new SimulationException("Some of the Node Origins are not producing precise spiking output");
      }
      if ( !o.getUnits().equals(units) ) {
        throw new SimulationException("Some of the Node Origins are producing outputs with non-matching units");
      }
     
      values[i] = ((PreciseSpikeOutput) o).getSpikeTimes()[0];
    }
   
View Full Code Here


    TimeSeries1D result = null;

    if (stateName.equals("V")) {
      result = new TimeSeries1DImpl(myTime, myVoltageHistory, Units.AVU);
    } else {
      throw new SimulationException("The state name " + stateName + " is unknown.");
    }

    return result;
  }
View Full Code Here

      }
      if (mySocketTimeout > 0)
        mySocket.setSoTimeout(mySocketTimeout);
    }
    catch( Exception e ) {
      throw new SimulationException(e);
    }
  }
View Full Code Here

    if (isSender() && (startTime + myUpdateInterval / 2.0) >= myNextUpdate) {
      if (mySocket == null)
        // If for some reason the socket hasn't been initialized, then initialize it.
        initialize();
      if (myTerminations.isEmpty())
        throw new SimulationException("SocketUDPNode is sender, but has no terminations to get data from.");
      else {
        // TODO: Test with spiking outputs?
        float[] values = new float[myDimension];
        Iterator<PassthroughTermination> it = myTerminations.values().iterator();
        while (it.hasNext()) {
          PassthroughTermination termination = it.next();
          InstantaneousOutput io = termination.getValues();
          if (io instanceof RealOutput) {
            values = MU.sum(values, ((RealOutput) io).getValues());
          } else if (io instanceof SpikeOutput) {
            boolean[] spikes = ((SpikeOutput) io).getValues();
            for (int i = 0; i < spikes.length; i++) {
              if (spikes[i]) {
                              values[i] += 1f/(endTime - startTime);
                          }
            }
          } else if (io == null) {
            throw new SimulationException("Null input to Termination " + termination.getName());
          } else {
            throw new SimulationException("Output type unknown: " + io.getClass().getName());
          }
        }
        // Send values over the socket.
        // Datagram format:
        // - bytes 1-4: Timestamp (float)
        // - bytes 4-(myDim+1)*4: values[i] (float)
        ByteBuffer buffer = ByteBuffer.allocate((myDimension + 1) * 4);
        buffer.order(myByteOrder);
        buffer.putFloat((float)((startTime + endTime + myUpdateInterval) / 2.0));
        for(int i = 0; i < myDimension; i++)
          buffer.putFloat(values[i]);
        byte[] bufArray = buffer.array();
        DatagramPacket packet = new DatagramPacket(bufArray, bufArray.length, myDestAddress, myDestPort);
        try {
          mySocket.send(packet);
        }
        catch (IOException e) {
          // TODO: Handle this better
          throw new SimulationException(e);
        }
      }
    }
    if (isReceiver()) {
      float[] values = new float[myOrigin.getDimensions()];
      float[] tempValues = new float[myOrigin.getDimensions()+1];

      // Check items in priority queue to see if there is anything that is good to go (i.e. timestamp is within
      // startTime and endTime.
      boolean foundItem = false;
      boolean foundFutureItem = false;
      int i = 0;
      while (!mySocketBuffer.isEmpty()) {
        tempValues = (float[]) mySocketBuffer.peek();
        foundItem = (tempValues[0] >= startTime && tempValues[0] <= endTime) ||  myIgnoreTimestamp;
        foundFutureItem = tempValues[0] > endTime;
        if (foundItem)
          mySocketBuffer.remove();
        else
          break;
      }
      if (foundItem) {
        // If an item was found in the queue (i.e. message with future timestamp was received before), use this
        // data instead of waiting on socket for new data.
        for (i = 0; i < myOrigin.getDimensions(); i++)
          values[i] = tempValues[i+1];
      }
      else if (foundFutureItem || startTime < myNextUpdate) {
        // Buffer contained item in the future, so skip waiting for a new packet to arrive, and hurry
        // the heck up.
        values = ((RealOutputImpl) myOrigin.getValues()).getValues().clone();
      }
      else {
        // If no items were found in queue, wait on socket for new data.
        try {
          byte[] bufArray = new byte[(myOrigin.getDimensions() + 1) * 4];
          DatagramPacket packet = new DatagramPacket(bufArray, bufArray.length);
         
          while (true) {
            mySocket.receive(packet);
           
            ByteBuffer buffer = ByteBuffer.wrap(bufArray);
            buffer.order(myByteOrder);
            for (i = 0; i < myOrigin.getDimensions() + 1; i++) {
              tempValues[i] = buffer.getFloat();
            }
           
            // Check for timestamp for validity (i.e. within the start and end of this run call).
            if ((tempValues[0] >= startTime && tempValues[0] <= endTime) || myIgnoreTimestamp || tempValues[0] < 0) {
              // Valid timestamp encountered; or have been instructed to ignore timestamps.
              // No further actions required, just break out of while loop.
              System.arraycopy(tempValues, 1, values, 0, myOrigin.getDimensions());
              break;
            }
            else if (tempValues[0] > endTime) {
              // Future timestamp encountered, place into priority queue, use previous origin value as
              // current value, and then out of while loop.
              // Note: we break out of the while loop because receiving future timestamps means this
              //       system is (potentially) running slow.
              mySocketBuffer.add(tempValues.clone());
              values = ((RealOutputImpl) myOrigin.getValues()).getValues().clone();
              break;
            }
            // Past timestamp encountered. Just ignore it, and wait for another packet.
          }
        }
        catch (SocketTimeoutException e){
          // If a timeout occurs, don't really do anything, just keep the origin at the previous value.
          values = ((RealOutputImpl) myOrigin.getValues()).getValues().clone();
        }
        catch (Exception e){
          // TODO: Handle this better
          throw new SimulationException(e);
        }
      }
      myOrigin.setValues(new RealOutputImpl(values, Units.UNK, endTime));
    }
    if (startTime >= myNextUpdate)
View Full Code Here

      myConstantValue = constantValue;
    }
   
    public TimeSeries getHistory(String stateName) throws SimulationException {
      if (!stateName.equals("x")) {
        throw new SimulationException("No such state");
      }
     
      return new TimeSeries1DImpl(new float[]{0}, new float[]{myConstantValue}, Units.UNK);
    }
View Full Code Here

      float[] times = myCurrent.getTimes();
      result = new TimeSeries1DImpl(new float[]{times[times.length-1]}, new float[]{myUnscaledCurrent}, Units.ACU);
    } else if (myGenerator instanceof Probeable) {
      result = ((Probeable) myGenerator).getHistory(stateName);
    } else {
      throw new SimulationException("The state " + stateName + " is unknown");
    }
    return result;
  }
View Full Code Here

    public void run(float startTime, float endTime) throws SimulationException {
        if (!finished) {
            try {
                myTermination.updateTransform(endTime, startIdx, endIdx);
            } catch (StructuralException e) {
                throw new SimulationException(e.getMessage());
            }
            finished = true;
        }
    }
View Full Code Here

            if (spikes[i]) {
                            values[i] += 1f/(endTime - startTime);
                        }
          }
        } else if (io == null) {
          throw new SimulationException("Null input to Termination " + termination.getName());
        } else {
          throw new SimulationException("Output type unknown: " + io.getClass().getName());
        }
      }
      myOrigin.setValues(new RealOutputImpl(values, Units.UNK, endTime));
    }
  }
View Full Code Here

      return myName;
    }

    public void setValues(InstantaneousOutput values) throws SimulationException {
      if (values.getDimension() != myDimension) {
        throw new SimulationException("Input is wrong dimension (expected " + myDimension + " got " + values.getDimension() + ")");
      }

      if (myTransform != null) {
        if (values instanceof RealOutput) {
          float[] transformed = MU.prod(myTransform, ((RealOutput) values).getValues());
          values = new RealOutputImpl(transformed, values.getUnits(), values.getTime());
        } else {
          throw new SimulationException("Transforms can only be performed on RealOutput in a PassthroughNode");
        }
      }

      myValues = values;
    }
View Full Code Here

  /**
   * @see ca.nengo.model.Termination#setValues(ca.nengo.model.InstantaneousOutput)
   */
    public void setValues(InstantaneousOutput values) throws SimulationException {
    if (values.getDimension() != getDimensions()) {
      throw new SimulationException("Input to this Termination must have dimension " + getDimensions());
    }

    for (Termination myNodeTermination : myNodeTerminations) {
      myNodeTermination.setValues(values);
    }
View Full Code Here

TOP

Related Classes of ca.nengo.model.SimulationException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.