XTC文件读取错误 [英] XTC file reading error

查看:171
本文介绍了XTC文件读取错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "xdrfile/xdrfile_xtc.h"
#include "xdrfile/xdrfile.h"
#include<stdio.h>

int main()
{   
    int nat;
    int step;
    float time;
    float prec;
    int status;
    matrix box;
    rvec k[3];
    XDRFILE* xfp=xdrfile_open("test2.xtc","r");
    status=read_xtc(xfp,nat,&step,&time,box,k,&prec);
    xdrfile_close(xfp); 
    return 0;
}

我试图使用xtc库运行代码以读取GROMACS的轨迹框架...我遇到了错误,

I tried to run the code using the xtc library to read a trajectory frame of GROMACS... I am getting an error,

Segmentation error

可以请您帮忙吗???

Can you please help???

推荐答案

查看第二个参数nat必须设置为检索到的read_xtc_natoms函数的值.该值必须始终进行初始化.

Second parameter nat must be set to a value retrieved read_xtc_natoms function. The value must be initialized anyway.

此外,数组k的宽度必须与nat的值匹配.

Moreover the array k width must match the nat value.

#include "xdrfile/xdrfile_xtc.h"
#include "xdrfile/xdrfile.h"
#include<stdio.h>

int main(void)
{
    int nat;
    int step;
    float time;
    float prec;
    int status;
    matrix box;
    int status;

    char fileName[] = "test2.xtc";

    status = read_xtc_natoms(fileName, &nat);

    if (status != exdrOK)
    {
        XDRFILE* xfp = xdrfile_open(fileName, "r");
        if (xfp != NULL)
        {
            rvec k[nat];
            status = read_xtc(xfp, nat, &step, &time, box, k, &prec);
            xdrfile_close(xfp);
        }
        else
        {
            perror("File not opened:");
        }
    }
    else
    {
        fprintf(stderr, "read_xtc_natoms failure; return code %d", status);
    }

    return 0;
}

在上面的代码中,我使用了 VLA 来创建k数组,但您也可以使用动态内存:

In the above code I used VLAs to create the k array, but you can use dynamic memory too:

#include "xdrfile/xdrfile_xtc.h"
#include "xdrfile/xdrfile.h"
#include<stdio.h>

int main(void)
{
    int nat;
    int step;
    float time;
    float prec;
    int status;
    matrix box;
    int status;

    char fileName[] = "test2.xtc";

    status = read_xtc_natoms(fileName, &nat);

    if (status != exdrOK)
    {
        rvec *k = malloc(nat * sizeof(rvec));

        if (k != NULL)
        {
            XDRFILE* xfp = xdrfile_open(fileName, "r");
            if (xfp != NULL)
            {
                status = read_xtc(xfp, nat, &step, &time, box, k, &prec);
                xdrfile_close(xfp);
            }
            else
            {
                perror("File not opened:");
            }

            free(k);
        }
        else
        {
            fprintf(stderr, "Error in dynamic allocation of k vector\n");
        }
    }
    else
    {
        fprintf(stderr, "read_xtc_natoms failure; return code %d", status);
    }

    return 0;
}

这篇关于XTC文件读取错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆