Skip to content

Commit 4e25960

Browse files
committed
Added formatting changes for lines and rods when writing to the files to be like fortran outputs
1 parent 2d7091a commit 4e25960

File tree

3 files changed

+220
-181
lines changed

3 files changed

+220
-181
lines changed

source/Line.cpp

Lines changed: 91 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "Util/Interp.hpp"
3636
#include <tuple>
3737
// #include <random>
38+
#include <iomanip>
3839

3940
#ifdef USE_VTK
4041
#include "Util/VTK_Util.hpp"
@@ -46,6 +47,10 @@
4647
#include <vtkXMLPolyDataWriter.h>
4748
#endif
4849

50+
// Formating constants for line files outputs (iomanip)
51+
constexpr int WIDTH = 20; // Width for output
52+
constexpr int PRECISION = 7; // Precision for output
53+
4954
using namespace std;
5055

5156
namespace moordyn {
@@ -302,80 +307,87 @@ Line::initialize()
302307
// 1st line with the fields
303308

304309
// output time
305-
*outfile << "Time"
306-
<< "\t ";
310+
*outfile << setw(10) << right
311+
<< "Time";
307312

308313
// output positions
309314
if (channels.find("p") != string::npos) {
310315
for (unsigned int i = 0; i <= N; i++) {
311-
*outfile << "Node" << i << "px \t Node" << i << "py \t Node"
312-
<< i << "pz \t ";
316+
*outfile << setw(WIDTH) << right << ("Node" + to_string((int)i) + "px")
317+
<< setw(WIDTH) << right << ("Node" + to_string((int)i) + "py")
318+
<< setw(WIDTH) << right << ("Node" + to_string((int)i) + "pz");
313319
}
314320
}
315321
// output curvatures
316322
if (channels.find("K") != string::npos) {
317323
for (unsigned int i = 0; i <= N; i++) {
318-
*outfile << "Node" << i << "Ku \t ";
324+
*outfile << setw(WIDTH) << right << ("Node" + to_string((int)i) + "Ku");
319325
}
320326
}
321327
// output velocities
322328
if (channels.find("v") != string::npos) {
323329
for (unsigned int i = 0; i <= N; i++) {
324-
*outfile << "Node" << i << "vx \t Node" << i << "vy \t Node"
325-
<< i << "vz \t ";
330+
*outfile << setw(WIDTH) << right << ("Node" + to_string((int)i) + "vx")
331+
<< setw(WIDTH) << right << ("Node" + to_string((int)i) + "vy")
332+
<< setw(WIDTH) << right << ("Node" + to_string((int)i) + "vz");
326333
}
327334
}
328335
// output wave velocities
329336
if (channels.find("U") != string::npos) {
330337
for (unsigned int i = 0; i <= N; i++) {
331-
*outfile << "Node" << i << "Ux \t Node" << i << "Uy \t Node"
332-
<< i << "Uz \t ";
338+
*outfile << setw(WIDTH) << right << ("Node" + to_string((int)i) + "Ux")
339+
<< setw(WIDTH) << right << ("Node" + to_string((int)i) + "Uy")
340+
<< setw(WIDTH) << right << ("Node" + to_string((int)i) + "Uz");
333341
}
334342
}
335343
// output hydro force
336344
if (channels.find("D") != string::npos) {
337345
for (unsigned int i = 0; i <= N; i++) {
338-
*outfile << "Node" << i << "Dx \t Node" << i << "Dy \t Node"
339-
<< i << "Dz \t ";
346+
*outfile << setw(WIDTH) << right << ("Node" + to_string((int)i) + "Dx")
347+
<< setw(WIDTH) << right << ("Node" + to_string((int)i) + "Dy")
348+
<< setw(WIDTH) << right << ("Node" + to_string((int)i) + "Dz");
340349
}
341350
}
342351
// output VIV lift force
343352
if (channels.find("V") != string::npos) {
344353
for (unsigned int i = 0; i <= N; i++) {
345-
*outfile << "Node" << i << "Vx \t Node" << i << "Vy \t Node"
346-
<< i << "Vz \t ";
354+
*outfile << setw(WIDTH) << right << ("Node" + to_string((int)i) + "Vx")
355+
<< setw(WIDTH) << right << ("Node" + to_string((int)i) + "Vy")
356+
<< setw(WIDTH) << right << ("Node" + to_string((int)i) + "Vz");
347357
}
348358
}
349359
// output segment tensions
350360
if (channels.find("t") != string::npos) {
351361
for (unsigned int i = 1; i <= N; i++) {
352-
*outfile << "Seg" << i << "Te \t ";
362+
*outfile << setw(WIDTH) << right << ("Seg" + to_string((int)i) + "Te");
353363
}
354364
}
355365
// output internal damping force
356366
if (channels.find("c") != string::npos) {
357367
for (unsigned int i = 1; i <= N; i++) {
358-
*outfile << "Seg" << i << "cx \t Seg" << i << "cy \t Seg" << i
359-
<< "cz \t ";
368+
*outfile << setw(WIDTH) << right << ("Seg" + to_string((int)i) + "cx")
369+
<< setw(WIDTH) << right << ("Seg" + to_string((int)i) + "cy")
370+
<< setw(WIDTH) << right << ("Seg" + to_string((int)i) + "cz");
360371
}
361372
}
362373
// output segment strains
363374
if (channels.find("s") != string::npos) {
364375
for (unsigned int i = 1; i <= N; i++) {
365-
*outfile << "Seg" << i << "St \t ";
376+
*outfile << setw(WIDTH) << right << ("Seg" + to_string((int)i) + "St");
366377
}
367378
}
368379
// output segment strain rates
369380
if (channels.find("d") != string::npos) {
370381
for (unsigned int i = 1; i <= N; i++) {
371-
*outfile << "Seg" << i << "dSt \t ";
382+
*outfile << setw(WIDTH) << right << ("Seg" + to_string((int)i) + "dSt");
372383
}
373384
}
374385
// output seabed contact forces
375386
if (channels.find("b") != string::npos) {
376387
for (unsigned int i = 0; i <= N; i++) {
377-
*outfile << "Node" << i << "bx \t Node" << i << "by \t Node"
378-
<< i << "bz \t ";
388+
*outfile << setw(WIDTH) << right << ("Node" + to_string((int)i) + "bx")
389+
<< setw(WIDTH) << right << ("Node" + to_string((int)i) + "by")
390+
<< setw(WIDTH) << right << ("Node" + to_string((int)i) + "bz");
379391
}
380392
}
381393

@@ -385,64 +397,75 @@ Line::initialize()
385397
// 2nd line with the units
386398

387399
// output time
388-
*outfile << "(s)"
389-
<< "\t ";
400+
*outfile << setw(10) << right
401+
<< "(s)";
390402

391403
// output positions
392404
if (channels.find("p") != string::npos) {
393405
for (unsigned int i = 0; i <= 3 * N + 2; i++)
394-
*outfile << "(m) \t";
406+
*outfile << setw(WIDTH) << right
407+
<< "(m)";
395408
}
396409
// output curvatures?
397410
if (channels.find("K") != string::npos) {
398411
for (unsigned int i = 0; i <= N; i++) {
399-
*outfile << "(1/m) \t ";
412+
*outfile << setw(WIDTH) << right
413+
<< "(1/m)";
400414
}
401415
}
402416
// output velocities?
403417
if (channels.find("v") != string::npos) {
404418
for (unsigned int i = 0; i <= 3 * N + 2; i++)
405-
*outfile << "(m/s) \t";
419+
*outfile << setw(WIDTH) << right
420+
<< "(m/s)";
406421
}
407422
// output wave velocities?
408423
if (channels.find("U") != string::npos) {
409424
for (unsigned int i = 0; i <= 3 * N + 2; i++)
410-
*outfile << "(m/s) \t";
425+
*outfile << setw(WIDTH) << right
426+
<< "(m/s)";
411427
}
412428
// output hydro force
413429
if (channels.find("D") != string::npos) {
414430
for (unsigned int i = 0; i <= 3 * N + 2; i++)
415-
*outfile << "(N) \t";
431+
*outfile << setw(WIDTH) << right
432+
<< "(N)";
416433
}
417434
// output VIV force
418435
if (channels.find("V") != string::npos) {
419436
for (unsigned int i = 0; i <= 3 * N + 2; i++)
420-
*outfile << "(N) \t";
437+
*outfile << setw(WIDTH) << right
438+
<< "(N)";
421439
}
422440
// output segment tensions?
423441
if (channels.find("t") != string::npos) {
424442
for (unsigned int i = 0; i < N; i++)
425-
*outfile << "(N) \t";
443+
*outfile << setw(WIDTH) << right
444+
<< "(N)";
426445
}
427446
// output internal damping force?
428447
if (channels.find("c") != string::npos) {
429448
for (unsigned int i = 0; i < 3 * N; i++)
430-
*outfile << "(N) \t";
449+
*outfile << setw(WIDTH) << right
450+
<< "(N)";
431451
}
432452
// output segment strains?
433453
if (channels.find("s") != string::npos) {
434454
for (unsigned int i = 0; i < N; i++)
435-
*outfile << "(-) \t";
455+
*outfile << setw(WIDTH) << right
456+
<< "(-)";
436457
}
437458
// output segment strain rates?
438459
if (channels.find("d") != string::npos) {
439460
for (unsigned int i = 0; i < N; i++)
440-
*outfile << "(-/s) \t";
461+
*outfile << setw(WIDTH) << right
462+
<< "(-/s)";
441463
}
442464
// output seabed contact force?
443465
if (channels.find("b") != string::npos) {
444466
for (unsigned int i = 0; i <= 3 * N + 2; i++)
445-
*outfile << "(N) \t";
467+
*outfile << setw(WIDTH) << right
468+
<< "(N)";
446469
}
447470

448471
*outfile << "\n";
@@ -1554,66 +1577,73 @@ Line::Output(real time)
15541577
// Flags changed to just be one character (case sensitive) per output flag.
15551578
// To match FASTv8 version.
15561579

1580+
// Helper to format and write a single value
1581+
auto write_val = [&](real val) {
1582+
*outfile << std::setw(WIDTH)
1583+
<< std::right
1584+
<< std::scientific
1585+
<< std::setprecision(PRECISION)
1586+
<< val;
1587+
};
1588+
15571589
if (outfile) // if not a null pointer (indicating no output)
15581590
{
15591591
if (!outfile->is_open()) {
15601592
LOGWRN << "Unable to write to output file " << endl;
15611593
return;
15621594
}
1595+
// Loops through the nodes
1596+
auto write_vec_array = [&](const std::vector<vec>& arr) {
1597+
for (unsigned int i = 0; i <= N; i++)
1598+
for (unsigned int J = 0; J < 3; J++){
1599+
write_val(arr[i][J]);}
1600+
1601+
};
1602+
// Loops through the nodes for scalars
1603+
auto write_scalar_array = [&](const std::vector<real>& arr) {
1604+
for (unsigned int i = 0; i <= N; i++)
1605+
write_val(arr[i]);
1606+
};
15631607
// output time
1564-
*outfile << time << "\t ";
1608+
*outfile << setw(10) << right << fixed << setprecision(4)
1609+
<< time;
15651610

15661611
// output positions?
15671612
// if (find(channels.begin(), channels.end(), "position") !=
15681613
// channels.end())
15691614
if (channels.find("p") != string::npos) {
1570-
for (unsigned int i = 0; i <= N; i++) // loop through nodes
1571-
{
1572-
for (unsigned int J = 0; J < 3; J++)
1573-
*outfile << r[i][J] << "\t ";
1574-
}
1615+
write_vec_array(r); // position
15751616
}
1617+
15761618
// output curvatures?
15771619
if (channels.find("K") != string::npos) {
1578-
for (unsigned int i = 0; i <= N; i++) {
1579-
*outfile << Kurv[i] << "\t ";
1580-
}
1620+
write_scalar_array(Kurv);
15811621
}
15821622
// output velocities?
15831623
if (channels.find("v") != string::npos) {
1584-
for (unsigned int i = 0; i <= N; i++) {
1585-
for (int J = 0; J < 3; J++)
1586-
*outfile << rd[i][J] << "\t ";
1587-
}
1624+
write_vec_array(rd);
15881625
}
15891626
// output wave velocities?
15901627
if (channels.find("U") != string::npos) {
15911628
auto [_z, U, _ud, _pdyn] = waves->getWaveKinLine(lineId);
1592-
for (unsigned int i = 0; i <= N; i++) {
1593-
for (int J = 0; J < 3; J++)
1594-
*outfile << U[i][J] << "\t ";
1595-
}
1629+
write_vec_array(U);
15961630
}
15971631
// output hydro drag force?
15981632
if (channels.find("D") != string::npos) {
15991633
for (unsigned int i = 0; i <= N; i++) {
16001634
for (int J = 0; J < 3; J++)
1601-
*outfile << Dp[i][J] + Dq[i][J] + Ap[i][J] + Aq[i][J]
1602-
<< "\t ";
1635+
write_val(Dp[i][J] + Dq[i][J] + Ap[i][J] + Aq[i][J]);
16031636
}
16041637
}
16051638

16061639
// output VIV force (only CF for now)
16071640
if (channels.find("V") != string::npos) {
1608-
for (unsigned int i = 0; i <= N; i++) {
1609-
for (int J = 0; J < 3; J++)
1610-
*outfile << Lf[i][J] << "\t ";
1611-
}
1641+
write_vec_array(Lf);
16121642
}
16131643
// output segment tensions?
16141644
if (channels.find("t") != string::npos) {
16151645
for (unsigned int i = 0; i < N; i++) {
1616-
*outfile << T[i].norm() << "\t ";
1646+
write_val(T[i].norm());
16171647
// >>> preparation below for switching to outputs at nodes
16181648
// <<< note that tension of end nodes will need weight and
16191649
// buoyancy adjustment
@@ -1627,29 +1657,23 @@ Line::Output(real time)
16271657
}
16281658
// output internal damping force?
16291659
if (channels.find("c") != string::npos) {
1630-
for (unsigned int i = 0; i < N; i++) {
1631-
for (int J = 0; J < 3; J++)
1632-
*outfile << Td[i][J] << "\t ";
1633-
}
1660+
write_vec_array(Td); // internal damping force
16341661
}
16351662
// output segment strains?
16361663
if (channels.find("s") != string::npos) {
16371664
for (unsigned int i = 0; i < N; i++) {
1638-
*outfile << lstr[i] / l[i] - 1.0 << "\t ";
1665+
write_val(lstr[i] / l[i] - 1.0);
16391666
}
16401667
}
16411668
// output segment strain rates?
16421669
if (channels.find("d") != string::npos) {
16431670
for (unsigned int i = 0; i < N; i++) {
1644-
*outfile << ldstr[i] / l[i] << "\t ";
1671+
write_val(ldstr[i] / l[i]);
16451672
}
16461673
}
16471674
// output seabed contact forces?
16481675
if (channels.find("b") != string::npos) {
1649-
for (unsigned int i = 0; i <= N; i++) {
1650-
for (int J = 0; J < 3; J++)
1651-
*outfile << B[i][J] << "\t ";
1652-
}
1676+
write_vec_array(B);
16531677
}
16541678

16551679
*outfile << "\n";

0 commit comments

Comments
 (0)