// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void AbaqusSurfaceMeshWriter::execute()
{
  int err = 0;
  std::stringstream ss;

  SurfaceMeshDataContainer* sm = getSurfaceMeshDataContainer();
  dataCheck(false, 1, 1, 1);
  if(getErrorCondition() < 0)
  {
    return;
  }

  // Make sure any directory path is also available as the user may have just typed
  // in a path without actually creating the full path
  std::string parentPath = MXAFileInfo::parentPath(getOutputFile());
  if(!MXADir::mkdir(parentPath, true))
  {
    std::stringstream ss;
    ss << "Error creating parent path '" << parentPath << "'";
    notifyErrorMessage(ss.str(), -1);
    setErrorCondition(-1);
    return;
  }

  DREAM3D::SurfaceMesh::VertListPointer_t nodesPtr = sm->getVertices();
  DREAM3D::SurfaceMesh::FaceListPointer_t trianglePtr = sm->getFaces();

  // Get the Labels(GrainIds or Region Ids) for the triangles
  Int32ArrayType::Pointer faceLabelsPtr = boost::dynamic_pointer_cast<Int32ArrayType>(sm->getFaceData(DREAM3D::FaceData::SurfaceMeshFaceLabels));
  int32_t* faceLabels = faceLabelsPtr->GetPointer(0);

  // Store all the unique Spins
  std::set<int> uniqueSpins;
  for (int i = 0; i < trianglePtr->GetNumberOfTuples(); i++)
  {
    uniqueSpins.insert(faceLabels[i*2]);
    uniqueSpins.insert(faceLabels[i*2+1]);
  }

  FILE* f = fopen(m_OutputFile.c_str(), "wb");
  ScopedFileMonitor fileMonitor(f);

  err = writeHeader(f, nodesPtr->GetNumberOfTuples(), trianglePtr->GetNumberOfTuples(), uniqueSpins.size()-1);
  err = writeNodes(f);
  err = writeTriangles(f);
  err = writeGrains(f);

  setErrorCondition(0);
  notifyStatusMessage("Complete");

  return;
}
Example #2
0
/*** DEFINITIONS ***/
int main(int argc, char **argv)
{

/* local variables */
  FILE *problem_input, *problem_output;
  FILE *interactive_input, *interactive_output;
  FILE *grains;
  char grains_name[200];
  char problem_input_name[300], problem_output_name[300];
  char interactive_input_name[300];
  char interactive_output_name[300], tmp[100];
  char field_name[300];
  int problem_id;
  char c, arg[100], arg2[100], arg3[100]; /* string variable to read menu */
  int i, j,k, iaux, jaux, iprob, icount;
  int lel[100], nreq, iel, base_q;
  int field_id, mesh_id;
  double daux, eaux, t_wall, t_wall2, t_wall3;
  int max_gen, max_gen_diff;
  int nrno, nmno, nred, nmed, nrfa, nmfa, nrel, nmel;
  int indeks=0;
  int licznik,nel,nmaxel;
  double *x,*y,*z,*xa,*ya,*za;
  int *nrgrain,*elemgr;
  int *tmpfer,liczfer;
  double tmpx,tmpy,tmpz;
  int el_nodes[MMC_MAXELVNO+1];        // list of nodes of El
  double node_coor[3*MMC_MAXELVNO];  // coord of nodes of El
  double p[3];
  int spr,spr2,flag;
  double hsize, min, mn;
  int neig[6],neig2[6],matl,tmpnel,matl2,max,*counter,anc;
  double xs,ys,zs;
  int zxs,zys,zzs,zxa,zya,zza;
#ifdef PARALLEL
  int info;
#endif

   //arrays for grains

   x=(double *)calloc(1000000,sizeof(double));
   y=(double *)calloc(1000000,sizeof(double));
   z=(double *)calloc(1000000,sizeof(double));
   xa=(double *)calloc(1000000,sizeof(double));
   ya=(double *)calloc(1000000,sizeof(double));
   za=(double *)calloc(1000000,sizeof(double));
   nrgrain=(int *)calloc(1000000,sizeof(int));
   tmpfer=(int *)calloc(1000000,sizeof(int));

  /*++++++++++++++++ executable statements ++++++++++++++++*/

    /* very simple - for the beginning instead of GUI */
    if(argv[1]==NULL){
  	strcpy(work_dir,".");
    } else {
  	sprintf(work_dir,"%s",argv[1]);
    }

    utr_set_interactive(work_dir, argc, argv,
			&interactive_input, &interactive_output);

  #ifdef DEBUG
    fprintf(interactive_output,"Starting program in debug mode.\n");
  #endif
  #ifdef DEBUG_MMM
    fprintf(interactive_output,"Starting mesh module in debug mode.\n");
  #endif
  #ifdef DEBUG_APM
    fprintf(interactive_output,"Starting approximation module in debug mode.\n");
  #endif
  #ifdef DEBUG_SIM
    fprintf(interactive_output,"Starting solver interface in debug mode.\n");
  #endif
  #ifdef DEBUG_LSM
    fprintf(interactive_output,"Starting linear solver (adapter) module in debug mode.\n");
  #endif

  /*++++ initialization of problem, mesh and field data ++++*/
    problem_id=pdr_init_problem(work_dir, interactive_input, interactive_output);

  /*++++++++++++ main menu loop ++++++++++++++++++++++++++++*/
  do {

#ifdef PARALLEL
    if(pcr_my_proc_id()==pcr_print_master()){
#endif

    if(interactive_input == stdin){


      do {
	/* define a menu */
	printf("\nChoose a command from the menu:\n");
	printf("\ts - solve test problem - Laplace's quation \n");
	printf("\tp - postprocessing\n");
	printf("\te - compute error\n");
	printf("\tm - perform manual mesh adaptation \n");
	printf("\ta - perform automatic mesh adaptation \n");
	printf("\td - dump out data \n");
	printf("\tr - perform random test of h-adaptivity\n");
	if(pdv_problem.gr.en==1)
		printf("\tg - solve grains\n");
	printf("\tq - exit the program\n");

	scanf(" %c",&c);
      } while ( c != 'm' && c != 'd' && c != 'r' && c != 'g'
		&& c != 's' && c != 'e' && c != 'p'
		&& c != 'a' && c != 'q' && c != 'q' );

    } else{

      // read control data from interactive_input file
      fscanf(interactive_input,"%c\n",&c);

    }

#ifdef PARALLEL
    }

    pcr_bcast_char(pcr_print_master(),1,&c);
    //printf("After BCAST %c\n",c);

#endif


    if(c=='g'){

		j=0;

#ifdef PARALLEL
    if(pcr_my_proc_id()==pcr_print_master()){
#endif
		printf("Compute grains\n");
#ifdef PARALLEL
    }
#endif
		k=0;
		for(licznik=0;licznik<680;licznik+=10)
		{
				//problem_id=pdr_init_problem(work_dir, interactive_input, interactive_output);
#ifdef PARALLEL
    if(pcr_my_proc_id()==pcr_print_master()){
#endif
				printf("\nIteration=%d\n",licznik);
#ifdef PARALLEL
    }
#endif
				sprintf(grains_name,"%s/przemiana/iteration_%d.txti", work_dir,licznik);
//				grains = fopen(grains_name,"r");
//
//				if(grains==NULL)
//				{
//					printf("Unable to open grains file for %d iteration - make sure that you have 'przemiana' directory!!\n",licznik);
//					exit(-1);
//				}
//
//				utr_skip_rest_of_line(grains);//
//
//				for(i=0;i<90000;i++)
//					fscanf(grains,"%d %d %d %d\n",&tmpx,&tmpy,&nrgrain[i],&tmpfer);
//
//				fclose(grains);
//
//				max=nrgrain[0];
//				for(i=0;i<90000;i++)
//				{
//					if(nrgrain[i]>=max)
//						max=nrgrain[i];
//				}
//				//printf("max=%d\n",max);
//				counter=(int *)calloc(max+2,sizeof(int));
//				for(i=0;i<90000;i++)
//				{
//					counter[nrgrain[i]]++;
//				}
//				for(i=1;i<=max;i++)
//				{
//					printf("ziarno %d wystepuje %d razy\n",i,counter[i]);
//				}


				grains = fopen(grains_name,"r");
				if(grains==NULL)
				{
					printf("Unable to open grains file for %d iteration - make sure that you have 'przemiana' directory!!\n",licznik);
					exit(-1);
				}
				utr_skip_rest_of_line(grains);
				mn=pdv_problem.gr.multip;

				liczfer=0;
				for(i=0;i<90000;i++)
				{
						fscanf(grains,"%lf %lf %d %d\n",&tmpx,&tmpy,&nrgrain[i],&tmpfer[i]);
						if((tmpfer[i]==0))//&&(counter[nrgrain[i]]>4))
						{
							x[liczfer]=tmpx/mn;
							y[liczfer]=299/mn-tmpy/mn;
							liczfer++;
						}
						xa[i]=tmpx/mn;
						ya[i]=299/mn-tmpy/mn;
				}
				//printf("Ferrite grains for iteration %d - %d\n",licznik,liczfer);

				fclose(grains);

				elemgr=(int *)calloc(mmr_get_max_elem_id(mesh_id)+1,sizeof(int));

				nel=0;
				spr=0;
				while((nel=mmr_get_next_act_elem(mesh_id, nel))!=0)
				{
						mmr_el_node_coor(mesh_id,nel,el_nodes,node_coor);
						flag=0;
						for(i=0;i<liczfer;i++)
						{
							p[0]=x[i];
							p[1]=y[i];
							p[2]=0;
							spr=pointintriangle(p,&node_coor[0],&node_coor[3],&node_coor[6]);
							if(spr==1)
							{
								flag=1;
							}
						}
						if(flag==0)
						{
							spr=0;
							for(i=0;i<90000;i++)
							{
								p[0]=xa[i];
								p[1]=ya[i];
								p[2]=0;
								spr+=pointintriangle(p,&node_coor[0],&node_coor[3],&node_coor[6]);
							}
							if(spr==0)
							{
								flag=3;
							}
						}

						if(flag==1)
						{
							mmr_el_set_mate(mesh_id,nel,2);
						}
						else if(flag==0)
						{
							mmr_el_set_mate(mesh_id,nel,1);
						}
						else
						{
							xs=node_coor[0]+node_coor[3]+node_coor[6];
							xs=xs/3;
							xs=xs*mn;
							ys=node_coor[1]+node_coor[4]+node_coor[7];
							ys=ys/3;
							ys=ys*mn;
							zxs=round(xs);
							zys=round(ys);
							for(i=0;i<90000;i++)
							{
								zxa=round(xa[i]*mn);
								zya=round(ya[i]*mn);
								if((zxa==zxs)&&(zya==zys))
								{
									//printf("xa[%d]=%lf ya[%d]=%lf\n",zxa,xa[i],zya,ya[i]);
									if(tmpfer[i]==1)
										mmr_el_set_mate(mesh_id,nel,1);
									else
										mmr_el_set_mate(mesh_id,nel,2);
								}
							}

						}

						for(i=0;i<90000;i++)
						{
							p[0]=xa[i];
							p[1]=ya[i];
							p[2]=0;
							spr=pointintriangle(p,&node_coor[0],&node_coor[3],&node_coor[6]);
							if(spr==1)
							{
								elemgr[nel]=nrgrain[i];
							}
						}
						/*
						if(k==0)
							min=mmr_el_hsize(mesh_id,nel,NULL,NULL,NULL);
						hsize=mmr_el_hsize(mesh_id,nel,NULL,NULL,NULL);
						if(hsize<min)
							min=hsize;
						//printf("Element %d nalezy do ziarna %d\n",nel,elemgr[nel]);
						k++;
						/**/
				}
				/*
				printf("Min=%lf\n",min);
				getchar();
				/**/

				//solve
				sprintf(arg,"%s/solver.dat", work_dir);
				if(licznik==0)
				{
					 //t_wall2=time_clock();
					 sir_solve_lin_sys(problem_id, SIC_SEQUENTIAL, arg, -2, -2, -2, 2);
					 //t_wall2=time_clock()-t_wall2;
					 if(pdv_problem.gr.err==1)
					 {
						 pdr_get_zzhu_error(field_id);
					 	 pdr_free_list();
					 }
					 if(pdv_problem.gr.save==2||(pdv_problem.gr.save%10)==2||(pdv_problem.gr.save/10)==2)
					 {
						 sprintf(arg3, "%s/all_merged_grains_%d.vtu", work_dir,licznik);
						 utr_write_paraview_std_lin(mesh_id, field_id, arg3);
						 //mergeParaviewMeshAndField(mesh_id, field_id, arg3);
					 }
					 if(pdv_problem.gr.save==3||(pdv_problem.gr.save%10)==3||(pdv_problem.gr.save/10)==3)
					 {
						 sprintf(arg3, "%s/grains_%d.mesh", work_dir,licznik);
						 writeGrains(mesh_id,field_id,elemgr,licznik,arg3);
					 }

				}
				if(j!=liczfer&&licznik!=0)
				{
					 //t_wall2=time_clock();
					printf("wejście=%d\n",licznik);
					 sir_solve_lin_sys(problem_id, SIC_SEQUENTIAL, arg, -2, -2, -2, 2);
					 printf("wyjście!!\n");

					 //t_wall2=time_clock()-t_wall2;

					 if(pdv_problem.gr.err==1)
					 {
						 pdr_get_zzhu_error(field_id);
						 pdr_free_list();
					 }
					 if(pdv_problem.gr.save==2||(pdv_problem.gr.save%10)==2||(pdv_problem.gr.save/10)==2)
					 {
						 sprintf(arg3, "%s/all_merged_grains_%d.vtu", work_dir,licznik);
						 utr_write_paraview_std_lin(mesh_id, field_id, arg3);
						 //mergeParaviewMeshAndField(mesh_id, field_id, arg3);
					 }
					 if(pdv_problem.gr.save==3||(pdv_problem.gr.save%10)==3||(pdv_problem.gr.save/10)==3)
					 {
						 sprintf(arg3, "%s/grains_%d.mesh", work_dir,licznik);
						 writeGrains(mesh_id,field_id,elemgr,licznik,arg3);
					 }
					 if(pdv_problem.gr.adpt==1)
					 {
						 //t_wall=t_wall3=time_clock();
						 	 pdr_adapt(field_id);
						 //t_wall=time_clock()-t_wall;
						 pdr_free_list();
						 while(pdr_constr(field_id)!=5);
						 //t_wall3=time_clock()-t_wall3;
					 }
					 if(pdv_problem.gr.adpt==2)
					 {
						 pdr_adapt2(field_id);
						 pdr_free_list();

						 /*Adaptacja wielokrotna*/
						 /*
						 int nrbef=mmr_get_nr_node(mesh_id);
						 //printf("nrbef=%d\n",nrbef);
						 int nrafter=0;
						 while(nrafter!=nrbef)
						 {
							 nrbef=mmr_get_nr_node(mesh_id);
							 pdr_adapt2(field_id);
							 pdr_free_list();
							 sir_solve_lin_sys(problem_id, SIC_SEQUENTIAL, arg);
							 nrafter=mmr_get_nr_node(mesh_id);
							 //printf("nrafter=%d\n",nrafter);
							 //getchar();
						 }
						 /**/
						 //if(licznik==670)
						 while(pdr_constr(field_id)!=5);
					 }
					 if(pdv_problem.gr.adpt==3)
					 {
						 pdr_adapt2(field_id);
						 pdr_free_list();
						 sir_solve_lin_sys(problem_id, SIC_SEQUENTIAL, arg, -2, -2, -2, 2);
						 pdr_adapt(field_id);
						 pdr_free_list();
						 while(pdr_constr(field_id)!=5);
				 }
				}
				else if(j==liczfer)
				{
					 t_wall2=time_clock();
					 printf("wejście2=%d\n",licznik);
					 sir_solve_lin_sys(problem_id, SIC_SEQUENTIAL, arg, -2, -2, -2, 2);
					 t_wall2=time_clock()-t_wall2;

					 if(pdv_problem.gr.err==1)
					 {
						 pdr_get_zzhu_error(field_id);
					 	 pdr_free_list();
					 }
					 if(pdv_problem.gr.save==2||(pdv_problem.gr.save%10)==2||(pdv_problem.gr.save/10)==2)
					 {
						 sprintf(arg3, "%s/all_merged_grains_%d.vtu", work_dir,licznik);
						 utr_write_paraview_std_lin(mesh_id, field_id, arg3);
						 //mergeParaviewMeshAndField(mesh_id, field_id, arg3);
					 }
					 if(pdv_problem.gr.save==3||(pdv_problem.gr.save%10)==3||(pdv_problem.gr.save/10)==3)
					 {
						 sprintf(arg3, "%s/grains_%d.mesh", work_dir,licznik);
						 writeGrains(mesh_id,field_id,elemgr,licznik,arg3);
					 }
				}

				j=liczfer;
				//printf("\nCzas adaptacji: %lf\nCzas adaptacji z usuwaniem constr: %lf\nCzas obliczen: %lf\n",t_wall,t_wall3,t_wall2);
		}//licznik

    }
    else if(c=='d'){

      sprintf(arg,"%s/mesh_prism.dmp", work_dir);
      iaux = mmr_export_mesh(mesh_id, MMC_MOD_FEM_MESH_DATA, arg);
      if(iaux<0) {
	/* if error in writing data */
      fprintf(interactive_output,"Error in writing mesh data!\n");
      }
      sprintf(arg,"%s/field_std_lin.dmp", work_dir);
      iaux = apr_write_field(field_id, nreq,-1,0.000000001, arg);
      if(iaux<0) {
	/* if error in writing data */
      fprintf(interactive_output,"Error in writing field data!\n");
      }

      if(pdv_problem.inv==1)  //write stress invariants
    	  pdr_write_sigma(field_id);

      sprintf(arg3, "%s/paraview_%d.vtu", work_dir,indeks);
      utr_write_paraview_std_lin(mesh_id, field_id, arg3);

         //constraints remove
         //while(pdr_constr(field_id)!=5);

         sprintf(arg3, "%s/grains3D_%d.mesh", work_dir,indeks);
         writeGrains3Dconstr(mesh_id,field_id,elemgr,licznik,arg3);

    }
    else if(c=='s'){
      indeks++;
      printf("Start\n");
      if(pdv_problem.gr.en==1)
      {
    	  if(indeks==1)
    	  {
    		  printf("Wczytuję!\n");
			  //grains = fopen("data_3D.txt","r");
    		  grains = fopen("data_3D_small.txt","r");
			  if(grains==NULL)
			  {
				 printf("Unable to open grains file - make sure that you have 'data_3D' file!\n");
				 exit(-1);
			  }
			  //utr_skip_rest_of_line(grains);
			  mn=pdv_problem.gr.multip;

			  liczfer=0;
			  //for(i=0;i<1000000;i++)
			  for(i=0;i<125000;i++)
			  {
				  fscanf(grains,"%lf %lf %lf %d\n",&tmpx,&tmpy,&tmpz,&tmpfer[i]);
//				  if((tmpfer[i]==3))
//				  {
//					 x[liczfer]=tmpx;
//					 y[liczfer]=tmpy;
//					 z[liczfer]=tmpz;
//					 liczfer++;
//				  }
				  xa[i]=tmpx;
				  ya[i]=tmpy;
				  za[i]=tmpz;
				  //if(i==0)
					//  printf("Wczytane: xa[i]=%lf,ya[i]=%lf,za[i]=%lf",xa[i],ya[i],za[i]);
			  }
			  //printf("Ferrite grains - %d\n",liczfer);

			  fclose(grains);
    	  }

    	  elemgr=(int *)calloc(mmr_get_max_elem_id(mesh_id)+1,sizeof(int));

    	  printf("Setting material data\n");
    	  printf("\n");
    	  nel=0;
    	  nmaxel=mmr_get_max_elem_id(mesh_id);

//    	  while((nel=mmr_get_next_act_elem(mesh_id, nel))!=0)
//    	  {
//    		  mmr_el_set_mate(mesh_id,nel,-1);
//    		  //printf("%d\r",nel/nmaxel*100);
//    		  spr=0;
//    		  spr2=0;
//    		  mmr_el_node_coor(mesh_id,nel,el_nodes,node_coor);
////    		  for(i=0;i<18;i++)
////    		  {
////    			  printf("Node %d - %lf\n",i/3,node_coor[i]);
////    		  }
////    		  printf("\n");
//
//    		  flag=0;
//    	  	  for(i=0;i<liczfer;i++)
//    	  	  {
//    	  		 p[0]=x[i];
//    	  		 p[1]=y[i];
//    	  		 p[2]=0;
//    	  		 spr=pointintriangle(p,&node_coor[0],&node_coor[3],&node_coor[6]);
//    	  		 if(spr==1)
//    	  		   if(z[i]>node_coor[2] && z[i]<node_coor[11])
//   	  		       {
//   	  			      //spr2=1;
//   	  			      //printf("z[i]=%lf,node_coor[2,11]=%lf,%lf!\n",z[i],node_coor[2],node_coor[11]);
//   	  		          flag=1;
//    	  			  //printf("Punkt %lf,%lf,%lf należy do elementu %d o wspolrzednych trojkat: a=%lf,%lf,b=%lf,%lf,c=%lf,%lf ,czworokat - %lf,%lf\n",x[i],y[i],z[i],nel,node_coor[0],node_coor[1],node_coor[3],node_coor[4],node_coor[6],node_coor[7],node_coor[2],node_coor[11]);
//    	  		   }
//    	  	  }
//    	  	  if(flag==0)
//    	  	  {
//    	  		 spr=0;
//    	  		 spr2=0;
//    	  		 //for(i=0;i<1000000;i++)
//    	  		 for(i=0;i<125000;i++)
//    	  		 {
//    	  			p[0]=xa[i];
//    	  			p[1]=ya[i];
//    	  			p[2]=0;
//    	  			spr+=pointintriangle(p,&node_coor[0],&node_coor[3],&node_coor[6]);
//    	  		 }
//    	  		 if(spr==0)
//    	  		 {
//    	  			flag=3;
//    	  			//printf("flag=3 dla elementu %d o wspolrzednych - trojkat: (%lf %lf %lf),(%lf %lf %lf),(%lf %lf %lf),czworokat - %lf,%lf\n",nel,node_coor[0],node_coor[1],node_coor[2],node_coor[3],node_coor[4],node_coor[5],node_coor[6],node_coor[7],node_coor[8],node_coor[2],node_coor[11]);
//    	  		 }
//    	  	  }
//
//    	  	  if(flag==1)
//    	  	  {
//    	  		 mmr_el_set_mate(mesh_id,nel,2);
//    	  		 elemgr[nel]=2;
//    	  	  }
//    	  	  else if(flag==0)
//    	  	  {
//    	  		 mmr_el_set_mate(mesh_id,nel,1);
//    	  		 elemgr[nel]=1;
//    	  	  }
//    	  	  else
//    	  	  {
//    	  		mmr_el_set_mate(mesh_id,nel,3);
//    	  		//elemgr[nel]=1;
////    	  		 xs=node_coor[0]+node_coor[3]+node_coor[6];
////    	  		 xs=xs/3;
////    	  		 xs=xs*mn;
////    	  		 ys=node_coor[1]+node_coor[4]+node_coor[7];
////    	  		 ys=ys/3;
////    	  		 ys=ys*mn;
////    	  		 zs=node_coor[2]+node_coor[11];
////    	  		 zs=zs/2;
////    	  		 zs=zs*mn;
////    	  		 zxs=round(xs);
////    	  		 zys=round(ys);
////    	  		 zzs=round(zs);
////    	  		 //for(i=0;i<1000000;i++)
////    	  		 //if(nel==69324)
////    	  		 //{
////    	  		//	 printf("xs=%lf,ys=%lf,zs=%lf,zxs=%d,zys=%d,zzs=%d\n",xs,ys,zs,zxs,zys,zzs);
////    	  		 //}
////
////    	  		 for(i=0;i<125000;i++)
////    	  		 {
////    	  			zxa=round(xa[i]*mn);
////    	  			zya=round(ya[i]*mn);
////    	  			zza=round(za[i]*mn);
////    	  			//if(nel==69324&&i==0)
////    	  			//	printf("xa[i]=%lf,ya[i]=%lf,za[i]=%lf,zxa=%d zya=%d zza=%d\n",xa[i],ya[i],za[i],zxa,zya,zza);
////    	  			if((zxa==zxs)&&(zya==zys)&&(zza==zzs))
////    	  			{
////    	  			   //printf("xa[%d]=%lf ya[%d]=%lf za[%d]=%lf\n",zxa,xa[i],zya,ya[i],zza,za[i]);
////    	  			   if(tmpfer[i]==2)
////    	  			   {
////    	  				   mmr_el_set_mate(mesh_id,nel,1);
////    	  				   elemgr[nel]=1;
////    	  			   }
////    	  			   else
////    	  			   {
////    	  				   mmr_el_set_mate(mesh_id,nel,2);
////    	  				   elemgr[nel]=2;
////    	  			   }
////    	  			   //printf("OK!\n");
////    	  			}
////    	  			/*else
////    	  			{
////
////    	  				printf("błąd? %d\n",nel);
////    	  				printf("xa[%d]=%lf ya[%d]=%lf za[%d]=%lf\n",zxa,xa[i],zya,ya[i],zza,za[i]);
////    	  			  for(i=0;i<18;i++)
////    	      		  {
////    	      			  printf("Node %d - %lf\n",i/3,node_coor[i]);
////    	  	   		  }
////    	  	   		  printf("\n");
////    	  				exit(1);
////    	  				mmr_el_set_mate(mesh_id,nel,1);
////    	  			}*/
////    	  		 }
//    	  	  }
//
//    	  						/*
//    	  						if(k==0)
//    	  							min=mmr_el_hsize(mesh_id,nel,NULL,NULL,NULL);
//    	  						hsize=mmr_el_hsize(mesh_id,nel,NULL,NULL,NULL);
//    	  						if(hsize<min)
//    	  							min=hsize;
//    	  						//printf("Element %d nalezy do ziarna %d\n",nel,elemgr[nel]);
//    	  						k++;
//    	  						/**/
////    	  	  if(mmr_el_mate(mesh_id,nel)!=2 && mmr_el_mate(mesh_id,nel)!=1)
////    	  	  {
////    	  	      printf("Element %d - hsize: %lf ma material %d\n",nel,mmr_el_hsize(mesh_id,nel,NULL,NULL,NULL),mmr_el_mate(mesh_id,nel));
////    	  	      for(i=0;i<6;i++)
////    	  	    	  printf("Node %d - %lf,%lf,%lf\n",el_nodes[i+1],node_coor[3*i],node_coor[3*i+1],node_coor[3*i+2]);
////
////    	  	      //exit(-1);
////    	  	  }
//    	  } //loop over elements


    	  double odleglosc,srodek[3];
    	  nel=0;

    	  while((nel=mmr_get_next_act_elem(mesh_id, nel))!=0)
    	  {
    		  mmr_el_set_mate(mesh_id,nel,-1);
    		  //if(mmr_el_mate(mesh_id,nel)==3)
    		  //{
				  mmr_el_node_coor(mesh_id,nel,el_nodes,node_coor);
				  srodek[0]=node_coor[0]+node_coor[3]+node_coor[6];
				  srodek[0]=srodek[0]/3;
				  srodek[1]=node_coor[1]+node_coor[4]+node_coor[7];
				  srodek[1]=srodek[1]/3;
				  srodek[2]=node_coor[2]+node_coor[11];
				  srodek[2]/=2;
//				  printf("Srodek pryzmy o wspolrzednych:\t");
//				  for(i=0;i<18;i++)
//				  {
//					  printf("%lf ",node_coor[i]);
//					  if(i!=0&&(i+1)%3==0)
//						  printf("\n");
//				  }
//				  printf("\n");
//				  printf("%lf,%lf,%lf\n",srodek[0],srodek[1],srodek[2]);
				  double min=0;
				  int wsp=0;
    			  for(i=0;i<125000;i++)
    			  {
    				  //sortowanie babaelkowe??Minimum??
    				  odleglosc=sqrt(pow(xa[i]-srodek[0],2)+pow(ya[i]-srodek[1],2)+pow(za[i]-srodek[2],2));
    				  if(i==0)
    				  {
    					  min=odleglosc;
    					  wsp=i;
    				  }
    				  else
    				  {
    					  if(odleglosc<min)
    					  {
    						  min=odleglosc;
    						  wsp=i;
    					  }
    				  }
    			  }
    			  //printf("Minimalna odleglosc = %lf dla punktu %d o wsp: %lf,%lf,%lf - ferryt=%d\n",min,wsp,xa[wsp],ya[wsp],za[wsp],tmpfer[wsp]);
    			  mmr_el_set_mate(mesh_id,nel,tmpfer[wsp]-1);
    			  elemgr[nel]=tmpfer[wsp]-1;
    		  //}
    		  if(mmr_el_mate(mesh_id,nel)!=2 && mmr_el_mate(mesh_id,nel)!=1)
			  {
				  printf("Element %d - hsize: %lf ma material %d\n",nel,mmr_el_hsize(mesh_id,nel,NULL,NULL,NULL),mmr_el_mate(mesh_id,nel));
				  for(i=0;i<6;i++)
					  printf("Node %d - %lf,%lf,%lf\n",el_nodes[i+1],node_coor[3*i],node_coor[3*i+1],node_coor[3*i+2]);

				  exit(-1);
			  }
    	  }




//    	  int gen=0;
//    	  int anc=0;
//    	  int mat=0;
//    	  int weight1=0;
//    	  int weight2=0;
//    	  int weight3=0;
//    	  int neig[6];
//    	  nel=0;
//    	  while((nel=mmr_get_next_act_elem(mesh_id, nel))!=0)
//    	  {
//    		 if(mmr_el_mate(mesh_id,nel)==3)
//    		 {
//    			 printf("Element %d!! - ",nel);
//    			 gen=mmr_el_gen(mesh_id,nel);
//    			 printf("Generation level: %d - ",gen);
//    			 anc=mmr_el_ancestor(mesh_id,nel,gen-1);
//    			 mat=mmr_el_mate(mesh_id,anc);
//    			 printf("Przodek %d ma material %d \n",anc,mat);
//    			 mmr_el_set_mate(mesh_id,nel,mat);
    			 //mmr_el_set_mate(mesh_id,nel,1);
//    			 mmr_el_eq_neig(mesh_id,nel,neig,NULL);
//    			 for (i=1;i<6;i++)
//    			 {
//    				 printf("Sąsiad %d -%d",i,neig[i]);
//    				 if(neig[i]>0)
//    				 {
//    					 mat = mmr_el_mate(mesh_id,neig[i]);
//    					 printf(" - material %d",mat);
//    					 if(mat==1)
//    						 weight1++;
//    					 else if(mat==2)
//    						 weight2++;
//    					 else
//    						 weight3++;
//
//    				 }
//    				 printf("\n");
//    			 }
//    			 if(weight1>weight2)
//    			 {
//    				 mmr_el_set_mate(mesh_id,nel,1);
//    				 elemgr[nel]=1;
//    			 }
//    			 else if(weight2>=weight1)
//    			 {
//    		    	 mmr_el_set_mate(mesh_id,nel,2);
//    		    	 elemgr[nel]=2;
//    		     }
    			 //else if(weight3>=weight1 && weight3>=weight2)
    			 //{
    			//	 gen=mmr_el_gen(mesh_id,nel);
    			//	 anc=mmr_el_ancestor(mesh_id,nel,gen-1);
    			//	 mat=mmr_el_mate(mesh_id,anc);
    			//	 mmr_el_set_mate(mesh_id,nel,mat);
    			 //  	 elemgr[nel]=mat;
    			 //}

//    	  }


      }//end grains 3d
      //printf("\n");
#ifdef PARALLEL
    if(pcr_my_proc_id()==pcr_print_master()){
#endif
      fprintf(interactive_output,
	"\nBeginning solution of the test problem - Laplace's equation\n\n");
#ifdef PARALLEL
    }
#endif
      /* very simple time measurement */
      t_wall=time_clock();

#ifdef PARALLEL
      {
	int ione = 1;
/* initiate exchange tables for DOFs - for one field = one solver, one level */
	appr_init_exchange_tables(pdv_nr_proc, pcr_my_proc_id(), 1, &ione);
      }
#endif

      /* problem dependent interface with linear solvers */
      sprintf(arg,"%s/solver.dat", work_dir);
#ifndef PARALLEL
      sir_solve_lin_sys(problem_id, SIC_SEQUENTIAL, arg,-2,-2,-2,2);
#endif

#ifdef PARALLEL
      sir_solve_lin_sys(problem_id, SIC_PARALLEL, arg);
/* free exchange tables for DOFs - for one field = one solver */
      appr_free_exchange_tables(1);
#endif

      /* very simple time measurement */
      t_wall=time_clock()-t_wall;

#ifdef PARALLEL
    if(pcr_my_proc_id()==pcr_print_master()){
#endif
      fprintf(interactive_output,
	      "\nTime for solving a system of linear equations %lf\n\n",t_wall);

#ifdef PARALLEL
    }
#endif
    }//end of solve
    else if(c=='a'){
    	//pdr_adapt(field_id);
    	//pdr_create_patch(field_id);
    	//utr_adapt(problem_id,work_dir,interactive_input,interactive_output);
    	//pdr_free_list();
    	pdr_adapt(field_id);
    	//t_wall=time_clock()-t_wall;
    	pdr_free_list();

    }
    else if(c=='e'){

    	fprintf(interactive_output,"\n\nZienkiewicz-Zhu error estimator:\n\n");
    	pdr_get_zzhu_error(field_id);
    	//pdr_get_sigma(field_id);
    	//pdr_compute_eyoung(field_id);
    }
    else if(c=='p'){
//postprocessing
      pdr_post_process(field_id,interactive_output);

    }
    else if(c=='m'){

      utr_manual_refinement( problem_id, work_dir, 
			     interactive_input, interactive_output );

    }
    else if(c=='r'){

      utr_test_refinements(problem_id, work_dir, 
			   interactive_input, interactive_output );

    } /* end test of mesh refinements */

  } while(c != 'q');

  /* free allocated space */
  apr_free_field(field_id);
  mmr_free_mesh(mesh_id);

  fclose(interactive_input);
  fclose(interactive_output);

#ifdef PARALLEL
  pcr_exit_parallel();
#endif

  return(0);

}