1414from aiida_fans .helpers import make_input_dict
1515
1616
17- class FansCalcBase (CalcJob ):
18- """Base class of all calculations using FANS."""
17+ class FansCalculation (CalcJob ):
18+ """Calculations using FANS."""
1919
2020 @classmethod
2121 def define (cls , spec : CalcJobProcessSpec ) -> None :
@@ -39,6 +39,7 @@ def define(cls, spec: CalcJobProcessSpec) -> None:
3939 # Custom Metadata
4040 spec .input ("metadata.options.results_prefix" , valid_type = str , default = "" )
4141 spec .input ("metadata.options.results" , valid_type = list , default = [])
42+ spec .input ("metadata.options.stashed_microstructure" , valid_type = bool , default = True )
4243
4344 # Input Ports
4445 ## Microstructure Definition
@@ -69,6 +70,40 @@ def define(cls, spec: CalcJobProcessSpec) -> None:
6970
7071 def prepare_for_submission (self , folder : Folder ) -> CalcInfo :
7172 """Prepare the calculation for submission."""
73+ # Stashed Strategy:
74+ if self .options .stashed_microstructure :
75+ ms_filepath : Path = Path (self .inputs .code .computer .get_workdir ()) / \
76+ "stash/microstructures" / \
77+ self .inputs .microstructure .file .filename
78+ # if microstructure does not exist in stash, make it
79+ if not ms_filepath .is_file ():
80+ ms_filepath .parent .mkdir (parents = True , exist_ok = True )
81+ with self .inputs .microstructure .file .open (mode = 'rb' ) as source :
82+ with ms_filepath .open (mode = 'wb' ) as target :
83+ copyfileobj (source , target )
84+
85+ # input.json as dict
86+ input_dict = make_input_dict (self )
87+ input_dict ["microstructure" ]["filepath" ] = str (ms_filepath )
88+ # write input.json to working directory
89+ with folder .open (self .options .input_filename , "w" , "utf8" ) as json :
90+ dump (input_dict , json , indent = 4 )
91+ # Fragmented Strategy:
92+ else :
93+ datasetname : str = self .inputs .microstructure .datasetname .value
94+ with folder .open ("microstructure.h5" ,"bw" ) as f_dest :
95+ with h5File (f_dest ,"w" ) as h5_dest :
96+ with self .inputs .microstructure .file .open (mode = "rb" ) as f_src :
97+ with h5File (f_src ,'r' ) as h5_src :
98+ h5_src .copy (datasetname , h5_dest , name = datasetname )
99+
100+ # input.json as dict
101+ input_dict = make_input_dict (self )
102+ input_dict ["microstructure" ]["filepath" ] = "microstructure.h5"
103+ # write input.json to working directory
104+ with folder .open (self .options .input_filename , "w" , "utf8" ) as json :
105+ dump (input_dict , json , indent = 4 )
106+
72107 # Specifying the code info:
73108 codeinfo = CodeInfo ()
74109 codeinfo .code_uuid = self .inputs .code .uuid
@@ -87,60 +122,3 @@ def prepare_for_submission(self, folder: Folder) -> CalcInfo:
87122 ]
88123
89124 return calcinfo
90-
91-
92- class FansStashedCalculation (FansCalcBase ):
93- """Calculations using FANS and the "Stashed" microstructure distribution strategy."""
94-
95- @classmethod
96- def define (cls , spec : CalcJobProcessSpec ) -> None :
97- """Define inputs, outputs, and exit codes of the calculation."""
98- return super ().define (spec )
99-
100- def prepare_for_submission (self , folder : Folder ) -> CalcInfo :
101- """Prepare the calculation for submission."""
102- ms_filepath : Path = Path (self .inputs .code .computer .get_workdir ()) / \
103- "stash/microstructures" / \
104- self .inputs .microstructure .file .filename
105- # if microstructure does not exist in stash, make it
106- if not ms_filepath .is_file ():
107- ms_filepath .parent .mkdir (parents = True , exist_ok = True )
108- with self .inputs .microstructure .file .open (mode = 'rb' ) as source :
109- with ms_filepath .open (mode = 'wb' ) as target :
110- copyfileobj (source , target )
111-
112- # input.json as dict
113- input_dict = make_input_dict (self )
114- input_dict ["microstructure" ]["filepath" ] = str (ms_filepath )
115- # write input.json to working directory
116- with folder .open (self .options .input_filename , "w" , "utf8" ) as json :
117- dump (input_dict , json , indent = 4 )
118-
119- return super ().prepare_for_submission (folder )
120-
121- class FansFragmentedCalculation (FansCalcBase ):
122- """Calculations using FANS and the "Fragmented" microstructure distribution strategy."""
123-
124- @classmethod
125- def define (cls , spec : CalcJobProcessSpec ) -> None :
126- """Define inputs, outputs, and exit codes of the calculation."""
127- return super ().define (spec )
128-
129- def prepare_for_submission (self , folder : Folder ) -> CalcInfo :
130- """Prepare the calculation for submission."""
131- # Write Microstructure Subset to Folder
132- datasetname : str = self .inputs .microstructure .datasetname .value
133- with folder .open ("microstructure.h5" ,"bw" ) as f_dest :
134- with h5File (f_dest ,"w" ) as h5_dest :
135- with self .inputs .microstructure .file .open (mode = "rb" ) as f_src :
136- with h5File (f_src ,'r' ) as h5_src :
137- h5_src .copy (datasetname , h5_dest , name = datasetname )
138-
139- # input.json as dict
140- input_dict = make_input_dict (self )
141- input_dict ["microstructure" ]["filepath" ] = "microstructure.h5"
142- # write input.json to working directory
143- with folder .open (self .options .input_filename , "w" , "utf8" ) as json :
144- dump (input_dict , json , indent = 4 )
145-
146- return super ().prepare_for_submission (folder )
0 commit comments