示例#1
0
const MeshLib::CFEMesh* GridAdapter::toCFEMesh() const
{
	MeshLib::CFEMesh* mesh (new MeshLib::CFEMesh());
	std::cout << "Converting mesh object ... ";

	// set mesh nodes
	size_t nNodes = _nodes->size();
	for (size_t i = 0; i < nNodes; i++)
		mesh->nod_vector.push_back(new MeshLib::CNode(i, (*(*_nodes)[i])[0], (*(*_nodes)[i])[1], (*(*_nodes)[i])[2]));

	// set mesh elements
	size_t nElems = _elems->size();
	for (size_t i = 0; i < nElems; i++)
	{
		MeshLib::CElem* elem(new MeshLib::CElem());
		elem->setElementProperties((*_elems)[i]->type);
		elem->SetPatchIndex((*_elems)[i]->material);

		size_t nElemNodes = ((*_elems)[i]->nodes).size();
		for (size_t j = 0; j < nElemNodes; j++)
			elem->SetNodeIndex(j, (*_elems)[i]->nodes[j]);

		mesh->ele_vector.push_back(elem);
	}

	mesh->ConstructGrid();
	std::cout << "done." << "\n";

	return mesh;
}
示例#2
0
文件: misc.cpp 项目: drjod/ogs_kb1
void writeOGSMesh(const MeshLib::CFEMesh &mesh, const std::string file_base_name)
{
    const string fname = file_base_name + "_mesh_converted_from_GIS_shape_data.msh";
	ofstream os(fname.c_str(), ios::trunc); 
    os.setf(ios::scientific, ios::floatfield);
    setw(14);
    os.precision(10);

    os << "#FEM_MSH \n $PCS_TYPE\n NO_PCS\n $NODES" <<endl;
	const size_t nn = mesh.GetNodesNumber(false);
	os << nn <<endl;

	for(size_t i=0; i<nn; i++)
	{
        MeshLib::CNode *n_ptr = mesh.nod_vector[i];
		const double *xyz_ptr = n_ptr->getData(); 
		os<< n_ptr->GetIndex() <<" "<<xyz_ptr[0] <<" "<<xyz_ptr[1] <<" "<<xyz_ptr[2] <<"\n";
	}

    os << "$ELEMENTS" <<endl;
    const size_t ne = mesh.ele_vector.size();

	for(size_t i=0; i<ne; i++)
	{
       MeshLib::CElem *e_ptr = mesh.ele_vector[i];
	   e_ptr->WriteIndex(os);
    }

	os << "#STOP" <<endl;

	os.close();
}
void LegacyVtkInterface::WriteVTKCellDataPETSC(PetscViewer viewer) const
{
	size_t nelocal = _mesh->ele_vector.size();  // local size

	// count overall length of element vector
	long numAllPoints = 0;
	for (size_t i = 0; i < nelocal; i++)
	{
		MeshLib::CElem* ele = _mesh->ele_vector[i];
		numAllPoints = numAllPoints + (ele->GetNodesNumber(false)) + 1;
	}

	PetscScalar* ep, *eglobp;  // used for pointer
	PetscInt low, high, neglob, nn;
	int i;
	VecScatter ctx;

	// get my petsc rank
	int myrank;
	MPI_Comm_rank(PETSC_COMM_WORLD, &myrank);

	// test vtk output
	PETSc_Vec e, eglob, x;  //

	// vector for elements ...contains number of nodes connected to form a
	// element in first entry and then the node numbers
	VecCreate(PETSC_COMM_WORLD, &e);
	VecSetSizes(
	    e, numAllPoints,
	    PETSC_DECIDE);  // nummAllPoints is local lenght of the vector we need
	VecSetFromOptions(e);  //
	// get range of local variables
	VecGetOwnershipRange(e, &low, &high);
	VecGetSize(e, &neglob);
	// get local part of vector
	VecGetArray(e, &ep);

	// in order to get a global mesh the node numbers have to be corrected..
	// we have to get the position in the global node vector in order to  get
	// the correct  additive value
	// is there a simpler way to do it (as below)?
	MeshLib::CFEMesh* mesh = fem_msh_vector[0];
	//    const int nn = mesh->getNumNodesGlobal(); //global number of nodes
	//    without shadow nodes
	const size_t n_linear_pnts(mesh->GetNodesNumber(false));

	VecCreate(PETSC_COMM_WORLD, &x);
	VecSetSizes(x, n_linear_pnts, PETSC_DECIDE);
	VecSetFromOptions(x);  //
	// get range of local variables
	VecGetOwnershipRange(x, &low, &high);
	VecGetSize(x, &nn);
	//  cout << " " << low << " " << high << " \n";
	// low is now the first node number

	// now we can fill the element vector
	size_t anz_elem = 0;  // here a local value
	for (size_t i = 0; i < nelocal; i++)
	{
		MeshLib::CElem* ele = _mesh->ele_vector[i];
		//	cout << ele->GetNodesNumber(false)<< " " ;

		switch (ele->GetElementType())  // first entry: type of element as
		                                // defined for vtk ..better than number
		                                // of nodes
		{
			case MshElemType::LINE:
				ep[anz_elem] = 3;
				break;
			case MshElemType::QUAD:
				ep[anz_elem] = 9;
				break;
			case MshElemType::HEXAHEDRON:
				ep[anz_elem] = 12;
				break;
			case MshElemType::TRIANGLE:
				ep[anz_elem] = 5;
				break;
			case MshElemType::TETRAHEDRON:
				ep[anz_elem] = 10;
				break;
			case MshElemType::PRISM:  // VTK_WEDGE
				ep[anz_elem] = 13;
				break;
			case MshElemType::PYRAMID:
				ep[anz_elem] = 14;
				break;
			default:
				cerr << "PETSC VTK output::WriteVTKElementData MshElemType not "
				        "recogniced"
				     << "\n";
				break;
		}

		//   cout << ele->GetNodesNumber(false)<< " " ;
		for (size_t j = 0; j < ele->GetNodesNumber(false); j++)
		{
			ep[anz_elem + j + 1] =
			    ele->GetNodeIndeces()[j] + low;  // this are the nodes
			//		cout << "DEBUG " <<  ele->GetNodeIndeces()[j] << " " ;
		}
		// cout << " \n";
		anz_elem = anz_elem + (ele->GetNodesNumber(false)) + 1;
	}

	// now scatter the vectors to process rank 0
	VecScatterCreateToZero(e, &ctx, &eglob);
	VecScatterBegin(ctx, e, eglob, INSERT_VALUES, SCATTER_FORWARD);
	VecScatterEnd(ctx, e, eglob, INSERT_VALUES, SCATTER_FORWARD);
	// count number of elements
	if (myrank == 0)
	{
		VecGetArray(eglob, &eglobp);
		anz_elem = 0;  // here this es global number of elements
		i = 0;
		while (i < neglob)
		{
			anz_elem += 1;  // add one for the element
			int mswitch = (int)eglobp[i];
			switch (mswitch)  // first entry: type of element as defined for vtk
			                  // ..better than number of nodes
			{
				case 3:  // LINE
					i += 2 + 1;
					break;
				case 9:  // QUAD
					i += 4 + 1;
					break;
				case 12:  // HEXAHEDRON:
					i += 6 + 1;
					break;
				case 5:  // TRIANGLE:
					i += 3 + 1;
					break;
				case 10:  // TETRAHEDRON:
					i += 4 + 1;
					break;
				case 13:  // PRISM: // VTK_WEDGE
					i += 6 + 1;
					break;
				case 14:  // PYRAMID:
					i += 5 + 1;
					break;
				default:
					cerr << "PETSC VTK Output 1::WriteVTKElementData "
					        "MshElemType not handled, i. anz_elem. ep[i] type "
					     << i << " " << anz_elem << " " << eglobp[i] << "\n";
					exit(1);
					break;
			}
		}

		// write elements
		//	vtk_file << "CELLS " << numCells << " " << numAllPoints << "\n";
		PetscViewerASCIIPrintf(viewer, "CELLS %d %d\n", anz_elem, neglob);
		i = 0;
		std::stringstream ss;
		while (i < neglob)
		{
			switch ((int)eglobp[i])  // first entry: type of element as defined
			                         // for vtk ..better than number of nodes
			{
				case 3:  // LINE
					ss << " 2 ";
					// PetscViewerASCIIPrintf(viewer," 2 ");
					for (size_t j = 0; j < 2; j++)
						ss << eglobp[i + j + 1] << " ";
					//                	PetscViewerASCIIPrintf(viewer," %d
					//                ",(long) eglobp[i+j+1]);
					i += 2 + 1;
					break;
				case 9:  // QUAD
					ss << " 4 ";
					// PetscViewerASCIIPrintf(viewer," 4 ");
					for (size_t j = 0; j < 4; j++)
						ss << eglobp[i + j + 1] << " ";
					//                	PetscViewerASCIIPrintf(viewer," %d
					//                ",(long) eglobp[i+j+1]);
					i += 4 + 1;
					break;
				case 12:  // HEXAHEDRON:
					ss << " 6 ";
					// PetscViewerASCIIPrintf(viewer," 6 ");
					for (size_t j = 0; j < 6; j++)
						ss << eglobp[i + j + 1] << " ";
					// PetscViewerASCIIPrintf(viewer," %d ",(long)
					// eglobp[i+j+1]);
					i += 6 + 1;
					break;
				case 5:  // TRIANGLE:
					ss << " 3 ";
					// PetscViewerASCIIPrintf(viewer," 3 ");
					for (size_t j = 0; j < 3; j++)
						ss << eglobp[i + j + 1] << " ";
					// PetscViewerASCIIPrintf(viewer," %d ",(long)
					// eglobp[i+j+1]);
					i += 3 + 1;
					break;
				case 10:  // TETRAHEDRON:
					ss << " 4 ";
					//                PetscViewerASCIIPrintf(viewer," 4 ");
					for (size_t j = 0; j < 4; j++)
						ss << eglobp[i + j + 1] << " ";
					// PetscViewerASCIIPrintf(viewer," %d ",(long)
					// eglobp[i+j+1]);
					i += 4 + 1;
					break;
				case 13:  // PRISM: // VTK_WEDGE
					ss << " 6 ";
					//                PetscViewerASCIIPrintf(viewer," 6 ");
					for (size_t j = 0; j < 6; j++)
						ss << eglobp[i + j + 1] << " ";
					// PetscViewerASCIIPrintf(viewer," %d ",(long)
					// eglobp[i+j+1]);
					i += 6 + 1;
					break;
				case 14:  // PYRAMID:
					ss << " 5 ";
					//                PetscViewerASCIIPrintf(viewer," 5 ");
					for (size_t j = 0; j < 5; j++)
						ss << eglobp[i + j + 1] << " ";
					// PetscViewerASCIIPrintf(viewer," %d ",(long)
					// eglobp[i+j+1]);
					i += 5 + 1;
					break;
				default:
					cerr << "PETSC VTK Output 2::WriteVTKElementData "
					        "MshElemType not handled"
					     << "\n";
					break;
			}
			ss << "\n";
			//            PetscViewerASCIIPrintf(viewer," \n ");
			if (i % 1000 == 0)
			{
				PetscViewerASCIIPrintf(viewer, ss.str().c_str());
				ss.str("");
				ss.clear();
			}
		}
		if (!ss.str().empty())
		{
			PetscViewerASCIIPrintf(viewer, ss.str().c_str());
		}
		ss.str("");
		ss.clear();

		// write cell types
		//	vtk_file << "CELL_TYPES " << numCells << "\n";
		PetscViewerASCIIPrintf(viewer, "CELL_TYPES %d \n", anz_elem);
		i = 0;
		while (i < neglob)
		{
			switch ((int)eglobp[i])  // first entry: type of element as defined
			                         // for vtk ..better than number of nodes
			{
				case 3:  // LINE
					ss << " 3 \n";
					//                PetscViewerASCIIPrintf(viewer," 3 \n");
					i += 2 + 1;
					break;
				case 9:  // QUAD
					ss << " 9 \n";
					//                PetscViewerASCIIPrintf(viewer," 9 \n");
					i += 4 + 1;
					break;
				case 12:  // HEXAHEDRON:
					ss << " 12 \n";
					// PetscViewerASCIIPrintf(viewer," 12 \n");
					i += 6 + 1;
					break;
				case 5:  // TRIANGLE:
					ss << " 5 \n";
					// PetscViewerASCIIPrintf(viewer," 5 \n");
					i += 3 + 1;
					break;
				case 10:  // TETRAHEDRON:
					ss << " 10 \n";
					// PetscViewerASCIIPrintf(viewer," 10 \n");
					i += 4 + 1;
					break;
				case 13:  // PRISM: // VTK_WEDGE
					ss << " 13 \n";
					// PetscViewerASCIIPrintf(viewer," 13 \n");
					i += 6 + 1;
					break;
				case 14:  // PYRAMID:
					ss << " 14 \n";
					// PetscViewerASCIIPrintf(viewer," 14 \n");;
					i += 5 + 1;
					break;
				default:
					cerr << "COutput::WriteVTKElementData MshElemType not "
					        "handled"
					     << "\n";
					break;
			}
			if (i % 1000 == 0)
			{
				PetscViewerASCIIPrintf(viewer, ss.str().c_str());
				ss.str("");
				ss.clear();
			}
			//	PetscViewerASCIIPrintf(viewer," \n ");
		}
		if (!ss.str().empty())
		{
			PetscViewerASCIIPrintf(viewer, ss.str().c_str());
		}
		ss.str("");
		ss.clear();

		VecRestoreArray(eglob, &eglobp);
	}  // end myrank == 0

	VecRestoreArray(e, &ep);

	VecDestroy(&e);
	VecDestroy(&eglob);
	VecDestroy(&x);
}
void LegacyVtkInterface::WriteVTKPointDataPETSC(PetscViewer viewer) const
{
	PetscScalar* xp, *yp, *zp;  // used for pointer
	PetscInt low, high, nn;
	PetscInt count;
	int i;
	VecScatter ctx;

	MeshLib::CFEMesh* mesh = fem_msh_vector[0];
	//    const int nn = mesh->getNumNodesGlobal(); //global number of nodes
	//    without shadow nodes
	const size_t n_linear_pnts(mesh->GetNodesNumber(false));
	// get my petsc rank
	int myrank;
	MPI_Comm_rank(PETSC_COMM_WORLD, &myrank);

	// test vtk output
	PETSc_Vec x, y, z, xcoor, ycoor, zcoor;  //
	VecCreate(PETSC_COMM_WORLD, &x);
	VecSetSizes(x, n_linear_pnts, PETSC_DECIDE);
	VecSetFromOptions(x);  //
	// get range of local variables
	VecGetOwnershipRange(x, &low, &high);
	VecGetLocalSize(x, &count);
	VecGetSize(x, &nn);
	// first duplicate x vector
	VecDuplicate(x, &y);
	VecDuplicate(x, &z);
	// get local part of vectors
	VecGetArray(x, &xp);
	VecGetArray(y, &yp);
	VecGetArray(z, &zp);

	// write coordinates

	PetscViewerASCIIPrintf(viewer, "POINTS %d double\n", nn);

	const std::vector<MeshLib::CNode*> pointVector = mesh->getNodeVector();

	for (size_t i = 0; i < (size_t)count; i++)
	{
		double const* const coords(pointVector[i]->getData());
		// now fill the vectors
		// get the pointer to current node;
		// copy local coordinates to pointer
		xp[i] = coords[0];
		yp[i] = coords[1];
		zp[i] = coords[2];
	}

	// create a sequential vector and scatter the coordinates
	// to this seq. vector on proc 0
	VecScatterCreateToZero(x, &ctx, &xcoor);
	VecScatterBegin(ctx, x, xcoor, INSERT_VALUES, SCATTER_FORWARD);
	VecScatterEnd(ctx, x, xcoor, INSERT_VALUES, SCATTER_FORWARD);
	VecScatterCreateToZero(y, &ctx, &ycoor);
	VecScatterBegin(ctx, y, ycoor, INSERT_VALUES, SCATTER_FORWARD);
	VecScatterEnd(ctx, y, ycoor, INSERT_VALUES, SCATTER_FORWARD);
	VecScatterCreateToZero(z, &ctx, &zcoor);
	VecScatterBegin(ctx, z, zcoor, INSERT_VALUES, SCATTER_FORWARD);
	VecScatterEnd(ctx, z, zcoor, INSERT_VALUES, SCATTER_FORWARD);
	// now we have the global vector on rank 0 and can write
	if (myrank == 0)
	{
		VecGetArray(xcoor, &xp);
		VecGetArray(ycoor, &yp);
		VecGetArray(zcoor, &zp);
		for (i = 0; i < nn; i++)
		{
			PetscViewerASCIIPrintf(viewer, "%1.13lg %1.13lg %1.13lg \n", xp[i],
			                       yp[i], zp[i]);
		}
		VecRestoreArray(xcoor, &xp);
		VecRestoreArray(ycoor, &yp);
		VecRestoreArray(zcoor, &zp);
	}

	VecDestroy(&x);
	VecDestroy(&y);
	VecDestroy(&z);
	VecDestroy(&xcoor);
	VecDestroy(&ycoor);
	VecDestroy(&zcoor);

	return;
}