script.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2017 molio contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. // import * as util from 'util'
  7. import * as fs from 'fs'
  8. import { parse } from './reader/gro'
  9. import { Table } from './relational/table'
  10. const file = '1crn.gro'
  11. // const file = 'water.gro'
  12. // const file = 'test.gro'
  13. // const file = 'md_1u19_trj.gro'
  14. function getFloatArray(table: Table, name: string) {
  15. const column = table.getColumn(name)
  16. const n = table.rowCount
  17. const array = new Float32Array(n)
  18. for (let i = 0; i < n; ++i) {
  19. array[i] = column.getFloat(i)
  20. }
  21. return array
  22. }
  23. function getIntArray(table: Table, name: string) {
  24. const column = table.getColumn(name)
  25. const n = table.rowCount
  26. const array = new Int32Array(n)
  27. for (let i = 0; i < n; ++i) {
  28. array[i] = column.getInteger(i)
  29. }
  30. return array
  31. }
  32. fs.readFile(`./examples/${file}`, 'utf8', function (err,data) {
  33. if (err) {
  34. return console.log(err);
  35. }
  36. // console.log(data);
  37. console.time('parse')
  38. const parsed = parse(data)
  39. console.timeEnd('parse')
  40. if (parsed.isError) {
  41. console.log(parsed)
  42. } else {
  43. const groFile = parsed.result
  44. const header = groFile.blocks[0].getTable('header')
  45. if (header) {
  46. console.log(header.columnNames)
  47. console.log('title', header.getColumn('title').getString(0))
  48. console.log('timeInPs', header.getColumn('timeInPs').getFloat(0))
  49. console.log('numberOfAtoms', header.getColumn('numberOfAtoms').getInteger(0))
  50. console.log('boxX', header.getColumn('boxX').getFloat(0))
  51. console.log('boxY', header.getColumn('boxY').getFloat(0))
  52. console.log('boxZ', header.getColumn('boxZ').getFloat(0))
  53. } else {
  54. console.error('no header')
  55. }
  56. const atoms = groFile.blocks[0].getTable('atoms')
  57. if (atoms) {
  58. console.log(atoms.columnNames)
  59. console.log(`'${atoms.getColumn('residueNumber').getString(1)}'`)
  60. console.log(`'${atoms.getColumn('residueName').getString(1)}'`)
  61. console.log(`'${atoms.getColumn('atomName').getString(1)}'`)
  62. console.log(atoms.getColumn('z').getFloat(1))
  63. console.log(`'${atoms.getColumn('z').getString(1)}'`)
  64. const n = atoms.rowCount
  65. console.log('rowCount', n)
  66. console.time('getFloatArray x')
  67. const x = getFloatArray(atoms, 'x')
  68. console.timeEnd('getFloatArray x')
  69. console.log(x.length, x[0], x[x.length-1])
  70. console.time('getFloatArray y')
  71. const y = getFloatArray(atoms, 'y')
  72. console.timeEnd('getFloatArray y')
  73. console.log(y.length, y[0], y[y.length-1])
  74. console.time('getFloatArray z')
  75. const z = getFloatArray(atoms, 'z')
  76. console.timeEnd('getFloatArray z')
  77. console.log(z.length, z[0], z[z.length-1])
  78. console.time('getIntArray residueNumber')
  79. const residueNumber = getIntArray(atoms, 'residueNumber')
  80. console.timeEnd('getIntArray residueNumber')
  81. console.log(residueNumber.length, residueNumber[0], residueNumber[residueNumber.length-1])
  82. } else {
  83. console.error('no atoms')
  84. }
  85. }
  86. });