Skip to content

Storing velocity data in csv file

OpenLB – Open Source Lattice Boltzmann Code Forums on OpenLB General Topics Storing velocity data in csv file

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #5765
    achodankar
    Participant

    Hello Developers,
    I followed the steps from “Printing velocity data” post to save the data. I have modified the code to save in csv data format. I get a segmentation fault(core dumped) error after running the code for two time steps. The csv file generated for the two time steps have zero values. I would really appreciate any feedback on this issue.

    Here is the snippet of it:

    {
    SuperLatticePhysVelocity2D<T,DESCRIPTOR> velocityF(sLattice, converter);
    BlockReduction2D2D<T> velocityPlane(velocityF, 600, BlockDataSyncMode::ReduceOnly);
    ofstream myfile1;
    myfile1.open(“velocity_”+ std::to_string(iT) + “.csv”);
    myfile1 << “i,” << “j,” << “Ux,” <<“Uy”<< std::endl;
    int latticeR[2] { };
    for (latticeR[0]=0; latticeR[0] < velocityPlane.getBlockStructure().getNx(); ++latticeR[0])
    {
    for (latticeR[1]=0; latticeR[1] < velocityPlane.getBlockStructure().getNy(); ++latticeR[0])
    {
    T vel[2] { };
    velocityPlane(vel, latticeR);
    myfile1 << latticeR[0] << “,” << latticeR[1] << “,”<< vel[0] << “,”<< vel[1] << std::endl;
    }
    }
    myfile1.close();

    }

    Thank you.

    Yours sincerely,

    Abhijeet

    #5767
    Adrian
    Keymaster

    The increment operation in the nested loop contains a typo, it should be ++latticeR[1].

    #5773
    achodankar
    Participant

    Hello Adrian,
    Thank you. This fixes the problem. I missed on that bug. The velocity values are showing up to be zero for all time steps in the csv files. Is the above way the right way to extract the velocity values?

    Thank you.

    Yours sincerely,

    Abhijeet

    #5774
    Adrian
    Keymaster

    When is the code snippet called? What are you simulating? Are the functor results correct? (Check using e.g. a debugger).

    #5775
    achodankar
    Participant

    I am running the dkt2d example. The simulation results are correct when I view it in Paraview and usin ppm images. here is the code snippet:

    void getResults(SuperLattice2D<T, DESCRIPTOR>& sLattice,
    UnitConverter<T,DESCRIPTOR> const& converter, int iT,
    SuperGeometry2D<T>& superGeometry, Timer<double>& timer, SmoothIndicatorF2D<T,T,true> &particle1, SmoothIndicatorF2D<T,T,true> &particle2)
    {
    OstreamManager clout(std::cout, “getResults”);

    #ifdef WriteVTK
    SuperVTMwriter2D<T> vtkWriter(“sedimentation”);
    SuperLatticePhysVelocity2D<T, DESCRIPTOR> velocity(sLattice, converter);
    SuperLatticePhysPressure2D<T, DESCRIPTOR> pressure(sLattice, converter);
    SuperLatticePhysExternalPorosity2D<T, DESCRIPTOR> externalPor(sLattice, converter);
    vtkWriter.addFunctor(velocity);
    vtkWriter.addFunctor(pressure);
    vtkWriter.addFunctor(externalPor);

    if (iT == 0) {
    converter.write(“dkt”);
    SuperLatticeGeometry2D<T, DESCRIPTOR> geometry(sLattice, superGeometry);
    SuperLatticeCuboid2D<T, DESCRIPTOR> cuboid(sLattice);
    SuperLatticeRank2D<T, DESCRIPTOR> rank(sLattice);
    vtkWriter.write(geometry);
    vtkWriter.write(cuboid);
    vtkWriter.write(rank);
    vtkWriter.createMasterFile();

    }

    if (iT % converter.getLatticeTime(iTwrite) == 0) {
    vtkWriter.write(iT);
    }

    // Writes the vtk files
    const int vtkIter = converter.getLatticeTime( .3 );
    if ( iT%vtkIter == 0 && iT > 0 ) {
    vtkWriter.write( iT );

    {
    SuperEuklidNorm2D<T, DESCRIPTOR> normVel( velocity );
    BlockReduction2D2D<T> planeReduction( normVel, 600, BlockDataSyncMode::ReduceOnly );
    // write output as JPEG
    heatmap::write(planeReduction, iT);

    }

    //plot a gif of the simulation
    {
    SuperEuklidNorm2D<T,DESCRIPTOR> normVel( velocity );
    BlockReduction2D2D<T> planeReduction( normVel );
    //BlockReduction2D2D<T> planeReduction(normVel, sLattice);
    BlockGifWriter<T> gifWriter;
    //gifWriter.write(planeReduction, 0, 0.7, iT, “vel”); //static scale
    gifWriter.write( planeReduction, iT, ” vel ” ); // scaled
    //SuperEuklidNorm2D<T,DESCRIPTOR> press( pressure );
    //BlockReduction2D2D<T> planeReduction( press );
    //gifWriter.write( planeReduction, iT, ” press ” ); // scaled
    }
    {

    BlockGifWriter<T> gifWriter;
    SuperEuklidNorm2D<T,DESCRIPTOR> press( pressure );
    BlockReduction2D2D<T> planeReduction( press );
    gifWriter.write( planeReduction, iT, ” press ” ); // scaled
    }

    {
    SuperLatticePhysVelocity2D<T,DESCRIPTOR> velocityF(sLattice, converter);
    BlockReduction2D2D<T> velocityPlane(velocityF, 600, BlockDataSyncMode::ReduceOnly);
    ofstream myfile1;
    myfile1.open(“velocity_”+ std::to_string(iT) + “.csv”);
    myfile1 << “i,” << “j,” << “Ux,” <<“Uy”<< std::endl;
    int latticeR[2] { };
    T vel[2] { };
    velocityPlane(vel, latticeR);
    for (latticeR[0]=0; latticeR[0] < velocityPlane.getBlockStructure().getNx(); ++latticeR[0])
    {
    for (latticeR[1]=0; latticeR[1] < velocityPlane.getBlockStructure().getNy(); ++latticeR[1])
    {
    //cout<<“Enter-“<<std::endl;
    myfile1 << latticeR[0] << “,” << latticeR[1] << “,”<< vel[0] << “,”<< vel[1] << std::endl;
    }
    }
    myfile1.close();

    }

    }
    #endif

    #ifdef WriteGnuPlot
    if (iT % converter.getLatticeTime(iTwrite) == 0) {
    if (singleton::mpi().getRank() == 0) {

    ofstream myfile;
    myfile.open (gnuplotFilename.c_str(), ios::app);
    myfile
    << converter.getPhysTime(iT) << ” ”
    << std::setprecision(9)
    << particle2.getPos()[1] << ” ”
    << particle1.getPos()[1] << ” ”
    << particle2.getPos()[0] << ” ”
    << particle1.getPos()[0] << endl;
    myfile.close();
    }
    }
    #endif

    /// Writes output on the console
    if (iT % converter.getLatticeTime(iTwrite) == 0) {
    timer.update(iT);
    timer.printStep();
    sLattice.getStatistics().print(iT, converter.getPhysTime(iT));
    }

    return;
    }

    #5776
    achodankar
    Participant

    The code snippet is called in the main function while timestepping:

    for (std::size_t iT = 0; iT < converter.getLatticeTime(maxPhysT)+10; ++iT) {
    particle.simulateTimestep(“verlet”);
    getResults(sLattice, converter, iT, superGeometry, timer, circle1, circle2);
    sLattice.collideAndStream();
    superExt1.communicate();
    superExt2.communicate();
    superExt3.communicate();

    }

    #5777
    achodankar
    Participant

    Hello Adrian,
    The problem is resolved. I am getting the correct velocity values stored in it. While trying to fix the earlier problem, I put these code lines (T vel[2] { };velocityPlane(vel, latticeR);”) before the for loop. After placing it back in the for loop, it’s working fine. Everything is working now. Thank you very much for your prompt response.

    Yours sincerely,

    Abhijeet

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.