void Transmission::LoadTransmission() {
  MATFile *matFileHandle;
  matFileHandle = matOpen(transmissionFileName, "r");
  int numberOfDirectories;
  const char** directoryField= (const char** )matGetDir(matFileHandle, &numberOfDirectories);
  for(int i=0; i < numberOfDirectories; i++) {
    //std::cout<<directoryField[i]<<std::endl;
    mxArray* matArrayHandle = matGetVariable(matFileHandle, directoryField[i]);
    int numberOfRows = mxGetM(matArrayHandle);
    int numberOfColumns = mxGetN(matArrayHandle);
    double* matArrayAddress = mxGetPr(matArrayHandle);
    std::vector<double> transmissionDataEntry;
    for(int j=0;j<numberOfColumns;j++) {
      double matrixEntry = *(matArrayAddress+j);
      transmissionDataEntry.push_back(matrixEntry);
    }
    transmissionData.push_back(transmissionDataEntry);
  }

  gearRatios = transmissionData[0];
  gearEfficiency = transmissionData[1];
  gearInertias = transmissionData[2];

  //std::cout<<"Transmission data successfully loaded"<<std::endl;
  if (matClose(matFileHandle) != 0) {
    std::cout<<"Error closing file"<<'\n';
  }
}
void Machine::LoadMachine() {
  MATFile *matFileHandle;
  matFileHandle = matOpen(machineFileName, "r");
  int numberOfDirectories;
  const char** directoryField= (const char** )matGetDir(matFileHandle, &numberOfDirectories);
  for(int i=0; i < numberOfDirectories; i++) {
    //std::cout<<directoryField[i]<<std::endl;
    mxArray* matArrayHandle = matGetVariable(matFileHandle, directoryField[i]);
    int numberOfRows = mxGetM(matArrayHandle);
    int numberOfColumns = mxGetN(matArrayHandle);
    double* matArrayAddress = mxGetPr(matArrayHandle);
	  std::vector<std::vector<double>> machineData;
    for(int j=0;j<numberOfRows;j++) {
      std::vector<double> singleRowInArray;
      for(int k=0;k<numberOfColumns;k++) {
        double matrixEntry = *((matArrayAddress+j)+(k*numberOfRows));
        singleRowInArray.push_back(matrixEntry);
      }
      machineData.push_back(singleRowInArray);
    }
	  switch(i) {
     case 0:
      machineRPMRange = machineData[0];
      //std::cout<<"machineRPMRange"<<std::endl;
      //PrintMachineData(machineRPMRange);
      break;
     case 1:
      machineTorqueRange = machineData[0];
      //std::cout<<"machineTorqueRange"<<std::endl;
      //PrintMachineData(machineTorqueRange);
      break;
     case 2:
      machineEfficiencyData = machineData;
      //std::cout<<"machineEfficiencyData"<<std::endl;
      //PrintMachineData(machineEfficiencyData);
      break;
     case 3:
      rpmRangeForMaximumBrakeTorque = machineData[0];
      //std::cout<<"rpmRangeForMaximumBrakeTorque"<<std::endl;
      //PrintMachineData(rpmRangeForMaximumBrakeTorque);
      break;
     case 4:
      machineMaximumBrakeTorque = machineData[0]; 
      //std::cout<<"machineMaximumBrakeTorque"<<std::endl;
      //PrintMachineData(machineMaximumBrakeTorque);
      break;
     case 5:
      machineMaximumRegenerationTorque = machineData[0];
      //std::cout<<"machineMaximumRegenerationTorque"<<std::endl;
      //PrintMachineData(machineMaximumRegenerationTorque);
      break;
    }
  }
  //std::cout<<"Data for machine "<<machineIndex<<" successfully loaded"<<std::endl;
}
void Transmission::LoadTransmission() {
  MATFile *matFileHandle;
  matFileHandle = matOpen(transmissionFileName, "r");
  int numberOfDirectories;
  const char** directoryField= (const char** )matGetDir(matFileHandle, &numberOfDirectories);
  for(int i=0; i < numberOfDirectories; i++) {
    std::cout<<directoryField[i]<<std::endl;
    mxArray* matArrayHandle = matGetVariable(matFileHandle, directoryField[i]);
    int numberOfRows = mxGetM(matArrayHandle);
    int numberOfColumns = mxGetN(matArrayHandle);
    double* matArrayAddress = mxGetPr(matArrayHandle);
    std::vector<double> transmissionDataEntry;
    for(int j=0;j<numberOfColumns;j++) {
      double matrixEntry = *(matArrayAddress+j);
      transmissionDataEntry.push_back(matrixEntry);
    }
    transmissionData.push_back(transmissionDataEntry);
  }
  std::cout<<"Transmission data successfully loaded"<<std::endl<<std::endl;
}
void Buffer::LoadBuffer() {
  MATFile *matFileHandle;
  matFileHandle = matOpen(bufferFileName, "r");
  int numberOfDirectories;
  if(matFileHandle==NULL) {
    std::cout<<"ERROR IN FILE OPENING"<<std::endl;
  }
  const char** directoryField= (const char** )matGetDir(matFileHandle, &numberOfDirectories);
  for(int i=0; i < numberOfDirectories; i++) {
    //std::cout<<directoryField[i]<<std::endl;
    mxArray* matArrayHandle = matGetVariable(matFileHandle, directoryField[i]);
    int numberOfRows = mxGetM(matArrayHandle);
    int numberOfColumns = mxGetN(matArrayHandle);
    double* matArrayAddress = mxGetPr(matArrayHandle);
    for(int j=0;j<numberOfColumns;j++) {
      double matrixEntry = *(matArrayAddress+j);
      bufferData.push_back(matrixEntry);
    }
  }
  maximumBufferLevel = bufferData[0];
  //std::cout<<"Max. buffer level "<<maximumBufferLevel<<std::endl;
  minimumBufferLevel = bufferData[1];
  //std::cout<<"Min. buffer level "<<minimumBufferLevel<<std::endl;
  maximumStateOfBuffer = bufferData[2];
  //std::cout<<"Max. state of buffer "<<maximumStateOfBuffer<<std::endl;
  referenceStateOfBuffer = bufferData[3];
  // A dummy reference is given to all buffers, Fuel Tanks and Batteries. For batteries,
  // this value is updated eat each step. It remains constant for fuel tanks.
  //std::cout<<"Min. allowed state of buffer "<<referenceStateOfBuffer<<std::endl;
  openCircuitVoltage = bufferData[4];
  //std::cout<<"openCircuitVoltage "<<openCircuitVoltage<<std::endl;

  instantaneousBufferLevel=maximumStateOfBuffer*maximumBufferLevel;
  //bufferLevelOverMission.push_back(instantaneousBufferLevel);
  //std::cout<<"Inst. buffer level "<<instantaneousBufferLevel<<std::endl;

  //std::cout<<"Buffer data successfully loaded"<<std::endl;
  if (matClose(matFileHandle) != 0) {
    std::cout<<"Error closing file"<<'\n';
  }
}
void LoadMission(std::vector<std::vector<double>>& missionData) {
  MATFile *matFileHandle;
  matFileHandle = matOpen("data/MissionData.mat", "r");
  int numberOfDirectories;
  const char** directoryField= (const char** )matGetDir(matFileHandle, &numberOfDirectories);
  //std::cout<<"Number of directories: "<<numberOfDirectories<<std::endl;
  for(int i=0; i < numberOfDirectories; i++) {
    //std::cout<<directoryField[i]<<std::endl;
    mxArray* matArrayHandle = matGetVariable(matFileHandle, directoryField[i]);
    int numberOfRows = mxGetM(matArrayHandle);
    int numberOfColumns = mxGetN(matArrayHandle);
    double* matArrayAddress = mxGetPr(matArrayHandle);
    std::vector<double> missionDataEntry;
    for(int j=0;j<numberOfColumns;j++) {
      double matrixEntry = *(matArrayAddress+j);
      missionDataEntry.push_back(matrixEntry);
    }
    missionData.push_back(missionDataEntry);
  }
  std::cout<<"Mission data successfully loaded"<<std::endl<<std::endl;
}
Exemplo n.º 6
0
	int matFiles::readMatFile(const char *file, std::vector <std::vector<double> >& earSignals, double *fsHz) {
	  MATFile *pmat;
	  const char **dir;
	  const char *name;
	  int	  ndir;
	  int	  i;
	  mxArray *pa;
	  mxArray *var;
	  const size_t *dims;
	  size_t ndims;
	  double *data;
		
	  /*
	   * Open file to get directory
	   */
	  pmat = matOpen(file, "r");
	  if (pmat == NULL) {
		printf("Error opening file %s\n", file);
		return(1);
	  }
	  
	  /*
	   * get directory of MAT-file
	   */
	  dir = (const char **)matGetDir(pmat, &ndir);
	  if (dir == NULL) {
		printf("Error reading directory of file %s\n", file);
		return(1);
	  }
	  mxFree(dir);

	  /* In order to use matGetNextXXX correctly, reopen file to read in headers. */
	  if (matClose(pmat) != 0) {
		printf("Error closing file %s\n",file);
		return(1);
	  }
	  pmat = matOpen(file, "r");
	  if (pmat == NULL) {
		printf("Error reopening file %s\n", file);
		return(1);
	  }

	  /* Get headers of all variables */
	  /* Examining the header for each variable */
	  for (i=0; i < ndir; i++) {
		pa = matGetNextVariableInfo(pmat, &name);
		var = matGetVariable(pmat, name);
		data = mxGetPr(var);
		
		if (pa == NULL) {
		printf("Error reading in file %s\n", file);
		return(1);
		}

		if ( strcmp(name,"earSignals") == 0 )  {
			ndims = mxGetNumberOfDimensions(pa);
			dims = mxGetDimensions(pa);
			
			earSignals.resize(ndims);
			
			for ( size_t ii = 0 ; ii < ndims ; ++ii )
				earSignals[ii].resize(dims[0],0);
				
			size_t ii, iii;
			for ( ii = 0 ; ii < dims[0] ; ++ii )
				earSignals[0][ii] =  data[ii];

			for ( ii = dims[0], iii = 0 ; ii < dims[0] * 2 ; ++ii, ++iii )
				earSignals[1][iii] =  data[ii];
				
		} else	if ( strcmp(name,"fsHz") == 0 ) {
			ndims = mxGetNumberOfDimensions(pa);
			dims = mxGetDimensions(pa);
			
			assert( ndims == 2 );
			assert( dims[0] == 1 );
							
			*fsHz = data[0];
		}
			
		mxDestroyArray(pa);
	  }

	  if (matClose(pmat) != 0) {
		  printf("Error closing file %s\n",file);
		  return(1);
	  }
	  return(0);
	}