예제 #1
0
doublereal LiquidTransport::getElectricConduct()
{
    vector_fp gradT(m_nDim,0.0);
    vector_fp gradX(m_nDim * m_nsp);
    vector_fp gradV(m_nDim);
    for (size_t i = 0; i < m_nDim; i++) {
        for (size_t k = 0; k < m_nsp; k++) {
            gradX[ i*m_nDim + k] = 0.0;
        }
        gradV[i] = 1.0;
    }

    set_Grad_T(&gradT[0]);
    set_Grad_X(&gradX[0]);
    set_Grad_V(&gradV[0]);

    vector_fp fluxes(m_nsp * m_nDim);
    doublereal current;
    getSpeciesFluxesExt(m_nDim, &fluxes[0]);

    //sum over species charges, fluxes, Faraday to get current
    // Since we want the scalar conductivity, we need only consider one-dim
    for (size_t i = 0; i < 1; i++) {
        current = 0.0;
        for (size_t k = 0; k < m_nsp; k++) {
            current += m_chargeSpecies[k] * Faraday * fluxes[k] / m_mw[k];
        }
        //divide by unit potential gradient
        current /= - gradV[i];
    }
    return current;
}
/* ExtractHOG is for extracting HOG descriptor of an image
   Input:
           im: A grayscale image in height x width.
   Output:
           HOGBlock: The HOG descriptor of the input image.
*/
Mat HOGExtractor::ExtractHOG(const Mat& im) {
	// Pad the im in order to make the height and width the multiplication of
    // the size of cells.
	int height = im.rows;
	int width = im.cols;
	int padHeight = height % cells == 0 ? 0 : (cells - height % cells);
    int padWidth = width % cells == 0 ? 0 : (cells - width % cells);
	Mat paddedIm(height+padHeight, width+padWidth, CV_32FC1, Scalar(0));
	Range imRanges[2];
	imRanges[0] = Range(0, height);
	imRanges[1] = Range(0, width);
	im.copyTo(paddedIm(imRanges));
	height = paddedIm.rows;
	width = paddedIm.cols;

	/* TODO 1: 
       Compute the horizontal and vertical gradients for each pixel. Put them 
	   in gradX and gradY respectively. In addition, compute the angles (using
	   atan2) and magnitudes by gradX and gradY, and put them in angle and 
	   magnitude. 
	*/
	Mat hx(1, 3, CV_32FC1, Scalar(0));
	hx.at<float>(0, 0) = -1;
	hx.at<float>(0, 1) = 0;
	hx.at<float>(0, 2) = 1;
	Mat hy = -hx.t();

	Mat gradX(height, width, CV_32FC1, Scalar(0));
	Mat gradY(height, width, CV_32FC1, Scalar(0));
	Mat angle(height, width, CV_32FC1, Scalar(0));
	Mat magnit(height, width, CV_32FC1, Scalar(0));
	float pi = 3.1416;
	
	// Begin TODO 1
	filter2D(paddedIm, gradX, -1, hx);
	filter2D(paddedIm, gradY, -1, hy);
	magnitude(gradX, gradY, magnit);
	for (int i = 0; i < height; ++i)
		for (int j = 0; j < width; ++j)
			angle.at<float>(i, j) = atan2(gradY.at<float>(i, j), gradX.at<float>(i, j));

    // End TODO 1

	/* TODO 2:
	   Construct HOG for each cells, and put them in HOGCell. numberOfVerticalCell
	   and numberOfHorizontalCell are the numbers of cells in vertical and 
	   horizontal directions.
	   You should construct the histogram according to the bins. The bins range
	   from -pi to pi in this project, and the interval is given by
	   (2*pi)/bins.
	*/
	int numberOfVerticalCell = height / cells;
	int numberOfHorizontalCell = width / cells;
	Mat HOGCell(numberOfVerticalCell, numberOfHorizontalCell, 
		CV_32FC(bins), Scalar(0));
	float piInterval = 2 * pi / bins;
	// Begin TODO 2
	for (int i = 0; i < numberOfVerticalCell; ++i)
		for (int j = 0; j < numberOfHorizontalCell; ++j) 
			for (int p = i * cells; p < i * cells + cells; ++p)
				for (int q = j * cells; q < j * cells + cells; ++q) 
					HOGCell.at<Vec<float, 9>>(i, j)[min(bins - 1, int(floor((angle.at<float>(p, q) + pi) / piInterval)))] += magnit.at<float>(i, j);
	// End TODO 2

	/* TODO 3:
	   Concatenate HOGs of the cells within each blocks and normalize them.
	   The result should be stored in HOGBlock, where numberOfVerticalBlock and
	   numberOfHorizontalBlock are the number of blocks in vertical and
	   horizontal directions.
	*/
	int numberOfVerticalBlock = numberOfVerticalCell - 1;
	int numberOfHorizontalBlock = numberOfHorizontalCell - 1;
	Mat HOGBlock(numberOfVerticalBlock, numberOfHorizontalBlock, 
		CV_32FC(blocks*blocks*bins), Scalar(0));
	Mat Block(1, blocks * blocks * bins, CV_32FC1, Scalar(0));

	// Begin TODO 3
	for (int i = 0; i < numberOfVerticalBlock; ++i)
		for (int j = 0; j < numberOfHorizontalBlock; ++j) { 
			for (int k = 0; k < bins; ++k) {
				Block.at<float>(0, k) = HOGCell.at<Vec<float, 9>>(i, j)[k];
				Block.at<float>(0, k + bins) = HOGCell.at<Vec<float, 9>>(i, j + 1)[k];
				Block.at<float>(0, k + 2 * bins) = HOGCell.at<Vec<float, 9>>(i + 1, j)[k];
				Block.at<float>(0, k + 3 * bins) = HOGCell.at<Vec<float, 9>>(i + 1, j + 1)[k];
			}
			float sum = 0;
			for (int k = 0; k < blocks * blocks * bins; ++k) 
				sum += Block.at<float>(0,k);
			for (int k = 0; k < blocks * blocks * bins; ++k) 
				HOGBlock.at<Vec<float, 36>>(i, j)[k] = sum == 0 ? 0 : Block.at<float>(0, k) / sum;
		}
	// End TODO 3
	return HOGBlock;
}
void ATO::ModalObjective<PHAL::AlbanyTraits::Residual, Traits>::
evaluateFields(typename Traits::EvalData workset)
{

  Albany::MDArray F    = (*workset.stateArrayPtr)[FName];
  Albany::MDArray dEdp = (*workset.stateArrayPtr)[dFdpName];
  Albany::MDArray topo = (*workset.stateArrayPtr)[topology->getName()];
  std::vector<int> dims;
  gradX.dimensions(dims);
  int size = dims.size();

  int numCells = dims[0];
  int numQPs = dims[1];
  int numDims = dims[2];
  int numNodes = topo.dimension(1);

  if( size == 4 ){
    for(int cell=0; cell<numCells; cell++){
      double dE = 0.0;
      double dmass_term = 0.;
      double dstiffness_term = 0.;
      for(int qp=0; qp<numQPs; qp++){
        double topoVal = 0.0;
        for(int node=0; node<numNodes; node++)
          topoVal += topo(cell,node)*BF(cell,node,qp);
        double P = topology->Penalize(functionIndex,topoVal);
        double dP = topology->dPenalize(functionIndex,topoVal);
        double dE = 0.0;
        double dmass_term = 0.;
        double dstiffness_term = 0.;
        for(int i=0; i<numDims; i++) {
          dmass_term += val_qp(cell,qp,i)*val_qp(cell,qp,i) * qp_weights(cell,qp);
          for(int j=0; j<numDims; j++)
            dstiffness_term += dP*gradX(cell,qp,i,j)*workConj(cell,qp,i,j)*qp_weights(cell,qp);
        }
        for(int node=0; node<numNodes; node++)
        dEdp(cell,node) = -(dstiffness_term - dmass_term*eigval(0))*BF(cell,node,qp);
      }
//tevhack        std::cout << "dEdp(" << cell << ") = " << dEdp(cell) << std::endl;
    }
  } else {
    TEUCHOS_TEST_FOR_EXCEPTION(true, Teuchos::Exceptions::InvalidParameter,
      "Unexpected array dimensions in StiffnessObjective:" << size << std::endl);
  }

/*
  if( size == 3 ){
    for(int cell=0; cell<numCells; cell++){
      double dE = 0.0;
      double P = topology->Penalize(topo(cell));
      for(int qp=0; qp<numQPs; qp++) {
        for(int i=0; i<numDims; i++) {
          dE += val_qp(cell,qp,i) * val_qp(cell,qp,i);
        }
        for(int node=0; node<numNodes; node++)
          dEdp(cell,node) = dE/P*BF(cell,node,qp);
      }
      std::cout << "dEdp(" << cell << ") = " << dEdp(cell) << std::endl;
    }
  } else {
    TEUCHOS_TEST_FOR_EXCEPTION(
      true, 
      Teuchos::Exceptions::InvalidParameter,
      "Unexpected array dimensions in ModalObjective:" << size << std::endl
    );
  }
*/

  F(0) = -eigval(0);

}