Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Visualizing system from position data

  1. Aug 6, 2011 #1
    Hi,

    I have a bunch of text documents, each of which contains (among other information) the x- and y-positions of 1,000 different 2-D disks at some given time. I want to write a code in C which takes each text document and outputs a visualization of the system at that instant in time.

    The data in the text documents is arranged like this:

    [time] [particle diameter] [x-position] [y-position] [x-velocity] [y-velocity] [etc...]

    If I could only extract and visualize the position and diameter information, I would have a "snapshot" of the system at a given instant. (I will eventually string together the snapshots to see the motion of the system.)

    What would be the best way of going about this from a programming perspective? I've written .ppm files using C code before to visualize the Mandelbrot set... Would that be good? How can I extract and draw the relevant information (position and diameter)? Sample code is appreciated.

    Thanks.
     
  2. jcsd
  3. Aug 6, 2011 #2
    I also have Mathematica -- not sure how useful it might be in this case...
     
  4. Aug 6, 2011 #3
    You can write a C program to open the text file and parse each line, extracting the values of each parameter. It's not overly complex, depending how much C you know.

    There are different approaches, but you could use the string tokenizer to recognise the space delimiter between each parameter on a line, or you could parse the line one character a time until you find the delimiter, in each case you will be left with a string that represents one parameter, which you can save as a string, or convert to a number, or whatever is appropriate.

    It's good practise terminating your own strings, checking validity, converting between types, allocating memory correctly and whatnot. :)

    Once you have the data extracted, you're free to use it how you wish, generate your own visualizations, etc.

    I have a string processing assignment somewhere, I'll see if I can dig it out and show you some code but not today I'm out of time.
     
  5. Aug 7, 2011 #4
    I understand that conceptually, but I would be interested in looking at that parsing assignment -- my C skills are subpar. Thanks.
     
  6. Aug 7, 2011 #5
    The "space delimiter" method sounds nice. I have no idea what a string tokenizer is, and I'm not sure how to save something as a string, or what that means.
     
  7. Aug 7, 2011 #6
    Perhaps you can adapt this.

    Given the file fqq.txt in my Mathematica folder containing

    1 2 3 4 5 6 7 8 9
    2 1 4 3 5 7 6 8 4
    3 3 4 1 5 6 7 8 2

    This should read and extract your desired data.

    In[1]:= s=OpenRead["fqq.txt"];
    data=Reap[
    While[(w=Read[s,Record])=!=EndOfFile,
    Sow[ToExpression[StringJoin["{", StringReplace[w, " " -> ","], "}"]]];
    ]
    ][[2,1]];
    Close;
    Print[data];
    diaxy=Map[Take[#,{2,4}]&,data];
    Print[diaxy];

    From In[1]:= {{1,2,3,4,5,6,7,8,9},{2,1,4,3,5,7,6,8,4},{3,3,4,1,5,6,7,8,2}}

    From In[1]:= {{2,3,4},{1,4,3},{3,4,1}}

    Go through that a character at a time using the help system to understand each function until you know how that is working.
     
    Last edited: Aug 7, 2011
  8. Aug 8, 2011 #7
    Wow, that is amazing. I'll look through it (with a Mathematica 8 documentation tab open) and try to understand everything. Very much appreciated.
     
  9. Aug 8, 2011 #8
    OK for starters, a string is a sequence of characters. As opposed to a number which is treated differently. If you do this in C, you need a way to take those characters from your input file and at least in some cases, convert them to a number.

    Now for string tokenizing. strtok() is a function that takes as input a string and a delimiter(s) and essentially splits the big string into small strings. It's not super-intuitive to use, so here's an example:

    Code (Text):
    /* strtok example */
    #include <stdio.h>
    #include <string.h>

    int main ()
    {
      char str[] ="- This, a sample string.";
      char * pch;
      printf ("Splitting string \"%s\" into tokens:\n",str);
      pch = strtok (str," ,.-");
      while (pch != NULL)
      {
        printf ("%s\n",pch);
        pch = strtok (NULL, " ,.-");
      }
      return 0;
    }

    [URL]http://www.cplusplus.com/reference/clibrary/cstring/strtok/
    [/URL]

    As you can see, str is your starting string, pch is a pointer to char which will contain a reference to the current small string. The first call to strtok() splits str (it actually changes str and replaces the delimiter with '\0' (NULL char). Successive calls to strtok() (in a loop) do not supply the starting string str, and instead use NULL, and pch will be assigned the reference to the next sub-string until it hits the end.

    It's worth reading the man page, I fear my explanation is flawed. Here's an excerpt:

    Another option is to look at each character individually and parse the line manually. Here's some code:

    Code (Text):

    FILE *input_file; // a pointer to your input file

    int input_char; // the current char (fgetc returns an int, not a char, but this is not really important)
    char buffer[20]; // a place to store the substring
    int index = 0; // current place in the buffer
    char delimiter = ' '; // space character

    input_file = fopen("myfile.txt", "r"); // open the file for reading
    if(input_file == NULL)
        exit(1); // file open failed.

    while((input_char = fgetc(input_file)) != EOF) // while we are not at EndOfFile
    {
        if(input_char != delimiter) // if the char is not the delimiter
        {
            buffer[index] = input_char; // store it in the buffer
            index++; // increment the index
        }
        else
        {
            buffer[index] = '\0'; // manually terminate the substring!

            // lets assume you want the first substring time stored as an integer

            int time = atoi(buffer); // atoi() converts a string to an int.
        }
    }

    fclose(input_file); // close the file.
    This is an incomplete piece of code, you need more logic to know where you're up to in the string, to treat each piece of data appropriately, clear the buffer, etc. but I hope it illustrates the point.
     
    Last edited by a moderator: Apr 26, 2017
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook