|
139 | 139 | "source": [ |
140 | 140 | "from aiida_workgraph import task\n", |
141 | 141 | "from aiida_workgraph.manager import get_current_graph\n", |
142 | | - "from aiida.orm import StructureData, load_group, KpointsData, SinglefileData\n", |
| 142 | + "from aiida.orm import StructureData, load_group, KpointsData, SinglefileData, InstalledCode, List, Dict\n", |
143 | 143 | "from ase.io import iread\n", |
144 | 144 | "from pathlib import Path\n", |
145 | 145 | "import yaml\n", |
|
148 | 148 | "\n", |
149 | 149 | "\n", |
150 | 150 | "@task.graph(outputs = [\"test_file\", \"train_file\", \"valid_file\"])\n", |
151 | | - "def qe(**inputs):\n", |
| 151 | + "def qe(\n", |
| 152 | + " code: InstalledCode,\n", |
| 153 | + " kpoints_mesh: List,\n", |
| 154 | + " task_metadata: Dict,\n", |
| 155 | + " test_file: SinglefileData,\n", |
| 156 | + " train_file: SinglefileData,\n", |
| 157 | + " valid_file: SinglefileData\n", |
| 158 | + " ):\n", |
152 | 159 | "\n", |
153 | 160 | " wg = get_current_graph()\n", |
154 | 161 | "\n", |
155 | | - " task_inputs = inputs[\"task_params\"]['task_inputs']\n", |
156 | | - " code =inputs[\"task_params\"][\"code\"]\n", |
157 | | - "\n", |
158 | 162 | " kpoints = KpointsData()\n", |
159 | | - " kpoints.set_kpoints_mesh(task_inputs['kpoint_mesh'])\n", |
| 163 | + " kpoints.set_kpoints_mesh(kpoints_mesh)\n", |
160 | 164 | "\n", |
161 | 165 | " pseudo_family = load_group('SSSP/1.3/PBE/efficiency')\n", |
162 | | - " files = {\"test_file\": inputs['test_file'],\"train_file\":inputs['train_file'],\"valid_file\":inputs['valid_file']}\n", |
| 166 | + " \n", |
| 167 | + " files = {\"test_file\": test_file, \"train_file\": train_file, \"valid_file\": valid_file}\n", |
163 | 168 | "\n", |
164 | 169 | " for file_name, file in files.items():\n", |
165 | 170 | " with file.as_path() as path:\n", |
|
187 | 192 | " \n", |
188 | 193 | " qe_task = wg.add_task(\n", |
189 | 194 | " PwCalculation,\n", |
190 | | - " code = code,\n", |
191 | | - " parameters= pw_params,\n", |
192 | | - " kpoints= kpoints,\n", |
193 | | - " pseudos= pseudos,\n", |
194 | | - " metadata= task_inputs[\"metadata\"],\n", |
195 | | - " structure= structure,\n", |
| 195 | + " code=code,\n", |
| 196 | + " parameters=pw_params,\n", |
| 197 | + " kpoints=kpoints,\n", |
| 198 | + " pseudos=pseudos,\n", |
| 199 | + " metadata=task_metadata.value,\n", |
| 200 | + " structure=structure,\n", |
196 | 201 | " )\n", |
197 | 202 | " \n", |
198 | 203 | " structfile = f\"{file_name}.struct{i}\"\n", |
|
239 | 244 | " for file_name, structs in inputs.items():\n", |
240 | 245 | " path = Path(f\"mlip_{file_name}.extxyz\")\n", |
241 | 246 | "\n", |
242 | | - " for stuct_out_params in structs.values():\n", |
| 247 | + " for struct_out_params in structs.values():\n", |
243 | 248 | " \n", |
244 | | - " trajectory = stuct_out_params[\"trajectory\"]\n", |
| 249 | + " trajectory = struct_out_params[\"trajectory\"]\n", |
245 | 250 | "\n", |
246 | 251 | " fileStructure = trajectory.get_structure(index=0)\n", |
247 | 252 | " fileAtoms = fileStructure.get_ase()\n", |
|
253 | 258 | " fileAtoms.info[\"units\"] = {\"energy\": \"eV\",\"forces\": \"ev/Ang\",\"stress\": \"ev/Ang^3\"}\n", |
254 | 259 | " fileAtoms.set_array(\"qe_forces\", trajectory.arrays[\"forces\"][0])\n", |
255 | 260 | "\n", |
256 | | - " parameters = stuct_out_params[\"parameters\"]\n", |
| 261 | + " parameters = struct_out_params[\"parameters\"]\n", |
257 | 262 | " fileParams = parameters.get_dict()\n", |
258 | 263 | " fileAtoms.info[\"qe_energy\"] = fileParams[\"energy\"]\n", |
259 | 264 | " write(path, fileAtoms, append=True)\n", |
|
318 | 323 | " \"metadata\": {\"options\": {\"resources\": {\"num_machines\": 1}}},\n", |
319 | 324 | "}\n", |
320 | 325 | "\n", |
321 | | - "goemopt_inputs = {\n", |
| 326 | + "geomopt_inputs = {\n", |
322 | 327 | " \"fmax\": Float(0.1),\n", |
323 | 328 | " \"opt_cell_lengths\": Bool(False),\n", |
324 | 329 | " \"opt_cell_fully\": Bool(True),\n", |
|
332 | 337 | "}\n", |
333 | 338 | "\n", |
334 | 339 | "qe_inputs = {\n", |
335 | | - " \"task_inputs\": Dict({\n", |
336 | | - " \"metadata\": {\n", |
| 340 | + " \"task_metadata\": Dict({\n", |
337 | 341 | " \"options\": {\n", |
338 | 342 | " \"resources\": {\n", |
339 | 343 | " \"num_machines\": 1,\n", |
|
352 | 356 | " \"\"\",\n", |
353 | 357 | " \"append_text\": \"\",\n", |
354 | 358 | " },\n", |
355 | | - " },\n", |
356 | | - " \"kpoint_mesh\": List([1, 1, 1]),\n", |
357 | 359 | " }),\n", |
| 360 | + " \"kpoints_mesh\": List([1, 1, 1]),\n", |
358 | 361 | " \"code\": qe_code,\n", |
359 | 362 | "}" |
360 | 363 | ] |
|
364 | 367 | "id": "06ee80fd", |
365 | 368 | "metadata": {}, |
366 | 369 | "source": [ |
367 | | - "Now we can build the `Workgraph`. First we iterate through each structure in the initail structure file, and run `Geomopt` and `Descriptors` on them these give a `SinglefileData` instance of the structure outputs. These structures can then be passed to the `split_task`, which splits these structures up into training files. Then we run `QE` task, getting the outputs and passing them into the `training_files` task which, as the name suggests, it creates the training file from the `QE` task outputs. Finally we can run the training script. Ideally, if any of the inputs need to changed, they should be done in the cell above." |
| 370 | + "Now we can build the `Workgraph`. First we iterate through each structure in the initail structure file, and run `Geomopt` and `Descriptors` on them these give a `SinglefileData` instance of the structure outputs. These structures can then be passed to the `split_task`, which splits these structures up into train, test and validation files. Then we run `QE` task, getting the outputs and passing them into the `training_files` task which, as the name suggests, it creates the training file from the `QE` task outputs. Finally we can run the training script. Ideally, if any of the inputs need to changed, they should be done in the cell above." |
368 | 371 | ] |
369 | 372 | }, |
370 | 373 | { |
|
390 | 393 | " geomopt_calc = wg.add_task(\n", |
391 | 394 | " geomoptCalc,\n", |
392 | 395 | " **calc_inputs,\n", |
393 | | - " **goemopt_inputs,\n", |
| 396 | + " **geomopt_inputs,\n", |
394 | 397 | " struct=structure,\n", |
395 | 398 | " )\n", |
396 | 399 | " \n", |
|
416 | 419 | " test_file= split_task.outputs.test_file,\n", |
417 | 420 | " train_file= split_task.outputs.train_file,\n", |
418 | 421 | " valid_file= split_task.outputs.valid_file,\n", |
419 | | - " task_params = qe_inputs\n", |
| 422 | + " **qe_inputs\n", |
420 | 423 | " )\n", |
421 | 424 | "\n", |
422 | 425 | " training_files = wg.add_task(\n", |
|
441 | 444 | "id": "7f3c72ca", |
442 | 445 | "metadata": {}, |
443 | 446 | "source": [ |
444 | | - "Run and visualise the workgraph" |
| 447 | + "Visualise and run the workgraph" |
445 | 448 | ] |
446 | 449 | }, |
447 | 450 | { |
|
464 | 467 | "wg.run()" |
465 | 468 | ] |
466 | 469 | }, |
| 470 | + { |
| 471 | + "cell_type": "code", |
| 472 | + "execution_count": null, |
| 473 | + "id": "13e509e3", |
| 474 | + "metadata": {}, |
| 475 | + "outputs": [], |
| 476 | + "source": [ |
| 477 | + "wg.tasks.create_aiida_files.outputs.test_file" |
| 478 | + ] |
| 479 | + }, |
467 | 480 | { |
468 | 481 | "cell_type": "markdown", |
469 | 482 | "id": "2e36396f", |
|
484 | 497 | "import matplotlib.image as mpimg\n", |
485 | 498 | "\n", |
486 | 499 | "folder = wg.tasks.Train.outputs.remote_folder.value\n", |
487 | | - "picturePath = f\"{os.getcwd()}/traingraph.png\"\n", |
| 500 | + "picturePath = Path.cwd() / \"traingraph.png\"\n", |
| 501 | + "\n", |
488 | 502 | "folder.getfile(relpath='results/test_run-123_train_Default_stage_one.png',destpath=picturePath)\n", |
489 | 503 | "\n", |
490 | 504 | "img = mpimg.imread(picturePath)\n", |
|
0 commit comments