55import argparse
66from typing import Any
77import json
8+ import csv
89import numpy as np
910import matplotlib .pyplot as plt
1011
12+ from ipsframework .resourceHelper import get_platform_info
1113
12- def main (alpha : float , L :float , T_final :float , Nx :int , Nt :int ) -> dict [str , Any ]:
14+ def main (instance : str ,
15+ alpha : float , L :float , T_final :float , Nx :int , Nt :int ) -> dict [str , Any ]:
1316 """ Generate synthetic data to emulate an actual simulation or complex
1417 calculation.
1518
1619 As a side-effect it will save a plot to the current working directory with
1720 the name `solution.png`.
1821
22+ :param instance: instance name
1923 :param alpha: thermal diffusivity
2024 :param L: domain length
2125 :param T_final: final time
2226 :param Nx: number of spatial grid points
2327 :param Nt: number of time steps
2428 :returns: x, y, where x is the steps and u the corresponding values
25- """
29+ """
2630 # Discretization
2731 dx = L / (Nx - 1 )
2832 dt = T_final / Nt
@@ -33,9 +37,6 @@ def main(alpha: float, L:float, T_final:float, Nx:int, Nt:int) -> dict[str, Any]
3337 print ("Warning: Stability condition r <= 0.5 is not met. "
3438 "Results may be inaccurate." )
3539
36- # Initialize solution array
37- u = np .zeros (Nx )
38-
3940 # Initial condition (e.g., a sine wave)
4041 x = np .linspace (0 , L , Nx )
4142 u = np .sin (np .pi * x )
@@ -60,13 +61,36 @@ def main(alpha: float, L:float, T_final:float, Nx:int, Nt:int) -> dict[str, Any]
6061 plt .grid (True )
6162 plt .savefig ("solution.png" )
6263
64+ # Save some per-component stats
65+ stats_fname = f'{ instance } _stats_{ timestamp } .csv'
66+ run_env = get_platform_info ()
67+
68+ with open (stats_fname , 'w' ) as f :
69+ # Write run-time stats to a CSV as well as the runtime parameters
70+ # specific to this instance.
71+ writer = csv .writer (f )
72+ writer .writerow (
73+ ['instance' , 'hostname' , 'pid' , 'core' ,
74+ 'affinity' ,
75+ 'alpha' , 'L' , 'T_final' , 'Nx' , 'Nt' ,
76+ 'start' , 'end' ])
77+
78+ writer .writerow ([instance_id , run_env ['hostname' ],
79+ run_env ['pid' ], run_env ['core_id' ],
80+ run_env ['affinity' ],
81+ alpha , L , T_final , Nx , Nt ,
82+ start , time ()])
83+
6384 return {'x' : x .tolist (), 'u' : u .tolist ()}
6485
6586
6687
6788if __name__ == '__main__' :
6889 parser = argparse .ArgumentParser (description = 'Generate synthetic data to '
69- 'emulate an actual simulation or complex' )
90+ 'emulate an actual simulation '
91+ 'or complex' )
92+ parser .add_argument ('--instance' , type = str ,
93+ help = 'instance name' )
7094 parser .add_argument ('--alpha' , type = float , default = 1.0 ,)
7195 parser .add_argument ('--L' , type = float , default = 1.0 ,)
7296 parser .add_argument ('--T_final' , type = float , default = 1.0 ,)
@@ -75,7 +99,12 @@ def main(alpha: float, L:float, T_final:float, Nx:int, Nt:int) -> dict[str, Any]
7599
76100 args = parser .parse_args ()
77101
78- data = main (args .alpha , args .L , args .T_final , args .Nx , args .Nt )
102+ data = main (args .instance ,
103+ args .alpha , args .L , args .T_final , args .Nx , args .Nt )
104+
105+ file_name = f'{ args .instance } _solution.json'
106+
107+ print (f'Writing to { file_name } ' )
79108
80- with open ('solution.json' , 'w' ) as f :
109+ with open (file_name , 'w' ) as f :
81110 json .dump (data , f )
0 commit comments