CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

read a .dat file with fseek and fscanf problem

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 1 Post By federicabi
  • 1 Post By federicabi

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   September 23, 2016, 02:46
Question read a .dat file with fseek and fscanf problem
  #1
Member
 
Federica Biano
Join Date: Feb 2016
Location: Genova, Italy
Posts: 39
Rep Power: 10
federicabi is on a distinguished road
Hello!
I need your precious help to solve my problem.
I've never programmed in C++ and I tried recently to modify a library in sixDoFRigidBodyMotion. When I compiled it no errors came out, but when I run the code results are completely wrong.
Let me explain: I need to read for each timestep the forces.dat file and in particular the last line of it and take only two force's components. I used the functions fseek to go to the end of file and return at the beginning of the last line, and the function fscanf to read the line and save the two force's components into two variables.
This is my code
Code:
FILE *forze;
      forze = fopen("postProcessing/forces/1/forces.dat", "r");
      float timestep;
      float fxp;
      float fyp;
      float fzp;
      float fxv;
      float fyv;
      float fzv;
      float fxpo;
      float fypo;
      float fzpo;
      float mxp;
      float myp;
      float mzp;
      float mxv;
      float myv;
      float mzv;
      float mxpo;
      float mypo;
      float mzpo;
      

      fseek(forze,-271,SEEK_END);
           
      fscanf(forze, "%f    ((%le %f %f) (%le %f %f) (%f %f %f)) ((%f %f %f) (%f %f %f) (%f %f %f))", &timestep, &fxp, &fyp, &fzp, &fxv, &fyv, &fzv, &fxpo, &fypo, &fzpo, &mxp, &myp, &mzp, &mxv, &myv, &mzv, &mxpo, &mypo, &mzpo);
      
      fclose(forze);
      
      forzaxPrevIter = fxp + fxv + fxpo;
      
      alfaPrevIter = (alfaini_ * 3.1415 / 180) + asin(motion.orientation()[3]);
      
      alfa = alfaPrevIter;
      forzax = forzaxPrevIter;
    }
    
    restraintPosition = motion.transform(refAttachmentPt_);

    vector v = motion.velocity(restraintPosition);

    restraintForce = vector((-1)*forzax,0,(-1)*forzax*tan(alfa));

    restraintMoment = vector::zero;
What returns when the code is running aren't the force's components but the pointer values (I think) because they don't change every timestep but they change if I run another simulation.

What can I do to solve it? I tried also to make a C++ program and it does what I need, but when I put the code into openfoam it doesn't anymore.

Any help would be very appreciated!

Federica
ordinary likes this.
federicabi is offline   Reply With Quote

Old   September 27, 2016, 10:56
Default solved
  #2
Member
 
Federica Biano
Join Date: Feb 2016
Location: Genova, Italy
Posts: 39
Rep Power: 10
federicabi is on a distinguished road
If you are interested in the correct code, there you are:
Code:
FILE *forze;
      forze = fopen("postProcessing/forces/1/forces.dat", "r");
      float timestep;
      float fxp;
      float fyp;
      float fzp;
      float fxv;
      float fyv;
      float fzv;
      float fxpo;
      float fypo;
      float fzpo;
      float mxp;
      float myp;
      float mzp;
      float mxv;
      float myv;
      float mzv;
      float mxpo;
      float mypo;
      float mzpo;
      char c;

      fseek(forze,-285,SEEK_END);
      
      while(c != '\n')
      {
    c=fgetc(forze);
      }
           
      fscanf(forze, "%f    ((%f %f %f) (%f %f %f) (%f %f %f)) ((%f %f %f) (%f %f %f) (%f %f %f))", &timestep, &fxp, &fyp, &fzp, &fxv, &fyv, &fzv, &fxpo, &fypo, &fzpo, &mxp, &myp, &mzp, &mxv, &myv, &mzv, &mxpo, &mypo, &mzpo);
      
      fclose(forze);
      
      forzaxPrevIter = fxp + fxv + fxpo;
      
      alfaPrevIter = (alfaini_ * 3.1415 / 180) + asin(motion.orientation()[3]);
      
      alfa = alfaPrevIter;
      forzax = forzaxPrevIter;
Regards
ordinary likes this.
federicabi is offline   Reply With Quote

Old   February 20, 2017, 14:35
Default
  #3
Member
 
Emre
Join Date: Nov 2015
Location: Izmir, Turkey
Posts: 97
Rep Power: 10
ordinary is on a distinguished road
Hello,

Did you solve the problem? It seems interesting.
ordinary is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 03:08.