Is there a way to print a formatted table from an array in chapel?

Viewed 82

I am working on a heat transfer problem in chapel, and while I understand that I'm not going to be able to get a nice GUI I can work with, I would like to have something I can print that allows me to see at least the end result of running my code. So far what I have is hard to read because longer numbers will push out the whole row. I was looking at using formattedIO, but the documentation was confusing to me and seemed to focus more on one line formatting rather than anything table related. What I'm hoping for is some way to ensure that all the columns line up. Here is my code if it is helpful. Thanks in advance!

use Math;
use Time;

var rows = 10;
var cols = 10;

var times = 4;

var x = 5;
var y = 5;

var temps: [0..rows+1, 0..cols+1] real = 0;
var past: [0..rows+1, 0..cols+1] real;

temps[x,y] = 100;
past[x,y] = 100;

var t: Timer;
t.start();

for t in 1..times do {
  forall i in 1..rows do {
    for j in 1..cols do {
      temps[i,j] = floor((past[i-1,j]+past[i+1,j]+past[i,j-1]+past[i,j+1])/4);
      //floor was used to cut off extra decimals in an attempt to make the display better
    }
  }
  past = temps;
}

t.stop();
writeln(t.elapsed());
writeln(temps);

Usually the grid is a lot larger (1000 x 1000), but I made it smaller so I could see the values. I would really like to find a way to make it so that a larger grid could be printed in a way that is not awful. Would it maybe be necessary to use file output for larger grid sizes?

1 Answers

It would be great to have a library for printing formatted tables, but I don't know of one that is available yet.

For now you could compute the width of each table cell and then use format strings to specify padding to the maximum width. Here is an example:

use IO;

var data:[1..5, 1..5] real;

// Compute some numbers that need formatting help
for (ij,elt) in zip(data.domain, data) {
  var (i, j) = ij;
  if i == j {
    elt = 0.0;
  } else {
    var d = i-j;
    elt = d*d*d*17.20681 - j*0.1257201;
  }
}

// %r tries to use e.g. 12.25 and exponential for big/small
// here "r" is for "real"
// %dr ("decimal real") means always like 12.25
// %er ("exponential real") is always like 1.2e27
// and %{####.####} is available to show the pattern you want
// but will never output in exponential

// Note that you can specify padding:
//  %17r would be padded on the left with spaces to 17 columns
//  %017r would be padded on the left with zeros to 17 columns
//  %-17r would be padded on the right with spaces to 17 columns
// You can also use %*r to specify the padding in an argument

// Let's compute the maximum number of characters for each element
// This uses the string.format function
//  https://chapel-lang.org/docs/master/modules/standard/IO/FormattedIO.html#FormattedIO.string.format
// as well as promotion.

// compute the maximum size of any field
// (this could be adapted to be per-column, say)
var width = 0;
for i in data.domain.dim(0) {
  for j in data.domain.dim(1) {
    var mywidth = "%r".format(data[i,j]).size;
    if width < mywidth then
      width = mywidth;
  }
}
 
// Now format all of the elements with that size
for i in data.domain.dim(0) {
  var first = true;
  for j in data.domain.dim(1) {
    if first==false then
      write(" ");
    writef("%*r", width, data[i,j]);
    first = false;
  }
  writeln();
}
Related