Commits

jmht  committed 79c6a4f

NWChem importer test.

  • Participants
  • Parent commits d811766

Comments (0)

Files changed (1)

File compchem-importer/src/test/java/net/chempound/compchem/NWChemImporterTest.java

+package net.chempound.compchem;
+
+import com.hp.hpl.jena.rdf.arp.JenaReader;
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
+import com.hp.hpl.jena.rdf.model.RDFReader;
+import net.chempound.chemistry.ImageGenerator;
+import net.chempound.chemistry.InChIGenerator;
+import net.chempound.compchem.nwchem.NWChemLogImporter;
+import net.chempound.compchem.rdf.CompChemCalculation;
+import net.chempound.compchem.rdf.CompChemPersonalities;
+import net.chempound.content.DepositRequest;
+import net.chempound.rdf.ChempoundPersonalities;
+import net.chempound.rdf.IgnoringEmptyBaseUriErrorHandler;
+import net.chempound.storage.DepositResource;
+import net.chempound.util.MimeType;
+import nu.xom.Document;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.mock;
+
+public class NWChemImporterTest {
+
+    private ImageGenerator imageGenerator;
+    private InChIGenerator inchiGenerator;
+
+    static {
+        ChempoundPersonalities.init();
+        CompChemPersonalities.init();
+
+    }
+
+    @Before
+    public void setUp() throws Exception {
+        imageGenerator = mock(ImageGenerator.class);
+        inchiGenerator = mock(InChIGenerator.class);
+    }
+
+    @Test
+    public void testImportH2o631gs() throws Exception {
+
+        InputStream in = getClass().getResourceAsStream("/h2o_631gs.nwo");
+        NWChemLogImporter importer = new NWChemLogImporter(imageGenerator, inchiGenerator);
+        importer.setId("h2o_631gs");
+        importer.setLogFile(in, "/h2o_631gs.nwo");
+        DepositRequest request = importer.generateDepositRequest();
+
+        Model model = getRdfModel(request);
+        CompChemCalculation calc = model.getResource("").as(CompChemCalculation.class);
+
+        assertEquals("NWChem", calc.getPackageName());
+        assertEquals("6.1", calc.getPackageVersion());
+
+        assertEquals("scf", calc.getMethod());
+        assertEquals("energy", calc.getTask());
+        assertEquals("6-31g*", calc.getBasis());
+
+        assertEquals("jmhts-MacBook-Air.local", calc.getHostname());
+        assertEquals("HF SCF calculation on water with 6-31G* basis", calc.getJobTitle());
+        assertEquals("Sun Apr  1 13:56:19 2012", calc.getRunDate());
+
+        assertEquals("H 2 O 1", calc.getFormula());
+        assertEquals("C2v", calc.getPointGroup());
+        assertEquals("RHF", calc.getWavefunction());
+        assertEquals(0.0, calc.getCharge(), 0.1);
+
+        assertNotNull(calc.getFinalEnergy());
+        assertEquals(-76.010515918328, calc.getFinalEnergy(), 0.0000001);
+
+    }
+    
+    @Test
+    public void testImportBenzene321gMp2() throws Exception {
+        
+        InputStream in = getClass().getResourceAsStream("/benzene_321g_mp2.nwo");
+        NWChemLogImporter importer = new NWChemLogImporter(imageGenerator, inchiGenerator);
+        importer.setId("benzene_321g_mp2");
+        importer.setLogFile(in, "/benzene_321g_mp2.nwo");
+        DepositRequest request = importer.generateDepositRequest();
+        
+        Model model = getRdfModel(request);
+        CompChemCalculation calc = model.getResource("").as(CompChemCalculation.class);
+        
+        assertEquals("NWChem", calc.getPackageName());
+        assertEquals("6.1", calc.getPackageVersion());
+        
+        /* 
+         * Currently can't test this as the way we extract the MP2 method in the converters
+         * is a bit of a hack, so that both scf & mp2 are present in the parameter list. Need
+         * to add the if construct to the converters before we can do this properly.
+         */
+        
+        //assertEquals("mp2", calc.getMethod());
+        assertEquals("energy", calc.getTask());
+        assertEquals("3-21g", calc.getBasis());
+        
+        assertEquals("jmhts-MacBook-Air.local", calc.getHostname());
+        assertEquals("MP2 calculation on benzene with 3-21g basis", calc.getJobTitle());
+        assertEquals("Sun Apr  1 14:08:47 2012", calc.getRunDate());
+        
+        assertEquals("C 6 H 6", calc.getFormula());
+        assertEquals("D6h", calc.getPointGroup());
+        assertEquals("RHF", calc.getWavefunction());
+        assertEquals(0.0, calc.getCharge(), 0.1);
+        
+        assertNotNull(calc.getFinalEnergy());
+        assertEquals(-229.946062510210, calc.getFinalEnergy(), 0.0000001);
+        
+    }
+    
+    @Test
+    public void testImportH2oSto3gDftB3lypOpt() throws Exception {
+        
+        InputStream in = getClass().getResourceAsStream("/h2o_sto3g_dft_b3lyp_opt.nwo");
+        NWChemLogImporter importer = new NWChemLogImporter(imageGenerator, inchiGenerator);
+        importer.setId("h2o_sto3g_dft_b3lyp_opt");
+        importer.setLogFile(in, "/h2o_sto3g_dft_b3lyp_opt.nwo");
+        DepositRequest request = importer.generateDepositRequest();
+        
+        Model model = getRdfModel(request);
+        CompChemCalculation calc = model.getResource("").as(CompChemCalculation.class);
+        
+        assertEquals("NWChem", calc.getPackageName());
+        assertEquals("6.1", calc.getPackageVersion());
+        
+        assertEquals("dft", calc.getMethod());
+        assertEquals("geometry_optimization", calc.getTask());
+        assertEquals("sto-3g", calc.getBasis());
+        assertEquals("B3LYP", calc.getDftFunctional());
+        
+        assertEquals("jmhts-MacBook-Air.local", calc.getHostname());
+        assertEquals("DFT B3LYP calculation on water with STO-3G basis", calc.getJobTitle());
+        assertEquals("Wed Mar 28 22:51:21 2012", calc.getRunDate());
+        
+        assertEquals("H 2 O 1", calc.getFormula());
+        assertEquals("C2v", calc.getPointGroup());
+        assertEquals("closed shell", calc.getWavefunction());
+        assertEquals(0.0, calc.getCharge(), 0.1);
+        
+        assertNotNull(calc.getFinalEnergy());
+        assertEquals(-75.32277490, calc.getFinalEnergy(), 0.0000001);
+        
+    }
+
+    private Model getRdfModel(final DepositRequest request) throws IOException {
+        for (final DepositResource resource : request.getAggregatedResources()) {
+            if (MimeType.APPLICATION_RDF_XML.equals(resource.getMimeType())) {
+                return loadRdf(resource);
+            }
+        }
+        return null;
+    }
+
+    private Model loadRdf(final DepositResource resource) throws IOException {
+        final InputStream in = resource.openInputStream();
+        try {
+            final RDFReader reader = new JenaReader();
+            reader.setErrorHandler(new IgnoringEmptyBaseUriErrorHandler());
+
+            final Model model = ModelFactory.createDefaultModel();
+            reader.read(model, in, "");
+
+            return model;
+        } finally {
+            IOUtils.closeQuietly(in);
+        }
+    }
+
+}