//----------------------------------------------------------------------------
VectorReducer::VectorReducer(fei::SharedPtr<fei::Reducer> reducer,
                             fei::SharedPtr<fei::Vector> target,
                             bool isSolutionVector)
  : reducer_(reducer),
    target_(target),
    isSolution_(isSolutionVector)
{
  localProc_ = fei::localProc(target->getVectorSpace()->getCommunicator());
  numProcs_ = fei::numProcs(target->getVectorSpace()->getCommunicator());

  fei::Vector_core* target_core = dynamic_cast<fei::Vector_core*>(target.get());
  if (target_core == NULL) {
    throw std::runtime_error("fei::VectorReducer ERROR, target vector not dynamic_cast-able to fei::Vector_core.");
  }

  fei::SharedPtr<fei::VectorSpace> vecspace = target->getVectorSpace();
  int numEqns = vecspace->getNumIndices_SharedAndOwned();
  std::vector<int> eqns;
  vecspace->getIndices_SharedAndOwned(eqns);

  std::vector<int> overlap;
  for(int i=0; i<numEqns; ++i) {
    if (!reducer->isSlaveEqn(eqns[i])) {
      overlap.push_back(reducer->translateToReducedEqn(eqns[i]));
    }
    else {
      std::vector<int> masters;
      reducer->getSlaveMasterEqns(eqns[i], masters);
      for(unsigned j=0; j<masters.size(); ++j) {
        overlap.push_back(reducer->translateToReducedEqn(masters[j]));
      }
    }
  }

  target_core->setOverlap(overlap.size(), &overlap[0]);
}
fei::SharedPtr<fei::MatrixGraph> test_MatrixGraph::create_MatrixGraph(testData* testdata,
					 int localProc, int numProcs,
					 bool bothFields, bool nonSymmetric,
					 const char* name,
					 fei::SharedPtr<fei::VectorSpace> vectorSpacePtr,
					 fei::SharedPtr<fei::Factory> factory,
                                         const std::string& path,
					 bool turnOnDebugOutput)
{
  //
  //This function creates a MatrixGraph object, and initializes it as follows:
  //
  //setRowSpace(vectorSpacePtr)
  //
  //definePattern patternID=0, numIDs=4, idType=testdata->idTypes[0]
  //      fieldID=testdata->fieldIDs[0] if !bothFields, else
  //      fieldIDs=testdata->fieldIDs
  //
  //initConnectivityBlock blockID=0, numConnectivityLists=1
  //
  //initConnectivity blockID, 0, testdata->ids
  //
  //If nonSymmetric==true, then also do the following:
  //  definePattern patternID=1, numIDs=1, idType=testdata->idTypes[0]
  //     fieldID=testdata->fieldIDs[0] if !bothFields, else
  //      fieldIDs=testdata->fieldIDs
  //  definePattern patternID=2, numIDs=4, idType=testdata->idTypes[0]
  //     fieldID=testdata->fieldIDs[0] if !bothFields, else
  //      fieldIDs=testdata->fieldIDs
  //
  //initConnectivityBlock blockID=1, patterns 1 and 2
  //
  //initConnectivity blockID, 0, testdata->ids
  //
  fei::SharedPtr<fei::MatrixGraph> mgptr;
  if (factory.get() == NULL) {
    fei::SharedPtr<fei::MatrixGraph> tmp(new fei::MatrixGraph_Impl2(vectorSpacePtr,
							      vectorSpacePtr, name));
    mgptr = tmp;
  }
  else {
    mgptr = factory->createMatrixGraph(vectorSpacePtr, vectorSpacePtr, name);
  }

  fei::ParameterSet paramset;
  fei::Param param1("name", name);
  paramset.add(param1);
  if (turnOnDebugOutput) {
    if (path.empty()) {
      fei::Param param2("debugOutput", ".");
      paramset.add(param2);
    }
    else {
      fei::Param param2("debugOutput", path.c_str());
      paramset.add(param2);
    }
  }

  fei::MatrixGraph* matrixGraphPtr = mgptr.get();

  matrixGraphPtr->setParameters(paramset);

  matrixGraphPtr->setRowSpace(vectorSpacePtr);

  int patternID = 0;
  int numIDs = 4;
  int idType = testdata->idTypes[0];
  int fieldID = testdata->fieldIDs[0];

  if (bothFields) {
    std::vector<int> numFieldsPerID(numIDs, 2);
    std::vector<int> fieldIDsArray(numIDs*2);
    for(int i=0; i<numIDs; ++i) {
      fieldIDsArray[i*2] = testdata->fieldIDs[0];
      fieldIDsArray[i*2+1] = testdata->fieldIDs[1];
    }

    patternID = matrixGraphPtr->definePattern(numIDs, idType,
					 &numFieldsPerID[0],
					 &fieldIDsArray[0]);
  }
  else {
    patternID = matrixGraphPtr->definePattern(numIDs, idType, fieldID);
  }

  int blockID = 0;
  int numConnectivityLists = 1;

  matrixGraphPtr->initConnectivityBlock(blockID,
					   numConnectivityLists,
					   patternID);

  matrixGraphPtr->initConnectivity(blockID, 0, &(testdata->ids[0]));

  if (!nonSymmetric) {
    return(mgptr);
  }

  int patternID1 = 1, patternID2 = 2;
  int numRowIDs = 1, numColIDs = 4;

  if (bothFields) {
    std::vector<int> numFieldsPerID(numIDs, 2);
    std::vector<int> fieldIDsArray(numIDs*2);
    for(int i=0; i<numIDs; ++i) {
      fieldIDsArray[i*2] = testdata->fieldIDs[0];
      fieldIDsArray[i*2+1] = testdata->fieldIDs[1];
    }

    patternID1 = matrixGraphPtr->definePattern(numRowIDs, idType,
					 &numFieldsPerID[0],
					 &fieldIDsArray[0]);
    patternID2 = matrixGraphPtr->definePattern(numColIDs, idType,
					 &numFieldsPerID[0],
					 &fieldIDsArray[0]);
  }
  else {
    patternID1 = matrixGraphPtr->definePattern(numRowIDs,
					   idType, fieldID);
    patternID2 = matrixGraphPtr->definePattern(numColIDs,
					   idType, fieldID);
  }

  blockID = 1;

  matrixGraphPtr->initConnectivityBlock(blockID,
					   numConnectivityLists,
					   patternID1, patternID2);

  matrixGraphPtr->initConnectivity(blockID, 0,
					  &(testdata->ids[0]),
					  &(testdata->ids[0]));

  return(mgptr);
}