From f1d54e94faf189dc6ddb164812575d7bce8d5b3c Mon Sep 17 00:00:00 2001
From: ajz34 <17110220038@fudan.edu.cn>
Date: Tue, 9 Jun 2020 21:09:37 +0800
Subject: [PATCH] pyxdh: UHF-UB3LYP grad

---
 .idea/workspace.xml                           | 208 ++++++++++++------
 pyxdh/DerivOnce/__init__.py                   |  12 +-
 pyxdh/DerivOnce/deriv_once_scf.py             |   5 +-
 pyxdh/DerivOnce/deriv_once_uscf.py            |  93 +++++++-
 pyxdh/DerivOnce/grad_uscf.py                  |  33 ++-
 pyxdh/Utilities/deriv_numerical.py            |   2 +-
 pyxdh/Utilities/test_molecules.py             |  12 +-
 .../ncdft_derivonce_uhf_ub3lyp.dat            | Bin 0 -> 293 bytes
 .../ncdft_derivonce_uhf_ub3lyp.py             |  54 +++++
 9 files changed, 337 insertions(+), 82 deletions(-)
 create mode 100644 pyxdh/Validation/numerical_deriv/ncdft_derivonce_uhf_ub3lyp.dat
 create mode 100644 pyxdh/Validation/numerical_deriv/ncdft_derivonce_uhf_ub3lyp.py

diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 87948b8..b6a47f7 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -19,10 +19,15 @@
     <select />
   </component>
   <component name="ChangeListManager">
-    <list default="true" id="912d8545-b18a-4722-b0d8-e8c61f686f07" name="Default Changelist" comment="pyxdh: Now user can specify kernel engine. LibXC should be at least v5.0.0 to make LYP 3rd derivative correct.">
+    <list default="true" id="912d8545-b18a-4722-b0d8-e8c61f686f07" name="Default Changelist" comment="">
+      <change afterPath="$PROJECT_DIR$/pyxdh/Validation/numerical_deriv/ncdft_derivonce_uhf_ub3lyp.dat" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/pyxdh/Validation/numerical_deriv/ncdft_derivonce_uhf_ub3lyp.py" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/pyxdh/DerivOnce/__init__.py" beforeDir="false" afterPath="$PROJECT_DIR$/pyxdh/DerivOnce/__init__.py" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/pyxdh/DerivOnce/deriv_once_scf.py" beforeDir="false" afterPath="$PROJECT_DIR$/pyxdh/DerivOnce/deriv_once_scf.py" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/pyxdh/DerivOnce/deriv_once_uscf.py" beforeDir="false" afterPath="$PROJECT_DIR$/pyxdh/DerivOnce/deriv_once_uscf.py" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/pyxdh/DerivOnce/grad_uscf.py" beforeDir="false" afterPath="$PROJECT_DIR$/pyxdh/DerivOnce/grad_uscf.py" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/pyxdh/Utilities/deriv_numerical.py" beforeDir="false" afterPath="$PROJECT_DIR$/pyxdh/Utilities/deriv_numerical.py" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/pyxdh/Utilities/test_molecules.py" beforeDir="false" afterPath="$PROJECT_DIR$/pyxdh/Utilities/test_molecules.py" afterDir="false" />
     </list>
     <option name="SHOW_DIALOG" value="false" />
@@ -50,7 +55,7 @@
     <property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
     <property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
     <property name="WebServerToolWindowFactoryState" value="false" />
-    <property name="last_opened_file_path" value="$PROJECT_DIR$/pyxdh/DerivOnce/grad_uscf.py" />
+    <property name="last_opened_file_path" value="$PROJECT_DIR$/pyxdh/Validation/numerical_deriv" />
     <property name="node.js.detected.package.eslint" value="true" />
     <property name="node.js.detected.package.tslint" value="true" />
     <property name="node.js.path.for.package.eslint" value="project" />
@@ -76,7 +81,12 @@
       </console-settings>
     </option>
   </component>
-  <component name="RunManager" selected="Python tests.pytest for grad_uscf.Test_GradUSCF.test_UB3LYP_grad">
+  <component name="RecentsManager">
+    <key name="CopyFile.RECENT_KEYS">
+      <recent name="E:\Git-Repo\Py_xDH\pyxdh\Validation\numerical_deriv" />
+    </key>
+  </component>
+  <component name="RunManager" selected="Python tests.pytest for grad_uscf.Test_GradUSCF.test_UHF_UB3LYP_grad">
     <configuration name="libxc_kxc" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
       <module name="Py_xDH" />
       <option name="INTERPRETER_OPTIONS" value="" />
@@ -99,20 +109,21 @@
       <option name="INPUT_FILE" value="" />
       <method v="2" />
     </configuration>
-    <configuration name="test_xcfun" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
+    <configuration name="ncdft_derivonce_uhf_ub3lyp" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
       <module name="Py_xDH" />
       <option name="INTERPRETER_OPTIONS" value="" />
       <option name="PARENT_ENVS" value="true" />
       <envs>
         <env name="PYTHONUNBUFFERED" value="1" />
+        <env name="PYTHONPATH" value="/mnt/e/Git-Others/pyscf:$PYTHONPATH" />
       </envs>
       <option name="SDK_HOME" value="" />
-      <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/pyxdh/tmp/unrestricted_test" />
-      <option name="IS_MODULE_SDK" value="false" />
+      <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/pyxdh/Validation/numerical_deriv" />
+      <option name="IS_MODULE_SDK" value="true" />
       <option name="ADD_CONTENT_ROOTS" value="true" />
       <option name="ADD_SOURCE_ROOTS" value="true" />
       <EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
-      <option name="SCRIPT_NAME" value="$PROJECT_DIR$/pyxdh/tmp/unrestricted_test/test_xcfun.py" />
+      <option name="SCRIPT_NAME" value="$PROJECT_DIR$/pyxdh/Validation/numerical_deriv/ncdft_derivonce_uhf_ub3lyp.py" />
       <option name="PARAMETERS" value="" />
       <option name="SHOW_COMMAND_LINE" value="false" />
       <option name="EMULATE_TERMINAL" value="false" />
@@ -121,7 +132,7 @@
       <option name="INPUT_FILE" value="" />
       <method v="2" />
     </configuration>
-    <configuration name="xcfun_grid" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
+    <configuration name="ucphf_hacking" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
       <module name="Py_xDH" />
       <option name="INTERPRETER_OPTIONS" value="" />
       <option name="PARENT_ENVS" value="true" />
@@ -129,12 +140,12 @@
         <env name="PYTHONUNBUFFERED" value="1" />
       </envs>
       <option name="SDK_HOME" value="" />
-      <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/pyxdh/tmp" />
+      <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/pyxdh/tmp/unrestricted_test" />
       <option name="IS_MODULE_SDK" value="false" />
       <option name="ADD_CONTENT_ROOTS" value="true" />
       <option name="ADD_SOURCE_ROOTS" value="true" />
       <EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
-      <option name="SCRIPT_NAME" value="$PROJECT_DIR$/pyxdh/tmp/xcfun_grid.py" />
+      <option name="SCRIPT_NAME" value="$PROJECT_DIR$/pyxdh/tmp/unrestricted_test/ucphf_hacking.py" />
       <option name="PARAMETERS" value="" />
       <option name="SHOW_COMMAND_LINE" value="false" />
       <option name="EMULATE_TERMINAL" value="false" />
@@ -183,6 +194,26 @@
       <option name="_new_targetType" value="&quot;PYTHON&quot;" />
       <method v="2" />
     </configuration>
+    <configuration name="pytest for grad_uscf.Test_GradUSCF.test_UHF_UB3LYP_grad" type="tests" factoryName="py.test" nameIsGenerated="true">
+      <module name="Py_xDH" />
+      <option name="INTERPRETER_OPTIONS" value="" />
+      <option name="PARENT_ENVS" value="true" />
+      <envs>
+        <env name="PYTHONPATH" value="/mnt/e/Git-Others/pyscf:/mnt/e/Git-Repo/Py_xDH:$PYTHONPATH" />
+      </envs>
+      <option name="SDK_HOME" value="" />
+      <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/pyxdh/DerivOnce" />
+      <option name="IS_MODULE_SDK" value="true" />
+      <option name="ADD_CONTENT_ROOTS" value="true" />
+      <option name="ADD_SOURCE_ROOTS" value="true" />
+      <EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
+      <option name="_new_keywords" value="&quot;&quot;" />
+      <option name="_new_parameters" value="&quot;&quot;" />
+      <option name="_new_additionalArguments" value="&quot;&quot;" />
+      <option name="_new_target" value="&quot;grad_uscf.Test_GradUSCF.test_UHF_UB3LYP_grad&quot;" />
+      <option name="_new_targetType" value="&quot;PYTHON&quot;" />
+      <method v="2" />
+    </configuration>
     <configuration name="pytest for grad_uscf.Test_GradUSCF.test_UHF_grad" type="tests" factoryName="py.test" temporary="true" nameIsGenerated="true">
       <module name="Py_xDH" />
       <option name="INTERPRETER_OPTIONS" value="" />
@@ -238,22 +269,23 @@
       <method v="2" />
     </configuration>
     <list>
-      <item itemvalue="Python.xcfun_grid" />
-      <item itemvalue="Python.test_xcfun" />
       <item itemvalue="Python.libxc_kxc" />
+      <item itemvalue="Python.ncdft_derivonce_uhf_ub3lyp" />
+      <item itemvalue="Python.ucphf_hacking" />
       <item itemvalue="Python tests.pytest in grad_uscf.py" />
       <item itemvalue="Python tests.pytest for dipderiv_mp2.Test_DipDerivMP2.test_B2PLYP_dipderiv" />
       <item itemvalue="Python tests.pytest for pyxdh" />
       <item itemvalue="Python tests.pytest for grad_uscf.Test_GradUSCF.test_UHF_grad" />
       <item itemvalue="Python tests.pytest for grad_uscf.Test_GradUSCF.test_UB3LYP_grad" />
+      <item itemvalue="Python tests.pytest for grad_uscf.Test_GradUSCF.test_UHF_UB3LYP_grad" />
     </list>
     <recent_temporary>
       <list>
+        <item itemvalue="Python.ncdft_derivonce_uhf_ub3lyp" />
+        <item itemvalue="Python.ucphf_hacking" />
         <item itemvalue="Python tests.pytest for grad_uscf.Test_GradUSCF.test_UB3LYP_grad" />
         <item itemvalue="Python tests.pytest for grad_uscf.Test_GradUSCF.test_UHF_grad" />
         <item itemvalue="Python.libxc_kxc" />
-        <item itemvalue="Python.test_xcfun" />
-        <item itemvalue="Python.xcfun_grid" />
       </list>
     </recent_temporary>
   </component>
@@ -287,7 +319,9 @@
       <workItem from="1591579948838" duration="4710000" />
       <workItem from="1591591692320" duration="5711000" />
       <workItem from="1591605332759" duration="1469000" />
-      <workItem from="1591612951033" duration="6425000" />
+      <workItem from="1591612951033" duration="10397000" />
+      <workItem from="1591665553856" duration="19078000" />
+      <workItem from="1591702790005" duration="5068000" />
     </task>
     <task id="LOCAL-00001" summary="pyxdh: UHF Gradient">
       <created>1591017061437</created>
@@ -310,7 +344,14 @@
       <option name="project" value="LOCAL" />
       <updated>1591613805618</updated>
     </task>
-    <option name="localTasksCounter" value="4" />
+    <task id="LOCAL-00004" summary="pyxdh: UB3LYP grad">
+      <created>1591619557688</created>
+      <option name="number" value="00004" />
+      <option name="presentableId" value="LOCAL-00004" />
+      <option name="project" value="LOCAL" />
+      <updated>1591619557688</updated>
+    </task>
+    <option name="localTasksCounter" value="5" />
     <servers />
   </component>
   <component name="TypeScriptGeneratedFilesManager">
@@ -332,84 +373,98 @@
     <MESSAGE value="pyxdh: UHF Gradient" />
     <MESSAGE value="pyxdh: modify nocc initialization in NCDFT, and mute warning in GridHelper" />
     <MESSAGE value="pyxdh: Now user can specify kernel engine. LibXC should be at least v5.0.0 to make LYP 3rd derivative correct." />
-    <option name="LAST_COMMIT_MESSAGE" value="pyxdh: Now user can specify kernel engine. LibXC should be at least v5.0.0 to make LYP 3rd derivative correct." />
+    <MESSAGE value="pyxdh: UB3LYP grad" />
+    <option name="LAST_COMMIT_MESSAGE" value="pyxdh: UB3LYP grad" />
   </component>
   <component name="WindowStateProjectService">
-    <state x="173" y="164" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1591619194201">
+    <state x="173" y="164" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1591689999690">
       <screen x="0" y="40" width="1536" height="824" />
     </state>
     <state x="64" y="99" key="#com.intellij.execution.impl.EditConfigurationsDialog/0.40.1536.824/-1858.0.1474.864@-1858.0.1474.864" timestamp="1591016736712" />
     <state x="173" y="164" key="#com.intellij.execution.impl.EditConfigurationsDialog/0.40.1536.824/-1858.0.1474.864@0.40.1536.824" timestamp="1591619194201" />
+    <state x="173" y="164" key="#com.intellij.execution.impl.EditConfigurationsDialog/0.40.1536.824/1986.0.1470.960@0.40.1536.824" timestamp="1591689999690" />
     <state x="2152" y="144" key="#com.intellij.execution.impl.EditConfigurationsDialog/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591607330815" />
     <state x="2404" y="42" key="#pytest_for_dipderiv_mp2.Test_DipDerivMP2.test_B2PLYP_dipderiv" timestamp="1591607222018">
       <screen x="1986" y="0" width="1470" height="960" />
     </state>
     <state x="2404" y="42" key="#pytest_for_dipderiv_mp2.Test_DipDerivMP2.test_B2PLYP_dipderiv/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591607222018" />
-    <state x="497" y="131" width="850" height="716" key="#xdebugger.evaluate" timestamp="1591605196597">
+    <state x="465" y="40" key="#pytest_for_grad_uscf.Test_GradUSCF.test_UHF_UB3LYP_grad" timestamp="1591706415193">
       <screen x="0" y="40" width="1536" height="824" />
     </state>
-    <state x="497" y="131" width="850" height="716" key="#xdebugger.evaluate/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591605196597" />
+    <state x="465" y="40" key="#pytest_for_grad_uscf.Test_GradUSCF.test_UHF_UB3LYP_grad/0.40.1536.824/-1858.0.1474.864@0.40.1536.824" timestamp="1591706415193" />
+    <state x="242" y="194" width="850" height="567" key="#xdebugger.evaluate" timestamp="1591707087209">
+      <screen x="0" y="40" width="1536" height="824" />
+    </state>
+    <state x="242" y="194" width="850" height="567" key="#xdebugger.evaluate/0.40.1536.824/-1858.0.1474.864@0.40.1536.824" timestamp="1591707087209" />
+    <state x="602" y="70" key="#xdebugger.evaluate/0.40.1536.824/1986.0.1470.960@0.40.1536.824" timestamp="1591684033582" />
+    <state x="497" y="131" width="850" height="716" key="#xdebugger.evaluate/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591681631424" />
     <state width="1468" height="440" key="DebuggerActiveHint" timestamp="1591257376643">
       <screen x="1986" y="0" width="1470" height="960" />
     </state>
     <state width="1468" height="440" key="DebuggerActiveHint/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591257376643" />
-    <state x="2579" y="225" width="602" height="505" key="EnvironmentVariablesDialog" timestamp="1591607118086">
-      <screen x="1986" y="0" width="1470" height="960" />
+    <state x="620" y="233" width="703" height="508" key="EnvironmentVariablesDialog" timestamp="1591706413473">
+      <screen x="0" y="40" width="1536" height="824" />
     </state>
+    <state x="620" y="233" width="703" height="508" key="EnvironmentVariablesDialog/0.40.1536.824/-1858.0.1474.864@0.40.1536.824" timestamp="1591706413473" />
+    <state x="620" y="233" key="EnvironmentVariablesDialog/0.40.1536.824/1986.0.1470.960@0.40.1536.824" timestamp="1591689994168" />
     <state x="2579" y="225" width="602" height="505" key="EnvironmentVariablesDialog/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591607118086" />
     <state x="2502" y="234" key="FileChooserDialogImpl" timestamp="1591591721491">
       <screen x="1986" y="0" width="1470" height="960" />
     </state>
     <state x="-1341" y="211" key="FileChooserDialogImpl/0.40.1536.824/-1858.0.1474.864@-1858.0.1474.864" timestamp="1591016734753" />
     <state x="2502" y="234" key="FileChooserDialogImpl/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591591721491" />
-    <state width="1493" height="361" key="GridCell.Tab.0.bottom" timestamp="1591619463312">
+    <state width="1493" height="185" key="GridCell.Tab.0.bottom" timestamp="1591707056984">
       <screen x="0" y="40" width="1536" height="824" />
     </state>
     <state width="1431" height="224" key="GridCell.Tab.0.bottom/0.40.1536.824/-1858.0.1474.864@-1858.0.1474.864" timestamp="1591018976711" />
-    <state width="1493" height="361" key="GridCell.Tab.0.bottom/0.40.1536.824/-1858.0.1474.864@0.40.1536.824" timestamp="1591619463312" />
-    <state width="1493" height="261" key="GridCell.Tab.0.bottom/0.40.1536.824/1986.0.1470.960@0.40.1536.824" timestamp="1591075137709" />
-    <state width="1427" height="296" key="GridCell.Tab.0.bottom/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591607345812" />
-    <state width="1493" height="361" key="GridCell.Tab.0.center" timestamp="1591619463312">
+    <state width="1493" height="185" key="GridCell.Tab.0.bottom/0.40.1536.824/-1858.0.1474.864@0.40.1536.824" timestamp="1591707056984" />
+    <state width="1412" height="201" key="GridCell.Tab.0.bottom/0.40.1536.824/1986.0.1470.960@0.40.1536.824" timestamp="1591693173661" />
+    <state width="1427" height="230" key="GridCell.Tab.0.bottom/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591681560959" />
+    <state width="1493" height="185" key="GridCell.Tab.0.center" timestamp="1591707056984">
       <screen x="0" y="40" width="1536" height="824" />
     </state>
     <state width="1431" height="224" key="GridCell.Tab.0.center/0.40.1536.824/-1858.0.1474.864@-1858.0.1474.864" timestamp="1591018976711" />
-    <state width="1493" height="361" key="GridCell.Tab.0.center/0.40.1536.824/-1858.0.1474.864@0.40.1536.824" timestamp="1591619463312" />
-    <state width="1493" height="261" key="GridCell.Tab.0.center/0.40.1536.824/1986.0.1470.960@0.40.1536.824" timestamp="1591075137708" />
-    <state width="1427" height="296" key="GridCell.Tab.0.center/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591607345812" />
-    <state width="1493" height="361" key="GridCell.Tab.0.left" timestamp="1591619463311">
+    <state width="1493" height="185" key="GridCell.Tab.0.center/0.40.1536.824/-1858.0.1474.864@0.40.1536.824" timestamp="1591707056984" />
+    <state width="1412" height="201" key="GridCell.Tab.0.center/0.40.1536.824/1986.0.1470.960@0.40.1536.824" timestamp="1591693173661" />
+    <state width="1427" height="230" key="GridCell.Tab.0.center/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591681560959" />
+    <state width="1493" height="185" key="GridCell.Tab.0.left" timestamp="1591707056984">
       <screen x="0" y="40" width="1536" height="824" />
     </state>
     <state width="1431" height="224" key="GridCell.Tab.0.left/0.40.1536.824/-1858.0.1474.864@-1858.0.1474.864" timestamp="1591018976711" />
-    <state width="1493" height="361" key="GridCell.Tab.0.left/0.40.1536.824/-1858.0.1474.864@0.40.1536.824" timestamp="1591619463311" />
-    <state width="1493" height="261" key="GridCell.Tab.0.left/0.40.1536.824/1986.0.1470.960@0.40.1536.824" timestamp="1591075137708" />
-    <state width="1427" height="296" key="GridCell.Tab.0.left/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591607345812" />
-    <state width="1493" height="361" key="GridCell.Tab.0.right" timestamp="1591619463312">
+    <state width="1493" height="185" key="GridCell.Tab.0.left/0.40.1536.824/-1858.0.1474.864@0.40.1536.824" timestamp="1591707056984" />
+    <state width="1412" height="201" key="GridCell.Tab.0.left/0.40.1536.824/1986.0.1470.960@0.40.1536.824" timestamp="1591693173661" />
+    <state width="1427" height="230" key="GridCell.Tab.0.left/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591681560959" />
+    <state width="1493" height="185" key="GridCell.Tab.0.right" timestamp="1591707056984">
       <screen x="0" y="40" width="1536" height="824" />
     </state>
     <state width="1431" height="224" key="GridCell.Tab.0.right/0.40.1536.824/-1858.0.1474.864@-1858.0.1474.864" timestamp="1591018976711" />
-    <state width="1493" height="361" key="GridCell.Tab.0.right/0.40.1536.824/-1858.0.1474.864@0.40.1536.824" timestamp="1591619463312" />
-    <state width="1493" height="261" key="GridCell.Tab.0.right/0.40.1536.824/1986.0.1470.960@0.40.1536.824" timestamp="1591075137709" />
-    <state width="1427" height="296" key="GridCell.Tab.0.right/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591607345812" />
-    <state width="1427" height="187" key="GridCell.Tab.1.bottom" timestamp="1591605279924">
-      <screen x="1986" y="0" width="1470" height="960" />
+    <state width="1493" height="185" key="GridCell.Tab.0.right/0.40.1536.824/-1858.0.1474.864@0.40.1536.824" timestamp="1591707056984" />
+    <state width="1412" height="201" key="GridCell.Tab.0.right/0.40.1536.824/1986.0.1470.960@0.40.1536.824" timestamp="1591693173661" />
+    <state width="1427" height="230" key="GridCell.Tab.0.right/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591681560959" />
+    <state width="1493" height="185" key="GridCell.Tab.1.bottom" timestamp="1591707070471">
+      <screen x="0" y="40" width="1536" height="824" />
     </state>
-    <state width="1493" height="261" key="GridCell.Tab.1.bottom/0.40.1536.824/1986.0.1470.960@0.40.1536.824" timestamp="1591075137636" />
-    <state width="1427" height="187" key="GridCell.Tab.1.bottom/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591605279924" />
-    <state width="1427" height="187" key="GridCell.Tab.1.center" timestamp="1591605279924">
-      <screen x="1986" y="0" width="1470" height="960" />
+    <state width="1493" height="185" key="GridCell.Tab.1.bottom/0.40.1536.824/-1858.0.1474.864@0.40.1536.824" timestamp="1591707070471" />
+    <state width="1493" height="100" key="GridCell.Tab.1.bottom/0.40.1536.824/1986.0.1470.960@0.40.1536.824" timestamp="1591693173657" />
+    <state width="1427" height="230" key="GridCell.Tab.1.bottom/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591681564462" />
+    <state width="1493" height="185" key="GridCell.Tab.1.center" timestamp="1591707070470">
+      <screen x="0" y="40" width="1536" height="824" />
     </state>
-    <state width="1493" height="261" key="GridCell.Tab.1.center/0.40.1536.824/1986.0.1470.960@0.40.1536.824" timestamp="1591075137636" />
-    <state width="1427" height="187" key="GridCell.Tab.1.center/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591605279924" />
-    <state width="1427" height="187" key="GridCell.Tab.1.left" timestamp="1591605279924">
-      <screen x="1986" y="0" width="1470" height="960" />
+    <state width="1493" height="185" key="GridCell.Tab.1.center/0.40.1536.824/-1858.0.1474.864@0.40.1536.824" timestamp="1591707070470" />
+    <state width="1493" height="100" key="GridCell.Tab.1.center/0.40.1536.824/1986.0.1470.960@0.40.1536.824" timestamp="1591693173657" />
+    <state width="1427" height="230" key="GridCell.Tab.1.center/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591681564462" />
+    <state width="1493" height="185" key="GridCell.Tab.1.left" timestamp="1591707070470">
+      <screen x="0" y="40" width="1536" height="824" />
     </state>
-    <state width="1493" height="261" key="GridCell.Tab.1.left/0.40.1536.824/1986.0.1470.960@0.40.1536.824" timestamp="1591075137636" />
-    <state width="1427" height="187" key="GridCell.Tab.1.left/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591605279924" />
-    <state width="1427" height="187" key="GridCell.Tab.1.right" timestamp="1591605279924">
-      <screen x="1986" y="0" width="1470" height="960" />
+    <state width="1493" height="185" key="GridCell.Tab.1.left/0.40.1536.824/-1858.0.1474.864@0.40.1536.824" timestamp="1591707070470" />
+    <state width="1493" height="100" key="GridCell.Tab.1.left/0.40.1536.824/1986.0.1470.960@0.40.1536.824" timestamp="1591693173657" />
+    <state width="1427" height="230" key="GridCell.Tab.1.left/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591681564462" />
+    <state width="1493" height="185" key="GridCell.Tab.1.right" timestamp="1591707070471">
+      <screen x="0" y="40" width="1536" height="824" />
     </state>
-    <state width="1493" height="261" key="GridCell.Tab.1.right/0.40.1536.824/1986.0.1470.960@0.40.1536.824" timestamp="1591075137636" />
-    <state width="1427" height="187" key="GridCell.Tab.1.right/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591605279924" />
+    <state width="1493" height="185" key="GridCell.Tab.1.right/0.40.1536.824/-1858.0.1474.864@0.40.1536.824" timestamp="1591707070471" />
+    <state width="1493" height="100" key="GridCell.Tab.1.right/0.40.1536.824/1986.0.1470.960@0.40.1536.824" timestamp="1591693173657" />
+    <state width="1427" height="230" key="GridCell.Tab.1.right/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591681564462" />
     <state x="2223" y="124" key="SettingsEditor" timestamp="1591607196033">
       <screen x="1986" y="0" width="1470" height="960" />
     </state>
@@ -419,14 +474,15 @@
       <screen x="1986" y="0" width="1470" height="960" />
     </state>
     <state x="2514" y="345" key="com.jetbrains.plugins.remotesdk.ui.RemoteMappingsConfiguration/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1590477943382" />
-    <state x="-1822" y="-17" width="1488" height="811" maximized="true" key="dock-window-1" timestamp="1591619194625">
-      <screen x="-1858" y="0" width="1474" height="864" />
+    <state x="-7" y="40" width="1357" height="838" key="dock-window-1" timestamp="1591683344548">
+      <screen x="0" y="40" width="1536" height="824" />
     </state>
     <state x="-1822" y="-17" width="1488" height="811" maximized="true" key="dock-window-1/0.40.1536.824/-1858.0.1474.864@0.40.1536.824" timestamp="1591619194625" />
-    <state x="416" y="216" width="703" height="582" key="search.everywhere.popup" timestamp="1591615741599">
+    <state x="-7" y="40" width="1357" height="838" key="dock-window-1/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591683344548" />
+    <state x="416" y="216" width="703" height="582" key="search.everywhere.popup" timestamp="1591706648311">
       <screen x="0" y="40" width="1536" height="824" />
     </state>
-    <state x="416" y="216" width="703" height="582" key="search.everywhere.popup/0.40.1536.824/-1858.0.1474.864@0.40.1536.824" timestamp="1591615741599" />
+    <state x="416" y="216" width="703" height="582" key="search.everywhere.popup/0.40.1536.824/-1858.0.1474.864@0.40.1536.824" timestamp="1591706648311" />
     <state x="2382" y="205" width="675" height="679" key="search.everywhere.popup/0.40.1536.824/1982.0.1474.960@1982.0.1474.960" timestamp="1591333918246" />
     <state x="2384" y="205" width="672" height="678" key="search.everywhere.popup/0.40.1536.824/1986.0.1470.960@1986.0.1470.960" timestamp="1591591497189" />
   </component>
@@ -438,20 +494,48 @@
           <line>17</line>
           <option name="timeStamp" value="8" />
         </line-breakpoint>
+        <line-breakpoint enabled="true" suspend="THREAD" type="python-line">
+          <url>file://$USER_HOME$/AppData/Local/JetBrains/PyCharm2020.1/remote_sources/-1053058360/1179071206/pyscf/hessian/rhf.py</url>
+          <line>58</line>
+          <option name="timeStamp" value="11" />
+        </line-breakpoint>
+        <line-breakpoint enabled="true" suspend="THREAD" type="python-line">
+          <url>file://$USER_HOME$/AppData/Local/JetBrains/PyCharm2020.1/remote_sources/-1053058360/1179071206/pyscf/hessian/rhf.py</url>
+          <line>576</line>
+          <option name="timeStamp" value="13" />
+        </line-breakpoint>
+        <line-breakpoint enabled="true" suspend="THREAD" type="python-line">
+          <url>file://$USER_HOME$/AppData/Local/JetBrains/PyCharm2020.1/remote_sources/-1053058360/1179071206/pyscf/hessian/uhf.py</url>
+          <line>306</line>
+          <option name="timeStamp" value="14" />
+        </line-breakpoint>
+        <line-breakpoint enabled="true" suspend="THREAD" type="python-line">
+          <url>file://$USER_HOME$/AppData/Local/JetBrains/PyCharm2020.1/remote_sources/-1053058360/1179071206/pyscf/hessian/uhf.py</url>
+          <line>341</line>
+          <option name="timeStamp" value="15" />
+        </line-breakpoint>
+        <line-breakpoint enabled="true" suspend="THREAD" type="python-line">
+          <url>file://$PROJECT_DIR$/pyxdh/DerivOnce/grad_scf.py</url>
+          <line>317</line>
+          <option name="timeStamp" value="17" />
+        </line-breakpoint>
       </breakpoints>
     </breakpoint-manager>
   </component>
   <component name="com.intellij.coverage.CoverageDataManagerImpl">
-    <SUITE FILE_PATH="coverage/Py_xDH$grid_gen.coverage" NAME="grid_gen Coverage Results" MODIFIED="1590480714382" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/pyxdh/tmp_dft_speed" />
-    <SUITE FILE_PATH="coverage/Py_xDH$pytest_in_grad_uscf_py.coverage" NAME="pytest in grad_uscf.py Coverage Results" MODIFIED="1591017002521" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="" />
-    <SUITE FILE_PATH="coverage/Py_xDH$pytest_for_pyxdh.coverage" NAME="pytest for pyxdh Coverage Results" MODIFIED="1591612967056" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="" />
+    <SUITE FILE_PATH="coverage/Py_xDH$ucphf_hacking.coverage" NAME="ucphf_hacking Coverage Results" MODIFIED="1591681560839" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/pyxdh/tmp/unrestricted_test" />
     <SUITE FILE_PATH="coverage/Py_xDH$grad_uscf.coverage" NAME="grad_uscf Coverage Results" MODIFIED="1591016595229" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/pyxdh/DerivOnce" />
     <SUITE FILE_PATH="coverage/Py_xDH$test_xcfun.coverage" NAME="test_xcfun Coverage Results" MODIFIED="1591272538650" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/pyxdh/tmp/unrestricted_test" />
     <SUITE FILE_PATH="coverage/Py_xDH$libxc_kxc.coverage" NAME="libxc_kxc Coverage Results" MODIFIED="1591584199298" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/pyxdh/tmp/unrestricted_test" />
+    <SUITE FILE_PATH="coverage/Py_xDH$pytest_for_dipole_mp2_Test_DipoleMP2_test_dipole_xyg3.coverage" NAME="pytest for dipole_mp2.Test_DipoleMP2.test_dipole_xyg3 Coverage Results" MODIFIED="1591592861708" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
+    <SUITE FILE_PATH="coverage/Py_xDH$grid_gen.coverage" NAME="grid_gen Coverage Results" MODIFIED="1590480714382" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/pyxdh/tmp_dft_speed" />
+    <SUITE FILE_PATH="coverage/Py_xDH$ncdft_derivonce_uhf_ub3lyp.coverage" NAME="ncdft_derivonce_uhf_ub3lyp Coverage Results" MODIFIED="1591707031834" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/pyxdh/Validation/numerical_deriv" />
+    <SUITE FILE_PATH="coverage/Py_xDH$pytest_in_grad_uscf_py.coverage" NAME="pytest in grad_uscf.py Coverage Results" MODIFIED="1591017002521" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="" />
+    <SUITE FILE_PATH="coverage/Py_xDH$pytest_for_pyxdh.coverage" NAME="pytest for pyxdh Coverage Results" MODIFIED="1591612967056" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="" />
+    <SUITE FILE_PATH="coverage/Py_xDH$pytest_for_grad_uscf_Test_GradUSCF_test_UHF_UB3LYP_grad.coverage" NAME="pytest for grad_uscf.Test_GradUSCF.test_UHF_UB3LYP_grad Coverage Results" MODIFIED="1591707056843" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/pyxdh/DerivOnce" />
     <SUITE FILE_PATH="coverage/Py_xDH$pytest_for_dipderiv_mp2_Test_DipDerivMP2_test_B2PLYP_dipderiv.coverage" NAME="pytest for dipderiv_mp2.Test_DipDerivMP2.test_B2PLYP_dipderiv Coverage Results" MODIFIED="1591607224381" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/pyxdh/DerivTwice" />
     <SUITE FILE_PATH="coverage/Py_xDH$pytest_for_grad_uscf_Test_GradUSCF_test_UB3LYP_grad.coverage" NAME="pytest for grad_uscf.Test_GradUSCF.test_UB3LYP_grad Coverage Results" MODIFIED="1591619463301" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/pyxdh/DerivOnce" />
     <SUITE FILE_PATH="coverage/Py_xDH$xcfun_grid.coverage" NAME="xcfun_grid Coverage Results" MODIFIED="1591075406484" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/pyxdh/tmp" />
-    <SUITE FILE_PATH="coverage/Py_xDH$pytest_for_dipole_mp2_Test_DipoleMP2_test_dipole_xyg3.coverage" NAME="pytest for dipole_mp2.Test_DipoleMP2.test_dipole_xyg3 Coverage Results" MODIFIED="1591592861708" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
     <SUITE FILE_PATH="coverage/Py_xDH$pytest_for_grad_uscf_Test_GradUSCF_test_UHF_grad.coverage" NAME="pytest for grad_uscf.Test_GradUSCF.test_UHF_grad Coverage Results" MODIFIED="1591619117571" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/pyxdh/DerivOnce" />
   </component>
 </project>
\ No newline at end of file
diff --git a/pyxdh/DerivOnce/__init__.py b/pyxdh/DerivOnce/__init__.py
index 223825f..3f810cc 100644
--- a/pyxdh/DerivOnce/__init__.py
+++ b/pyxdh/DerivOnce/__init__.py
@@ -1,17 +1,21 @@
 __all__ = [
     "DerivOnceSCF", "DerivOnceNCDFT",  # deriv_once_scf
-    "GradSCF", "GradNCDFT",  # grad_scf
     "DerivOnceMP2", "DerivOnceXDH",  # deriv_once_mp2
+    "GradSCF", "GradNCDFT",  # grad_scf
     "GradMP2", "GradXDH",  # grad_mp2
     "DipoleSCF", "DipoleNCDFT",  # dipole_scf
     "DipoleMP2", "DipoleXDH",  # dipole_mp2
-    "DerivOnceUSCF",  # deriv_once_uscf
+
+    "DerivOnceUSCF", "DerivOnceUNCDFT",  # deriv_once_uscf
+    "GradUSCF", "GradUNCDFT", # grad_uscf
 ]
 
 from pyxdh.DerivOnce.deriv_once_scf import DerivOnceSCF, DerivOnceNCDFT
-from pyxdh.DerivOnce.grad_scf import GradSCF, GradNCDFT
 from pyxdh.DerivOnce.deriv_once_mp2 import DerivOnceMP2, DerivOnceXDH
+from pyxdh.DerivOnce.grad_scf import GradSCF, GradNCDFT
 from pyxdh.DerivOnce.grad_mp2 import GradMP2, GradXDH
 from pyxdh.DerivOnce.dipole_scf import DipoleSCF, DipoleNCDFT
 from pyxdh.DerivOnce.dipole_mp2 import DipoleMP2, DipoleXDH
-from pyxdh.DerivOnce.deriv_once_uscf import DerivOnceUSCF
+
+from pyxdh.DerivOnce.deriv_once_uscf import DerivOnceUSCF, DerivOnceUNCDFT
+from pyxdh.DerivOnce.grad_uscf import GradUSCF, GradUNCDFT
diff --git a/pyxdh/DerivOnce/deriv_once_scf.py b/pyxdh/DerivOnce/deriv_once_scf.py
index 8d6f4cc..0b176b5 100644
--- a/pyxdh/DerivOnce/deriv_once_scf.py
+++ b/pyxdh/DerivOnce/deriv_once_scf.py
@@ -387,7 +387,7 @@ def mol_slice(self, atm_id):
         _, _, p0, p1 = self.mol.aoslice_by_atom()[atm_id]
         return slice(p0, p1)
 
-    def Ax0_Core(self, si, sj, sk, sl, reshape=True, in_cphf=False):
+    def Ax0_Core(self, si, sj, sk, sl, reshape=True, in_cphf=False, C=None):
         """
 
         Parameters
@@ -408,7 +408,8 @@ def Ax0_Core(self, si, sj, sk, sl, reshape=True, in_cphf=False):
         -------
         fx : function which pass matrix, then return Ax @ X.
         """
-        C = self.C
+        if C is None:
+            C = self.C
         nao = self.nao
         resp = self.resp_cphf if in_cphf else self.resp
 
diff --git a/pyxdh/DerivOnce/deriv_once_uscf.py b/pyxdh/DerivOnce/deriv_once_uscf.py
index e3961c4..6b2fe1a 100644
--- a/pyxdh/DerivOnce/deriv_once_uscf.py
+++ b/pyxdh/DerivOnce/deriv_once_uscf.py
@@ -1,16 +1,14 @@
 import numpy as np
-from abc import ABC, abstractmethod
+from abc import ABC
 from functools import partial
 import os
 import warnings
-import copy
-from pyscf.scf._response_functions import _gen_rhf_response
+from pyscf.scf._response_functions import _gen_uhf_response
 
-from pyscf import gto, dft, grad, hessian
-from pyscf.scf import cphf
+from pyscf import dft, grad, hessian
+from pyscf.scf import ucphf
 
-from pyxdh.Utilities import timing
-from pyxdh.DerivOnce.deriv_once_scf import DerivOnceSCF
+from pyxdh.DerivOnce.deriv_once_scf import DerivOnceSCF, DerivOnceNCDFT
 from pyxdh.Utilities import GridIterator, KernelHelper, timing
 
 MAXMEM = float(os.getenv("MAXMEM", 2))
@@ -45,6 +43,10 @@ def initialization_pyscf(self):
             self.scf_hess = hessian.UHF(self.scf_eng)
         return
 
+    @property
+    def nvir(self):
+        return self.nmo - self.nocc[0], self.nmo - self.nocc[1]
+
     @property
     def so(self):
         return slice(0, self.nocc[0]), slice(0, self.nocc[1])
@@ -100,6 +102,81 @@ def _get_F_1_mo(self):
             return 0
         return np.einsum("xAuv, xup, xvq -> xApq", self.F_1_ao, self.C, self.C)
 
+    @property
+    def resp(self):
+        if self._resp is NotImplemented:
+            self._resp = _gen_uhf_response(self.scf_eng, mo_coeff=self.C, mo_occ=self.mo_occ, hermi=1, max_memory=self.grdit_memory)
+        return self._resp
+
+    def _get_B_1(self):
+        sa = self.sa
+        so = self.so
+
+        B_1 = self.F_1_mo.copy()
+        if isinstance(self.S_1_mo, np.ndarray):
+            B_1 += (
+                - np.einsum("xApq, xq -> xApq", self.S_1_mo, self.e)
+                - 0.5 * np.array(self.Ax0_Core(sa, sa, so, so)((self.S_1_mo[0, :, so[0], so[0]], self.S_1_mo[1, :, so[1], so[1]])))
+            )
+        return B_1
 
-
+    @property
+    def resp_cphf(self):
+        if self._resp_cphf is NotImplemented:
+            if self.xc_type == "HF":
+                self._resp_cphf = self.resp
+            else:
+                mf = dft.RKS(self.mol)
+                mf.xc = self.scf_eng.xc
+                mf.grids = self.cphf_grids
+                self._resp_cphf = _gen_uhf_response(mf, mo_coeff=self.C, mo_occ=self.mo_occ, hermi=1, max_memory=self.grdit_memory)
+        return self._resp_cphf
+
+    def Ax0_Core(self, si, sj, sk, sl, reshape=True, in_cphf=False, C=None):
+        if C is None:
+            C = self.C
+        nao = self.nao
+        resp = self.resp_cphf if in_cphf else self.resp
+
+        @timing
+        def fx(X_):
+            # For simplicity, shape of X should be (2, dim_prop, sk, sl)
+            have_first_dim = len(X_[0].shape) == 3
+            prop_dim = X_[0].shape[0] if have_first_dim else 1
+
+            dm = np.zeros((2, prop_dim, nao, nao))
+            dm[0] = C[0][:, sk[0]] @ X_[0] @ C[0][:, sl[0]].T
+            dm[1] = C[1][:, sk[1]] @ X_[1] @ C[1][:, sl[1]].T
+            dm += dm.swapaxes(-1, -2)
+            if (not have_first_dim):
+                dm.shape = (2, nao, nao)
+
+            ax_ao = resp(dm)
+            Ax = (
+                C[0][:, si[0]].T @ ax_ao[0] @ C[0][:, sj[0]],
+                C[1][:, si[1]].T @ ax_ao[1] @ C[1][:, sj[1]]
+            )
+            return Ax
+
+        return fx
+
+
+class DerivOnceUNCDFT(DerivOnceUSCF, DerivOnceNCDFT):
+
+    def _get_Z(self):
+        so, sv = self.so, self.sv
+        nocc, nvir = self.nocc, self.nvir
+        Ax0_Core = self.Ax0_Core
+        e, mo_occ = self.e, self.mo_occ
+        F_0_mo = self.nc_deriv.F_0_mo
+        def fx(X):
+            X_ = (
+                X[:, :nocc[0] * nvir[0]].reshape((nvir[0], nocc[0])),
+                X[:, nocc[0] * nvir[0]:].reshape((nvir[1], nocc[1]))
+            )
+            return np.concatenate([v.ravel() for v in Ax0_Core(sv, so, sv, so, in_cphf=True)(X_)])
+        Z = ucphf.solve(fx, e, mo_occ, (F_0_mo[0, None, sv[0], so[0]], F_0_mo[1, None, sv[1], so[1]]), max_cycle=100, tol=self.cphf_tol)[0]
+        # output Z shape is (1, nvir, nocc), we remove the first dimension
+        Z = (Z[0][0], Z[1][0])
+        return Z
 
diff --git a/pyxdh/DerivOnce/grad_uscf.py b/pyxdh/DerivOnce/grad_uscf.py
index 8b9e41e..419de96 100644
--- a/pyxdh/DerivOnce/grad_uscf.py
+++ b/pyxdh/DerivOnce/grad_uscf.py
@@ -4,7 +4,7 @@
 
 from pyscf import grad
 
-from pyxdh.DerivOnce import DerivOnceUSCF, DerivOnceNCDFT, GradSCF
+from pyxdh.DerivOnce import DerivOnceUSCF, GradSCF, DerivOnceUNCDFT
 from pyxdh.Utilities import GridIterator, KernelHelper
 
 MAXMEM = float(os.getenv("MAXMEM", 2))
@@ -55,6 +55,25 @@ def _get_E_1(self):
         return E_1.reshape((natm, 3))
 
 
+class GradUNCDFT(DerivOnceUNCDFT, GradUSCF):
+
+    @property
+    def DerivOnceMethod(self):
+        return GradUSCF
+
+    def _get_E_1(self):
+        natm = self.natm
+        so, sv = self.so, self.sv
+        B_1 = self.B_1
+        Z = self.Z
+        E_1 = (
+            + self.nc_deriv.E_1
+            + 2 * np.einsum("ai, Aai -> A", Z[0], B_1[0, :, sv[0], so[0]]).reshape((natm, 3))
+            + 2 * np.einsum("ai, Aai -> A", Z[1], B_1[1, :, sv[1], so[1]]).reshape((natm, 3))
+        )
+        return E_1
+
+
 class Test_GradUSCF:
 
     def test_UHF_grad(self):
@@ -78,3 +97,15 @@ def test_UB3LYP_grad(self):
         helper = GradUSCF({"scf_eng": CH3.gga_eng})
         formchk = FormchkInterface(resource_filename("pyxdh", "Validation/gaussian/CH3-B3LYP-freq.fchk"))
         assert(np.allclose(helper.E_1, formchk.grad(), atol=1e-6, rtol=1e-4))
+
+    def test_UHF_UB3LYP_grad(self):
+
+        from pyxdh.Utilities.test_molecules import Mol_CH3
+        from pkg_resources import resource_filename
+        import pickle
+
+        CH3 = Mol_CH3()
+        helper = GradUNCDFT({"scf_eng": CH3.hf_eng, "nc_eng": CH3.gga_eng, "cphf_tol": 1e-10})
+        with open(resource_filename("pyxdh", "Validation/numerical_deriv/ncdft_derivonce_uhf_ub3lyp.dat"), "rb") as f:
+            ref_grad = pickle.load(f)["grad"].reshape(-1, 3)
+        assert (np.allclose(helper.E_1, ref_grad, atol=1e-5, rtol=1e-4))
diff --git a/pyxdh/Utilities/deriv_numerical.py b/pyxdh/Utilities/deriv_numerical.py
index 0f718bb..16fc99c 100644
--- a/pyxdh/Utilities/deriv_numerical.py
+++ b/pyxdh/Utilities/deriv_numerical.py
@@ -50,7 +50,7 @@ def move_mol(self, movelist):
         for tup in movelist:
             mol_ret_coords[tup[0], tup[1]] += tup[2] * self.interval * lib.param.BOHR
         mol_ret.set_geom_(mol_ret_coords)
-        return mol_ret
+        return mol_ret.build()
 
     def perform_mf(self):
         natm = self.mol.natm
diff --git a/pyxdh/Utilities/test_molecules.py b/pyxdh/Utilities/test_molecules.py
index 76b8494..2800d79 100644
--- a/pyxdh/Utilities/test_molecules.py
+++ b/pyxdh/Utilities/test_molecules.py
@@ -3,7 +3,7 @@
 
 class Mol_H2O2:
 
-    def __init__(self, xc="B3LYPg", mol=None):
+    def __init__(self, xc="B3LYPg", mol=None, atom_grid=(99, 590)):
 
         if mol is None:
             mol = gto.Mole()
@@ -19,6 +19,7 @@ def __init__(self, xc="B3LYPg", mol=None):
 
         self.mol = mol
         self.xc = xc
+        self.atom_grid = atom_grid
 
         self._hf_eng = NotImplemented
         self._hf_grad = NotImplemented
@@ -48,7 +49,7 @@ def gga_eng(self):
         if self._gga_eng is not NotImplemented:
             return self._gga_eng
 
-        grids = self.gen_grids()
+        grids = self.gen_grids(atom_grid=self.atom_grid)
 
         gga_eng = scf.RKS(self.mol)
         gga_eng.grids = grids
@@ -70,9 +71,12 @@ def gga_grad(self):
         self._gga_grad = gga_grad
         return self._gga_grad
 
-    def gen_grids(self, rad_points=99, sph_points=590):
+    def gen_grids(self, rad_points=99, sph_points=590, atom_grid=None):
         grids = dft.Grids(self.mol)
-        grids.atom_grid = (rad_points, sph_points)
+        grids.atom_grid = atom_grid
+        if atom_grid is None:
+            grids.atom_grid = (rad_points, sph_points)
+        grids.radi_method = dft.gauss_chebyshev
         grids.becke_scheme = dft.gen_grid.stratmann
         grids.build()
         return grids
diff --git a/pyxdh/Validation/numerical_deriv/ncdft_derivonce_uhf_ub3lyp.dat b/pyxdh/Validation/numerical_deriv/ncdft_derivonce_uhf_ub3lyp.dat
new file mode 100644
index 0000000000000000000000000000000000000000..a0148e6ec4211afd5c62e93cd56563f200098d7e
GIT binary patch
literal 293
zcmZo*ohrr100y;FG<sOlixN|&^a$mZ<`z`yCFd8V>H(RlMVZM{dbs1$it=-lO4HI(
zi>6GTqA@K2qL1N|vWH4X)+T#vtMCeK)AjprYDvuBdujLn{Z7$Fr3yRtZ|ctMv5B2v
zFU%#Rs`+=Sz2?!@ns2cU_TL08I%}O4*?-*}sQYI|jr~Rr_1L1oUi)*c8DH!Kn(b>0
zde=Yh++shahZXD|Ae~ZDS&#~}yoV{xVv09|H)H#hpeY*O%sq@YQ~dn=y#50LnDAyO
znUdtq(>kSx(RoS<P*D$CN@hWRPU@5l7L92VP?s-Y*3ER%dT7sZVP<W@$A0?*u5!I$
VF30SrWN>>kw@%662k9);0{}19bL;>B

literal 0
HcmV?d00001

diff --git a/pyxdh/Validation/numerical_deriv/ncdft_derivonce_uhf_ub3lyp.py b/pyxdh/Validation/numerical_deriv/ncdft_derivonce_uhf_ub3lyp.py
new file mode 100644
index 0000000..26654de
--- /dev/null
+++ b/pyxdh/Validation/numerical_deriv/ncdft_derivonce_uhf_ub3lyp.py
@@ -0,0 +1,54 @@
+import pickle
+from pyscf import scf
+import numpy as np
+
+from pyxdh.Utilities.test_molecules import Mol_CH3
+from pyxdh.Utilities import NumericDiff, NucCoordDerivGenerator, DipoleDerivGenerator
+from pyxdh.DerivOnce import GradUNCDFT
+
+
+def mol_to_grad_helper(mol):
+    CH3 = Mol_CH3(mol=mol)
+    CH3.hf_eng.kernel()
+    config = {
+        "scf_eng": CH3.hf_eng,
+        "nc_eng": CH3.gga_eng
+    }
+    helper = GradUNCDFT(config)
+    return helper
+
+
+def dipole_generator(component, interval):
+    CH3 = Mol_CH3()
+    mol = CH3.mol
+
+    def get_hcore(mol=mol):
+        return scf.rhf.get_hcore(mol) - interval * mol.intor("int1e_r")[component]
+
+    CH3.hf_eng.get_hcore = get_hcore
+    CH3.gga_eng.get_hcore = get_hcore
+
+    config = {
+        "scf_eng": CH3.hf_eng,
+        "nc_eng": CH3.gga_eng
+    }
+    helper = GradUNCDFT(config)
+    return helper
+
+
+if __name__ == '__main__':
+    result_dict = {}
+    CH3 = Mol_CH3()
+    mol = CH3.mol
+
+    num_obj = NucCoordDerivGenerator(CH3.mol, mol_to_grad_helper)
+    num_dif = NumericDiff(num_obj, lambda helper: helper.eng)
+    result_dict["grad"] = num_dif.derivative
+
+    num_obj = DipoleDerivGenerator(dipole_generator)
+    num_dif = NumericDiff(num_obj, lambda helper: helper.eng)
+    dip_nuc = np.einsum("A, At -> t", mol.atom_charges(), mol.atom_coords())
+    result_dict["dipole"] = num_dif.derivative + dip_nuc
+
+    with open("ncdft_derivonce_uhf_ub3lyp.dat", "wb") as f:
+        pickle.dump(result_dict, f, pickle.HIGHEST_PROTOCOL)